ProductService.php 28 KB

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