Product.php 25 KB

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