ProductService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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->warranty_time = $data['warranty_time'] ?? 0;
  160. $model->install_time = $data['install_time'] ?? 0;
  161. $model->size = $data['size'] ?? '';
  162. $model->unit = $data['unit'] ?? 0;
  163. $model->bar_code = $data['bar_code'] ?? '';
  164. $model->cost = $data['cost'] ?? 0;
  165. $model->retail_price = $data['retail_price'] ?? 0;
  166. $model->mark = $data['mark'] ?? '';
  167. $model->state = $data['state'] ?? 0;
  168. $model->save();
  169. $time = time();
  170. ProductIntroduction::where('product_id',$data['id'])
  171. ->where('del_time',0)
  172. ->update(['del_time' => $time]);
  173. if(! empty($data['introduction'])){
  174. $models = new ProductIntroduction();
  175. $models->product_id = $model->id;
  176. $models->introduction = $data['introduction'];
  177. $models->save();
  178. }
  179. $old = ProductInfo::where('del_time',0)
  180. ->where('product_id',$data['id'])
  181. ->select('file')
  182. ->get()->toArray();
  183. $old = array_column($old,'file');
  184. ProductInfo::where('del_time',0)
  185. ->where('product_id',$data['id'])
  186. ->update(['del_time' => $time]);
  187. $new = [];
  188. if(! empty($data['img'])){
  189. $insert = [];
  190. foreach ($data['img'] as $value){
  191. $insert[] = [
  192. 'product_id' => $model->id,
  193. 'file' => $value['url'],
  194. 'type' => ProductInfo::type_one,
  195. 'name' => $value['name'],
  196. 'crt_time' => $time,
  197. ];
  198. $new[] = $value['url'];
  199. }
  200. ProductInfo::insert($insert);
  201. }
  202. if(! empty($data['file'])){
  203. $insert = [];
  204. foreach ($data['file'] as $value){
  205. $insert[] = [
  206. 'product_id' => $model->id,
  207. 'file' => $value['url'],
  208. 'type' => ProductInfo::type_two,
  209. 'name' => $value['name'],
  210. 'crt_time' => $time,
  211. ];
  212. $new[] = $value['url'];
  213. }
  214. ProductInfo::insert($insert);
  215. }
  216. ProductPriceDetail::where('del_time',0)
  217. ->where('product_id',$data['id'])
  218. ->update(['del_time' => $time]);
  219. if(! empty($data['product_price'])){
  220. $insert = [];
  221. foreach ($data['product_price'] as $value){
  222. $insert[] = [
  223. 'product_id' => $model->id,
  224. 'basic_type_id' => $value['basic_type_id'],
  225. 'price' => $value['price'] ?? 0,
  226. 'crt_time' => $time,
  227. ];
  228. }
  229. ProductPriceDetail::insert($insert);
  230. }
  231. DB::commit();
  232. }catch (\Exception $exception){
  233. DB::rollBack();
  234. return [false,$exception->getMessage()];
  235. }
  236. $this->delStorageFile($old, $new);
  237. return [true,''];
  238. }
  239. /**
  240. * 产品新增
  241. * @param $data
  242. * @param $user
  243. * @return array
  244. */
  245. public function productAdd($data,$user){
  246. list($status,$msg) = $this->productRule($data, $user);
  247. if(!$status) return [$status,$msg];
  248. try {
  249. DB::beginTransaction();
  250. $model = new Product();
  251. $model->product_category_id = $data['product_category_id'] ?? 0;
  252. $model->product_category = $data['product_category'] ?? '';
  253. $model->title = $data['title'];
  254. $model->code = $data['code'] ?? '';
  255. $model->warranty_time = $data['warranty_time'] ?? 0;
  256. $model->install_time = $data['install_time'] ?? 0;
  257. $model->size = $data['size'] ?? '';
  258. $model->unit = $data['unit'] ?? 0;
  259. $model->bar_code = $data['bar_code'] ?? '';
  260. $model->cost = $data['cost'] ?? 0;
  261. $model->retail_price = $data['retail_price'] ?? 0;
  262. $model->mark = $data['mark'] ?? '';
  263. $model->state = $data['state'] ?? 0;
  264. $model->crt_id = $user['id'];
  265. $model->depart_id = $data['depart_id'] ?? 0;
  266. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  267. $model->save();
  268. $time = time();
  269. if(! empty($data['introduction'])){
  270. $models = new ProductIntroduction();
  271. $models->product_id = $model->id;
  272. $models->introduction = $data['introduction'];
  273. $models->save();
  274. }
  275. if(! empty($data['img'])){
  276. $insert = [];
  277. foreach ($data['img'] as $value){
  278. $insert[] = [
  279. 'product_id' => $model->id,
  280. 'file' => $value['url'],
  281. 'type' => ProductInfo::type_one,
  282. 'name' => $value['name'],
  283. 'crt_time' => $time,
  284. ];
  285. }
  286. ProductInfo::insert($insert);
  287. }
  288. if(! empty($data['file'])){
  289. $insert = [];
  290. foreach ($data['file'] as $value){
  291. $insert[] = [
  292. 'product_id' => $model->id,
  293. 'file' => $value['url'],
  294. 'type' => ProductInfo::type_two,
  295. 'name' => $value['name'],
  296. 'crt_time' => $time,
  297. ];
  298. }
  299. ProductInfo::insert($insert);
  300. }
  301. if(! empty($data['product_price'])){
  302. $insert = [];
  303. foreach ($data['product_price'] as $value){
  304. $insert[] = [
  305. 'product_id' => $model->id,
  306. 'basic_type_id' => $value['basic_type_id'],
  307. 'price' => $value['price'] ?? 0,
  308. 'crt_time' => $time,
  309. ];
  310. }
  311. ProductPriceDetail::insert($insert);
  312. }
  313. DB::commit();
  314. }catch (\Exception $exception){
  315. DB::rollBack();
  316. return [false,$exception->getMessage()];
  317. }
  318. return [true,''];
  319. }
  320. /**
  321. * 产品删除
  322. * @param $data
  323. * @return array
  324. */
  325. public function productDel($data){
  326. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  327. try {
  328. DB::beginTransaction();
  329. $time = time();
  330. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  331. ProductIntroduction::where('product_id',$data['id'])
  332. ->where('del_time',0)
  333. ->update(['del_time' => $time]);
  334. $old = ProductInfo::where('del_time',0)
  335. ->where('product_id',$data['id'])
  336. ->select('file')
  337. ->get()->toArray();
  338. $old = array_column($old,'file');
  339. ProductInfo::where('del_time',0)
  340. ->where('product_id',$data['id'])
  341. ->update(['del_time' => $time]);
  342. ProductPriceDetail::where('del_time',0)
  343. ->where('product_id',$data['id'])
  344. ->update(['del_time' => $time]);
  345. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  346. DB::commit();
  347. }catch (\Exception $exception){
  348. DB::rollBack();
  349. return [false,$exception->getMessage()];
  350. }
  351. $this->delStorageFile($old,[]);
  352. return [true,''];
  353. }
  354. /**
  355. * 产品详情
  356. * @param $data
  357. * @param $user
  358. * @return array
  359. */
  360. public function productDetail($data,$user){
  361. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  362. $customer = Product::where('del_time',0)
  363. ->where('id',$data['id'])
  364. ->first();
  365. if(empty($customer)) return [false,'产品不存在或已被删除'];
  366. $customer = $customer->toArray();
  367. $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
  368. $category = ProductCategory::where('id',$customer['product_category_id'])
  369. ->value('title');
  370. $customer['product_category_title'] = $category;
  371. $customer['introduction'] = "";
  372. $in = ProductIntroduction::where('del_time',0)
  373. ->where('product_id',$data['id'])
  374. ->first();
  375. if(! empty($in)) $customer['introduction'] = $in->introduction;
  376. $model = BasicType::TopClear($user,$data);
  377. $basic = $model->where('del_time',0)
  378. ->where('type',22)
  379. ->select('title','id','type')
  380. ->orderby('id', 'asc')->get()->toArray();
  381. $detail = ProductPriceDetail::where('del_time',0)
  382. ->where('product_id',$data['id'])
  383. ->select('product_id','basic_type_id','price')
  384. ->get()->toArray();
  385. $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
  386. ->pluck('title','id')
  387. ->toArray();
  388. //是否总公司
  389. $is_main = $user['is_all_depart'];
  390. $top_depart = $user['depart_top'][0] ?? [];
  391. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  392. $customer['product_price'] = [];
  393. //展示金额
  394. foreach ($detail as $value){
  395. if(! $is_main && ($top_depart['basic_type_id'] != $value['basic_type_id'])) {
  396. $is_show = 0;
  397. }else{
  398. $is_show = 1;
  399. }
  400. $customer['product_price'][$value['basic_type_id']] = [
  401. 'basic_type_id' => $value['basic_type_id'],
  402. 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
  403. 'price' => $value['price'],
  404. 'is_show' => $is_show,
  405. ];
  406. }
  407. foreach ($basic as $value){
  408. if(! $is_main && ($top_depart['basic_type_id'] != $value['id'])) {
  409. $is_show = 0;
  410. }else{
  411. $is_show = 1;
  412. }
  413. if(! isset($customer['product_price'][$value['id']])){
  414. $customer['product_price'][$value['id']] = [
  415. 'basic_type_id' => $value['id'],
  416. 'basic_type_title' => $title_map[$value['id']] ?? '',
  417. 'price' => 0,
  418. 'is_show' => $is_show,
  419. ];
  420. }
  421. }
  422. $customer['product_price'] = array_values($customer['product_price']);
  423. //单位
  424. $title = BasicType::where('id',$customer['unit'])->value('title');
  425. $customer['unit_name'] = $title;
  426. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  427. $customer_info = ProductInfo::where('del_time',0)
  428. ->where('product_id',$customer['id'])
  429. ->select('id','product_id','file','type','name')
  430. ->get()->toArray();
  431. foreach ($customer_info as $value){
  432. $tmp = [
  433. 'url' => $value['file'],
  434. 'name' => $value['name'],
  435. ];
  436. if($value['type'] == ProductInfo::type_one){
  437. $customer['img'][] = $tmp;
  438. }elseif ($value['type'] == ProductInfo::type_two){
  439. $customer['file'][] = $tmp;
  440. }
  441. }
  442. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  443. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  444. //可见范围
  445. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  446. $customer['depart'] = $return[0] ?? [];
  447. $customer['employee'] = $return[1] ?? [];
  448. return [true, $customer];
  449. }
  450. /**
  451. * 产品列表
  452. * @param $data
  453. * @param $user
  454. * @return array
  455. */
  456. public function productList($data,$user){
  457. $model = Product::ProductClear($user,$data);
  458. $model = $model->where('del_time',0)
  459. ->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','install_time')
  460. ->orderby('id', 'desc');
  461. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  462. if(isset($data['state'])) $model->where('state', $data['state']);
  463. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  464. if(! empty($data['product_category'])) {
  465. $product_category = ProductCategory::where('del_time',0)
  466. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  467. ->select('id')
  468. ->get()->toArray();
  469. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  470. }
  471. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  472. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  473. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  474. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  475. $model->where('crt_time','>=',$return[0]);
  476. $model->where('crt_time','<=',$return[1]);
  477. }
  478. $list = $this->limit($model,'',$data);
  479. $list = $this->fillData($list,$user,$data);
  480. return [true, $list];
  481. }
  482. public function productList2($data,$user){
  483. $model = ProductCategory::TopClear($user,$data);
  484. $model = $model->where('del_time',0)
  485. ->select('title','id','parent_id')
  486. ->orderby('id','desc');
  487. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  488. $list = $model->get()->toArray();
  489. if(! empty($list)) {
  490. $productList = Product::where('del_time',0)
  491. ->whereIn('product_category_id',array_column($list,'id'))
  492. ->select('id','product_category_id','title','code')
  493. ->get()->toArray();
  494. $productMap = [];
  495. foreach ($productList as $value){
  496. $productMap[$value['product_category_id']][] = $value;
  497. }
  498. foreach ($list as $key => $value){
  499. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  500. }
  501. $list = $this->makeTree(0,$list);
  502. $list = $this->set_sort_circle($list);
  503. }
  504. return [200, $list];
  505. }
  506. /**
  507. * 产品参数规则
  508. * @param $data
  509. * @param $is_add
  510. * @return array
  511. */
  512. public function productRule(&$data, $user, $is_add = true){
  513. if(empty($data['title'])) return [false,'产品名称不能为空'];
  514. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  515. if(empty($data['code'])) return [false,'产品编码不能为空'];
  516. if(empty($data['cost'])) return [false,'成本不能为空'];
  517. $res = $this->checkNumber($data['cost']);
  518. if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值'];
  519. if(empty($data['retail_price'])) return [false,'零售价不能为空'];
  520. $res = $this->checkNumber($data['retail_price']);
  521. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
  522. if(! empty($data['product_price'])){
  523. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  524. ->pluck('title','id')
  525. ->toArray();
  526. foreach ($data['product_price'] as $value){
  527. if(! empty($value['price'])) {
  528. $tmp = $map[$value['basic_type_id']] ?? '';
  529. $res = $this->checkNumber($value['price']);
  530. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  531. }
  532. }
  533. }
  534. if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
  535. //所属部门 以及 顶级部门
  536. if(empty($data['depart_id'])) {
  537. $data['depart_id'] = $this->getDepart($user);
  538. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  539. }
  540. //总社id
  541. $top_depart_id = $user['head'] ?? [];
  542. $top_depart_id = $top_depart_id['id'] ?? 0;
  543. if($is_add){
  544. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  545. ->where('del_time',0)
  546. ->exists();
  547. }else{
  548. if(empty($data['id'])) return [false,'ID不能为空'];
  549. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  550. ->where('id','<>',$data['id'])
  551. ->where('del_time',0)
  552. ->exists();
  553. }
  554. if($bool) return [false,'产品名称、编码不能重复'];
  555. return [true, $data];
  556. }
  557. /**
  558. * 拼接数据
  559. * @param $data
  560. * @return array
  561. */
  562. public function fillData($data, $user, $search){
  563. if(empty($data['data'])) return $data;
  564. $type = $search['type'] ?? 0;
  565. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  566. ->pluck('emp_name','id')
  567. ->toArray();
  568. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  569. ->pluck('title','id')
  570. ->toArray();
  571. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  572. ->orWhere('type',BasicType::type_22)
  573. ->pluck('title','id')
  574. ->toArray();
  575. //产品使用价格
  576. $detail_map = $this->getProductPrice(array_column($data['data'],'id'), $type);
  577. //是否总公司
  578. $is_main = $user['is_all_depart'];
  579. $top_depart = $user['depart_top'][0] ?? [];
  580. foreach ($data['data'] as $key => $value){
  581. $tmp = [];
  582. if(isset($detail_map[$value['id']])){
  583. $d = $detail_map[$value['id']];
  584. foreach ($d as $v){
  585. if(! $is_main && ($top_depart['basic_type_id'] != $v['basic_type_id'])) {
  586. $is_show = 0;
  587. }else{
  588. $is_show = 1;
  589. }
  590. if($top_depart['basic_type_id'] != $v['basic_type_id']) {
  591. $is_use = 0;
  592. }else{
  593. $is_use = 1;
  594. }
  595. $tmp[] = [
  596. 'basic_type_id' => $v['basic_type_id'],
  597. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  598. 'price' => $v['price'],
  599. 'is_show' => $is_show,
  600. 'is_use' => $is_use
  601. ];
  602. }
  603. }
  604. $data['data'][$key]['product_price'] = $tmp;
  605. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  606. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  607. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  608. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  609. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  610. }
  611. return $data;
  612. }
  613. //获取产品字典
  614. public function getProductDetail($product_id = []){
  615. if(empty($product_id)) return [];
  616. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  617. return array_column($pro,null,'id');
  618. }
  619. //获取产品使用价格
  620. public function getProductPrice($product_id = [], $type = 1){
  621. //type 1 采购 2 合同 和 活动包
  622. $detail_map = [];
  623. $time = time();
  624. if($type == 1) {
  625. //供应商活动价格
  626. $activity = ProductActivityPrice::from('product_activity_price as a')
  627. ->join('product_activity as b','b.id','a.product_activity_id')
  628. ->where('a.del_time',0)
  629. ->whereIn('a.product_id',$product_id)
  630. ->where('a.start_time','<=',$time)
  631. ->where('a.end_time','>=',$time)
  632. ->where('b.type',ProductActivity::type_two)
  633. ->select('a.product_id','a.basic_type_id','a.price')
  634. ->get()->toArray();
  635. foreach ($activity as $value){
  636. $detail_map[$value['product_id']][] = $value;
  637. }
  638. //分社价 没有供应商活动价格使用分社价格
  639. $detail = ProductPriceDetail::where('del_time',0)
  640. ->whereIn('product_id',$product_id)
  641. ->select('product_id','basic_type_id','price')
  642. ->get()->toArray();
  643. foreach ($detail as $value){
  644. if(! isset($detail_map[$value['product_id']])){
  645. $detail_map[$value['product_id']][] = $value;
  646. }
  647. }
  648. }else{
  649. //零售活动价格
  650. $activity = ProductActivityPrice::from('product_activity_price as a')
  651. ->join('product_activity as b','b.id','a.product_activity_id')
  652. ->where('a.del_time',0)
  653. ->whereIn('a.product_id',$product_id)
  654. ->where('a.start_time','<=',$time)
  655. ->where('a.end_time','>=',$time)
  656. ->where('b.type',ProductActivity::type_one)
  657. ->select('a.product_id','a.basic_type_id','a.price')
  658. ->get()->toArray();
  659. foreach ($activity as $value){
  660. $detail_map[$value['product_id']][] = $value;
  661. }
  662. }
  663. return $detail_map;
  664. }
  665. public function getProductPriceTmp($product_id = []){
  666. //type 0 采购 1 合同
  667. //分社价
  668. $detail = ProductPriceDetail::where('del_time',0)
  669. ->whereIn('product_id',$product_id)
  670. ->select('product_id','basic_type_id','price')
  671. ->get()->toArray();
  672. $detail_map = [];
  673. foreach ($detail as $value){
  674. $detail_map[$value['product_id']][] = $value;
  675. }
  676. $return = $detail_map;
  677. //活动价格
  678. $time = time();
  679. $activity = ProductActivityPrice::where('del_time',0)
  680. ->whereIn('product_id',$product_id)
  681. ->where('start_time','<=',$time)
  682. ->where('end_time','>=',$time)
  683. ->select('product_id','basic_type_id','price')
  684. ->get()->toArray();
  685. if(! empty($activity)){
  686. foreach ($activity as $value){
  687. if(! isset($detail_map[$value['product_id']])) {
  688. $return[$value['product_id']][] = $value;
  689. }else{
  690. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  691. if(! in_array($value['basic_type_id'], $basic_type)){
  692. $return[$value['product_id']][] = $value;
  693. continue;
  694. }
  695. foreach ($detail_map[$value['product_id']] as $key => $val){
  696. if($value['basic_type_id'] == $val['basic_type_id']) {
  697. //活动价格替换
  698. $return[$value['product_id']][$key]['price'] = $value['price'];
  699. }
  700. }
  701. }
  702. }
  703. }unset($detail_map);
  704. return $return;
  705. }
  706. }