ProductService.php 33 KB

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