ProductService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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\Role;
  13. use App\Model\RoleMenuButton;
  14. use App\Model\SeeRange;
  15. use Illuminate\Support\Facades\DB;
  16. /**
  17. * 产品管理
  18. */
  19. class ProductService extends Service
  20. {
  21. /**
  22. * 产品分类编辑
  23. * @param $data
  24. * @param $user
  25. * @return array
  26. */
  27. public function productCategoryEdit($data,$user){
  28. list($status,$msg) = $this->productCategoryRule($data,$user,false);
  29. if(!$status) return [$status,$msg];
  30. $update = $msg['data'][0];
  31. $model = new ProductCategory();
  32. $model->where('id',$data['id'])->update($update);
  33. return [true,''];
  34. }
  35. /**
  36. * 产品分类新增
  37. * @param $data
  38. * @param $user
  39. * @return array
  40. */
  41. public function productCategoryAdd($data,$user){
  42. list($status,$msg) = $this->productCategoryRule($data,$user);
  43. if(!$status) return [$status,$msg];
  44. ProductCategory::insert($msg['data']);
  45. return [true,''];
  46. }
  47. /**
  48. * 产品分类删除
  49. * @param $data
  50. * @return array
  51. */
  52. public function productCategoryDel($data){
  53. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  54. $bool = Product::where('del_time',0)
  55. ->where('product_category_id',$data['id'])
  56. ->exists();
  57. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  58. try {
  59. DB::beginTransaction();
  60. ProductCategory::where('id',$data['id'])->update([
  61. 'del_time' => time()
  62. ]);
  63. DB::commit();
  64. }catch (\Exception $exception){
  65. DB::rollBack();
  66. return [false,$exception->getMessage()];
  67. }
  68. return [true,''];
  69. }
  70. /**
  71. * 产品分类列表
  72. * @param $data
  73. * @param $user
  74. * @return array
  75. */
  76. public function productCategoryList($data,$user){
  77. $model = ProductCategory::TopClear($user,$data);
  78. $model = $model->where('del_time',0)
  79. ->select('title','id','parent_id')
  80. ->orderby('id','desc');
  81. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  82. $list = $model->get()->toArray();
  83. $list_tree = $list;
  84. if(! empty($list_tree)) {
  85. $list_tree = $this->makeTree(0,$list_tree);
  86. $list_tree = $this->set_sort_circle($list_tree);
  87. }
  88. return [200, ['data' => $list,'tree' => $list_tree]];
  89. }
  90. /**
  91. * 产品分类参数规则
  92. * @param $data
  93. * @param $is_add
  94. * @return array
  95. */
  96. public function productCategoryRule($data,$user, $is_add = true){
  97. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  98. //所属部门 以及 顶级部门
  99. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  100. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  101. $title = array_column($data['data'],'title');
  102. $title = array_map(function($val) {
  103. return $val !== null ? $val : 0;
  104. }, $title);
  105. $title_count = array_count_values($title);
  106. foreach ($title as $value){
  107. if(empty($value)) return [false,'名称不能为空!'];
  108. if($title_count[$value] > 1) return [false,'名称不能重复'];
  109. }
  110. foreach ($data['data'] as $key => $value){
  111. $data['data'][$key]['upd_time'] = time();
  112. if($is_add){
  113. $parent_id = 0;
  114. if(! empty($value['parent_id'])) {
  115. $bool = Product::where('del_time',0)
  116. ->where('product_category_id',$value['parent_id'])
  117. ->exists();
  118. if($bool) {
  119. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  120. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  121. }
  122. $parent_id = $value['parent_id'];
  123. }
  124. $data['data'][$key]['parent_id'] = $parent_id;
  125. $data['data'][$key]['crt_time'] = time();
  126. $data['data'][$key]['depart_id'] = $data['depart_id'];
  127. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  128. $bool = ProductCategory::where('title',$value['title'])
  129. ->where('top_depart_id',$data['top_depart_id'])
  130. ->where('del_time',0)
  131. ->exists();
  132. }else{
  133. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  134. $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id');
  135. $bool = ProductCategory::where('title',$value['title'])
  136. ->where('top_depart_id',$top_depart_id)
  137. ->where('id','<>',$data['id'])
  138. ->where('del_time',0)
  139. ->exists();
  140. }
  141. if($bool) return [false,'分类名称不能重复'];
  142. }
  143. return [true, $data];
  144. }
  145. /**
  146. * 产品编辑
  147. * @param $data
  148. * @param $user
  149. * @return array
  150. */
  151. public function productEdit($data,$user){
  152. list($status,$msg) = $this->productRule($data, $user, false);
  153. if(!$status) return [$status,$msg];
  154. try {
  155. DB::beginTransaction();
  156. $model = Product::where('id',$data['id'])->first();
  157. $model->product_category_id = $data['product_category_id'] ?? 0;
  158. $model->product_category = $data['product_category'] ?? '';
  159. $model->title = $data['title'];
  160. $model->code = $data['code'] ?? '';
  161. $model->warranty_time = $data['warranty_time'] ?? 0;
  162. $model->install_time = $data['install_time'] ?? 0;
  163. $model->size = $data['size'] ?? '';
  164. $model->unit = $data['unit'] ?? 0;
  165. $model->bar_code = $data['bar_code'] ?? '';
  166. $model->cost = $data['cost'] ?? 0;
  167. $model->retail_price = $data['retail_price'] ?? 0;
  168. $model->mark = $data['mark'] ?? '';
  169. $model->state = $data['state'] ?? 0;
  170. $model->save();
  171. $time = time();
  172. ProductIntroduction::where('product_id',$data['id'])
  173. ->where('del_time',0)
  174. ->update(['del_time' => $time]);
  175. if(! empty($data['introduction'])){
  176. $models = new ProductIntroduction();
  177. $models->product_id = $model->id;
  178. $models->introduction = $data['introduction'];
  179. $models->save();
  180. }
  181. $old = ProductInfo::where('del_time',0)
  182. ->where('product_id',$data['id'])
  183. ->select('file')
  184. ->get()->toArray();
  185. $old = array_column($old,'file');
  186. ProductInfo::where('del_time',0)
  187. ->where('product_id',$data['id'])
  188. ->update(['del_time' => $time]);
  189. $new = [];
  190. if(! empty($data['img'])){
  191. $insert = [];
  192. foreach ($data['img'] as $value){
  193. $insert[] = [
  194. 'product_id' => $model->id,
  195. 'file' => $value['url'],
  196. 'type' => ProductInfo::type_one,
  197. 'name' => $value['name'],
  198. 'crt_time' => $time,
  199. ];
  200. $new[] = $value['url'];
  201. }
  202. ProductInfo::insert($insert);
  203. }
  204. if(! empty($data['file'])){
  205. $insert = [];
  206. foreach ($data['file'] as $value){
  207. $insert[] = [
  208. 'product_id' => $model->id,
  209. 'file' => $value['url'],
  210. 'type' => ProductInfo::type_two,
  211. 'name' => $value['name'],
  212. 'crt_time' => $time,
  213. ];
  214. $new[] = $value['url'];
  215. }
  216. ProductInfo::insert($insert);
  217. }
  218. ProductPriceDetail::where('del_time',0)
  219. ->where('product_id',$data['id'])
  220. ->update(['del_time' => $time]);
  221. if(! empty($data['product_price'])){
  222. $insert = [];
  223. foreach ($data['product_price'] as $value){
  224. $insert[] = [
  225. 'product_id' => $model->id,
  226. 'basic_type_id' => $value['basic_type_id'],
  227. 'price' => $value['price'] ?? 0,
  228. 'crt_time' => $time,
  229. ];
  230. }
  231. ProductPriceDetail::insert($insert);
  232. }
  233. DB::commit();
  234. }catch (\Exception $exception){
  235. DB::rollBack();
  236. return [false,$exception->getMessage()];
  237. }
  238. $this->delStorageFile($old, $new);
  239. return [true,''];
  240. }
  241. /**
  242. * 产品新增
  243. * @param $data
  244. * @param $user
  245. * @return array
  246. */
  247. public function productAdd($data,$user){
  248. list($status,$msg) = $this->productRule($data, $user);
  249. if(!$status) return [$status,$msg];
  250. try {
  251. DB::beginTransaction();
  252. $model = new Product();
  253. $model->product_category_id = $data['product_category_id'] ?? 0;
  254. $model->product_category = $data['product_category'] ?? '';
  255. $model->title = $data['title'];
  256. $model->code = $data['code'] ?? '';
  257. $model->warranty_time = $data['warranty_time'] ?? 0;
  258. $model->install_time = $data['install_time'] ?? 0;
  259. $model->size = $data['size'] ?? '';
  260. $model->unit = $data['unit'] ?? 0;
  261. $model->bar_code = $data['bar_code'] ?? '';
  262. $model->cost = $data['cost'] ?? 0;
  263. $model->retail_price = $data['retail_price'] ?? 0;
  264. $model->mark = $data['mark'] ?? '';
  265. $model->state = $data['state'] ?? 0;
  266. $model->crt_id = $user['id'];
  267. $model->depart_id = $data['depart_id'] ?? 0;
  268. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  269. $model->save();
  270. $time = time();
  271. if(! empty($data['introduction'])){
  272. $models = new ProductIntroduction();
  273. $models->product_id = $model->id;
  274. $models->introduction = $data['introduction'];
  275. $models->save();
  276. }
  277. if(! empty($data['img'])){
  278. $insert = [];
  279. foreach ($data['img'] as $value){
  280. $insert[] = [
  281. 'product_id' => $model->id,
  282. 'file' => $value['url'],
  283. 'type' => ProductInfo::type_one,
  284. 'name' => $value['name'],
  285. 'crt_time' => $time,
  286. ];
  287. }
  288. ProductInfo::insert($insert);
  289. }
  290. if(! empty($data['file'])){
  291. $insert = [];
  292. foreach ($data['file'] as $value){
  293. $insert[] = [
  294. 'product_id' => $model->id,
  295. 'file' => $value['url'],
  296. 'type' => ProductInfo::type_two,
  297. 'name' => $value['name'],
  298. 'crt_time' => $time,
  299. ];
  300. }
  301. ProductInfo::insert($insert);
  302. }
  303. if(! empty($data['product_price'])){
  304. $insert = [];
  305. foreach ($data['product_price'] as $value){
  306. $insert[] = [
  307. 'product_id' => $model->id,
  308. 'basic_type_id' => $value['basic_type_id'],
  309. 'price' => $value['price'] ?? 0,
  310. 'crt_time' => $time,
  311. ];
  312. }
  313. ProductPriceDetail::insert($insert);
  314. }
  315. DB::commit();
  316. }catch (\Exception $exception){
  317. DB::rollBack();
  318. return [false,$exception->getMessage()];
  319. }
  320. return [true,''];
  321. }
  322. /**
  323. * 产品删除
  324. * @param $data
  325. * @return array
  326. */
  327. public function productDel($data){
  328. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  329. try {
  330. DB::beginTransaction();
  331. $time = time();
  332. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  333. ProductIntroduction::where('product_id',$data['id'])
  334. ->where('del_time',0)
  335. ->update(['del_time' => $time]);
  336. $old = ProductInfo::where('del_time',0)
  337. ->where('product_id',$data['id'])
  338. ->select('file')
  339. ->get()->toArray();
  340. $old = array_column($old,'file');
  341. ProductInfo::where('del_time',0)
  342. ->where('product_id',$data['id'])
  343. ->update(['del_time' => $time]);
  344. ProductPriceDetail::where('del_time',0)
  345. ->where('product_id',$data['id'])
  346. ->update(['del_time' => $time]);
  347. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  348. DB::commit();
  349. }catch (\Exception $exception){
  350. DB::rollBack();
  351. return [false,$exception->getMessage()];
  352. }
  353. $this->delStorageFile($old,[]);
  354. return [true,''];
  355. }
  356. /**
  357. * 产品详情
  358. * @param $data
  359. * @param $user
  360. * @return array
  361. */
  362. public function productDetail($data,$user){
  363. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  364. $customer = Product::where('del_time',0)
  365. ->where('id',$data['id'])
  366. ->first();
  367. if(empty($customer)) return [false,'产品不存在或已被删除'];
  368. $customer = $customer->toArray();
  369. $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
  370. $category = ProductCategory::where('id',$customer['product_category_id'])
  371. ->value('title');
  372. $customer['product_category_title'] = $category;
  373. $customer['introduction'] = "";
  374. $in = ProductIntroduction::where('del_time',0)
  375. ->where('product_id',$data['id'])
  376. ->first();
  377. if(! empty($in)) $customer['introduction'] = $in->introduction;
  378. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  379. $model = BasicType::TopClear($user,$data);
  380. $basic = $model->where('del_time',0)
  381. ->where('type',22)
  382. ->select('title','id','type')
  383. ->orderby('id', 'asc')->get()->toArray();
  384. $detail = ProductPriceDetail::where('del_time',0)
  385. ->where('product_id',$data['id'])
  386. ->select('product_id','basic_type_id','price')
  387. ->get()->toArray();
  388. $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
  389. ->pluck('title','id')
  390. ->toArray();
  391. //是否总公司
  392. $is_main = $user['is_all_depart'];
  393. $top_depart = $user['depart_top'][0] ?? [];
  394. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  395. $customer['product_price'] = [];
  396. //特殊功能按钮
  397. $special_button = $user['special_button'] ?? [];
  398. //成本隐藏
  399. $price = "******";
  400. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $customer['cost'];
  401. $customer['cost_show'] = $price;
  402. //所有金额
  403. foreach ($basic as $value){
  404. $show = 0;
  405. if(in_array(RoleMenuButton::special_one,$special_button)){
  406. $show = 1;
  407. }else{
  408. if($top_depart['basic_type_id'] == $value['id']) $show = 1;
  409. }
  410. $customer['product_price'][$value['id']] = [
  411. 'basic_type_id' => $value['id'],
  412. 'basic_type_title' => $title_map[$value['id']] ?? '',
  413. 'price' => 0,
  414. 'is_show' => $show,
  415. ];
  416. }
  417. //展示金额
  418. foreach ($detail as $value){
  419. if(isset($customer['product_price'][$value['basic_type_id']])) $customer['product_price'][$value['basic_type_id']]['price'] = $value['price'];
  420. }
  421. $customer['product_price'] = array_values($customer['product_price']);
  422. //单位
  423. $title = BasicType::where('id',$customer['unit'])->value('title');
  424. $customer['unit_name'] = $title;
  425. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  426. $customer_info = ProductInfo::where('del_time',0)
  427. ->where('product_id',$customer['id'])
  428. ->select('id','product_id','file','type','name')
  429. ->get()->toArray();
  430. foreach ($customer_info as $value){
  431. $tmp = [
  432. 'url' => $value['file'],
  433. 'name' => $value['name'],
  434. ];
  435. if($value['type'] == ProductInfo::type_one){
  436. $customer['img'][] = $tmp;
  437. }elseif ($value['type'] == ProductInfo::type_two){
  438. $customer['file'][] = $tmp;
  439. }
  440. }
  441. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  442. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  443. //可见范围
  444. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  445. $customer['depart'] = $return[0] ?? [];
  446. $customer['employee'] = $return[1] ?? [];
  447. return [true, $customer];
  448. }
  449. /**
  450. * 产品列表
  451. * @param $data
  452. * @param $user
  453. * @return array
  454. */
  455. public function productList($data,$user){
  456. $model = Product::ProductClear($user,$data);
  457. $model = $model->where('del_time',0)
  458. ->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')
  459. ->orderby('id', 'desc');
  460. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  461. if(isset($data['state'])) $model->where('state', $data['state']);
  462. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  463. if(! empty($data['product_category'])) {
  464. $product_category = ProductCategory::where('del_time',0)
  465. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  466. ->select('id')
  467. ->get()->toArray();
  468. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  469. }
  470. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  471. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  472. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  473. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  474. $model->where('crt_time','>=',$return[0]);
  475. $model->where('crt_time','<=',$return[1]);
  476. }
  477. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  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']}' AND 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']}' AND 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. $top_depart = $user['depart_top'][0] ?? [];
  579. //特殊功能按钮
  580. $special_button = $user['special_button'] ?? [];
  581. foreach ($data['data'] as $key => $value){
  582. $tmp = [];
  583. if(isset($detail_map[$value['id']])){
  584. $d = $detail_map[$value['id']];
  585. foreach ($d as $v){
  586. $is_show = 0;
  587. if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
  588. $is_use = 0;
  589. if($top_depart['basic_type_id'] == $v['basic_type_id']) $is_use = 1;
  590. $tmp[] = [
  591. 'basic_type_id' => $v['basic_type_id'],
  592. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  593. 'price' => $v['price'],
  594. 'is_show' => $is_show,
  595. 'is_use' => $is_use
  596. ];
  597. }
  598. }
  599. $data['data'][$key]['product_price'] = $tmp;
  600. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  601. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  602. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  603. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  604. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  605. //成本隐藏
  606. $price = "******";
  607. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $value['cost'];
  608. $data['data'][$key]['cost_show'] = $price;
  609. }
  610. return $data;
  611. }
  612. //获取产品字典
  613. public function getProductDetail($product_id = []){
  614. if(empty($product_id)) return [];
  615. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  616. return array_column($pro,null,'id');
  617. }
  618. //获取产品使用价格
  619. public function getProductPrice($product_id = [], $type = 1){
  620. if(! is_array($product_id)) $product_id = [$product_id];
  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. }