ProductService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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\ProductRange;
  8. use Illuminate\Support\Facades\DB;
  9. class ProductService extends Service
  10. {
  11. public function productCategoryEdit($data,$user){
  12. list($status,$msg) = $this->productCategoryRule($data,false);
  13. if(!$status) return [$status,$msg];
  14. $update = $msg['data'][0];
  15. $model = new ProductCategory();
  16. $model->where('id',$data['id'])->update($update);
  17. return [true,''];
  18. }
  19. public function productCategoryAdd($data,$user){
  20. list($status,$msg) = $this->productCategoryRule($data);
  21. if(!$status) return [$status,$msg];
  22. ProductCategory::insert($msg['data']);
  23. return [true,''];
  24. }
  25. public function productCategoryDel($data){
  26. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  27. $bool = Product::where('del_time',0)
  28. ->where('product_category_id',$data['id'])
  29. ->exists();
  30. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  31. try {
  32. DB::beginTransaction();
  33. ProductCategory::where('id',$data['id'])->update([
  34. 'del_time' => time()
  35. ]);
  36. DB::commit();
  37. }catch (\Exception $exception){
  38. DB::rollBack();
  39. return [false,$exception->getMessage()];
  40. }
  41. return [true,''];
  42. }
  43. public function productCategoryList($data,$user){
  44. $model = ProductCategory::where('del_time',0)
  45. ->select('title','id','parent_id')
  46. ->orderby('id','desc');
  47. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  48. $list = $model->get()->toArray();
  49. if(! empty($list)) {
  50. $list = $this->makeTree(0,$list);
  51. $list = $this->set_sort_circle($list);
  52. }
  53. return [true, $list];
  54. }
  55. public function productCategoryRule($data, $is_add = true){
  56. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  57. $title = array_column($data['data'],'title');
  58. $title = array_map(function($val) {
  59. return $val !== null ? $val : 0;
  60. }, $title);
  61. $title_count = array_count_values($title);
  62. foreach ($title as $value){
  63. if(empty($value)) return [false,'名称不能为空!'];
  64. if($title_count[$value] > 1) return [false,'名称不能重复'];
  65. }
  66. foreach ($data['data'] as $key => $value){
  67. $data['data'][$key]['upd_time'] = time();
  68. if($is_add){
  69. $parent_id = 0;
  70. if(! empty($value['parent_id'])) {
  71. $bool = Product::where('del_time',0)
  72. ->where('product_category_id',$value['parent_id'])
  73. ->exists();
  74. if($bool) {
  75. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value();
  76. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  77. }
  78. $parent_id = $value['parent_id'];
  79. }
  80. $data['data'][$key]['parent_id'] = $parent_id;
  81. $data['data'][$key]['crt_time'] = time();
  82. $bool = ProductCategory::where('title',$value['title'])
  83. ->where('del_time',0)
  84. ->exists();
  85. }else{
  86. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  87. $bool = ProductCategory::where('title',$value['title'])
  88. ->where('id','<>',$data['id'])
  89. ->where('del_time',0)
  90. ->exists();
  91. }
  92. if($bool) return [false,'分类名称不能重复'];
  93. }
  94. return [true, $data];
  95. }
  96. public function productEdit($data,$user){
  97. list($status,$msg) = $this->productRule($data,false);
  98. if(!$status) return [$status,$msg];
  99. try {
  100. DB::beginTransaction();
  101. $model = Product::where('id',$data['id'])->first();
  102. $model->product_category_id = $data['product_category_id'] ?? 0;
  103. $model->title = $data['title'];
  104. $model->code = $data['code'] ?? '';
  105. $model->size = $data['size'] ?? '';
  106. $model->unit = $data['unit'] ?? '';
  107. $model->bar_code = $data['bar_code'] ?? '';
  108. $model->retail_price = $data['retail_price'] ?? 0;
  109. $model->mark = $data['mark'] ?? '';
  110. $model->state = $data['state'] ?? 0;
  111. $model->save();
  112. $time = time();
  113. ProductInfo::where('del_time',0)
  114. ->where('product_id',$data['id'])
  115. ->update(['del_time' => $time]);
  116. if(! empty($data['img'])){
  117. $insert = [];
  118. foreach ($data['img'] as $value){
  119. $insert[] = [
  120. 'product_id' => $model->id,
  121. 'file' => $value,
  122. 'type' => ProductInfo::type_one,
  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,
  134. 'type' => ProductInfo::type_two,
  135. 'crt_time' => $time,
  136. ];
  137. }
  138. ProductInfo::insert($insert);
  139. }
  140. ProductRange::where('del_time',0)
  141. ->where('product_id',$data['id'])
  142. ->update(['del_time' => $time]);
  143. if(! empty($data['depart'])){
  144. $insert = [];
  145. foreach ($data['depart'] as $value){
  146. $insert[] = [
  147. 'product_id' => $model->id,
  148. 'file' => $value,
  149. 'type' => ProductRange::type_one,
  150. 'crt_time' => $time,
  151. ];
  152. }
  153. ProductRange::insert($insert);
  154. }
  155. if(! empty($data['employee'])){
  156. $insert = [];
  157. foreach ($data['employee'] as $value){
  158. $insert[] = [
  159. 'product_id' => $model->id,
  160. 'file' => $value,
  161. 'type' => ProductRange::type_two,
  162. 'crt_time' => $time,
  163. ];
  164. }
  165. ProductRange::insert($insert);
  166. }
  167. DB::commit();
  168. }catch (\Exception $exception){
  169. DB::rollBack();
  170. return [false,$exception->getMessage()];
  171. }
  172. return [true,''];
  173. }
  174. public function productAdd($data,$user){
  175. list($status,$msg) = $this->productRule($data);
  176. if(!$status) return [$status,$msg];
  177. try {
  178. DB::beginTransaction();
  179. $model = new Product();
  180. $model->product_category_id = $data['product_category_id'] ?? 0;
  181. $model->title = $data['title'];
  182. $model->code = $data['code'] ?? '';
  183. $model->size = $data['size'] ?? '';
  184. $model->unit = $data['unit'] ?? '';
  185. $model->bar_code = $data['bar_code'] ?? '';
  186. $model->retail_price = $data['retail_price'] ?? 0;
  187. $model->mark = $data['mark'] ?? '';
  188. $model->state = $data['state'] ?? 0;
  189. $model->crt_id = $user['id'];
  190. $model->save();
  191. $time = time();
  192. if(! empty($data['img'])){
  193. $insert = [];
  194. foreach ($data['img'] as $value){
  195. $insert[] = [
  196. 'product_id' => $model->id,
  197. 'file' => $value,
  198. 'type' => ProductInfo::type_one,
  199. 'crt_time' => $time,
  200. ];
  201. }
  202. ProductInfo::insert($insert);
  203. }
  204. if(! empty($data['file'])){
  205. $insert = [];
  206. foreach ($data['file'] as $value){
  207. $insert[] = [
  208. 'product_id' => $model->id,
  209. 'file' => $value,
  210. 'type' => ProductInfo::type_two,
  211. 'crt_time' => $time,
  212. ];
  213. }
  214. ProductInfo::insert($insert);
  215. }
  216. if(! empty($data['depart'])){
  217. $insert = [];
  218. foreach ($data['depart'] as $value){
  219. $insert[] = [
  220. 'product_id' => $model->id,
  221. 'file' => $value,
  222. 'type' => ProductRange::type_one,
  223. 'crt_time' => $time,
  224. ];
  225. }
  226. ProductRange::insert($insert);
  227. }
  228. if(! empty($data['employee'])){
  229. $insert = [];
  230. foreach ($data['employee'] as $value){
  231. $insert[] = [
  232. 'product_id' => $model->id,
  233. 'file' => $value,
  234. 'type' => ProductRange::type_two,
  235. 'crt_time' => $time,
  236. ];
  237. }
  238. ProductRange::insert($insert);
  239. }
  240. DB::commit();
  241. }catch (\Exception $exception){
  242. DB::rollBack();
  243. return [false,$exception->getMessage()];
  244. }
  245. return [true,''];
  246. }
  247. public function productDel($data){
  248. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  249. try {
  250. DB::beginTransaction();
  251. $time = time();
  252. Product::where('id',$data['id'])->update(['del_time' => $time]);
  253. ProductInfo::where('del_time',0)
  254. ->where('product_id',$data['id'])
  255. ->update(['del_time' => $time]);
  256. ProductRange::where('del_time',0)
  257. ->where('product_id',$data['id'])
  258. ->update(['del_time' => $time]);
  259. DB::commit();
  260. }catch (\Exception $exception){
  261. DB::rollBack();
  262. return [false,$exception->getMessage()];
  263. }
  264. return [true,''];
  265. }
  266. public function productDetail($data){
  267. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  268. $customer = Product::where('del_time',0)
  269. ->where('id',$data['id'])
  270. ->first();
  271. if(empty($customer)) return [false,'产品不存在或已被删除'];
  272. $customer = $customer->toArray();
  273. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  274. $customer_info = ProductInfo::where('del_time',0)
  275. ->where('product_id',$customer['id'])
  276. ->select('id','product_id','file','type')
  277. ->get()->toArray();
  278. foreach ($customer_info as $value){
  279. if($value['type'] == ProductInfo::type_one){
  280. $customer['img'][] = [
  281. $value['file']
  282. ];
  283. }elseif ($value['type'] == ProductInfo::type_two){
  284. $customer['file'][] = [
  285. $value['file']
  286. ];
  287. }
  288. }
  289. $customer_range = ProductRange::where('del_time',0)
  290. ->where('product_id',$customer['id'])
  291. ->select('id','product_id','depart_id','type','employee_id')
  292. ->get()->toArray();
  293. foreach ($customer_range as $value){
  294. if($value['type'] == ProductRange::type_one){
  295. $customer['depart'][] = [
  296. $value['depart_id']
  297. ];
  298. }elseif ($value['type'] == ProductRange::type_two){
  299. $customer['employee'][] = [
  300. $value['employee_id']
  301. ];
  302. }
  303. }
  304. return [true, $customer];
  305. }
  306. public function productList($data,$user){
  307. $model = Product::where('del_time',0)
  308. ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','state','crt_id','crt_time','mark')
  309. ->orderby('id', 'desc');
  310. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  311. if(isset($data['state'])) $model->where('state', $data['state']);
  312. $list = $this->limit($model,'',$data);
  313. $list = $this->fillData($list);
  314. return [true, $list];
  315. }
  316. public function productRule($data, $is_add = true){
  317. if(empty($data['title'])) return [false,'产品名称不能为空'];
  318. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  319. if(empty($data['code'])) return [false,'产品编码不能为空'];
  320. if(! empty($data['retail_price'])){
  321. if(! is_numeric($data['retail_price']) || $data['retail_price'] < 0) return [false,'零售价请填写正确'];
  322. $formattedNumber = number_format($data['retail_price'], 2, '.', '');
  323. if($formattedNumber != $data['retail_price']) return [false,'零售价请不要超过两位小数'];
  324. }
  325. if($is_add){
  326. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  327. ->where('del_time',0)
  328. ->exists();
  329. }else{
  330. if(empty($data['id'])) return [false,'ID不能为空'];
  331. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  332. ->where('id','<>',$data['id'])
  333. ->where('del_time',0)
  334. ->exists();
  335. }
  336. if($bool) return [false,'产品名称或编码不能重复'];
  337. return [true, $data];
  338. }
  339. public function fillData($data){
  340. if(empty($data['data'])) return $data;
  341. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  342. ->pluck('emp_name','id')
  343. ->toArray();
  344. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  345. ->pluck('title','id')
  346. ->toArray();
  347. foreach ($data['data'] as $key => $value){
  348. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  349. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  350. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  351. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  352. }
  353. return $data;
  354. }
  355. }