ProductService.php 20 KB

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