ProductService.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Employee;
  5. use App\Model\Product;
  6. use App\Model\ProductActivityPrice;
  7. use App\Model\ProductCategory;
  8. use App\Model\ProductInfo;
  9. use App\Model\ProductIntroduction;
  10. use App\Model\ProductPriceDetail;
  11. use App\Model\SeeRange;
  12. use Illuminate\Support\Facades\DB;
  13. /**
  14. * 产品管理
  15. */
  16. class ProductService extends Service
  17. {
  18. /**
  19. * 产品分类编辑
  20. * @param $data
  21. * @param $user
  22. * @return array
  23. */
  24. public function productCategoryEdit($data,$user){
  25. list($status,$msg) = $this->productCategoryRule($data,$user,false);
  26. if(!$status) return [$status,$msg];
  27. $update = $msg['data'][0];
  28. $model = new ProductCategory();
  29. $model->where('id',$data['id'])->update($update);
  30. return [true,''];
  31. }
  32. /**
  33. * 产品分类新增
  34. * @param $data
  35. * @param $user
  36. * @return array
  37. */
  38. public function productCategoryAdd($data,$user){
  39. list($status,$msg) = $this->productCategoryRule($data,$user);
  40. if(!$status) return [$status,$msg];
  41. ProductCategory::insert($msg['data']);
  42. return [true,''];
  43. }
  44. /**
  45. * 产品分类删除
  46. * @param $data
  47. * @return array
  48. */
  49. public function productCategoryDel($data){
  50. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  51. $bool = Product::where('del_time',0)
  52. ->where('product_category_id',$data['id'])
  53. ->exists();
  54. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  55. try {
  56. DB::beginTransaction();
  57. ProductCategory::where('id',$data['id'])->update([
  58. 'del_time' => time()
  59. ]);
  60. DB::commit();
  61. }catch (\Exception $exception){
  62. DB::rollBack();
  63. return [false,$exception->getMessage()];
  64. }
  65. return [true,''];
  66. }
  67. /**
  68. * 产品分类列表
  69. * @param $data
  70. * @param $user
  71. * @return array
  72. */
  73. public function productCategoryList($data,$user){
  74. $model = ProductCategory::TopClear($user,$data);
  75. $model = $model->where('del_time',0)
  76. ->select('title','id','parent_id')
  77. ->orderby('id','desc');
  78. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  79. $list = $model->get()->toArray();
  80. $list_tree = $list;
  81. if(! empty($list_tree)) {
  82. $list_tree = $this->makeTree(0,$list_tree);
  83. $list_tree = $this->set_sort_circle($list_tree);
  84. }
  85. return [200, ['data' => $list,'tree' => $list_tree]];
  86. }
  87. /**
  88. * 产品分类参数规则
  89. * @param $data
  90. * @param $is_add
  91. * @return array
  92. */
  93. public function productCategoryRule($data,$user, $is_add = true){
  94. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  95. //所属部门 以及 顶级部门
  96. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  97. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  98. $title = array_column($data['data'],'title');
  99. $title = array_map(function($val) {
  100. return $val !== null ? $val : 0;
  101. }, $title);
  102. $title_count = array_count_values($title);
  103. foreach ($title as $value){
  104. if(empty($value)) return [false,'名称不能为空!'];
  105. if($title_count[$value] > 1) return [false,'名称不能重复'];
  106. }
  107. foreach ($data['data'] as $key => $value){
  108. $data['data'][$key]['upd_time'] = time();
  109. if($is_add){
  110. $parent_id = 0;
  111. if(! empty($value['parent_id'])) {
  112. $bool = Product::where('del_time',0)
  113. ->where('product_category_id',$value['parent_id'])
  114. ->exists();
  115. if($bool) {
  116. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  117. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  118. }
  119. $parent_id = $value['parent_id'];
  120. }
  121. $data['data'][$key]['parent_id'] = $parent_id;
  122. $data['data'][$key]['crt_time'] = time();
  123. $data['data'][$key]['depart_id'] = $data['depart_id'];
  124. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  125. $bool = ProductCategory::where('title',$value['title'])
  126. ->where('top_depart_id',$data['top_depart_id'])
  127. ->where('del_time',0)
  128. ->exists();
  129. }else{
  130. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  131. $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id');
  132. $bool = ProductCategory::where('title',$value['title'])
  133. ->where('top_depart_id',$top_depart_id)
  134. ->where('id','<>',$data['id'])
  135. ->where('del_time',0)
  136. ->exists();
  137. }
  138. if($bool) return [false,'分类名称不能重复'];
  139. }
  140. return [true, $data];
  141. }
  142. /**
  143. * 产品编辑
  144. * @param $data
  145. * @param $user
  146. * @return array
  147. */
  148. public function productEdit($data,$user){
  149. list($status,$msg) = $this->productRule($data, $user, false);
  150. if(!$status) return [$status,$msg];
  151. try {
  152. DB::beginTransaction();
  153. $model = Product::where('id',$data['id'])->first();
  154. $model->product_category_id = $data['product_category_id'] ?? 0;
  155. $model->title = $data['title'];
  156. $model->code = $data['code'] ?? '';
  157. $model->sn_code = $data['sn_code'] ?? '';
  158. $model->size = $data['size'] ?? '';
  159. $model->unit = $data['unit'] ?? 0;
  160. $model->bar_code = $data['bar_code'] ?? '';
  161. $model->cost = $data['cost'] ?? 0;
  162. $model->retail_price = $data['retail_price'] ?? 0;
  163. $model->mark = $data['mark'] ?? '';
  164. $model->state = $data['state'] ?? 0;
  165. $model->save();
  166. $time = time();
  167. ProductIntroduction::where('product_id',$data['id'])
  168. ->where('del_time',0)
  169. ->update(['del_time' => $time]);
  170. if(! empty($data['introduction'])){
  171. $models = new ProductIntroduction();
  172. $models->product_id = $model->id;
  173. $models->introduction = $data['introduction'];
  174. $models->save();
  175. }
  176. ProductInfo::where('del_time',0)
  177. ->where('product_id',$data['id'])
  178. ->update(['del_time' => $time]);
  179. if(! empty($data['img'])){
  180. $insert = [];
  181. foreach ($data['img'] as $value){
  182. $insert[] = [
  183. 'product_id' => $model->id,
  184. 'file' => $value['url'],
  185. 'type' => ProductInfo::type_one,
  186. 'name' => $value['name'],
  187. 'crt_time' => $time,
  188. ];
  189. }
  190. ProductInfo::insert($insert);
  191. }
  192. if(! empty($data['file'])){
  193. $insert = [];
  194. foreach ($data['file'] as $value){
  195. $insert[] = [
  196. 'product_id' => $model->id,
  197. 'file' => $value['url'],
  198. 'type' => ProductInfo::type_two,
  199. 'name' => $value['name'],
  200. 'crt_time' => $time,
  201. ];
  202. }
  203. ProductInfo::insert($insert);
  204. }
  205. ProductPriceDetail::where('del_time',0)
  206. ->where('product_id',$data['id'])
  207. ->update(['del_time' => $time]);
  208. if(! empty($data['product_price'])){
  209. $insert = [];
  210. foreach ($data['product_price'] as $value){
  211. $insert[] = [
  212. 'product_id' => $model->id,
  213. 'basic_type_id' => $value['basic_type_id'],
  214. 'price' => $value['price'] ?? 0,
  215. 'crt_time' => $time,
  216. ];
  217. }
  218. ProductPriceDetail::insert($insert);
  219. }
  220. DB::commit();
  221. }catch (\Exception $exception){
  222. DB::rollBack();
  223. return [false,$exception->getMessage()];
  224. }
  225. return [true,''];
  226. }
  227. /**
  228. * 产品新增
  229. * @param $data
  230. * @param $user
  231. * @return array
  232. */
  233. public function productAdd($data,$user){
  234. list($status,$msg) = $this->productRule($data, $user);
  235. if(!$status) return [$status,$msg];
  236. try {
  237. DB::beginTransaction();
  238. $model = new Product();
  239. $model->product_category_id = $data['product_category_id'] ?? 0;
  240. $model->title = $data['title'];
  241. $model->code = $data['code'] ?? '';
  242. $model->sn_code = $data['sn_code'] ?? '';
  243. $model->size = $data['size'] ?? '';
  244. $model->unit = $data['unit'] ?? 0;
  245. $model->bar_code = $data['bar_code'] ?? '';
  246. $model->cost = $data['cost'] ?? 0;
  247. $model->retail_price = $data['retail_price'] ?? 0;
  248. $model->mark = $data['mark'] ?? '';
  249. $model->state = $data['state'] ?? 0;
  250. $model->crt_id = $user['id'];
  251. $model->depart_id = $data['depart_id'] ?? 0;
  252. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  253. $model->save();
  254. $time = time();
  255. if(! empty($data['introduction'])){
  256. $models = new ProductIntroduction();
  257. $models->product_id = $model->id;
  258. $models->introduction = $data['introduction'];
  259. $models->save();
  260. }
  261. if(! empty($data['img'])){
  262. $insert = [];
  263. foreach ($data['img'] as $value){
  264. $insert[] = [
  265. 'product_id' => $model->id,
  266. 'file' => $value['url'],
  267. 'type' => ProductInfo::type_one,
  268. 'name' => $value['name'],
  269. 'crt_time' => $time,
  270. ];
  271. }
  272. ProductInfo::insert($insert);
  273. }
  274. if(! empty($data['file'])){
  275. $insert = [];
  276. foreach ($data['file'] as $value){
  277. $insert[] = [
  278. 'product_id' => $model->id,
  279. 'file' => $value['url'],
  280. 'type' => ProductInfo::type_two,
  281. 'name' => $value['name'],
  282. 'crt_time' => $time,
  283. ];
  284. }
  285. ProductInfo::insert($insert);
  286. }
  287. if(! empty($data['product_price'])){
  288. $insert = [];
  289. foreach ($data['product_price'] as $value){
  290. $insert[] = [
  291. 'product_id' => $model->id,
  292. 'basic_type_id' => $value['basic_type_id'],
  293. 'price' => $value['price'] ?? 0,
  294. 'crt_time' => $time,
  295. ];
  296. }
  297. ProductPriceDetail::insert($insert);
  298. }
  299. DB::commit();
  300. }catch (\Exception $exception){
  301. DB::rollBack();
  302. return [false,$exception->getMessage()];
  303. }
  304. return [true,''];
  305. }
  306. /**
  307. * 产品删除
  308. * @param $data
  309. * @return array
  310. */
  311. public function productDel($data){
  312. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  313. try {
  314. DB::beginTransaction();
  315. $time = time();
  316. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  317. ProductIntroduction::where('product_id',$data['id'])
  318. ->where('del_time',0)
  319. ->update(['del_time' => $time]);
  320. ProductInfo::where('del_time',0)
  321. ->where('product_id',$data['id'])
  322. ->update(['del_time' => $time]);
  323. ProductPriceDetail::where('del_time',0)
  324. ->where('product_id',$data['id'])
  325. ->update(['del_time' => $time]);
  326. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  327. DB::commit();
  328. }catch (\Exception $exception){
  329. DB::rollBack();
  330. return [false,$exception->getMessage()];
  331. }
  332. return [true,''];
  333. }
  334. /**
  335. * 产品详情
  336. * @param $data
  337. * @param $user
  338. * @return array
  339. */
  340. public function productDetail($data,$user){
  341. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  342. $customer = Product::where('del_time',0)
  343. ->where('id',$data['id'])
  344. ->first();
  345. if(empty($customer)) return [false,'产品不存在或已被删除'];
  346. $customer = $customer->toArray();
  347. $category = ProductCategory::where('id',$customer['product_category_id'])
  348. ->value('title');
  349. $customer['product_category_title'] = $category;
  350. $customer['introduction'] = "";
  351. $in = ProductIntroduction::where('del_time',0)
  352. ->where('product_id',$data['id'])
  353. ->first();
  354. if(! empty($in)) $customer['introduction'] = $in->introduction;
  355. $detail = ProductPriceDetail::where('del_time',0)
  356. ->where('product_id',$data['id'])
  357. ->select('product_id','basic_type_id','price')
  358. ->get()->toArray();
  359. $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
  360. ->pluck('title','id')
  361. ->toArray();
  362. //是否总公司
  363. $is_main = $user['is_all_depart'];
  364. $top_depart = $user['depart_top'][0] ?? [];
  365. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  366. $customer['product_price'] = [];
  367. //展示金额
  368. foreach ($detail as $value){
  369. if(! $is_main && ($top_depart['basic_type_id'] != $value['basic_type_id'])) {
  370. $is_show = 0;
  371. }else{
  372. $is_show = 1;
  373. }
  374. $customer['product_price'][] = [
  375. 'basic_type_id' => $value['basic_type_id'],
  376. 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
  377. 'price' => $value['price'],
  378. 'is_show' => $is_show,
  379. ];
  380. }
  381. //单位
  382. $title = BasicType::where('id',$customer['unit'])->value('title');
  383. $customer['unit_name'] = $title;
  384. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  385. $customer_info = ProductInfo::where('del_time',0)
  386. ->where('product_id',$customer['id'])
  387. ->select('id','product_id','file','type','name')
  388. ->get()->toArray();
  389. foreach ($customer_info as $value){
  390. $tmp = [
  391. 'url' => $value['file'],
  392. 'name' => $value['name'],
  393. ];
  394. if($value['type'] == ProductInfo::type_one){
  395. $customer['img'][] = $tmp;
  396. }elseif ($value['type'] == ProductInfo::type_two){
  397. $customer['file'][] = $tmp;
  398. }
  399. }
  400. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  401. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  402. //可见范围
  403. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  404. $customer['depart'] = $return[0] ?? [];
  405. $customer['employee'] = $return[1] ?? [];
  406. return [true, $customer];
  407. }
  408. /**
  409. * 产品列表
  410. * @param $data
  411. * @param $user
  412. * @return array
  413. */
  414. public function productList($data,$user){
  415. $model = Product::ProductClear($user,$data);
  416. $model = $model->where('del_time',0)
  417. ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','state','crt_id','crt_time','mark','depart_id','top_depart_id','sn_code')
  418. ->orderby('id', 'desc');
  419. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  420. if(isset($data['state'])) $model->where('state', $data['state']);
  421. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  422. $list = $this->limit($model,'',$data);
  423. $list = $this->fillData($list,$user,$data);
  424. return [true, $list];
  425. }
  426. /**
  427. * 产品参数规则
  428. * @param $data
  429. * @param $is_add
  430. * @return array
  431. */
  432. public function productRule(&$data, $user, $is_add = true){
  433. if(empty($data['title'])) return [false,'产品名称不能为空'];
  434. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  435. if(empty($data['code'])) return [false,'产品编码不能为空'];
  436. if(empty($data['sn_code'])) return [false,'产品SN编码不能为空'];
  437. if(empty($data['cost'])) return [false,'成本不能为空'];
  438. $res = $this->checkNumber($data['cost']);
  439. if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值'];
  440. if(empty($data['retail_price'])) return [false,'零售价不能为空'];
  441. $res = $this->checkNumber($data['retail_price']);
  442. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
  443. if(! empty($data['product_price'])){
  444. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  445. ->pluck('title','id')
  446. ->toArray();
  447. foreach ($data['product_price'] as $value){
  448. if(! empty($value['price'])) {
  449. $tmp = $map[$value['basic_type_id']] ?? '';
  450. $res = $this->checkNumber($value['price']);
  451. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  452. }
  453. }
  454. }
  455. //所属部门 以及 顶级部门
  456. if(empty($data['depart_id'])) {
  457. $data['depart_id'] = $this->getDepart($user);
  458. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  459. }
  460. //总社id
  461. $top_depart_id = $user['head'] ?? [];
  462. $top_depart_id = $top_depart_id['id'] ?? 0;
  463. if($is_add){
  464. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}' OR sn_code = '{$data['sn_code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  465. ->where('del_time',0)
  466. ->exists();
  467. }else{
  468. if(empty($data['id'])) return [false,'ID不能为空'];
  469. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}' OR sn_code = '{$data['sn_code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  470. ->where('id','<>',$data['id'])
  471. ->where('del_time',0)
  472. ->exists();
  473. }
  474. if($bool) return [false,'产品名称、编码与SN编码不能重复'];
  475. return [true, $data];
  476. }
  477. /**
  478. * 拼接数据
  479. * @param $data
  480. * @return array
  481. */
  482. public function fillData($data, $user, $search){
  483. if(empty($data['data'])) return $data;
  484. $type = $search['type'] ?? 0;
  485. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  486. ->pluck('emp_name','id')
  487. ->toArray();
  488. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  489. ->pluck('title','id')
  490. ->toArray();
  491. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  492. ->orWhere('type',BasicType::type_22)
  493. ->pluck('title','id')
  494. ->toArray();
  495. //产品使用价格
  496. $detail_map = $this->getProductPrice(array_column($data['data'],'id'), $type);
  497. //是否总公司
  498. $is_main = $user['is_all_depart'];
  499. $top_depart = $user['depart_top'][0] ?? [];
  500. foreach ($data['data'] as $key => $value){
  501. $tmp = [];
  502. if(isset($detail_map[$value['id']])){
  503. $d = $detail_map[$value['id']];
  504. foreach ($d as $v){
  505. if(! $is_main && ($top_depart['basic_type_id'] != $v['basic_type_id'])) {
  506. $is_show = 0;
  507. }else{
  508. $is_show = 1;
  509. }
  510. if($top_depart['basic_type_id'] != $v['basic_type_id']) {
  511. $is_use = 0;
  512. }else{
  513. $is_use = 1;
  514. }
  515. $tmp[] = [
  516. 'basic_type_id' => $v['basic_type_id'],
  517. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  518. 'price' => $v['price'],
  519. 'is_show' => $is_show,
  520. 'is_use' => $is_use
  521. ];
  522. }
  523. }
  524. $data['data'][$key]['product_price'] = $tmp;
  525. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  526. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  527. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  528. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  529. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  530. }
  531. return $data;
  532. }
  533. //获取产品字典
  534. public function getProductDetail($product_id = []){
  535. if(empty($product_id)) return [];
  536. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  537. return array_column($pro,null,'id');
  538. }
  539. //获取产品使用价格
  540. public function getProductPrice($product_id = [], $type = 1){
  541. //type 1 采购 2 合同
  542. $detail_map = [];
  543. if($type == 1) {
  544. //分社价
  545. $detail = ProductPriceDetail::where('del_time',0)
  546. ->whereIn('product_id',$product_id)
  547. ->select('product_id','basic_type_id','price')
  548. ->get()->toArray();
  549. foreach ($detail as $value){
  550. $detail_map[$value['product_id']][] = $value;
  551. }
  552. }else{
  553. //活动价格
  554. $time = time();
  555. $activity = ProductActivityPrice::where('del_time',0)
  556. ->whereIn('product_id',$product_id)
  557. ->where('start_time','<=',$time)
  558. ->where('end_time','>=',$time)
  559. ->select('product_id','basic_type_id','price')
  560. ->get()->toArray();
  561. foreach ($activity as $value){
  562. $detail_map[$value['product_id']][] = $value;
  563. }
  564. }
  565. return $detail_map;
  566. }
  567. public function getProductPriceTmp($product_id = []){
  568. //type 0 采购 1 合同
  569. //分社价
  570. $detail = ProductPriceDetail::where('del_time',0)
  571. ->whereIn('product_id',$product_id)
  572. ->select('product_id','basic_type_id','price')
  573. ->get()->toArray();
  574. $detail_map = [];
  575. foreach ($detail as $value){
  576. $detail_map[$value['product_id']][] = $value;
  577. }
  578. $return = $detail_map;
  579. //活动价格
  580. $time = time();
  581. $activity = ProductActivityPrice::where('del_time',0)
  582. ->whereIn('product_id',$product_id)
  583. ->where('start_time','<=',$time)
  584. ->where('end_time','>=',$time)
  585. ->select('product_id','basic_type_id','price')
  586. ->get()->toArray();
  587. if(! empty($activity)){
  588. foreach ($activity as $value){
  589. if(! isset($detail_map[$value['product_id']])) {
  590. $return[$value['product_id']][] = $value;
  591. }else{
  592. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  593. if(! in_array($value['basic_type_id'], $basic_type)){
  594. $return[$value['product_id']][] = $value;
  595. continue;
  596. }
  597. foreach ($detail_map[$value['product_id']] as $key => $val){
  598. if($value['basic_type_id'] == $val['basic_type_id']) {
  599. //活动价格替换
  600. $return[$value['product_id']][$key]['price'] = $value['price'];
  601. }
  602. }
  603. }
  604. }
  605. }unset($detail_map);
  606. return $return;
  607. }
  608. }