Product.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Product as Request;
  3. use App\Models\AdminHistory;
  4. use App\Models\Business;
  5. use App\Models\City;
  6. use App\Models\Course;
  7. use App\Models\OrdersProduct;
  8. use App\Models\Producer;
  9. use App\Models\Product as Model;
  10. use Illuminate\Support\Carbon;
  11. use App\Models\Product\Spec as ProductSpec;
  12. use App\Models\Product\Type as ProductType;
  13. use App\Models\Product\Attr as ProductAttr;
  14. use App\Models\Product\Skus as ProductSkus;
  15. use App\Models\Product\City as ProductCity;
  16. use App\Models\ProductPhoto;
  17. use Illuminate\Support\Facades\DB;
  18. use App\Models\WeiBan\Tags as WeiBanTags;
  19. use App\Models\FilesManager;
  20. /**
  21. * 产品管理
  22. *
  23. * @author 刘相欣
  24. *
  25. */
  26. class Product extends Auth{
  27. protected function _initialize(){
  28. parent::_initialize();
  29. $this->assign('breadcrumb1','基础信息');
  30. $this->assign('breadcrumb2','产品管理');
  31. }
  32. /**
  33. * 首页列表
  34. *
  35. * */
  36. public function index(Model $Model,City $City,ProductCity $ProductCity,OrdersProduct $OrdersProduct,AdminHistory $AdminHistory){
  37. // 接受参数
  38. $code = request('product_code','');
  39. $name = request('name','');
  40. $status = request('status');
  41. $startTime = request('start_time','');
  42. $cityIds = request('city_ids',[]);
  43. // 编码转ID
  44. $id = $code ? $Model->codeToId($code) : 0;
  45. // 查询条件
  46. $map = [];
  47. // 是否有城市
  48. $whereIn = $cityIds ? $cityIds : [];
  49. // 编码ID
  50. if( $id ) $map[] = ['product.id','=',$id];
  51. if( $name ) $map[] = ['product.name','like','%'.$name.'%'];
  52. if( $startTime ) $map[] = ['product.insert_time','>=',Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp()];
  53. if( $startTime ) $map[] = ['product.insert_time','<=',Carbon::createFromFormat('Y-m-d',$startTime)->endOfDay()->getTimestamp()];
  54. if( !is_null($status) ) $map[] = ['product.status','=',$status];
  55. // 查询数据
  56. $list = $Model->query();
  57. if( $whereIn ) $list = $list->join('product_city','product_city.product_id','=','product.id')->whereIn('product_city.city_id',$whereIn);
  58. $list = $list->groupBy('product.id')
  59. ->where($map)
  60. ->orderBy('product.sort')
  61. ->orderByDesc('product.id')
  62. ->select(['product.*'])
  63. ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  64. // 统计销量
  65. $salesList = $OrdersProduct->query()->where([['status','<>',4]])->whereIn('product_id',array_column($list->items(),'id'))->groupBy('product_id')->select([DB::raw('sum(`buy_num`) as sales_total'),'product_id'])->pluck('sales_total','product_id')->toArray();
  66. // 循环处理数据
  67. foreach ($list as $key => $value) {
  68. // 判断总库存
  69. if( !$value['stock_total'] ) {
  70. // 以首次添加时的为准
  71. $value['stock_total']= (int) $AdminHistory->query()->where([['table_name','=','product'],['primary_id','=',$value['id']],['notes_type','=',1],['column_name','=','stock']])->orderBy('update_time')->value('after_value');
  72. // 修改库存
  73. $Model->edit($value['id'],['stock_total'=>$value['stock_total']]);
  74. }
  75. // id转编号
  76. $value['product_code'] = $Model->idToCode($value['id']);
  77. // id转编号
  78. $cityName = [];
  79. // 获取城市列表
  80. $cityIds = $ProductCity->query()->where([['product_id','=',$value['id']]])->pluck('city_id');
  81. // 如果有的话
  82. if( $cityIds ) {
  83. // 返回结果
  84. foreach ($cityIds as $cityId) {
  85. // 城市ID
  86. if( $cityId == 1 ) continue;
  87. // 获取城市名
  88. $cityName[] = $City->getOne($cityId,'name');
  89. }
  90. }
  91. // 销量统计
  92. $value['sales_total'] = isset($salesList[$value['id']])?$salesList[$value['id']]:0;
  93. // 城市
  94. $value['city_name'] = implode('、',$cityName);
  95. // 重组
  96. $list[$key] = $value;
  97. }
  98. // 获取列表
  99. $cityList = $City->getCityList();
  100. // 分配数据
  101. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  102. $this->assign('cityList',$cityList);
  103. $this->assign('list', $list);
  104. // 加载模板
  105. return $this->fetch();
  106. }
  107. /**
  108. * 表格导入
  109. *
  110. * */
  111. public function import_execl( Request $request,Model $Model,FilesManager $FilesManager){
  112. // 验证参数
  113. // $request->scene('import_execl')->validate();
  114. // 获取表格信息
  115. $file = request()->file('order_file');
  116. // 返回结果
  117. $sheetList = $FilesManager->excelToProduct($file);
  118. // 如果不存在结果
  119. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  120. // 循环处理
  121. foreach ($sheetList as $value) {
  122. // 获取详情描述
  123. $description = $value['description'];
  124. unset($value['description']);
  125. // 写入
  126. $id = $Model->add($value);
  127. // 提示新增失败
  128. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  129. // 更新内容
  130. $result = $Model->updateDesc($id,$description);
  131. // 提示新增失败
  132. if( !$result ) return json_send(['code'=>'error','msg'=>'更新内容失败']);
  133. }
  134. // 提示成功
  135. return json_send(['code'=>'success','msg'=>'导入成功','path'=>'']);
  136. }
  137. /**
  138. * 添加
  139. *
  140. * */
  141. public function add( Request $request, Model $Model,Course $Course,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  142. if( request()->isMethod('post') ){
  143. // 验证参数
  144. $request->scene('add')->validate();
  145. // 组合数据
  146. $data['name'] = request('name','');
  147. $data['thumb'] = request('thumb','');
  148. $data['poster'] = request('poster','');
  149. $data['spec'] = request('spec','');
  150. $data['price'] = request('price',0);
  151. $data['market_price'] = request('market_price',0);
  152. $data['producer_id'] = request('producer_id',0);
  153. $data['business_id'] = request('business_id',0);
  154. $data['quota'] = request('quota',0);
  155. $data['quota_start'] = request('quota_start','');
  156. $data['quota_end'] = request('quota_end','');
  157. $data['puton_time'] = request('puton_time','');
  158. $data['putoff_time'] = request('putoff_time','');
  159. $data['stock'] = request('stock',0);
  160. $data['type_id'] = request('type_id',0);
  161. $data['status'] = 1;
  162. $data['course_id'] = request('course_id',0);
  163. $data['course_number'] = request('course_number',0);
  164. $data['admin_uid'] = admin('uid');
  165. $description = request('description','');
  166. $attr = request('attr',[]);
  167. $skuList = request('sku',[]);
  168. $cityIds = request('city_ids',[]);
  169. $photoList = request('photo_list',[]);
  170. $tagScope = request('tag_scope',[]);
  171. $data['tag_scope'] = implode(',',$tagScope);
  172. $tagExclude = request('tag_exclude',[]);
  173. $data['tag_exclude'] = implode(',',$tagExclude);
  174. // 循环
  175. foreach ($photoList as $key => $value) {
  176. if( !$value ) unset($photoList[$key]);
  177. }
  178. $photoList = array_values($photoList);
  179. // 如果没有选择,则意味着全部
  180. $cityIds = $cityIds ? $cityIds : [1];
  181. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  182. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  183. $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
  184. $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
  185. // 限购提示
  186. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  187. // 限购提示
  188. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  189. // 限购提示
  190. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  191. // 上下架
  192. if( $data['puton_time'] ) {
  193. // 下架时间必填
  194. if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
  195. // 下架时间必填
  196. if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
  197. }
  198. // 总库存
  199. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  200. // 总库存
  201. $data['stock_total'] = $data['stock'];
  202. // 获取规格属性
  203. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  204. // 开启事务
  205. DB::beginTransaction();
  206. try {
  207. // 写入
  208. $id = $Model->add($data);
  209. // 提示新增失败
  210. if( !$id ) {
  211. // 回滚
  212. DB::rollBack();
  213. // 提示
  214. return json_send(['code'=>'error','msg'=>'新增失败']);
  215. }
  216. // 组合数据
  217. foreach ($specAttr as $key => $value) {
  218. // 查询结果
  219. $value['id'] = $ProductAttr->upsertByName($value,$id);
  220. // 提示新增失败
  221. if( !$value['id'] ) {
  222. // 回滚
  223. DB::rollBack();
  224. // 提示
  225. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  226. }
  227. // 重组
  228. $specAttr[$key] = $value;
  229. }
  230. // 图册
  231. foreach ($photoList as $key => $value) {
  232. // 整理数据
  233. $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  234. // 重新整理
  235. $photoList[$key] = $value;
  236. }
  237. // 存在图册
  238. if( $photoList ) {
  239. // 写入失败
  240. $result = $ProductPhoto->query()->insert($photoList);
  241. // 提示新增失败
  242. if( !$result ) {
  243. // 回滚
  244. DB::rollBack();
  245. // 提示
  246. return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
  247. }
  248. }
  249. // 循环SKU
  250. foreach ($skuList as $attrNames => $value) {
  251. // 属性ID值
  252. $value['attr_ids'] = [];
  253. // 规格属性值组合
  254. $value['attr_names']= $attrNames;
  255. // 切割成数据
  256. $names = explode(',',$attrNames);
  257. // 循环处理
  258. foreach ($names as $name) {
  259. foreach ($specAttr as $vv) {
  260. if( $name == $vv['name'] ) {
  261. // 提示新增失败
  262. if( !$vv['id'] ) {
  263. // 回滚
  264. DB::rollBack();
  265. // 提示
  266. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  267. }
  268. $value['attr_ids'][] = $vv['id'];
  269. }
  270. }
  271. }
  272. // 转成好存储的数据
  273. $value['attr_ids'] = implode(',',$value['attr_ids']);
  274. // 转成好存储的数据
  275. $value['product_id'] = $id;
  276. // 转成好存储的数据
  277. $value['sku_thumb'] = (string)$value['sku_thumb'];
  278. // 转成好存储的数据
  279. $value['stock_total']= $value['stock'];
  280. // 转成好存储的数据
  281. $value['insert_time']= time();
  282. // 转成好存储的数据
  283. $value['update_time']= time();
  284. // SKU结果
  285. $skuList[$attrNames] = $value;
  286. }
  287. // 循环城市范围
  288. foreach ($cityIds as $key => $value) {
  289. // 重组数据
  290. $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  291. }
  292. // 写入城市范围
  293. $ProductCity->query()->insert($cityIds);
  294. // 返回结果
  295. $result = $ProductSkus->insert($skuList);
  296. // 提示新增失败
  297. if( !$result ) {
  298. // 回滚
  299. DB::rollBack();
  300. // 提示
  301. return json_send(['code'=>'error','msg'=>'SKU插入失败']);
  302. }
  303. // 更新内容
  304. $Model->updateDesc($id,$description);
  305. // 提交
  306. DB::commit();
  307. // 记录行为
  308. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  309. // 告知结果
  310. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  311. } catch (\Throwable $th) {
  312. // 回滚
  313. DB::rollBack();
  314. // 提示
  315. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  316. }
  317. }
  318. // 获取类型数据
  319. $typeList = $ProductType->getList();
  320. $cityList = $City->getCityList();
  321. $businessList = $Business->getList();
  322. $producerList = $Producer->getList();
  323. $courseList = $Course->getList();
  324. // 标签列表
  325. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  326. // 标签列表
  327. $tagList = [];
  328. // 循环数据
  329. foreach ($tagData as $value) {
  330. $tagList[$value['group']][] = $value['name'];
  331. }
  332. // 分配数据
  333. $this->assign('crumbs','新增');
  334. $this->assign('tagList',$tagList);
  335. $this->assign('typeList',$typeList);
  336. $this->assign('cityList',$cityList);
  337. $this->assign('businessList',$businessList);
  338. $this->assign('producerList',$producerList);
  339. $this->assign('courseList',$courseList);
  340. // 加载模板
  341. return $this->fetch();
  342. }
  343. /**
  344. * 编辑
  345. *
  346. * */
  347. public function edit( Request $request, Model $Model,Course $Course,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  348. if(request()->isMethod('post')){
  349. // 验证参数
  350. $request->scene('edit')->validate();
  351. // 组合数据
  352. $id = request('id',0);
  353. $data['name'] = request('name','');
  354. $data['thumb'] = request('thumb','');
  355. $data['poster'] = request('poster','');
  356. $data['spec'] = request('spec','');
  357. $data['price'] = request('price',0);
  358. $data['market_price'] = request('market_price',0);
  359. $data['quota'] = request('quota',0);
  360. $data['quota_start'] = request('quota_start','');
  361. $data['quota_end'] = request('quota_end','');
  362. $data['puton_time'] = request('puton_time','');
  363. $data['putoff_time'] = request('putoff_time','');
  364. $data['producer_id'] = request('producer_id',0);
  365. $data['business_id'] = request('business_id',0);
  366. $data['stock'] = request('stock',0);
  367. $data['status'] = 1;
  368. $data['course_id'] = request('course_id',0);
  369. $data['course_number'] = request('course_number',0);
  370. $data['type_id'] = request('type_id',0);
  371. $description = request('description','');
  372. $attr = request('attr',[]);
  373. $skuList = request('sku',[]);
  374. $cityIds = request('city_ids',[]);
  375. $photoList = request('photo_list',[]);
  376. $tagScope = request('tag_scope',[]);
  377. $data['tag_scope'] = implode(',',$tagScope);
  378. $tagExclude = request('tag_exclude',[]);
  379. $data['tag_exclude'] = implode(',',$tagExclude);
  380. // 循环
  381. foreach ($photoList as $key => $value) {
  382. if( !$value ) unset($photoList[$key]);
  383. }
  384. $photoList = array_values($photoList);
  385. // 如果没有选择,则意味着全部
  386. $cityIds = $cityIds ? $cityIds : [1];
  387. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  388. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  389. $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
  390. $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
  391. // 限购提示
  392. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  393. // 限购提示
  394. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  395. // 限购提示
  396. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  397. // 上下架
  398. if( $data['puton_time'] ) {
  399. // 下架时间必填
  400. if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
  401. // 下架时间必填
  402. if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
  403. }
  404. // 总库存
  405. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  406. // 总库存
  407. $data['stock_total'] = $data['stock'];
  408. // 获取规格属性
  409. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  410. // 开启事务
  411. DB::beginTransaction();
  412. try {
  413. // 写入
  414. $result = $Model->edit($id,$data);
  415. // 提示新增失败
  416. if( !$result ) {
  417. // 回滚
  418. DB::rollBack();
  419. // 提示
  420. return json_send(['code'=>'error','msg'=>'修改失败']);
  421. }
  422. // 获取旧图册
  423. $oldPhoto = $ProductPhoto->getListByProductId($id);
  424. // 循环个数
  425. for ($i=0; $i < 4; $i++) {
  426. // 如果存在旧数据与新数据,修改
  427. if( isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->update(['thumb'=>(string)$photoList[$i],'update_time'=>time()]);
  428. // 如果存在旧数据,不存在新数据,删除
  429. if( isset($oldPhoto[$i]) && !isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->delete();
  430. // 如果不存在旧数据,存在新数据,新增
  431. if( !isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->add(['sort'=>$i,'product_id'=>$id,'thumb'=>(string)$photoList[$i],'insert_time'=>time(),'update_time'=>time()]);
  432. }
  433. // 组合数据
  434. foreach ($specAttr as $key => $value) {
  435. // 查询结果
  436. $value['id'] = $ProductAttr->upsertByName($value,$id);
  437. // 提示新增失败
  438. if( !$value['id'] ) {
  439. // 回滚
  440. DB::rollBack();
  441. // 提示
  442. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  443. }
  444. // 重组
  445. $specAttr[$key] = $value;
  446. }
  447. // 如果不在这个属性列表中的数据删除
  448. $ProductAttr->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($specAttr,'id'))->delete();
  449. // 循环SKU
  450. foreach ($skuList as $attrNames => $value) {
  451. // 属性ID值
  452. $value['attr_ids'] = [];
  453. // 规格属性值组合
  454. $value['attr_names']= $attrNames;
  455. // 切割成数据
  456. $names = explode(',',$attrNames);
  457. // 循环处理
  458. foreach ($names as $name) {
  459. foreach ($specAttr as $vv) {
  460. if( $name == $vv['name'] ) {
  461. // 提示新增失败
  462. if( !$vv['id'] ) {
  463. // 回滚
  464. DB::rollBack();
  465. // 提示
  466. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  467. }
  468. $value['attr_ids'][] = $vv['id'];
  469. }
  470. }
  471. }
  472. // 转成好存储的数据
  473. $value['attr_ids'] = implode(',',$value['attr_ids']);
  474. // 转成好存储的数据
  475. $value['sku_thumb'] = (string)$value['sku_thumb'];
  476. // 转成好存储的数据
  477. $value['stock_total']= $value['stock'];
  478. // 转成好存储的数据
  479. $value['product_id']= $id;
  480. // 查询
  481. $oldSkuId = $ProductSkus->query()->where([['product_id','=',$id],['attr_ids','=',$value['attr_ids']]])->value('id');
  482. // 判断是否存在ID
  483. $value['id'] = $oldSkuId ? $ProductSkus->edit($oldSkuId,$value) : $ProductSkus->add($value);
  484. // SKU结果
  485. $skuList[$attrNames]= $value;
  486. }
  487. // 如果不在这个属性列表中的数据删除
  488. $ProductSkus->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($skuList,'id'))->delete();
  489. // 获取之前的城市ID
  490. $oldCityIds = $ProductCity->getListByProductId($id);
  491. // 循环城市范围
  492. foreach ($cityIds as $key => $value) {
  493. // 不存在则新增
  494. if( !in_array($value,$oldCityIds) ) $ProductCity->add(['city_id'=>$value,'product_id'=>$id]);
  495. }
  496. // 不存在的城市删除
  497. $ProductCity->query()->where([['product_id','=',$id]])->whereNotIn('city_id',$cityIds)->delete();
  498. // 更新内容
  499. $Model->updateDesc($id,$description);
  500. // 提交
  501. DB::commit();
  502. // 记录行为
  503. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  504. // 告知结果
  505. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  506. } catch (\Throwable $th) {
  507. // 回滚
  508. DB::rollBack();
  509. // 提示
  510. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage() .'=>'.$th->getLine()]);
  511. }
  512. }
  513. // 接收参数
  514. $id = request('id',0);
  515. // 查询数据
  516. $oldData = $Model->where(['id'=>$id])->first();
  517. // 如果是没有数据
  518. if( !$oldData ) return $this->error('查无数据');
  519. // 产品信息转格式
  520. $oldData = $oldData->toArray();
  521. $oldData['description'] = $Model->getDesc($id);
  522. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  523. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  524. $oldData['tag_exclude'] = explode(',',$oldData['tag_exclude']);
  525. $photoList = $ProductPhoto->getListByProductId($id);
  526. // 获取产品属性
  527. $attrList = $ProductAttr->getListByProductId($id);
  528. // 规格属性
  529. $specList = [];
  530. // 获取数据
  531. foreach ($attrList as $value) {
  532. $value['active'] = 0;
  533. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  534. $specList[$value['spec_id']]['attr_list'][] = $value;
  535. }
  536. // 产品类型
  537. $skuList = $ProductSkus->getListByProductId($id);
  538. // 循环处理
  539. foreach ($skuList as $key => $value) {
  540. // 数据解析
  541. $value['attr_ids'] = explode(',',$value['attr_ids']);
  542. // 循环处理
  543. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  544. // 获取ids对应的name
  545. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  546. }
  547. // 数据合并
  548. $value['attr_ids'] = implode(',',$value['attr_ids']);
  549. // 重组
  550. $skuList[$key] = $value;
  551. }
  552. // 获取类型数据
  553. $typeList = $ProductType->getList();
  554. $cityList = $City->getCityList();
  555. $businessList = $Business->getList();
  556. $producerList = $Producer->getList();
  557. $courseList = $Course->getList();
  558. // 标签列表
  559. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  560. // 标签列表
  561. $tagList = [];
  562. // 循环数据
  563. foreach ($tagData as $value) {
  564. $tagList[$value['group']][] = $value['name'];
  565. }
  566. // 分配数据
  567. $this->assign('tagList',$tagList);
  568. $this->assign('typeList',$typeList);
  569. $this->assign('cityList',$cityList);
  570. $this->assign('businessList',$businessList);
  571. $this->assign('producerList',$producerList);
  572. $this->assign('oldData',$oldData);
  573. $this->assign('photoList',$photoList);
  574. $this->assign('skuList',$skuList);
  575. $this->assign('specList',$specList);
  576. $this->assign('courseList',$courseList);
  577. $this->assign('crumbs','修改');
  578. // 加载模板
  579. return $this->fetch();
  580. }
  581. /**
  582. * 编辑
  583. *
  584. * */
  585. public function copy( Request $request, Model $Model,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  586. if( request()->isMethod('post') ){
  587. // 验证参数
  588. $request->scene('add')->validate();
  589. // 组合数据
  590. $data['name'] = request('name','');
  591. $data['thumb'] = request('thumb','');
  592. $data['poster'] = request('poster','');
  593. $data['spec'] = request('spec','');
  594. $data['price'] = request('price',0);
  595. $data['market_price'] = request('market_price',0);
  596. $data['producer_id'] = request('producer_id',0);
  597. $data['business_id'] = request('business_id',0);
  598. $data['quota'] = request('quota',0);
  599. $data['quota_start'] = request('quota_start','');
  600. $data['quota_end'] = request('quota_end','');
  601. $data['stock'] = request('stock',0);
  602. $data['status'] = 1;
  603. $data['admin_uid'] = admin('uid');
  604. $description = request('description','');
  605. $attr = request('attr',[]);
  606. $skuList = request('sku',[]);
  607. $cityIds = request('city_ids',[]);
  608. $photoList = request('photo_list',[]);
  609. $tagScope = request('tag_scope',[]);
  610. $data['tag_scope'] = implode(',',$tagScope);
  611. $tagExclude = request('tag_exclude',[]);
  612. $data['tag_exclude'] = implode(',',$tagExclude);
  613. // 循环
  614. foreach ($photoList as $key => $value) {
  615. if( !$value ) unset($photoList[$key]);
  616. }
  617. $photoList = array_values($photoList);
  618. // 如果没有选择,则意味着全部
  619. $cityIds = $cityIds ? $cityIds : [1];
  620. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  621. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  622. // 限购提示
  623. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  624. // 限购提示
  625. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  626. // 限购提示
  627. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  628. // 总库存
  629. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  630. // 总库存
  631. $data['stock_total'] = $data['stock'];
  632. // 获取规格属性
  633. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  634. // 开启事务
  635. DB::beginTransaction();
  636. try {
  637. // 写入
  638. $id = $Model->add($data);
  639. // 提示新增失败
  640. if( !$id ) {
  641. // 回滚
  642. DB::rollBack();
  643. // 提示
  644. return json_send(['code'=>'error','msg'=>'新增失败']);
  645. }
  646. // 图册
  647. foreach ($photoList as $key => $value) {
  648. // 整理数据
  649. $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  650. // 重新整理
  651. $photoList[$key] = $value;
  652. }
  653. // 存在图册
  654. if( $photoList ) {
  655. // 写入失败
  656. $result = $ProductPhoto->query()->insert($photoList);
  657. // 提示新增失败
  658. if( !$result ) {
  659. // 回滚
  660. DB::rollBack();
  661. // 提示
  662. return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
  663. }
  664. }
  665. // 组合数据
  666. foreach ($specAttr as $key => $value) {
  667. // 查询结果
  668. $value['id'] = $ProductAttr->upsertByName($value,$id);
  669. // 提示新增失败
  670. if( !$value['id'] ) {
  671. // 回滚
  672. DB::rollBack();
  673. // 提示
  674. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  675. }
  676. // 重组
  677. $specAttr[$key] = $value;
  678. }
  679. // 循环SKU
  680. foreach ($skuList as $attrNames => $value) {
  681. // 属性ID值
  682. $value['attr_ids'] = [];
  683. // 规格属性值组合
  684. $value['attr_names']= $attrNames;
  685. // 切割成数据
  686. $names = explode(',',$attrNames);
  687. // 循环处理
  688. foreach ($names as $name) {
  689. foreach ($specAttr as $vv) {
  690. if( $name == $vv['name'] ) {
  691. // 提示新增失败
  692. if( !$vv['id'] ) {
  693. // 回滚
  694. DB::rollBack();
  695. // 提示
  696. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  697. }
  698. $value['attr_ids'][] = $vv['id'];
  699. }
  700. }
  701. }
  702. // 转成好存储的数据
  703. $value['attr_ids'] = implode(',',$value['attr_ids']);
  704. // 转成好存储的数据
  705. $value['product_id']= $id;
  706. // 转成好存储的数据
  707. $value['sku_thumb'] = (string) $value['sku_thumb'];
  708. // 转成好存储的数据
  709. $value['stock_total']= $value['stock'];
  710. // 转成好存储的数据
  711. $value['insert_time']= time();
  712. // 转成好存储的数据
  713. $value['update_time']= time();
  714. // SKU结果
  715. $skuList[$attrNames] = $value;
  716. }
  717. // 循环城市范围
  718. foreach ($cityIds as $key => $value) {
  719. // 重组数据
  720. $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  721. }
  722. // 写入城市范围
  723. $ProductCity->query()->insert($cityIds);
  724. // 返回结果
  725. $result = $ProductSkus->insert($skuList);
  726. // 提示新增失败
  727. if( !$result ) {
  728. // 回滚
  729. DB::rollBack();
  730. // 提示
  731. return json_send(['code'=>'error','msg'=>'SKU插入失败']);
  732. }
  733. // 更新内容
  734. $Model->updateDesc($id,$description);
  735. // 提交
  736. DB::commit();
  737. // 记录行为
  738. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  739. // 告知结果
  740. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  741. } catch (\Throwable $th) {
  742. // 回滚
  743. DB::rollBack();
  744. // 提示
  745. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  746. }
  747. }
  748. // 接收参数
  749. $id = request('id',0);
  750. // 查询数据
  751. $oldData = $Model->where(['id'=>$id])->first();
  752. // 如果是没有数据
  753. if( !$oldData ) return $this->error('查无数据');
  754. // 产品信息转格式
  755. $oldData = $oldData->toArray();
  756. $oldData['description'] = $Model->getDesc($id);
  757. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  758. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  759. $oldData['tag_exclude'] = explode(',',$oldData['tag_exclude']);
  760. $photoList = $ProductPhoto->getListByProductId($id);
  761. // 获取产品属性
  762. $attrList = $ProductAttr->getListByProductId($id);
  763. // 规格属性
  764. $specList = [];
  765. // 获取数据
  766. foreach ($attrList as $value) {
  767. $value['active'] = 0;
  768. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  769. $specList[$value['spec_id']]['attr_list'][] = $value;
  770. }
  771. // 产品类型
  772. $skuList = $ProductSkus->getListByProductId($id);
  773. // 循环处理
  774. foreach ($skuList as $key => $value) {
  775. // 数据解析
  776. $value['attr_ids'] = explode(',',$value['attr_ids']);
  777. // 循环处理
  778. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  779. // 获取ids对应的name
  780. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  781. }
  782. // 数据合并
  783. $value['attr_ids'] = implode(',',$value['attr_ids']);
  784. // 重组
  785. $skuList[$key] = $value;
  786. }
  787. // 标签列表
  788. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  789. // 标签列表
  790. $tagList = [];
  791. // 循环数据
  792. foreach ($tagData as $value) {
  793. $tagList[$value['group']][] = $value['name'];
  794. }
  795. // 获取类型数据
  796. $typeList = $ProductType->getList();
  797. $cityList = $City->getCityList();
  798. $businessList = $Business->getList();
  799. $producerList = $Producer->getList();
  800. // 分配数据
  801. $this->assign('tagList',$tagList);
  802. $this->assign('typeList',$typeList);
  803. $this->assign('cityList',$cityList);
  804. $this->assign('businessList',$businessList);
  805. $this->assign('producerList',$producerList);
  806. $this->assign('oldData',$oldData);
  807. $this->assign('photoList',$photoList);
  808. $this->assign('skuList',$skuList);
  809. $this->assign('specList',$specList);
  810. $this->assign('crumbs','复制');
  811. // 加载模板
  812. return $this->fetch();
  813. }
  814. /**
  815. * 状态
  816. *
  817. * */
  818. public function set_status( Request $request, Model $Model ){
  819. // 验证参数
  820. $request->scene('set_status')->validate();
  821. // 接收参数
  822. $id = request('id',0);
  823. $status = request('status',0);
  824. // 查询数据
  825. $result = $Model->edit($id,['status'=>$status]);
  826. // 提示新增失败
  827. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  828. // 记录行为
  829. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  830. // 告知结果
  831. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  832. }
  833. /**
  834. * 获取某个类目下的规格
  835. *
  836. */
  837. public function get_spec_html( ProductSpec $ProductSpec){
  838. // 接收参数
  839. $typeId = request('type_id',0);
  840. // 查询数据
  841. $specList = $ProductSpec->getList();
  842. // 循环处理
  843. foreach ($specList as $key => $value) {
  844. if( $value['type_id'] != $typeId ) unset($specList[$key]);
  845. }
  846. // 分配数据
  847. $this->assign('specList',$specList);
  848. // 加载模板
  849. return $this->fetch();
  850. }
  851. /**
  852. * 获取某个类目下的规格
  853. *
  854. */
  855. public function get_sku_html( ProductSpec $ProductSpec,ProductSkus $ProductSkus){
  856. // 接收参数
  857. $attr = request('attr',[]);
  858. $id = request('product_id',0);
  859. // 产品类型
  860. $oldSkus = $id ? $ProductSkus->getListByProductId($id) : [];
  861. // 获取规格属性
  862. $specAttr = $this->getSpecAttr($attr,$ProductSpec);
  863. // 组合SKU
  864. $brushList = [];
  865. // 循环规格属性
  866. foreach ($specAttr as $specId => $value) {
  867. // 获取SKU组合
  868. $brushList[$specId] = array_column($value['attr_list'],'name');
  869. }
  870. // sku列表
  871. $skuList = $this->brush([],$brushList);
  872. // 循环规格属性
  873. foreach ($skuList as $newKey => $new) {
  874. // 获取新数据
  875. $new = ['attr_names'=>$new,'price'=>0,'stock'=>0,'status'=>0,'sku_thumb'=>'','course_number'=>0];
  876. // 循环旧的sku
  877. foreach ($oldSkus as $old) {
  878. // 如果有相等的规格
  879. if( $old['attr_names'] == $new['attr_names']) {
  880. $new['sku_thumb']= $old['sku_thumb'];
  881. $new['price'] = $old['price'];
  882. $new['stock'] = $old['stock'];
  883. $new['status'] = $old['status'];
  884. $new['course_number'] = $old['course_number'];
  885. }
  886. }
  887. $skuList[$newKey] = $new;
  888. }
  889. // 分配数据
  890. $this->assign('specAttr',$specAttr);
  891. $this->assign('skuList',$skuList);
  892. // 加载模板
  893. return $this->fetch();
  894. }
  895. /**
  896. * 获取规格属性
  897. * @param ProductSpec $ProductSpec
  898. *
  899. */
  900. private function getSpecAttr($attr,$ProductSpec,$onlyList=false){
  901. // 组合参数
  902. $specAttr = [];
  903. // 循环处理数据
  904. foreach ($attr as $specId => $value) {
  905. $specAttr[$specId]['spec_id'] = $specId;
  906. $specAttr[$specId]['spec_name'] = $ProductSpec->getOne($specId,'name');
  907. $specAttr[$specId]['attr_list'] = [];
  908. foreach ($value['name'] as $key => $name) {
  909. // 如果没有名称的,跳过
  910. if( !$name ) continue;
  911. // 结果
  912. $temp = ['id'=>0,'spec_id'=>$specId,'thumb'=>'','name'=>'','remark'=>''];
  913. // 获取名称
  914. $temp['name'] = str_ireplace(',','',$name);
  915. // 获取对应的备注信息
  916. if( !empty($value['remark'][$key]) ) $temp['remark'] = $value['remark'][$key];
  917. if( !empty($value['thumb'][$key]) ) $temp['thumb'] = $value['thumb'][$key];
  918. // 获取对应的数据
  919. $specAttr[$specId]['attr_list'][]= $temp;
  920. }
  921. // 没有属性的规格,移除
  922. if( !$specAttr[$specId]['attr_list'] ) unset($specAttr[$specId]);
  923. }
  924. // 仅获取属性列表
  925. if( $onlyList ) {
  926. // 属性列表
  927. $attrList = [];
  928. // 判断结果
  929. foreach ($specAttr as $value) {
  930. // 获取数据
  931. array_push($attrList,...$value['attr_list']);
  932. }
  933. // 返回结果
  934. $specAttr = $attrList;
  935. }
  936. // 返回结果
  937. return $specAttr;
  938. }
  939. /**
  940. * 组合SKU
  941. *
  942. */
  943. private function brush($res = [], $arr = []){
  944. // 获取第一个规格的属性
  945. if (empty($res)) $res = (array) array_shift($arr);
  946. // 如果只有一个规格,返回该规格的属性即可
  947. if (empty($arr)) return $res;
  948. // 接下来要参与计算的一组属性
  949. $current = array_shift($arr);
  950. // 计算的列表
  951. $last = [];
  952. // 循环上一次已经算出的集合
  953. foreach ($res as $attr) {
  954. foreach ($current as $col_val) {
  955. $last[] = $attr . ',' . $col_val;
  956. }
  957. }
  958. return $this->brush($last,$arr); # 递归处理, 直到$arr滚到最后一组属性
  959. }
  960. /**
  961. * 排序
  962. *
  963. * */
  964. public function set_sort( Request $request, Model $Model ){
  965. // 验证参数
  966. $request->scene('set_sort')->validate();
  967. // 接收参数
  968. $id = request('id',0);
  969. $sort = request('sort',0);
  970. // 查询数据
  971. $result = $Model->edit($id,['sort'=>$sort]);
  972. // 提示新增失败
  973. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  974. // 记录行为
  975. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['sort'=>$sort]);
  976. // 告知结果
  977. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  978. }
  979. }