ProductService.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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\ProductCategory;
  8. use App\Model\ProductInfo;
  9. use App\Model\ProductIntroduction;
  10. use App\Model\ProductInventory;
  11. use App\Model\ProductPriceDetail;
  12. use App\Model\ProductRange;
  13. use Illuminate\Support\Facades\DB;
  14. /**
  15. * 产品管理
  16. */
  17. class ProductService extends Service
  18. {
  19. /**
  20. * 产品分类编辑
  21. * @param $data
  22. * @param $user
  23. * @return array
  24. */
  25. public function productCategoryEdit($data,$user){
  26. list($status,$msg) = $this->productCategoryRule($data,false);
  27. if(!$status) return [$status,$msg];
  28. $update = $msg['data'][0];
  29. $model = new ProductCategory();
  30. $model->where('id',$data['id'])->update($update);
  31. return [true,''];
  32. }
  33. /**
  34. * 产品分类新增
  35. * @param $data
  36. * @param $user
  37. * @return array
  38. */
  39. public function productCategoryAdd($data,$user){
  40. list($status,$msg) = $this->productCategoryRule($data);
  41. if(!$status) return [$status,$msg];
  42. ProductCategory::insert($msg['data']);
  43. return [true,''];
  44. }
  45. /**
  46. * 产品分类删除
  47. * @param $data
  48. * @return array
  49. */
  50. public function productCategoryDel($data){
  51. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  52. $bool = Product::where('del_time',0)
  53. ->where('product_category_id',$data['id'])
  54. ->exists();
  55. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  56. try {
  57. DB::beginTransaction();
  58. ProductCategory::where('id',$data['id'])->update([
  59. 'del_time' => time()
  60. ]);
  61. DB::commit();
  62. }catch (\Exception $exception){
  63. DB::rollBack();
  64. return [false,$exception->getMessage()];
  65. }
  66. return [true,''];
  67. }
  68. /**
  69. * 产品分类列表
  70. * @param $data
  71. * @param $user
  72. * @return array
  73. */
  74. public function productCategoryList($data,$user){
  75. $model = ProductCategory::where('del_time',0)
  76. ->select('title','id','parent_id')
  77. ->orderby('id','desc');
  78. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  79. $list = $this->limit($model,'',$data);
  80. return [true, $list];
  81. }
  82. /**
  83. * 产品分类参数规则
  84. * @param $data
  85. * @param $is_add
  86. * @return array
  87. */
  88. public function productCategoryRule($data, $is_add = true){
  89. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  90. $title = array_column($data['data'],'title');
  91. $title = array_map(function($val) {
  92. return $val !== null ? $val : 0;
  93. }, $title);
  94. $title_count = array_count_values($title);
  95. foreach ($title as $value){
  96. if(empty($value)) return [false,'名称不能为空!'];
  97. if($title_count[$value] > 1) return [false,'名称不能重复'];
  98. }
  99. foreach ($data['data'] as $key => $value){
  100. $data['data'][$key]['upd_time'] = time();
  101. if($is_add){
  102. $parent_id = 0;
  103. if(! empty($value['parent_id'])) {
  104. $bool = Product::where('del_time',0)
  105. ->where('product_category_id',$value['parent_id'])
  106. ->exists();
  107. if($bool) {
  108. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  109. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  110. }
  111. $parent_id = $value['parent_id'];
  112. }
  113. $data['data'][$key]['parent_id'] = $parent_id;
  114. $data['data'][$key]['crt_time'] = time();
  115. $bool = ProductCategory::where('title',$value['title'])
  116. ->where('del_time',0)
  117. ->exists();
  118. }else{
  119. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  120. $bool = ProductCategory::where('title',$value['title'])
  121. ->where('id','<>',$data['id'])
  122. ->where('del_time',0)
  123. ->exists();
  124. }
  125. if($bool) return [false,'分类名称不能重复'];
  126. }
  127. return [true, $data];
  128. }
  129. /**
  130. * 产品编辑
  131. * @param $data
  132. * @param $user
  133. * @return array
  134. */
  135. public function productEdit($data,$user){
  136. list($status,$msg) = $this->productRule($data, $user, false);
  137. if(!$status) return [$status,$msg];
  138. try {
  139. DB::beginTransaction();
  140. $model = Product::where('id',$data['id'])->first();
  141. $model->product_category_id = $data['product_category_id'] ?? 0;
  142. $model->title = $data['title'];
  143. $model->code = $data['code'] ?? '';
  144. $model->size = $data['size'] ?? '';
  145. $model->unit = $data['unit'] ?? 0;
  146. $model->bar_code = $data['bar_code'] ?? '';
  147. $model->cost = $data['cost'] ?? 0;
  148. $model->depart_price = $data['depart_price'] ?? 0;
  149. $model->retail_price = $data['retail_price'] ?? 0;
  150. $model->mark = $data['mark'] ?? '';
  151. $model->state = $data['state'] ?? 0;
  152. $model->save();
  153. $time = time();
  154. ProductIntroduction::where('product_id',$data['id'])
  155. ->where('del_time',0)
  156. ->update(['del_time' => $time]);
  157. if(! empty($data['introduction'])){
  158. $models = new ProductIntroduction();
  159. $models->product_id = $model->id;
  160. $models->introduction = $data['introduction'];
  161. $models->save();
  162. }
  163. ProductInfo::where('del_time',0)
  164. ->where('product_id',$data['id'])
  165. ->update(['del_time' => $time]);
  166. if(! empty($data['img'])){
  167. $insert = [];
  168. foreach ($data['img'] as $value){
  169. $insert[] = [
  170. 'product_id' => $model->id,
  171. 'file' => $value['url'],
  172. 'type' => ProductInfo::type_one,
  173. 'name' => $value['name'],
  174. 'crt_time' => $time,
  175. ];
  176. }
  177. ProductInfo::insert($insert);
  178. }
  179. if(! empty($data['file'])){
  180. $insert = [];
  181. foreach ($data['file'] as $value){
  182. $insert[] = [
  183. 'product_id' => $model->id,
  184. 'file' => $value['url'],
  185. 'type' => ProductInfo::type_two,
  186. 'name' => $value['name'],
  187. 'crt_time' => $time,
  188. ];
  189. }
  190. ProductInfo::insert($insert);
  191. }
  192. ProductRange::where('del_time',0)
  193. ->where('product_id',$data['id'])
  194. ->update(['del_time' => $time]);
  195. if(! empty($data['depart'])){
  196. $insert = [];
  197. foreach ($data['depart'] as $value){
  198. $insert[] = [
  199. 'product_id' => $model->id,
  200. 'depart_id' => $value,
  201. 'type' => ProductRange::type_one,
  202. 'crt_time' => $time,
  203. ];
  204. }
  205. ProductRange::insert($insert);
  206. }
  207. if(! empty($data['employee'])){
  208. $insert = [];
  209. foreach ($data['employee'] as $value){
  210. $insert[] = [
  211. 'product_id' => $model->id,
  212. 'employee_id' => $value,
  213. 'type' => ProductRange::type_two,
  214. 'crt_time' => $time,
  215. ];
  216. }
  217. ProductRange::insert($insert);
  218. }
  219. ProductPriceDetail::where('del_time',0)
  220. ->where('product_id',$data['id'])
  221. ->update(['del_time' => $time]);
  222. if(! empty($data['product_price'])){
  223. $insert = [];
  224. foreach ($data['product_price'] as $value){
  225. $insert[] = [
  226. 'product_id' => $model->id,
  227. 'basic_type_id' => $value['basic_type_id'],
  228. 'grade' => $value['grade'],
  229. 'crt_time' => $time,
  230. ];
  231. }
  232. ProductPriceDetail::insert($insert);
  233. }
  234. DB::commit();
  235. }catch (\Exception $exception){
  236. DB::rollBack();
  237. return [false,$exception->getMessage()];
  238. }
  239. return [true,''];
  240. }
  241. /**
  242. * 产品新增
  243. * @param $data
  244. * @param $user
  245. * @return array
  246. */
  247. public function productAdd($data,$user){
  248. list($status,$msg) = $this->productRule($data, $user);
  249. if(!$status) return [$status,$msg];
  250. try {
  251. DB::beginTransaction();
  252. $model = new Product();
  253. $model->product_category_id = $data['product_category_id'] ?? 0;
  254. $model->title = $data['title'];
  255. $model->code = $data['code'] ?? '';
  256. $model->size = $data['size'] ?? '';
  257. $model->unit = $data['unit'] ?? 0;
  258. $model->bar_code = $data['bar_code'] ?? '';
  259. $model->cost = $data['cost'] ?? 0;
  260. $model->depart_price = $data['depart_price'] ?? 0;
  261. $model->retail_price = $data['retail_price'] ?? 0;
  262. $model->mark = $data['mark'] ?? '';
  263. $model->state = $data['state'] ?? 0;
  264. $model->crt_id = $user['id'];
  265. $model->depart_id = $data['depart_id'] ?? 0;
  266. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  267. $model->save();
  268. $time = time();
  269. if(! empty($data['introduction'])){
  270. $models = new ProductIntroduction();
  271. $models->product_id = $model->id;
  272. $models->introduction = $data['introduction'];
  273. $models->save();
  274. }
  275. if(! empty($data['img'])){
  276. $insert = [];
  277. foreach ($data['img'] as $value){
  278. $insert[] = [
  279. 'product_id' => $model->id,
  280. 'file' => $value['url'],
  281. 'type' => ProductInfo::type_one,
  282. 'name' => $value['name'],
  283. 'crt_time' => $time,
  284. ];
  285. }
  286. ProductInfo::insert($insert);
  287. }
  288. if(! empty($data['file'])){
  289. $insert = [];
  290. foreach ($data['file'] as $value){
  291. $insert[] = [
  292. 'product_id' => $model->id,
  293. 'file' => $value['url'],
  294. 'type' => ProductInfo::type_two,
  295. 'name' => $value['name'],
  296. 'crt_time' => $time,
  297. ];
  298. }
  299. ProductInfo::insert($insert);
  300. }
  301. if(! empty($data['depart'])){
  302. $insert = [];
  303. foreach ($data['depart'] as $value){
  304. $insert[] = [
  305. 'product_id' => $model->id,
  306. 'depart_id' => $value,
  307. 'type' => ProductRange::type_one,
  308. 'crt_time' => $time,
  309. ];
  310. }
  311. ProductRange::insert($insert);
  312. }
  313. if(! empty($data['employee'])){
  314. $insert = [];
  315. foreach ($data['employee'] as $value){
  316. $insert[] = [
  317. 'product_id' => $model->id,
  318. 'employee_id' => $value,
  319. 'type' => ProductRange::type_two,
  320. 'crt_time' => $time,
  321. ];
  322. }
  323. ProductRange::insert($insert);
  324. }
  325. if(! empty($data['product_price'])){
  326. $insert = [];
  327. foreach ($data['product_price'] as $value){
  328. $insert[] = [
  329. 'product_id' => $model->id,
  330. 'basic_type_id' => $value['basic_type_id'],
  331. 'grade' => $value['grade'],
  332. 'crt_time' => $time,
  333. ];
  334. }
  335. ProductPriceDetail::insert($insert);
  336. }
  337. DB::commit();
  338. }catch (\Exception $exception){
  339. DB::rollBack();
  340. return [false,$exception->getMessage()];
  341. }
  342. return [true,''];
  343. }
  344. /**
  345. * 产品删除
  346. * @param $data
  347. * @return array
  348. */
  349. public function productDel($data){
  350. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  351. try {
  352. DB::beginTransaction();
  353. $time = time();
  354. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  355. ProductIntroduction::where('product_id',$data['id'])
  356. ->where('del_time',0)
  357. ->update(['del_time' => $time]);
  358. ProductInfo::where('del_time',0)
  359. ->where('product_id',$data['id'])
  360. ->update(['del_time' => $time]);
  361. ProductRange::where('del_time',0)
  362. ->where('product_id',$data['id'])
  363. ->update(['del_time' => $time]);
  364. ProductPriceDetail::where('del_time',0)
  365. ->where('product_id',$data['id'])
  366. ->update(['del_time' => $time]);
  367. DB::commit();
  368. }catch (\Exception $exception){
  369. DB::rollBack();
  370. return [false,$exception->getMessage()];
  371. }
  372. return [true,''];
  373. }
  374. /**
  375. * 产品详情
  376. * @param $data
  377. * @param $user
  378. * @return array
  379. */
  380. public function productDetail($data,$user){
  381. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  382. $customer = Product::where('del_time',0)
  383. ->where('id',$data['id'])
  384. ->first();
  385. if(empty($customer)) return [false,'产品不存在或已被删除'];
  386. $customer = $customer->toArray();
  387. $category = ProductCategory::where('id',$customer['product_category_id'])
  388. ->value('title');
  389. $customer['product_category_title'] = $category;
  390. $customer['introduction'] = "";
  391. $in = ProductIntroduction::where('del_time',0)
  392. ->where('product_id',$data['id'])
  393. ->first();
  394. if(! empty($in)) $customer['introduction'] = $in->introduction;
  395. $detail = ProductPriceDetail::where('del_time',0)
  396. ->where('product_id',$data['id'])
  397. ->select('product_id','basic_type_id','grade')
  398. ->get()->toArray();
  399. $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
  400. ->pluck('title','id')
  401. ->toArray();
  402. $customer['product_price'] = [];
  403. foreach ($detail as $value){
  404. $customer['product_price'][] = [
  405. 'basic_type_id' => $value['basic_type_id'],
  406. 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
  407. 'grade' => $value['grade'],
  408. ];
  409. }
  410. //单位
  411. $title = BasicType::where('id',$customer['unit'])->value('title');
  412. $customer['unit_name'] = $title;
  413. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  414. $customer_info = ProductInfo::where('del_time',0)
  415. ->where('product_id',$customer['id'])
  416. ->select('id','product_id','file','type','name')
  417. ->get()->toArray();
  418. foreach ($customer_info as $value){
  419. $tmp = [
  420. 'url' => $value['file'],
  421. 'name' => $value['name'],
  422. ];
  423. if($value['type'] == ProductInfo::type_one){
  424. $customer['img'][] = $tmp;
  425. }elseif ($value['type'] == ProductInfo::type_two){
  426. $customer['file'][] = $tmp;
  427. }
  428. }
  429. $customer_range = ProductRange::where('del_time',0)
  430. ->where('product_id',$customer['id'])
  431. ->select('id','product_id','depart_id','type','employee_id')
  432. ->get()->toArray();
  433. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_range,'employee_id'))))
  434. ->pluck('emp_name','id')
  435. ->toArray();
  436. $depart_map = Depart::whereIn('id',array_unique(array_column($customer_range,'depart_id')))
  437. ->pluck('title','id')
  438. ->toArray();
  439. foreach ($customer_range as $value){
  440. if($value['type'] == ProductRange::type_one){
  441. $tmp = [
  442. 'id' => $value['depart_id'],
  443. 'name' => $depart_map[$value['depart_id']],
  444. ];
  445. $customer['depart'][] = $tmp;
  446. }elseif ($value['type'] == ProductRange::type_two){
  447. $tmp = [
  448. 'id' => $value['employee_id'],
  449. 'name' => $emp_map[$value['employee_id']] ?? '',
  450. ];
  451. $customer['employee'][] = $tmp;
  452. }
  453. }
  454. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  455. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  456. return [true, $customer];
  457. }
  458. /**
  459. * 产品列表
  460. * @param $data
  461. * @param $user
  462. * @return array
  463. */
  464. public function productList($data,$user){
  465. $model = new Product(['userData' => $user]);
  466. $model = $model->where('del_time',0)
  467. ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','depart_price','state','crt_id','crt_time','mark','depart_id','top_depart_id')
  468. ->orderby('id', 'desc');
  469. //getALL传入后无视设置范围
  470. if(empty($data['getAll']) && $user['id'] != Employee::SPECIAL_ADMIN) {
  471. $user_id = $user['id'];
  472. $depart_id = $user['depart_range'];
  473. $product_id = ProductRange::where('del_time',0)
  474. ->where(function ($query) use($user_id, $depart_id) {
  475. $query->where('employee_id',$user_id)
  476. ->orWhereIn('depart_id', $depart_id);
  477. })->select('product_id')->get()
  478. ->toArray();
  479. $model->whereIn('id',array_unique(array_column($product_id,'product_id')));
  480. }
  481. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  482. if(isset($data['state'])) $model->where('state', $data['state']);
  483. $list = $this->limit($model,'',$data);
  484. $list = $this->fillData($list);
  485. return [true, $list];
  486. }
  487. /**
  488. * 产品参数规则
  489. * @param $data
  490. * @param $is_add
  491. * @return array
  492. */
  493. public function productRule(&$data, $user, $is_add = true){
  494. if(empty($data['title'])) return [false,'产品名称不能为空'];
  495. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  496. if(empty($data['code'])) return [false,'产品编码不能为空'];
  497. if(empty($data['cost'])) return [false,'成本不能为空'];
  498. $res = $this->checkNumber($data['cost']);
  499. if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值'];
  500. if(empty($data['depart_price'])) return [false,'分社价格不能为空'];
  501. $res = $this->checkNumber($data['depart_price']);
  502. if(! $res) return [false,'分社价格请输入不超过两位小数并且大于0的数值'];
  503. if(empty($data['retail_price'])) return [false,'零售价不能为空'];
  504. $res = $this->checkNumber($data['retail_price']);
  505. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
  506. //所属部门 以及 顶级部门
  507. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  508. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  509. if($is_add){
  510. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  511. ->where('del_time',0)
  512. ->exists();
  513. }else{
  514. if(empty($data['id'])) return [false,'ID不能为空'];
  515. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  516. ->where('id','<>',$data['id'])
  517. ->where('del_time',0)
  518. ->exists();
  519. }
  520. if($bool) return [false,'产品名称或编码不能重复'];
  521. return [true, $data];
  522. }
  523. /**
  524. * 拼接数据
  525. * @param $data
  526. * @return array
  527. */
  528. public function fillData($data){
  529. if(empty($data['data'])) return $data;
  530. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  531. ->pluck('emp_name','id')
  532. ->toArray();
  533. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  534. ->pluck('title','id')
  535. ->toArray();
  536. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  537. ->pluck('title','id')
  538. ->toArray();
  539. foreach ($data['data'] as $key => $value){
  540. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  541. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  542. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  543. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  544. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  545. }
  546. return $data;
  547. }
  548. }