ProductService.php 19 KB

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