ProductService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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();
  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,
  123. 'type' => ProductInfo::type_one,
  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,
  135. 'type' => ProductInfo::type_two,
  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'] ?? '';
  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,
  199. 'type' => ProductInfo::type_one,
  200. 'crt_time' => $time,
  201. ];
  202. }
  203. ProductInfo::insert($insert);
  204. }
  205. if(! empty($data['file'])){
  206. $insert = [];
  207. foreach ($data['file'] as $value){
  208. $insert[] = [
  209. 'product_id' => $model->id,
  210. 'file' => $value,
  211. 'type' => ProductInfo::type_two,
  212. 'crt_time' => $time,
  213. ];
  214. }
  215. ProductInfo::insert($insert);
  216. }
  217. if(! empty($data['depart'])){
  218. $insert = [];
  219. foreach ($data['depart'] as $value){
  220. $insert[] = [
  221. 'product_id' => $model->id,
  222. 'file' => $value,
  223. 'type' => ProductRange::type_one,
  224. 'crt_time' => $time,
  225. ];
  226. }
  227. ProductRange::insert($insert);
  228. }
  229. if(! empty($data['employee'])){
  230. $insert = [];
  231. foreach ($data['employee'] as $value){
  232. $insert[] = [
  233. 'product_id' => $model->id,
  234. 'file' => $value,
  235. 'type' => ProductRange::type_two,
  236. 'crt_time' => $time,
  237. ];
  238. }
  239. ProductRange::insert($insert);
  240. }
  241. DB::commit();
  242. }catch (\Exception $exception){
  243. DB::rollBack();
  244. return [false,$exception->getMessage()];
  245. }
  246. return [true,''];
  247. }
  248. public function productDel($data){
  249. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  250. try {
  251. DB::beginTransaction();
  252. $time = time();
  253. Product::where('id',$data['id'])->update(['del_time' => $time]);
  254. ProductInfo::where('del_time',0)
  255. ->where('product_id',$data['id'])
  256. ->update(['del_time' => $time]);
  257. ProductRange::where('del_time',0)
  258. ->where('product_id',$data['id'])
  259. ->update(['del_time' => $time]);
  260. DB::commit();
  261. }catch (\Exception $exception){
  262. DB::rollBack();
  263. return [false,$exception->getMessage()];
  264. }
  265. return [true,''];
  266. }
  267. public function productDetail($data,$user){
  268. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  269. $customer = Product::where('del_time',0)
  270. ->where('id',$data['id'])
  271. ->first();
  272. if(empty($customer)) return [false,'产品不存在或已被删除'];
  273. $customer = $customer->toArray();
  274. //库存
  275. $customer['product_inventory'] = [];
  276. $depart_id = array_column($user['rule_depart'],'depart_id');
  277. $inventory = $this->getProductInventory([$data['id']], $depart_id);
  278. if(! empty($inventory)) $customer['product_inventory'] = $inventory;
  279. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  280. $customer_info = ProductInfo::where('del_time',0)
  281. ->where('product_id',$customer['id'])
  282. ->select('id','product_id','file','type')
  283. ->get()->toArray();
  284. foreach ($customer_info as $value){
  285. if($value['type'] == ProductInfo::type_one){
  286. $customer['img'][] = [
  287. $value['file']
  288. ];
  289. }elseif ($value['type'] == ProductInfo::type_two){
  290. $customer['file'][] = [
  291. $value['file']
  292. ];
  293. }
  294. }
  295. $customer_range = ProductRange::where('del_time',0)
  296. ->where('product_id',$customer['id'])
  297. ->select('id','product_id','depart_id','type','employee_id')
  298. ->get()->toArray();
  299. foreach ($customer_range as $value){
  300. if($value['type'] == ProductRange::type_one){
  301. $customer['depart'][] = [
  302. $value['depart_id']
  303. ];
  304. }elseif ($value['type'] == ProductRange::type_two){
  305. $customer['employee'][] = [
  306. $value['employee_id']
  307. ];
  308. }
  309. }
  310. return [true, $customer];
  311. }
  312. public function productList($data,$user){
  313. $model = Product::where('del_time',0)
  314. ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','state','crt_id','crt_time','mark')
  315. ->orderby('id', 'desc');
  316. if(! $user['is_main']) {
  317. $user_id = $user['id'];
  318. $depart_id = array_column($user['rule_depart'],'depart_id');
  319. $product_id = ProductRange::where('del_time',0)
  320. ->where(function ($query) use($user_id, $depart_id) {
  321. $query->where('employee_id',$user_id)
  322. ->orWhereIn('depart_id', $depart_id);
  323. })->select('product_id')->get()
  324. ->toArray();
  325. $model->whereIn('id',array_column($product_id,'product_id'));
  326. }
  327. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  328. if(isset($data['state'])) $model->where('state', $data['state']);
  329. $list = $this->limit($model,'',$data);
  330. $list = $this->fillData($list);
  331. return [true, $list];
  332. }
  333. public function productRule($data, $is_add = true){
  334. if(empty($data['title'])) return [false,'产品名称不能为空'];
  335. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  336. if(empty($data['code'])) return [false,'产品编码不能为空'];
  337. if(! empty($data['retail_price'])){
  338. if(! is_numeric($data['retail_price']) || $data['retail_price'] < 0) return [false,'零售价请填写正确'];
  339. $formattedNumber = number_format($data['retail_price'], 2, '.', '');
  340. if($formattedNumber != $data['retail_price']) return [false,'零售价请不要超过两位小数'];
  341. }
  342. if($is_add){
  343. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  344. ->where('del_time',0)
  345. ->exists();
  346. }else{
  347. if(empty($data['id'])) return [false,'ID不能为空'];
  348. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  349. ->where('id','<>',$data['id'])
  350. ->where('del_time',0)
  351. ->exists();
  352. }
  353. if($bool) return [false,'产品名称或编码不能重复'];
  354. return [true, $data];
  355. }
  356. public function fillData($data){
  357. if(empty($data['data'])) return $data;
  358. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  359. ->pluck('emp_name','id')
  360. ->toArray();
  361. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  362. ->pluck('title','id')
  363. ->toArray();
  364. foreach ($data['data'] as $key => $value){
  365. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  366. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  367. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  368. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  369. }
  370. return $data;
  371. }
  372. public function getProductInventory($product_id = [],$depart_id = []){
  373. if(empty($product_id)) return [];
  374. $result = ProductInventory::whereIn('product_id',$product_id)
  375. ->when(! empty($depart_id), function ($query) use ($depart_id) {
  376. return $query->whereIn('depart_id', $depart_id);
  377. })
  378. ->select('id','product_id','number','depart_id','lock_number')
  379. ->get()
  380. ->toArray();
  381. return $result;
  382. }
  383. }