Product.php 25 KB

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