Product.php 27 KB

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