ProductService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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\ProductInventory;
  10. use App\Model\ProductRange;
  11. use Illuminate\Support\Facades\DB;
  12. class ProductService extends Service
  13. {
  14. public function productCategoryEdit($data,$user){
  15. list($status,$msg) = $this->productCategoryRule($data,false);
  16. if(!$status) return [$status,$msg];
  17. $update = $msg['data'][0];
  18. $model = new ProductCategory();
  19. $model->where('id',$data['id'])->update($update);
  20. return [true,''];
  21. }
  22. public function productCategoryAdd($data,$user){
  23. list($status,$msg) = $this->productCategoryRule($data);
  24. if(!$status) return [$status,$msg];
  25. ProductCategory::insert($msg['data']);
  26. return [true,''];
  27. }
  28. public function productCategoryDel($data){
  29. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  30. $bool = Product::where('del_time',0)
  31. ->where('product_category_id',$data['id'])
  32. ->exists();
  33. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  34. try {
  35. DB::beginTransaction();
  36. ProductCategory::where('id',$data['id'])->update([
  37. 'del_time' => time()
  38. ]);
  39. DB::commit();
  40. }catch (\Exception $exception){
  41. DB::rollBack();
  42. return [false,$exception->getMessage()];
  43. }
  44. return [true,''];
  45. }
  46. public function productCategoryList($data,$user){
  47. $model = ProductCategory::where('del_time',0)
  48. ->select('title','id','parent_id')
  49. ->orderby('id','desc');
  50. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  51. $list = $this->limit($model,'',$data);
  52. $list = $this->fillData($list);
  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->retail_price = $data['retail_price'] ?? 0;
  109. $model->mark = $data['mark'] ?? '';
  110. $model->state = $data['state'] ?? 0;
  111. $model->save();
  112. $time = time();
  113. ProductInfo::where('del_time',0)
  114. ->where('product_id',$data['id'])
  115. ->update(['del_time' => $time]);
  116. if(! empty($data['img'])){
  117. $insert = [];
  118. foreach ($data['img'] as $value){
  119. $insert[] = [
  120. 'product_id' => $model->id,
  121. 'file' => $value['url'],
  122. 'type' => ProductInfo::type_one,
  123. 'name' => $value['name'],
  124. 'crt_time' => $time,
  125. ];
  126. }
  127. ProductInfo::insert($insert);
  128. }
  129. if(! empty($data['file'])){
  130. $insert = [];
  131. foreach ($data['file'] as $value){
  132. $insert[] = [
  133. 'product_id' => $model->id,
  134. 'file' => $value['url'],
  135. 'type' => ProductInfo::type_two,
  136. 'name' => $value['name'],
  137. 'crt_time' => $time,
  138. ];
  139. }
  140. ProductInfo::insert($insert);
  141. }
  142. ProductRange::where('del_time',0)
  143. ->where('product_id',$data['id'])
  144. ->update(['del_time' => $time]);
  145. if(! empty($data['depart'])){
  146. $insert = [];
  147. foreach ($data['depart'] as $value){
  148. $insert[] = [
  149. 'product_id' => $model->id,
  150. 'file' => $value,
  151. 'type' => ProductRange::type_one,
  152. 'crt_time' => $time,
  153. ];
  154. }
  155. ProductRange::insert($insert);
  156. }
  157. if(! empty($data['employee'])){
  158. $insert = [];
  159. foreach ($data['employee'] as $value){
  160. $insert[] = [
  161. 'product_id' => $model->id,
  162. 'file' => $value,
  163. 'type' => ProductRange::type_two,
  164. 'crt_time' => $time,
  165. ];
  166. }
  167. ProductRange::insert($insert);
  168. }
  169. DB::commit();
  170. }catch (\Exception $exception){
  171. DB::rollBack();
  172. return [false,$exception->getMessage()];
  173. }
  174. return [true,''];
  175. }
  176. public function productAdd($data,$user){
  177. list($status,$msg) = $this->productRule($data);
  178. if(!$status) return [$status,$msg];
  179. try {
  180. DB::beginTransaction();
  181. $model = new Product();
  182. $model->product_category_id = $data['product_category_id'] ?? 0;
  183. $model->title = $data['title'];
  184. $model->code = $data['code'] ?? '';
  185. $model->size = $data['size'] ?? '';
  186. $model->unit = $data['unit'] ?? 0;
  187. $model->bar_code = $data['bar_code'] ?? '';
  188. $model->retail_price = $data['retail_price'] ?? 0;
  189. $model->mark = $data['mark'] ?? '';
  190. $model->state = $data['state'] ?? 0;
  191. $model->crt_id = $user['id'];
  192. $model->save();
  193. $time = time();
  194. if(! empty($data['img'])){
  195. $insert = [];
  196. foreach ($data['img'] as $value){
  197. $insert[] = [
  198. 'product_id' => $model->id,
  199. 'file' => $value['url'],
  200. 'type' => ProductInfo::type_one,
  201. 'name' => $value['name'],
  202. 'crt_time' => $time,
  203. ];
  204. }
  205. ProductInfo::insert($insert);
  206. }
  207. if(! empty($data['file'])){
  208. $insert = [];
  209. foreach ($data['file'] as $value){
  210. $insert[] = [
  211. 'product_id' => $model->id,
  212. 'file' => $value['url'],
  213. 'type' => ProductInfo::type_two,
  214. 'name' => $value['name'],
  215. 'crt_time' => $time,
  216. ];
  217. }
  218. ProductInfo::insert($insert);
  219. }
  220. if(! empty($data['depart'])){
  221. $insert = [];
  222. foreach ($data['depart'] as $value){
  223. $insert[] = [
  224. 'product_id' => $model->id,
  225. 'file' => $value,
  226. 'type' => ProductRange::type_one,
  227. 'crt_time' => $time,
  228. ];
  229. }
  230. ProductRange::insert($insert);
  231. }
  232. if(! empty($data['employee'])){
  233. $insert = [];
  234. foreach ($data['employee'] as $value){
  235. $insert[] = [
  236. 'product_id' => $model->id,
  237. 'file' => $value,
  238. 'type' => ProductRange::type_two,
  239. 'crt_time' => $time,
  240. ];
  241. }
  242. ProductRange::insert($insert);
  243. }
  244. DB::commit();
  245. }catch (\Exception $exception){
  246. DB::rollBack();
  247. return [false,$exception->getMessage()];
  248. }
  249. return [true,''];
  250. }
  251. public function productDel($data){
  252. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  253. try {
  254. DB::beginTransaction();
  255. $time = time();
  256. Product::where('id',$data['id'])->update(['del_time' => $time]);
  257. ProductInfo::where('del_time',0)
  258. ->where('product_id',$data['id'])
  259. ->update(['del_time' => $time]);
  260. ProductRange::where('del_time',0)
  261. ->where('product_id',$data['id'])
  262. ->update(['del_time' => $time]);
  263. DB::commit();
  264. }catch (\Exception $exception){
  265. DB::rollBack();
  266. return [false,$exception->getMessage()];
  267. }
  268. return [true,''];
  269. }
  270. public function productDetail($data,$user){
  271. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  272. $customer = Product::where('del_time',0)
  273. ->where('id',$data['id'])
  274. ->first();
  275. if(empty($customer)) return [false,'产品不存在或已被删除'];
  276. $customer = $customer->toArray();
  277. //单位
  278. $title = BasicType::where('id',$customer['unit'])->value('title');
  279. $customer['unit_name'] = $title;
  280. //库存
  281. $customer['product_inventory'] = [];
  282. $depart_id = array_column($user['rule_depart'],'depart_id');
  283. $inventory = $this->getProductInventory([$data['id']], $depart_id);
  284. if(! empty($inventory)) $customer['product_inventory'] = $inventory;
  285. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  286. $customer_info = ProductInfo::where('del_time',0)
  287. ->where('product_id',$customer['id'])
  288. ->select('id','product_id','file','type','name')
  289. ->get()->toArray();
  290. foreach ($customer_info as $value){
  291. $tmp = [
  292. 'url' => $value['file'],
  293. 'name' => $value['name'],
  294. ];
  295. if($value['type'] == ProductInfo::type_one){
  296. $customer['img'][] = $tmp;
  297. }elseif ($value['type'] == ProductInfo::type_two){
  298. $customer['file'][] = $tmp;
  299. }
  300. }
  301. $customer_range = ProductRange::where('del_time',0)
  302. ->where('product_id',$customer['id'])
  303. ->select('id','product_id','depart_id','type','employee_id')
  304. ->get()->toArray();
  305. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_range,'employee_id'))))
  306. ->pluck('emp_name','id')
  307. ->toArray();
  308. $depart_map = Depart::whereIn('id',array_unique(array_column($customer_range,'depart_id')))
  309. ->pluck('title','id')
  310. ->toArray();
  311. foreach ($customer_range as $value){
  312. if($value['type'] == ProductRange::type_one){
  313. $tmp = [
  314. 'id' => $value['depart_id'],
  315. 'name' => $depart_map[$value['depart_id']],
  316. ];
  317. $customer['depart'][] = $tmp;
  318. }elseif ($value['type'] == ProductRange::type_two){
  319. $tmp = [
  320. 'id' => $value['employee_id'],
  321. 'name' => $emp_map[$value['employee_id']],
  322. ];
  323. $customer['employee'][] = $tmp;
  324. }
  325. }
  326. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  327. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  328. return [true, $customer];
  329. }
  330. public function productList($data,$user){
  331. $model = Product::where('del_time',0)
  332. ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','state','crt_id','crt_time','mark')
  333. ->orderby('id', 'desc');
  334. if(! $user['is_main']) {
  335. $user_id = $user['id'];
  336. $depart_id = array_column($user['rule_depart'],'depart_id');
  337. $product_id = ProductRange::where('del_time',0)
  338. ->where(function ($query) use($user_id, $depart_id) {
  339. $query->where('employee_id',$user_id)
  340. ->orWhereIn('depart_id', $depart_id);
  341. })->select('product_id')->get()
  342. ->toArray();
  343. $model->whereIn('id',array_column($product_id,'product_id'));
  344. }
  345. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  346. if(isset($data['state'])) $model->where('state', $data['state']);
  347. $list = $this->limit($model,'',$data);
  348. $list = $this->fillData($list);
  349. return [true, $list];
  350. }
  351. public function productRule($data, $is_add = true){
  352. if(empty($data['title'])) return [false,'产品名称不能为空'];
  353. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  354. if(empty($data['code'])) return [false,'产品编码不能为空'];
  355. if(! empty($data['retail_price'])){
  356. $res = $this->checkNumber($data['retail_price']);
  357. if(! $res) return [false,'零售价请输入不超过两位小数的数值'];
  358. }
  359. if($is_add){
  360. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  361. ->where('del_time',0)
  362. ->exists();
  363. }else{
  364. if(empty($data['id'])) return [false,'ID不能为空'];
  365. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  366. ->where('id','<>',$data['id'])
  367. ->where('del_time',0)
  368. ->exists();
  369. }
  370. if($bool) return [false,'产品名称或编码不能重复'];
  371. return [true, $data];
  372. }
  373. public function fillData($data){
  374. if(empty($data['data'])) return $data;
  375. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  376. ->pluck('emp_name','id')
  377. ->toArray();
  378. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  379. ->pluck('title','id')
  380. ->toArray();
  381. $basic = BasicType::where('id',array_unique(array_column($data['data'],'unit')))
  382. ->pluck('title','id')
  383. ->toArray();
  384. foreach ($data['data'] as $key => $value){
  385. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  386. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  387. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  388. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  389. $data['data'][$key]['unit_name'] = $basic[$value['unit']] ?? '';
  390. }
  391. return $data;
  392. }
  393. public function getProductInventory($product_id = [],$depart_id = []){
  394. if(empty($product_id)) return [];
  395. $result = ProductInventory::whereIn('product_id',$product_id)
  396. ->when(! empty($depart_id), function ($query) use ($depart_id) {
  397. return $query->whereIn('depart_id', $depart_id);
  398. })
  399. ->select('id','product_id','number','depart_id','lock_number')
  400. ->get()
  401. ->toArray();
  402. return $result;
  403. }
  404. }