ProductService.php 26 KB

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