Product.php 31 KB

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