Product.php 36 KB

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