Product.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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\Models\WeiBan\Tags as WeiBanTags;
  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,WeiBanTags $WeiBanTags,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. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  295. // 标签列表
  296. $tagList = [];
  297. // 循环数据
  298. foreach ($tagData as $value) {
  299. $tagList[$value['group']][] = $value['name'];
  300. }
  301. // 分配数据
  302. $this->assign('crumbs','新增');
  303. $this->assign('tagList',$tagList);
  304. $this->assign('typeList',$typeList);
  305. $this->assign('cityList',$cityList);
  306. $this->assign('businessList',$businessList);
  307. $this->assign('producerList',$producerList);
  308. $this->assign('courseList',$courseList);
  309. // 加载模板
  310. return $this->fetch();
  311. }
  312. /**
  313. * 编辑
  314. *
  315. * */
  316. public function edit( Request $request, Model $Model,Course $Course,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  317. if(request()->isMethod('post')){
  318. // 验证参数
  319. $request->scene('edit')->validate();
  320. // 组合数据
  321. $id = request('id',0);
  322. $data['name'] = request('name','');
  323. $data['thumb'] = request('thumb','');
  324. $data['poster'] = request('poster','');
  325. $data['spec'] = request('spec','');
  326. $data['price'] = request('price',0);
  327. $data['market_price'] = request('market_price',0);
  328. $data['quota'] = request('quota',0);
  329. $data['quota_start'] = request('quota_start','');
  330. $data['quota_end'] = request('quota_end','');
  331. $data['puton_time'] = request('puton_time','');
  332. $data['putoff_time'] = request('putoff_time','');
  333. $data['producer_id'] = request('producer_id',0);
  334. $data['business_id'] = request('business_id',0);
  335. $data['stock'] = request('stock',0);
  336. $data['status'] = 1;
  337. $data['course_id'] = request('course_id',0);
  338. $data['course_number'] = request('course_number',0);
  339. $data['type_id'] = request('type_id',0);
  340. $description = request('description','');
  341. $attr = request('attr',[]);
  342. $skuList = request('sku',[]);
  343. $cityIds = request('city_ids',[]);
  344. $photoList = request('photo_list',[]);
  345. $tagScope = request('tag_scope',[]);
  346. $data['tag_scope'] = implode(',',$tagScope);
  347. $tagExclude = request('tag_exclude',[]);
  348. $data['tag_exclude'] = implode(',',$tagExclude);
  349. // 循环
  350. foreach ($photoList as $key => $value) {
  351. if( !$value ) unset($photoList[$key]);
  352. }
  353. $photoList = array_values($photoList);
  354. // 如果没有选择,则意味着全部
  355. $cityIds = $cityIds ? $cityIds : [1];
  356. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  357. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  358. $data['puton_time'] = $data['puton_time'] ? strtotime($data['puton_time']) : 0;
  359. $data['putoff_time'] = $data['putoff_time'] ? strtotime($data['putoff_time']) : 0;
  360. // 限购提示
  361. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  362. // 限购提示
  363. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  364. // 限购提示
  365. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  366. // 上下架
  367. if( $data['puton_time'] ) {
  368. // 下架时间必填
  369. if( !$data['putoff_time'] ) return json_send(['code'=>'error','msg'=>'请填写自动下架时间','data'=>['error'=>'自动上架请填写下架时间']]);
  370. // 下架时间必填
  371. if( $data['putoff_time'] <= $data['puton_time'] ) return json_send(['code'=>'error','msg'=>'下架时间请晚于上架时间','data'=>['error'=>'下架时间请晚于上架时间']]);
  372. }
  373. // 总库存
  374. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  375. // 总库存
  376. $data['stock_total'] = $data['stock'];
  377. // 获取规格属性
  378. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  379. // 开启事务
  380. DB::beginTransaction();
  381. try {
  382. // 写入
  383. $result = $Model->edit($id,$data);
  384. // 提示新增失败
  385. if( !$result ) {
  386. // 回滚
  387. DB::rollBack();
  388. // 提示
  389. return json_send(['code'=>'error','msg'=>'修改失败']);
  390. }
  391. // 获取旧图册
  392. $oldPhoto = $ProductPhoto->getListByProductId($id);
  393. // 循环个数
  394. for ($i=0; $i < 4; $i++) {
  395. // 如果存在旧数据与新数据,修改
  396. if( isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->update(['thumb'=>(string)$photoList[$i],'update_time'=>time()]);
  397. // 如果存在旧数据,不存在新数据,删除
  398. if( isset($oldPhoto[$i]) && !isset($photoList[$i]) ) $ProductPhoto->query()->where([['product_id','=',$id],['sort','=',$i]])->delete();
  399. // 如果不存在旧数据,存在新数据,新增
  400. if( !isset($oldPhoto[$i]) && isset($photoList[$i]) ) $ProductPhoto->add(['sort'=>$i,'product_id'=>$id,'thumb'=>(string)$photoList[$i],'insert_time'=>time(),'update_time'=>time()]);
  401. }
  402. // 组合数据
  403. foreach ($specAttr as $key => $value) {
  404. // 查询结果
  405. $value['id'] = $ProductAttr->upsertByName($value,$id);
  406. // 提示新增失败
  407. if( !$value['id'] ) {
  408. // 回滚
  409. DB::rollBack();
  410. // 提示
  411. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  412. }
  413. // 重组
  414. $specAttr[$key] = $value;
  415. }
  416. // 如果不在这个属性列表中的数据删除
  417. $ProductAttr->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($specAttr,'id'))->delete();
  418. // 循环SKU
  419. foreach ($skuList as $attrNames => $value) {
  420. // 属性ID值
  421. $value['attr_ids'] = [];
  422. // 规格属性值组合
  423. $value['attr_names']= $attrNames;
  424. // 切割成数据
  425. $names = explode(',',$attrNames);
  426. // 循环处理
  427. foreach ($names as $name) {
  428. foreach ($specAttr as $vv) {
  429. if( $name == $vv['name'] ) {
  430. // 提示新增失败
  431. if( !$vv['id'] ) {
  432. // 回滚
  433. DB::rollBack();
  434. // 提示
  435. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  436. }
  437. $value['attr_ids'][] = $vv['id'];
  438. }
  439. }
  440. }
  441. // 转成好存储的数据
  442. $value['attr_ids'] = implode(',',$value['attr_ids']);
  443. // 转成好存储的数据
  444. $value['sku_thumb'] = (string)$value['sku_thumb'];
  445. // 转成好存储的数据
  446. $value['stock_total']= $value['stock'];
  447. // 转成好存储的数据
  448. $value['product_id']= $id;
  449. // 查询
  450. $oldSkuId = $ProductSkus->query()->where([['product_id','=',$id],['attr_ids','=',$value['attr_ids']]])->value('id');
  451. // 判断是否存在ID
  452. $value['id'] = $oldSkuId ? $ProductSkus->edit($oldSkuId,$value) : $ProductSkus->add($value);
  453. // SKU结果
  454. $skuList[$attrNames]= $value;
  455. }
  456. // 如果不在这个属性列表中的数据删除
  457. $ProductSkus->query()->where([['product_id','=',$id]])->whereNotIn('id',array_column($skuList,'id'))->delete();
  458. // 获取之前的城市ID
  459. $oldCityIds = $ProductCity->getListByProductId($id);
  460. // 循环城市范围
  461. foreach ($cityIds as $key => $value) {
  462. // 不存在则新增
  463. if( !in_array($value,$oldCityIds) ) $ProductCity->add(['city_id'=>$value,'product_id'=>$id]);
  464. }
  465. // 不存在的城市删除
  466. $ProductCity->query()->where([['product_id','=',$id]])->whereNotIn('city_id',$cityIds)->delete();
  467. // 更新内容
  468. $Model->updateDesc($id,$description);
  469. // 提交
  470. DB::commit();
  471. // 记录行为
  472. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  473. // 告知结果
  474. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  475. } catch (\Throwable $th) {
  476. // 回滚
  477. DB::rollBack();
  478. // 提示
  479. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage() .'=>'.$th->getLine()]);
  480. }
  481. }
  482. // 接收参数
  483. $id = request('id',0);
  484. // 查询数据
  485. $oldData = $Model->where(['id'=>$id])->first();
  486. // 如果是没有数据
  487. if( !$oldData ) return $this->error('查无数据');
  488. // 产品信息转格式
  489. $oldData = $oldData->toArray();
  490. $oldData['description'] = $Model->getDesc($id);
  491. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  492. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  493. $oldData['tag_exclude'] = explode(',',$oldData['tag_exclude']);
  494. $photoList = $ProductPhoto->getListByProductId($id);
  495. // 获取产品属性
  496. $attrList = $ProductAttr->getListByProductId($id);
  497. // 规格属性
  498. $specList = [];
  499. // 获取数据
  500. foreach ($attrList as $value) {
  501. $value['active'] = 0;
  502. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  503. $specList[$value['spec_id']]['attr_list'][] = $value;
  504. }
  505. // 产品类型
  506. $skuList = $ProductSkus->getListByProductId($id);
  507. // 循环处理
  508. foreach ($skuList as $key => $value) {
  509. // 数据解析
  510. $value['attr_ids'] = explode(',',$value['attr_ids']);
  511. // 循环处理
  512. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  513. // 获取ids对应的name
  514. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  515. }
  516. // 数据合并
  517. $value['attr_ids'] = implode(',',$value['attr_ids']);
  518. // 重组
  519. $skuList[$key] = $value;
  520. }
  521. // 获取类型数据
  522. $typeList = $ProductType->getList();
  523. $cityList = $City->getCityList();
  524. $businessList = $Business->getList();
  525. $producerList = $Producer->getList();
  526. $courseList = $Course->getList();
  527. // 标签列表
  528. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  529. // 标签列表
  530. $tagList = [];
  531. // 循环数据
  532. foreach ($tagData as $value) {
  533. $tagList[$value['group']][] = $value['name'];
  534. }
  535. // 分配数据
  536. $this->assign('tagList',$tagList);
  537. $this->assign('typeList',$typeList);
  538. $this->assign('cityList',$cityList);
  539. $this->assign('businessList',$businessList);
  540. $this->assign('producerList',$producerList);
  541. $this->assign('oldData',$oldData);
  542. $this->assign('photoList',$photoList);
  543. $this->assign('skuList',$skuList);
  544. $this->assign('specList',$specList);
  545. $this->assign('courseList',$courseList);
  546. $this->assign('crumbs','修改');
  547. // 加载模板
  548. return $this->fetch();
  549. }
  550. /**
  551. * 编辑
  552. *
  553. * */
  554. public function copy( Request $request, Model $Model,WeiBanTags $WeiBanTags,ProductPhoto $ProductPhoto,Producer $Producer,Business $Business,ProductType $ProductType,ProductSpec $ProductSpec,ProductAttr $ProductAttr,ProductSkus $ProductSkus,City $City,ProductCity $ProductCity){
  555. if( request()->isMethod('post') ){
  556. // 验证参数
  557. $request->scene('add')->validate();
  558. // 组合数据
  559. $data['name'] = request('name','');
  560. $data['thumb'] = request('thumb','');
  561. $data['poster'] = request('poster','');
  562. $data['spec'] = request('spec','');
  563. $data['price'] = request('price',0);
  564. $data['market_price'] = request('market_price',0);
  565. $data['producer_id'] = request('producer_id',0);
  566. $data['business_id'] = request('business_id',0);
  567. $data['quota'] = request('quota',0);
  568. $data['quota_start'] = request('quota_start','');
  569. $data['quota_end'] = request('quota_end','');
  570. $data['stock'] = request('stock',0);
  571. $data['status'] = 1;
  572. $data['admin_uid'] = admin('uid');
  573. $description = request('description','');
  574. $attr = request('attr',[]);
  575. $skuList = request('sku',[]);
  576. $cityIds = request('city_ids',[]);
  577. $photoList = request('photo_list',[]);
  578. $tagScope = request('tag_scope',[]);
  579. $data['tag_scope'] = implode(',',$tagScope);
  580. $tagExclude = request('tag_exclude',[]);
  581. $data['tag_exclude'] = implode(',',$tagExclude);
  582. // 循环
  583. foreach ($photoList as $key => $value) {
  584. if( !$value ) unset($photoList[$key]);
  585. }
  586. $photoList = array_values($photoList);
  587. // 如果没有选择,则意味着全部
  588. $cityIds = $cityIds ? $cityIds : [1];
  589. $data['quota_start'] = $data['quota_start'] ? strtotime($data['quota_start']) : 0;
  590. $data['quota_end'] = $data['quota_end'] ? strtotime($data['quota_end']) : 0;
  591. // 限购提示
  592. if( !$data['thumb'] ) return json_send(['code'=>'error','msg'=>'请上传产品主图','data'=>['error'=>'请上传产品主图']]);
  593. // 限购提示
  594. if( $attr && !$skuList ) return json_send(['code'=>'error','msg'=>'规格属性存在时,请填写SKU','data'=>['error'=>'规格属性存在时,请填写SKU']]);
  595. // 限购提示
  596. if( $data['quota'] && ( !$data['quota_start'] || !$data['quota_end'] )) return json_send(['code'=>'error','msg'=>'限购必填限购时间','data'=>['error'=>'限购必填限购时间']]);
  597. // 总库存
  598. if( $skuList ) $data['stock'] = array_sum(array_column($skuList,'stock'));
  599. // 总库存
  600. $data['stock_total'] = $data['stock'];
  601. // 获取规格属性
  602. $specAttr = $this->getSpecAttr($attr,$ProductSpec,true);
  603. // 开启事务
  604. DB::beginTransaction();
  605. try {
  606. // 写入
  607. $id = $Model->add($data);
  608. // 提示新增失败
  609. if( !$id ) {
  610. // 回滚
  611. DB::rollBack();
  612. // 提示
  613. return json_send(['code'=>'error','msg'=>'新增失败']);
  614. }
  615. // 图册
  616. foreach ($photoList as $key => $value) {
  617. // 整理数据
  618. $value = ['sort'=>$key,'thumb'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  619. // 重新整理
  620. $photoList[$key] = $value;
  621. }
  622. // 存在图册
  623. if( $photoList ) {
  624. // 写入失败
  625. $result = $ProductPhoto->query()->insert($photoList);
  626. // 提示新增失败
  627. if( !$result ) {
  628. // 回滚
  629. DB::rollBack();
  630. // 提示
  631. return json_send(['code'=>'error','msg'=>'商品图册写入失败']);
  632. }
  633. }
  634. // 组合数据
  635. foreach ($specAttr as $key => $value) {
  636. // 查询结果
  637. $value['id'] = $ProductAttr->upsertByName($value,$id);
  638. // 提示新增失败
  639. if( !$value['id'] ) {
  640. // 回滚
  641. DB::rollBack();
  642. // 提示
  643. return json_send(['code'=>'error','msg'=>'商品属性新增失败']);
  644. }
  645. // 重组
  646. $specAttr[$key] = $value;
  647. }
  648. // 循环SKU
  649. foreach ($skuList as $attrNames => $value) {
  650. // 属性ID值
  651. $value['attr_ids'] = [];
  652. // 规格属性值组合
  653. $value['attr_names']= $attrNames;
  654. // 切割成数据
  655. $names = explode(',',$attrNames);
  656. // 循环处理
  657. foreach ($names as $name) {
  658. foreach ($specAttr as $vv) {
  659. if( $name == $vv['name'] ) {
  660. // 提示新增失败
  661. if( !$vv['id'] ) {
  662. // 回滚
  663. DB::rollBack();
  664. // 提示
  665. return json_send(['code'=>'error','msg'=>'属性SKU匹配失败']);
  666. }
  667. $value['attr_ids'][] = $vv['id'];
  668. }
  669. }
  670. }
  671. // 转成好存储的数据
  672. $value['attr_ids'] = implode(',',$value['attr_ids']);
  673. // 转成好存储的数据
  674. $value['product_id']= $id;
  675. // 转成好存储的数据
  676. $value['sku_thumb'] = (string) $value['sku_thumb'];
  677. // 转成好存储的数据
  678. $value['stock_total']= $value['stock'];
  679. // 转成好存储的数据
  680. $value['insert_time']= time();
  681. // 转成好存储的数据
  682. $value['update_time']= time();
  683. // SKU结果
  684. $skuList[$attrNames] = $value;
  685. }
  686. // 循环城市范围
  687. foreach ($cityIds as $key => $value) {
  688. // 重组数据
  689. $cityIds[$key] = ['city_id'=>$value,'product_id'=>$id,'insert_time'=>time(),'update_time'=>time()];
  690. }
  691. // 写入城市范围
  692. $ProductCity->query()->insert($cityIds);
  693. // 返回结果
  694. $result = $ProductSkus->insert($skuList);
  695. // 提示新增失败
  696. if( !$result ) {
  697. // 回滚
  698. DB::rollBack();
  699. // 提示
  700. return json_send(['code'=>'error','msg'=>'SKU插入失败']);
  701. }
  702. // 更新内容
  703. $Model->updateDesc($id,$description);
  704. // 提交
  705. DB::commit();
  706. // 记录行为
  707. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  708. // 告知结果
  709. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  710. } catch (\Throwable $th) {
  711. // 回滚
  712. DB::rollBack();
  713. // 提示
  714. return json_send(['code'=>'error','msg'=>'系统异常,写入失败','data'=>$th->getMessage()]);
  715. }
  716. }
  717. // 接收参数
  718. $id = request('id',0);
  719. // 查询数据
  720. $oldData = $Model->where(['id'=>$id])->first();
  721. // 如果是没有数据
  722. if( !$oldData ) return $this->error('查无数据');
  723. // 产品信息转格式
  724. $oldData = $oldData->toArray();
  725. $oldData['description'] = $Model->getDesc($id);
  726. $oldData['city_ids'] = $ProductCity->getListByProductId($id);
  727. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  728. $oldData['tag_exclude'] = explode(',',$oldData['tag_exclude']);
  729. $photoList = $ProductPhoto->getListByProductId($id);
  730. // 获取产品属性
  731. $attrList = $ProductAttr->getListByProductId($id);
  732. // 规格属性
  733. $specList = [];
  734. // 获取数据
  735. foreach ($attrList as $value) {
  736. $value['active'] = 0;
  737. if( !isset($specList[$value['spec_id']]) ) $specList[$value['spec_id']] = $ProductSpec->getOne($value['spec_id']);
  738. $specList[$value['spec_id']]['attr_list'][] = $value;
  739. }
  740. // 产品类型
  741. $skuList = $ProductSkus->getListByProductId($id);
  742. // 循环处理
  743. foreach ($skuList as $key => $value) {
  744. // 数据解析
  745. $value['attr_ids'] = explode(',',$value['attr_ids']);
  746. // 循环处理
  747. foreach ( $value['attr_ids'] as $k=>$attrId ) {
  748. // 获取ids对应的name
  749. $value['attr_ids'][$k] = $ProductAttr->getValueById($attrId,'name');
  750. }
  751. // 数据合并
  752. $value['attr_ids'] = implode(',',$value['attr_ids']);
  753. // 重组
  754. $skuList[$key] = $value;
  755. }
  756. // 标签列表
  757. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  758. // 标签列表
  759. $tagList = [];
  760. // 循环数据
  761. foreach ($tagData as $value) {
  762. $tagList[$value['group']][] = $value['name'];
  763. }
  764. // 获取类型数据
  765. $typeList = $ProductType->getList();
  766. $cityList = $City->getCityList();
  767. $businessList = $Business->getList();
  768. $producerList = $Producer->getList();
  769. // 分配数据
  770. $this->assign('tagList',$tagList);
  771. $this->assign('typeList',$typeList);
  772. $this->assign('cityList',$cityList);
  773. $this->assign('businessList',$businessList);
  774. $this->assign('producerList',$producerList);
  775. $this->assign('oldData',$oldData);
  776. $this->assign('photoList',$photoList);
  777. $this->assign('skuList',$skuList);
  778. $this->assign('specList',$specList);
  779. $this->assign('crumbs','复制');
  780. // 加载模板
  781. return $this->fetch();
  782. }
  783. /**
  784. * 状态
  785. *
  786. * */
  787. public function set_status( Request $request, Model $Model ){
  788. // 验证参数
  789. $request->scene('set_status')->validate();
  790. // 接收参数
  791. $id = request('id',0);
  792. $status = request('status',0);
  793. // 查询数据
  794. $result = $Model->edit($id,['status'=>$status]);
  795. // 提示新增失败
  796. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  797. // 记录行为
  798. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  799. // 告知结果
  800. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  801. }
  802. /**
  803. * 获取某个类目下的规格
  804. *
  805. */
  806. public function get_spec_html( ProductSpec $ProductSpec){
  807. // 接收参数
  808. $typeId = request('type_id',0);
  809. // 查询数据
  810. $specList = $ProductSpec->getList();
  811. // 循环处理
  812. foreach ($specList as $key => $value) {
  813. if( $value['type_id'] != $typeId ) unset($specList[$key]);
  814. }
  815. // 分配数据
  816. $this->assign('specList',$specList);
  817. // 加载模板
  818. return $this->fetch();
  819. }
  820. /**
  821. * 获取某个类目下的规格
  822. *
  823. */
  824. public function get_sku_html( ProductSpec $ProductSpec,ProductSkus $ProductSkus){
  825. // 接收参数
  826. $attr = request('attr',[]);
  827. $id = request('product_id',0);
  828. // 产品类型
  829. $oldSkus = $id ? $ProductSkus->getListByProductId($id) : [];
  830. // 获取规格属性
  831. $specAttr = $this->getSpecAttr($attr,$ProductSpec);
  832. // 组合SKU
  833. $brushList = [];
  834. // 循环规格属性
  835. foreach ($specAttr as $specId => $value) {
  836. // 获取SKU组合
  837. $brushList[$specId] = array_column($value['attr_list'],'name');
  838. }
  839. // sku列表
  840. $skuList = $this->brush([],$brushList);
  841. // 循环规格属性
  842. foreach ($skuList as $newKey => $new) {
  843. // 获取新数据
  844. $new = ['attr_names'=>$new,'price'=>0,'stock'=>0,'status'=>0,'sku_thumb'=>'','course_number'=>0];
  845. // 循环旧的sku
  846. foreach ($oldSkus as $old) {
  847. // 如果有相等的规格
  848. if( $old['attr_names'] == $new['attr_names']) {
  849. $new['sku_thumb']= $old['sku_thumb'];
  850. $new['price'] = $old['price'];
  851. $new['stock'] = $old['stock'];
  852. $new['status'] = $old['status'];
  853. $new['course_number'] = $old['course_number'];
  854. }
  855. }
  856. $skuList[$newKey] = $new;
  857. }
  858. // 分配数据
  859. $this->assign('specAttr',$specAttr);
  860. $this->assign('skuList',$skuList);
  861. // 加载模板
  862. return $this->fetch();
  863. }
  864. /**
  865. * 获取规格属性
  866. * @param ProductSpec $ProductSpec
  867. *
  868. */
  869. private function getSpecAttr($attr,$ProductSpec,$onlyList=false){
  870. // 组合参数
  871. $specAttr = [];
  872. // 循环处理数据
  873. foreach ($attr as $specId => $value) {
  874. $specAttr[$specId]['spec_id'] = $specId;
  875. $specAttr[$specId]['spec_name'] = $ProductSpec->getOne($specId,'name');
  876. $specAttr[$specId]['attr_list'] = [];
  877. foreach ($value['name'] as $key => $name) {
  878. // 如果没有名称的,跳过
  879. if( !$name ) continue;
  880. // 结果
  881. $temp = ['id'=>0,'spec_id'=>$specId,'thumb'=>'','name'=>'','remark'=>''];
  882. // 获取名称
  883. $temp['name'] = str_ireplace(',','',$name);
  884. // 获取对应的备注信息
  885. if( !empty($value['remark'][$key]) ) $temp['remark'] = $value['remark'][$key];
  886. if( !empty($value['thumb'][$key]) ) $temp['thumb'] = $value['thumb'][$key];
  887. // 获取对应的数据
  888. $specAttr[$specId]['attr_list'][]= $temp;
  889. }
  890. // 没有属性的规格,移除
  891. if( !$specAttr[$specId]['attr_list'] ) unset($specAttr[$specId]);
  892. }
  893. // 仅获取属性列表
  894. if( $onlyList ) {
  895. // 属性列表
  896. $attrList = [];
  897. // 判断结果
  898. foreach ($specAttr as $value) {
  899. // 获取数据
  900. array_push($attrList,...$value['attr_list']);
  901. }
  902. // 返回结果
  903. $specAttr = $attrList;
  904. }
  905. // 返回结果
  906. return $specAttr;
  907. }
  908. /**
  909. * 组合SKU
  910. *
  911. */
  912. private function brush($res = [], $arr = []){
  913. // 获取第一个规格的属性
  914. if (empty($res)) $res = (array) array_shift($arr);
  915. // 如果只有一个规格,返回该规格的属性即可
  916. if (empty($arr)) return $res;
  917. // 接下来要参与计算的一组属性
  918. $current = array_shift($arr);
  919. // 计算的列表
  920. $last = [];
  921. // 循环上一次已经算出的集合
  922. foreach ($res as $attr) {
  923. foreach ($current as $col_val) {
  924. $last[] = $attr . ',' . $col_val;
  925. }
  926. }
  927. return $this->brush($last,$arr); # 递归处理, 直到$arr滚到最后一组属性
  928. }
  929. /**
  930. * 排序
  931. *
  932. * */
  933. public function set_sort( Request $request, Model $Model ){
  934. // 验证参数
  935. $request->scene('set_sort')->validate();
  936. // 接收参数
  937. $id = request('id',0);
  938. $sort = request('sort',0);
  939. // 查询数据
  940. $result = $Model->edit($id,['sort'=>$sort]);
  941. // 提示新增失败
  942. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  943. // 记录行为
  944. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['sort'=>$sort]);
  945. // 告知结果
  946. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  947. }
  948. }