Product.php 34 KB

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