Product.php 37 KB

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