Product.php 28 KB

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