Product.php 32 KB

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