ProductService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\Product;
  5. use App\Model\ProductCategory;
  6. use App\Model\ProductInfo;
  7. use App\Model\ProductInventory;
  8. use App\Model\ProductRange;
  9. use Illuminate\Support\Facades\DB;
  10. class ProductService extends Service
  11. {
  12. public function productCategoryEdit($data,$user){
  13. list($status,$msg) = $this->productCategoryRule($data,false);
  14. if(!$status) return [$status,$msg];
  15. $update = $msg['data'][0];
  16. $model = new ProductCategory();
  17. $model->where('id',$data['id'])->update($update);
  18. return [true,''];
  19. }
  20. public function productCategoryAdd($data,$user){
  21. list($status,$msg) = $this->productCategoryRule($data);
  22. if(!$status) return [$status,$msg];
  23. ProductCategory::insert($msg['data']);
  24. return [true,''];
  25. }
  26. public function productCategoryDel($data){
  27. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  28. $bool = Product::where('del_time',0)
  29. ->where('product_category_id',$data['id'])
  30. ->exists();
  31. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  32. try {
  33. DB::beginTransaction();
  34. ProductCategory::where('id',$data['id'])->update([
  35. 'del_time' => time()
  36. ]);
  37. DB::commit();
  38. }catch (\Exception $exception){
  39. DB::rollBack();
  40. return [false,$exception->getMessage()];
  41. }
  42. return [true,''];
  43. }
  44. public function productCategoryList($data,$user){
  45. $model = ProductCategory::where('del_time',0)
  46. ->select('title','id','parent_id')
  47. ->orderby('id','desc');
  48. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  49. $list = $model->get()->toArray();
  50. if(! empty($list)) {
  51. $list = $this->makeTree(0,$list);
  52. $list = $this->set_sort_circle($list);
  53. }
  54. return [true, $list];
  55. }
  56. public function productCategoryRule($data, $is_add = true){
  57. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  58. $title = array_column($data['data'],'title');
  59. $title = array_map(function($val) {
  60. return $val !== null ? $val : 0;
  61. }, $title);
  62. $title_count = array_count_values($title);
  63. foreach ($title as $value){
  64. if(empty($value)) return [false,'名称不能为空!'];
  65. if($title_count[$value] > 1) return [false,'名称不能重复'];
  66. }
  67. foreach ($data['data'] as $key => $value){
  68. $data['data'][$key]['upd_time'] = time();
  69. if($is_add){
  70. $parent_id = 0;
  71. if(! empty($value['parent_id'])) {
  72. $bool = Product::where('del_time',0)
  73. ->where('product_category_id',$value['parent_id'])
  74. ->exists();
  75. if($bool) {
  76. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  77. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  78. }
  79. $parent_id = $value['parent_id'];
  80. }
  81. $data['data'][$key]['parent_id'] = $parent_id;
  82. $data['data'][$key]['crt_time'] = time();
  83. $bool = ProductCategory::where('title',$value['title'])
  84. ->where('del_time',0)
  85. ->exists();
  86. }else{
  87. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  88. $bool = ProductCategory::where('title',$value['title'])
  89. ->where('id','<>',$data['id'])
  90. ->where('del_time',0)
  91. ->exists();
  92. }
  93. if($bool) return [false,'分类名称不能重复'];
  94. }
  95. return [true, $data];
  96. }
  97. public function productEdit($data,$user){
  98. list($status,$msg) = $this->productRule($data,false);
  99. if(!$status) return [$status,$msg];
  100. try {
  101. DB::beginTransaction();
  102. $model = Product::where('id',$data['id'])->first();
  103. $model->product_category_id = $data['product_category_id'] ?? 0;
  104. $model->title = $data['title'];
  105. $model->code = $data['code'] ?? '';
  106. $model->size = $data['size'] ?? '';
  107. $model->unit = $data['unit'] ?? '';
  108. $model->bar_code = $data['bar_code'] ?? '';
  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'] ?? '';
  188. $model->bar_code = $data['bar_code'] ?? '';
  189. $model->retail_price = $data['retail_price'] ?? 0;
  190. $model->mark = $data['mark'] ?? '';
  191. $model->state = $data['state'] ?? 0;
  192. $model->crt_id = $user['id'];
  193. $model->save();
  194. $time = time();
  195. if(! empty($data['img'])){
  196. $insert = [];
  197. foreach ($data['img'] as $value){
  198. $insert[] = [
  199. 'product_id' => $model->id,
  200. 'file' => $value['url'],
  201. 'type' => ProductInfo::type_one,
  202. 'name' => $value['name'],
  203. 'crt_time' => $time,
  204. ];
  205. }
  206. ProductInfo::insert($insert);
  207. }
  208. if(! empty($data['file'])){
  209. $insert = [];
  210. foreach ($data['file'] as $value){
  211. $insert[] = [
  212. 'product_id' => $model->id,
  213. 'file' => $value['url'],
  214. 'type' => ProductInfo::type_two,
  215. 'name' => $value['name'],
  216. 'crt_time' => $time,
  217. ];
  218. }
  219. ProductInfo::insert($insert);
  220. }
  221. if(! empty($data['depart'])){
  222. $insert = [];
  223. foreach ($data['depart'] as $value){
  224. $insert[] = [
  225. 'product_id' => $model->id,
  226. 'file' => $value,
  227. 'type' => ProductRange::type_one,
  228. 'crt_time' => $time,
  229. ];
  230. }
  231. ProductRange::insert($insert);
  232. }
  233. if(! empty($data['employee'])){
  234. $insert = [];
  235. foreach ($data['employee'] as $value){
  236. $insert[] = [
  237. 'product_id' => $model->id,
  238. 'file' => $value,
  239. 'type' => ProductRange::type_two,
  240. 'crt_time' => $time,
  241. ];
  242. }
  243. ProductRange::insert($insert);
  244. }
  245. DB::commit();
  246. }catch (\Exception $exception){
  247. DB::rollBack();
  248. return [false,$exception->getMessage()];
  249. }
  250. return [true,''];
  251. }
  252. public function productDel($data){
  253. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  254. try {
  255. DB::beginTransaction();
  256. $time = time();
  257. Product::where('id',$data['id'])->update(['del_time' => $time]);
  258. ProductInfo::where('del_time',0)
  259. ->where('product_id',$data['id'])
  260. ->update(['del_time' => $time]);
  261. ProductRange::where('del_time',0)
  262. ->where('product_id',$data['id'])
  263. ->update(['del_time' => $time]);
  264. DB::commit();
  265. }catch (\Exception $exception){
  266. DB::rollBack();
  267. return [false,$exception->getMessage()];
  268. }
  269. return [true,''];
  270. }
  271. public function productDetail($data,$user){
  272. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  273. $customer = Product::where('del_time',0)
  274. ->where('id',$data['id'])
  275. ->first();
  276. if(empty($customer)) return [false,'产品不存在或已被删除'];
  277. $customer = $customer->toArray();
  278. //库存
  279. $customer['product_inventory'] = [];
  280. $depart_id = array_column($user['rule_depart'],'depart_id');
  281. $inventory = $this->getProductInventory([$data['id']], $depart_id);
  282. if(! empty($inventory)) $customer['product_inventory'] = $inventory;
  283. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  284. $customer_info = ProductInfo::where('del_time',0)
  285. ->where('product_id',$customer['id'])
  286. ->select('id','product_id','file','type','name')
  287. ->get()->toArray();
  288. foreach ($customer_info as $value){
  289. $tmp = [
  290. 'url' => $value['file'],
  291. 'name' => $value['name'],
  292. ];
  293. if($value['type'] == ProductInfo::type_one){
  294. $customer['img'][] = $tmp;
  295. }elseif ($value['type'] == ProductInfo::type_two){
  296. $customer['file'][] = $tmp;
  297. }
  298. }
  299. $customer_range = ProductRange::where('del_time',0)
  300. ->where('product_id',$customer['id'])
  301. ->select('id','product_id','depart_id','type','employee_id')
  302. ->get()->toArray();
  303. foreach ($customer_range as $value){
  304. if($value['type'] == ProductRange::type_one){
  305. $customer['depart'][] = [
  306. $value['depart_id']
  307. ];
  308. }elseif ($value['type'] == ProductRange::type_two){
  309. $customer['employee'][] = [
  310. $value['employee_id']
  311. ];
  312. }
  313. }
  314. return [true, $customer];
  315. }
  316. public function productList($data,$user){
  317. $model = Product::where('del_time',0)
  318. ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','state','crt_id','crt_time','mark')
  319. ->orderby('id', 'desc');
  320. if(! $user['is_main']) {
  321. $user_id = $user['id'];
  322. $depart_id = array_column($user['rule_depart'],'depart_id');
  323. $product_id = ProductRange::where('del_time',0)
  324. ->where(function ($query) use($user_id, $depart_id) {
  325. $query->where('employee_id',$user_id)
  326. ->orWhereIn('depart_id', $depart_id);
  327. })->select('product_id')->get()
  328. ->toArray();
  329. $model->whereIn('id',array_column($product_id,'product_id'));
  330. }
  331. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  332. if(isset($data['state'])) $model->where('state', $data['state']);
  333. $list = $this->limit($model,'',$data);
  334. $list = $this->fillData($list);
  335. return [true, $list];
  336. }
  337. public function productRule($data, $is_add = true){
  338. if(empty($data['title'])) return [false,'产品名称不能为空'];
  339. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  340. if(empty($data['code'])) return [false,'产品编码不能为空'];
  341. if(! empty($data['retail_price'])){
  342. $res = $this->checkNumber($data['retail_price']);
  343. if(! $res) return [false,'零售价请输入不超过两位小数的数值'];
  344. }
  345. if($is_add){
  346. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  347. ->where('del_time',0)
  348. ->exists();
  349. }else{
  350. if(empty($data['id'])) return [false,'ID不能为空'];
  351. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  352. ->where('id','<>',$data['id'])
  353. ->where('del_time',0)
  354. ->exists();
  355. }
  356. if($bool) return [false,'产品名称或编码不能重复'];
  357. return [true, $data];
  358. }
  359. public function fillData($data){
  360. if(empty($data['data'])) return $data;
  361. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  362. ->pluck('emp_name','id')
  363. ->toArray();
  364. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  365. ->pluck('title','id')
  366. ->toArray();
  367. foreach ($data['data'] as $key => $value){
  368. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  369. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  370. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  371. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  372. }
  373. return $data;
  374. }
  375. public function getProductInventory($product_id = [],$depart_id = []){
  376. if(empty($product_id)) return [];
  377. $result = ProductInventory::whereIn('product_id',$product_id)
  378. ->when(! empty($depart_id), function ($query) use ($depart_id) {
  379. return $query->whereIn('depart_id', $depart_id);
  380. })
  381. ->select('id','product_id','number','depart_id','lock_number')
  382. ->get()
  383. ->toArray();
  384. return $result;
  385. }
  386. }