Product.php 44 KB

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