ProductService.php 18 KB

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