ProductService.php 34 KB

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