ProductService.php 18 KB

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