Product.php 34 KB

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