ProductService.php 31 KB

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