Product.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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['sku_thumb'] = $value['sku_thumb'];
  239. // 转成好存储的数据
  240. $value['stock_total']= $value['stock'];
  241. // 转成好存储的数据
  242. $value['insert_time']= time();
  243. // 转成好存储的数据
  244. $value['update_time']= time();
  245. // SKU结果
  246. $skuList[$attrNames] = $value;
  247. }
  248. // 循环城市范围
  249. foreach ($cityIds as $key => $value) {
  250. // 重组数据
  251. $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  252. }
  253. // 写入城市范围
  254. $ProductCity->query()->insert($cityIds);
  255. // 返回结果
  256. $result = $ProductSkus->insert($skuList);
  257. // 提示新增失败
  258. if( !$result ) {
  259. // 回滚
  260. DB::rollBack();
  261. // 提示
  262. return json_send(['code'=>'error','msg'=>'SKU插入失败']);
  263. }
  264. // 更新内容
  265. $Model->updateDesc($id,$description);
  266. // 提交
  267. DB::commit();
  268. // 记录行为
  269. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  270. // 告知结果
  271. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  272. } catch (\Throwable $th) {
  273. // 回滚
  274. DB::rollBack();
  275. // 提示
  276. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  277. }
  278. }
  279. // 获取类型数据
  280. $typeList = $ProductType->getList();
  281. $cityList = $City->getCityList();
  282. $businessList = $Business->getList();
  283. $producerList = $Producer->getList();
  284. // 分配数据
  285. $this->assign('crumbs','新增');
  286. $this->assign('typeList',$typeList);
  287. $this->assign('cityList',$cityList);
  288. $this->assign('businessList',$businessList);
  289. $this->assign('producerList',$producerList);
  290. // 加载模板
  291. return $this->fetch();
  292. }
  293. /**
  294. * 编辑
  295. *
  296. * */
  297. 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){
  298. if(request()->isMethod('post')){
  299. // 验证参数
  300. $request->scene('edit')->validate();
  301. // 组合数据
  302. $id = request('id',0);
  303. $data['name'] = request('name','');
  304. $data['thumb'] = request('thumb','');
  305. $data['poster'] = request('poster','');
  306. $data['spec'] = request('spec','');
  307. $data['price'] = request('price',0);
  308. $data['market_price'] = request('market_price',0);
  309. $data['quota'] = request('quota',0);
  310. $data['quota_start'] = request('quota_start','');
  311. $data['quota_end'] = request('quota_end','');
  312. $data['puton_time'] = request('puton_time','');
  313. $data['putoff_time'] = request('putoff_time','');
  314. $data['producer_id'] = request('producer_id',0);
  315. $data['business_id'] = request('business_id',0);
  316. $data['stock'] = request('stock',0);
  317. $data['status'] = 1;
  318. $description = request('description','');
  319. $attr = request('attr',[]);
  320. $skuList = request('sku',[]);
  321. $cityIds = request('city_ids',[]);
  322. $photoList = request('photo_list',[]);
  323. // 循环
  324. foreach ($photoList as $key => $value) {
  325. if( !$value ) unset($photoList[$key]);
  326. }
  327. $photoList = array_values($photoList);
  328. // 如果没有选择,则意味着全部
  329. $cityIds = $cityIds ? $cityIds : [1];
  330. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  331. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  332. $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
  333. $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
  334. // 限购提示
  335. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  336. // 限购提示
  337. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  338. // 限购提示
  339. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  340. // 上下架
  341. if( $data['puton_time'] ) {
  342. // 下架时间必填
  343. if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
  344. // 下架时间必填
  345. if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
  346. }
  347. // 总库存
  348. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  349. // 总库存
  350. $data['stock_total'] = $data['stock'];
  351. // 获取规格属性
  352. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  353. // 开启事务
  354. DB::beginTransaction();
  355. try {
  356. // 写入
  357. $result = $Model->edit($id,$data);
  358. // 提示新增失败
  359. if( !$result ) {
  360. // 回滚
  361. DB::rollBack();
  362. // 提示
  363. return json_send(['code'=>'error','msg'=>'修改失败']);
  364. }
  365. // 获取旧图册
  366. $oldPhoto = $ProductPhoto->getListByProductId($id);
  367. // 循环个数
  368. for ($i=0; $i < 4; $i++) {
  369. // 如果存在旧数据与新数据,修改
  370. if( isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->update(['thumb'=>(string)$photoList[$i],'update_time'=>time()]);
  371. // 如果存在旧数据,不存在新数据,删除
  372. if( isset($oldPhoto[$i]) && !isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->delete();
  373. // 如果不存在旧数据,存在新数据,新增
  374. if( !isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->add(['sort'=>$i,'product_id'=>$id,'thumb'=>(string)$photoList[$i],'insert_time'=>time(),'update_time'=>time()]);
  375. }
  376. // 组合数据
  377. foreach ($specAttr as $key => $value) {
  378. // 查询结果
  379. $value['id'] = $ProductAttr->upsertByName($value['name'],$id,$value['spec_id']);
  380. // 提示新增失败
  381. if( !$value['id'] ) {
  382. // 回滚
  383. DB::rollBack();
  384. // 提示
  385. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  386. }
  387. // 重组
  388. $specAttr[$key] = $value;
  389. }
  390. // 如果不在这个属性列表中的数据删除
  391. $ProductAttr->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($specAttr,'id'))->delete();
  392. // 循环SKU
  393. foreach ($skuList as $attrNames => $value) {
  394. // 属性ID值
  395. $value['attr_ids'] = [];
  396. // 规格属性值组合
  397. $value['attr_names']= $attrNames;
  398. // 切割成数据
  399. $names = explode(',',$attrNames);
  400. // 循环处理
  401. foreach ($names as $name) {
  402. foreach ($specAttr as $vv) {
  403. if( $name == $vv['name'] ) {
  404. // 提示新增失败
  405. if( !$vv['id'] ) {
  406. // 回滚
  407. DB::rollBack();
  408. // 提示
  409. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  410. }
  411. $value['attr_ids'][] = $vv['id'];
  412. }
  413. }
  414. }
  415. // 转成好存储的数据
  416. $value['attr_ids'] = implode(',',$value['attr_ids']);
  417. // 转成好存储的数据
  418. $value['sku_thumb'] = $value['sku_thumb'];
  419. // 转成好存储的数据
  420. $value['stock_total']= $value['stock'];
  421. // 转成好存储的数据
  422. $value['product_id']= $id;
  423. // 查询
  424. $oldSkuId = $ProductSkus->query()->where([['product_id','=',$id],['attr_ids','=',$value['attr_ids']]])->value('id');
  425. // 判断是否存在ID
  426. $value['id'] = $oldSkuId ? $ProductSkus->edit($oldSkuId,$value) : $ProductSkus->add($value);
  427. // SKU结果
  428. $skuList[$attrNames]= $value;
  429. }
  430. // 如果不在这个属性列表中的数据删除
  431. $ProductSkus->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($skuList,'id'))->delete();
  432. // 获取之前的城市ID
  433. $oldCityIds = $ProductCity->getListByProductId($id);
  434. // 循环城市范围
  435. foreach ($cityIds as $key => $value) {
  436. // 不存在则新增
  437. if( !in_array($value,$oldCityIds) ) $ProductCity->add(['city_id'=>$value,'product_id'=>$id]);
  438. }
  439. // 不存在的城市删除
  440. $ProductCity->query()->where([['product_id','=',$id]])->whereNotIn('city_id',$cityIds)->delete();
  441. // 更新内容
  442. $Model->updateDesc($id,$description);
  443. // 提交
  444. DB::commit();
  445. // 记录行为
  446. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  447. // 告知结果
  448. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  449. } catch (\Throwable $th) {
  450. // 回滚
  451. DB::rollBack();
  452. // 提示
  453. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  454. }
  455. }
  456. // 接收参数
  457. $id = request('id',0);
  458. // 查询数据
  459. $oldData = $Model->where(['id'=>$id])->first();
  460. // 如果是没有数据
  461. if( !$oldData ) return $this->error('查无数据');
  462. // 产品信息转格式
  463. $oldData = $oldData->toArray();
  464. $oldData['description'] = $Model->getDesc($id);
  465. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  466. $photoList = $ProductPhoto->getListByProductId($id);
  467. // 获取产品属性
  468. $attrList = $ProductAttr->getListByProductId($id);
  469. // 规格属性
  470. $specList = [];
  471. // 获取数据
  472. foreach ($attrList as $value) {
  473. $value['active'] = 0;
  474. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  475. $specList[$value['spec_id']]['attr_list'][] = $value;
  476. }
  477. // 产品类型
  478. $skuList = $ProductSkus->getListByProductId($id);
  479. // 循环处理
  480. foreach ($skuList as $key => $value) {
  481. // 数据解析
  482. $value['attr_ids'] = explode(',',$value['attr_ids']);
  483. // 循环处理
  484. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  485. // 获取ids对应的name
  486. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  487. }
  488. // 数据合并
  489. $value['attr_ids'] = implode(',',$value['attr_ids']);
  490. // 重组
  491. $skuList[$key] = $value;
  492. }
  493. // 获取类型数据
  494. $typeList = $ProductType->getList();
  495. $cityList = $City->getCityList();
  496. $businessList = $Business->getList();
  497. $producerList = $Producer->getList();
  498. // 分配数据
  499. $this->assign('typeList',$typeList);
  500. $this->assign('cityList',$cityList);
  501. $this->assign('businessList',$businessList);
  502. $this->assign('producerList',$producerList);
  503. $this->assign('oldData',$oldData);
  504. $this->assign('photoList',$photoList);
  505. $this->assign('skuList',$skuList);
  506. $this->assign('specList',$specList);
  507. $this->assign('crumbs','修改');
  508. // 加载模板
  509. return $this->fetch();
  510. }
  511. /**
  512. * 编辑
  513. *
  514. * */
  515. 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){
  516. if( request()->isMethod('post') ){
  517. // 验证参数
  518. $request->scene('add')->validate();
  519. // 组合数据
  520. $data['name'] = request('name','');
  521. $data['thumb'] = request('thumb','');
  522. $data['poster'] = request('poster','');
  523. $data['spec'] = request('spec','');
  524. $data['price'] = request('price',0);
  525. $data['market_price'] = request('market_price',0);
  526. $data['producer_id'] = request('producer_id',0);
  527. $data['business_id'] = request('business_id',0);
  528. $data['quota'] = request('quota',0);
  529. $data['quota_start'] = request('quota_start','');
  530. $data['quota_end'] = request('quota_end','');
  531. $data['stock'] = request('stock',0);
  532. $data['status'] = 1;
  533. $data['admin_uid'] = admin('uid');
  534. $description = request('description','');
  535. $attr = request('attr',[]);
  536. $skuList = request('sku',[]);
  537. $cityIds = request('city_ids',[]);
  538. $photoList = request('photo_list',[]);
  539. // 循环
  540. foreach ($photoList as $key => $value) {
  541. if( !$value ) unset($photoList[$key]);
  542. }
  543. $photoList = array_values($photoList);
  544. // 如果没有选择,则意味着全部
  545. $cityIds = $cityIds ? $cityIds : [1];
  546. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  547. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  548. // 限购提示
  549. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  550. // 限购提示
  551. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  552. // 限购提示
  553. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  554. // 总库存
  555. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  556. // 总库存
  557. $data['stock_total'] = $data['stock'];
  558. // 获取规格属性
  559. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  560. // 开启事务
  561. DB::beginTransaction();
  562. try {
  563. // 写入
  564. $id = $Model->add($data);
  565. // 提示新增失败
  566. if( !$id ) {
  567. // 回滚
  568. DB::rollBack();
  569. // 提示
  570. return json_send(['code'=>'error','msg'=>'新增失败']);
  571. }
  572. // 图册
  573. foreach ($photoList as $key => $value) {
  574. // 整理数据
  575. $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  576. // 重新整理
  577. $photoList[$key] = $value;
  578. }
  579. // 存在图册
  580. if( $photoList ) {
  581. // 写入失败
  582. $result = $ProductPhoto->query()->insert($photoList);
  583. // 提示新增失败
  584. if( !$result ) {
  585. // 回滚
  586. DB::rollBack();
  587. // 提示
  588. return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
  589. }
  590. }
  591. // 组合数据
  592. foreach ($specAttr as $key => $value) {
  593. // 查询结果
  594. $value['id'] = $ProductAttr->upsertByName($value['name'],$id,$value['spec_id']);
  595. // 提示新增失败
  596. if( !$value['id'] ) {
  597. // 回滚
  598. DB::rollBack();
  599. // 提示
  600. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  601. }
  602. // 重组
  603. $specAttr[$key] = $value;
  604. }
  605. // 循环SKU
  606. foreach ($skuList as $attrNames => $value) {
  607. // 属性ID值
  608. $value['attr_ids'] = [];
  609. // 规格属性值组合
  610. $value['attr_names']= $attrNames;
  611. // 切割成数据
  612. $names = explode(',',$attrNames);
  613. // 循环处理
  614. foreach ($names as $name) {
  615. foreach ($specAttr as $vv) {
  616. if( $name == $vv['name'] ) {
  617. // 提示新增失败
  618. if( !$vv['id'] ) {
  619. // 回滚
  620. DB::rollBack();
  621. // 提示
  622. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  623. }
  624. $value['attr_ids'][] = $vv['id'];
  625. }
  626. }
  627. }
  628. // 转成好存储的数据
  629. $value['attr_ids'] = implode(',',$value['attr_ids']);
  630. // 转成好存储的数据
  631. $value['product_id']= $id;
  632. // 转成好存储的数据
  633. $value['sku_thumb'] = $value['sku_thumb'];
  634. // 转成好存储的数据
  635. $value['stock_total']= $value['stock'];
  636. // 转成好存储的数据
  637. $value['insert_time']= time();
  638. // 转成好存储的数据
  639. $value['update_time']= time();
  640. // SKU结果
  641. $skuList[$attrNames] = $value;
  642. }
  643. // 循环城市范围
  644. foreach ($cityIds as $key => $value) {
  645. // 重组数据
  646. $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  647. }
  648. // 写入城市范围
  649. $ProductCity->query()->insert($cityIds);
  650. // 返回结果
  651. $result = $ProductSkus->insert($skuList);
  652. // 提示新增失败
  653. if( !$result ) {
  654. // 回滚
  655. DB::rollBack();
  656. // 提示
  657. return json_send(['code'=>'error','msg'=>'SKU插入失败']);
  658. }
  659. // 更新内容
  660. $Model->updateDesc($id,$description);
  661. // 提交
  662. DB::commit();
  663. // 记录行为
  664. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  665. // 告知结果
  666. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  667. } catch (\Throwable $th) {
  668. // 回滚
  669. DB::rollBack();
  670. // 提示
  671. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  672. }
  673. }
  674. // 接收参数
  675. $id = request('id',0);
  676. // 查询数据
  677. $oldData = $Model->where(['id'=>$id])->first();
  678. // 如果是没有数据
  679. if( !$oldData ) return $this->error('查无数据');
  680. // 产品信息转格式
  681. $oldData = $oldData->toArray();
  682. $oldData['description'] = $Model->getDesc($id);
  683. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  684. $photoList = $ProductPhoto->getListByProductId($id);
  685. // 获取产品属性
  686. $attrList = $ProductAttr->getListByProductId($id);
  687. // 规格属性
  688. $specList = [];
  689. // 获取数据
  690. foreach ($attrList as $value) {
  691. $value['active'] = 0;
  692. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  693. $specList[$value['spec_id']]['attr_list'][] = $value;
  694. }
  695. // 产品类型
  696. $skuList = $ProductSkus->getListByProductId($id);
  697. // 循环处理
  698. foreach ($skuList as $key => $value) {
  699. // 数据解析
  700. $value['attr_ids'] = explode(',',$value['attr_ids']);
  701. // 循环处理
  702. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  703. // 获取ids对应的name
  704. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  705. }
  706. // 数据合并
  707. $value['attr_ids'] = implode(',',$value['attr_ids']);
  708. // 重组
  709. $skuList[$key] = $value;
  710. }
  711. // 获取类型数据
  712. $typeList = $ProductType->getList();
  713. $cityList = $City->getCityList();
  714. $businessList = $Business->getList();
  715. $producerList = $Producer->getList();
  716. // 分配数据
  717. $this->assign('typeList',$typeList);
  718. $this->assign('cityList',$cityList);
  719. $this->assign('businessList',$businessList);
  720. $this->assign('producerList',$producerList);
  721. $this->assign('oldData',$oldData);
  722. $this->assign('photoList',$photoList);
  723. $this->assign('skuList',$skuList);
  724. $this->assign('specList',$specList);
  725. $this->assign('crumbs','复制');
  726. // 加载模板
  727. return $this->fetch();
  728. }
  729. /**
  730. * 状态
  731. *
  732. * */
  733. public function set_status( Request $request, Model $Model ){
  734. // 验证参数
  735. $request->scene('set_status')->validate();
  736. // 接收参数
  737. $id = request('id',0);
  738. $status = request('status',0);
  739. // 查询数据
  740. $result = $Model->edit($id,['status'=>$status]);
  741. // 提示新增失败
  742. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  743. // 记录行为
  744. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  745. // 告知结果
  746. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  747. }
  748. /**
  749. * 获取某个类目下的规格
  750. *
  751. */
  752. public function get_spec_html( ProductSpec $ProductSpec){
  753. // 接收参数
  754. $typeId = request('type_id',0);
  755. // 查询数据
  756. $specList = $ProductSpec->getList();
  757. // 循环处理
  758. foreach ($specList as $key => $value) {
  759. if( $value['type_id'] != $typeId ) unset($specList[$key]);
  760. }
  761. // 分配数据
  762. $this->assign('specList',$specList);
  763. // 加载模板
  764. return $this->fetch();
  765. }
  766. /**
  767. * 获取某个类目下的规格
  768. *
  769. */
  770. public function get_sku_html( ProductSpec $ProductSpec,ProductSkus $ProductSkus){
  771. // 接收参数
  772. $attr = request('attr',[]);
  773. $id = request('product_id',0);
  774. // 产品类型
  775. $oldSkus = $id ? $ProductSkus->getListByProductId($id) : [];
  776. // 获取规格属性
  777. $specAttr = $this->getSpecAttr($attr,$ProductSpec);
  778. // 组合SKU
  779. $brushList = [];
  780. // 循环规格属性
  781. foreach ($specAttr as $specId => $value) {
  782. // 获取SKU组合
  783. $brushList[$specId] = array_column($value['attr_list'],'name');
  784. }
  785. // sku列表
  786. $skuList = $this->brush([],$brushList);
  787. // 循环规格属性
  788. foreach ($skuList as $newKey => $new) {
  789. // 获取新数据
  790. $new = ['attr_names'=>$new,'price'=>0,'stock'=>0,'status'=>0,'sku_thumb'=>''];
  791. // 循环旧的sku
  792. foreach ($oldSkus as $old) {
  793. // 如果有相等的规格
  794. if( $old['attr_names'] == $new['attr_names']) {
  795. $new['sku_thumb']= $old['sku_thumb'];
  796. $new['price'] = $old['price'];
  797. $new['stock'] = $old['stock'];
  798. $new['status'] = $old['status'];
  799. }
  800. }
  801. $skuList[$newKey] = $new;
  802. }
  803. // 分配数据
  804. $this->assign('specAttr',$specAttr);
  805. $this->assign('skuList',$skuList);
  806. // 加载模板
  807. return $this->fetch();
  808. }
  809. /**
  810. * 获取规格属性
  811. * @param ProductSpec $ProductSpec
  812. *
  813. */
  814. private function getSpecAttr($attr,$ProductSpec,$onlyList=false){
  815. // 组合参数
  816. $specAttr = [];
  817. // 循环处理数据
  818. foreach ($attr as $specId => $value) {
  819. $specAttr[$specId]['spec_id'] = $specId;
  820. $specAttr[$specId]['spec_name'] = $ProductSpec->getOne($specId,'name');
  821. $specAttr[$specId]['attr_list'] = [];
  822. foreach ($value['name'] as $key => $name) {
  823. // 如果没有名称的,跳过
  824. if( !$name ) continue;
  825. // 结果
  826. $temp = ['id'=>0,'spec_id'=>$specId,'thumb'=>'','name'=>'','remark'=>''];
  827. // 获取名称
  828. $temp['name'] = $name;
  829. // 获取对应的备注信息
  830. if( !empty($value['remark'][$key]) ) $temp['remark'] = $value['remark'][$key];
  831. if( !empty($value['thumb'][$key]) ) $temp['thumb'] = $value['thumb'][$key];
  832. // 获取对应的数据
  833. $specAttr[$specId]['attr_list'][]= $temp;
  834. }
  835. // 没有属性的规格,移除
  836. if( !$specAttr[$specId]['attr_list'] ) unset($specAttr[$specId]);
  837. }
  838. // 仅获取属性列表
  839. if( $onlyList ) {
  840. // 属性列表
  841. $attrList = [];
  842. // 判断结果
  843. foreach ($specAttr as $value) {
  844. // 获取数据
  845. array_push($attrList,...$value['attr_list']);
  846. }
  847. // 返回结果
  848. $specAttr = $attrList;
  849. }
  850. // 返回结果
  851. return $specAttr;
  852. }
  853. /**
  854. * 组合SKU
  855. *
  856. */
  857. private function brush($res = [], $arr = []){
  858. // 获取第一个规格的属性
  859. if (empty($res)) $res = (array) array_shift($arr);
  860. // 如果只有一个规格,返回该规格的属性即可
  861. if (empty($arr)) return $res;
  862. // 接下来要参与计算的一组属性
  863. $current = array_shift($arr);
  864. // 计算的列表
  865. $last = [];
  866. // 循环上一次已经算出的集合
  867. foreach ($res as $attr) {
  868. foreach ($current as $col_val) {
  869. $last[] = $attr . ',' . $col_val;
  870. }
  871. }
  872. return $this->brush($last,$arr); # 递归处理, 直到$arr滚到最后一组属性
  873. }
  874. /**
  875. * 排序
  876. *
  877. * */
  878. public function set_sort( Request $request, Model $Model ){
  879. // 验证参数
  880. $request->scene('set_sort')->validate();
  881. // 接收参数
  882. $id = request('id',0);
  883. $sort = request('sort',0);
  884. // 查询数据
  885. $result = $Model->edit($id,['sort'=>$sort]);
  886. // 提示新增失败
  887. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  888. // 记录行为
  889. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['sort'=>$sort]);
  890. // 告知结果
  891. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  892. }
  893. }