ProductService.php 17 KB

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