ProductService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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\ProductIntroduction;
  10. use App\Model\ProductInventory;
  11. use App\Model\ProductPriceDetail;
  12. use App\Model\ProductRange;
  13. use Illuminate\Support\Facades\DB;
  14. /**
  15. * 产品管理
  16. */
  17. class ProductService extends Service
  18. {
  19. /**
  20. * 产品分类编辑
  21. * @param $data
  22. * @param $user
  23. * @return array
  24. */
  25. public function productCategoryEdit($data,$user){
  26. list($status,$msg) = $this->productCategoryRule($data,false);
  27. if(!$status) return [$status,$msg];
  28. $update = $msg['data'][0];
  29. $model = new ProductCategory();
  30. $model->where('id',$data['id'])->update($update);
  31. return [true,''];
  32. }
  33. /**
  34. * 产品分类新增
  35. * @param $data
  36. * @param $user
  37. * @return array
  38. */
  39. public function productCategoryAdd($data,$user){
  40. list($status,$msg) = $this->productCategoryRule($data);
  41. if(!$status) return [$status,$msg];
  42. ProductCategory::insert($msg['data']);
  43. return [true,''];
  44. }
  45. /**
  46. * 产品分类删除
  47. * @param $data
  48. * @return array
  49. */
  50. public function productCategoryDel($data){
  51. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  52. $bool = Product::where('del_time',0)
  53. ->where('product_category_id',$data['id'])
  54. ->exists();
  55. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  56. try {
  57. DB::beginTransaction();
  58. ProductCategory::where('id',$data['id'])->update([
  59. 'del_time' => time()
  60. ]);
  61. DB::commit();
  62. }catch (\Exception $exception){
  63. DB::rollBack();
  64. return [false,$exception->getMessage()];
  65. }
  66. return [true,''];
  67. }
  68. /**
  69. * 产品分类列表
  70. * @param $data
  71. * @param $user
  72. * @return array
  73. */
  74. public function productCategoryList($data,$user){
  75. $model = ProductCategory::where('del_time',0)
  76. ->select('title','id','parent_id')
  77. ->orderby('id','desc');
  78. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  79. $list = $this->limit($model,'',$data);
  80. return [true, $list];
  81. }
  82. /**
  83. * 产品分类参数规则
  84. * @param $data
  85. * @param $is_add
  86. * @return array
  87. */
  88. public function productCategoryRule($data, $is_add = true){
  89. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  90. $title = array_column($data['data'],'title');
  91. $title = array_map(function($val) {
  92. return $val !== null ? $val : 0;
  93. }, $title);
  94. $title_count = array_count_values($title);
  95. foreach ($title as $value){
  96. if(empty($value)) return [false,'名称不能为空!'];
  97. if($title_count[$value] > 1) return [false,'名称不能重复'];
  98. }
  99. foreach ($data['data'] as $key => $value){
  100. $data['data'][$key]['upd_time'] = time();
  101. if($is_add){
  102. $parent_id = 0;
  103. if(! empty($value['parent_id'])) {
  104. $bool = Product::where('del_time',0)
  105. ->where('product_category_id',$value['parent_id'])
  106. ->exists();
  107. if($bool) {
  108. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  109. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  110. }
  111. $parent_id = $value['parent_id'];
  112. }
  113. $data['data'][$key]['parent_id'] = $parent_id;
  114. $data['data'][$key]['crt_time'] = time();
  115. $bool = ProductCategory::where('title',$value['title'])
  116. ->where('del_time',0)
  117. ->exists();
  118. }else{
  119. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  120. $bool = ProductCategory::where('title',$value['title'])
  121. ->where('id','<>',$data['id'])
  122. ->where('del_time',0)
  123. ->exists();
  124. }
  125. if($bool) return [false,'分类名称不能重复'];
  126. }
  127. return [true, $data];
  128. }
  129. /**
  130. * 产品编辑
  131. * @param $data
  132. * @param $user
  133. * @return array
  134. */
  135. public function productEdit($data,$user){
  136. list($status,$msg) = $this->productRule($data, $user, false);
  137. if(!$status) return [$status,$msg];
  138. try {
  139. DB::beginTransaction();
  140. $model = Product::where('id',$data['id'])->first();
  141. $model->product_category_id = $data['product_category_id'] ?? 0;
  142. $model->title = $data['title'];
  143. $model->code = $data['code'] ?? '';
  144. $model->size = $data['size'] ?? '';
  145. $model->unit = $data['unit'] ?? 0;
  146. $model->bar_code = $data['bar_code'] ?? '';
  147. $model->cost = $data['cost'] ?? 0;
  148. $model->retail_price = $data['retail_price'] ?? 0;
  149. $model->mark = $data['mark'] ?? '';
  150. $model->state = $data['state'] ?? 0;
  151. $model->save();
  152. $time = time();
  153. ProductIntroduction::where('product_id',$data['id'])
  154. ->where('del_time',0)
  155. ->update(['del_time' => $time]);
  156. if(! empty($data['introduction'])){
  157. $models = new ProductIntroduction();
  158. $models->product_id = $model->id;
  159. $models->introduction = $data['introduction'];
  160. $models->save();
  161. }
  162. ProductInfo::where('del_time',0)
  163. ->where('product_id',$data['id'])
  164. ->update(['del_time' => $time]);
  165. if(! empty($data['img'])){
  166. $insert = [];
  167. foreach ($data['img'] as $value){
  168. $insert[] = [
  169. 'product_id' => $model->id,
  170. 'file' => $value['url'],
  171. 'type' => ProductInfo::type_one,
  172. 'name' => $value['name'],
  173. 'crt_time' => $time,
  174. ];
  175. }
  176. ProductInfo::insert($insert);
  177. }
  178. if(! empty($data['file'])){
  179. $insert = [];
  180. foreach ($data['file'] as $value){
  181. $insert[] = [
  182. 'product_id' => $model->id,
  183. 'file' => $value['url'],
  184. 'type' => ProductInfo::type_two,
  185. 'name' => $value['name'],
  186. 'crt_time' => $time,
  187. ];
  188. }
  189. ProductInfo::insert($insert);
  190. }
  191. ProductRange::where('del_time',0)
  192. ->where('product_id',$data['id'])
  193. ->update(['del_time' => $time]);
  194. if(! empty($data['depart'])){
  195. $insert = [];
  196. foreach ($data['depart'] as $value){
  197. $insert[] = [
  198. 'product_id' => $model->id,
  199. 'depart_id' => $value,
  200. 'type' => ProductRange::type_one,
  201. 'crt_time' => $time,
  202. ];
  203. }
  204. ProductRange::insert($insert);
  205. }
  206. if(! empty($data['employee'])){
  207. $insert = [];
  208. foreach ($data['employee'] as $value){
  209. $insert[] = [
  210. 'product_id' => $model->id,
  211. 'employee_id' => $value,
  212. 'type' => ProductRange::type_two,
  213. 'crt_time' => $time,
  214. ];
  215. }
  216. ProductRange::insert($insert);
  217. }
  218. ProductPriceDetail::where('del_time',0)
  219. ->where('product_id',$data['id'])
  220. ->update(['del_time' => $time]);
  221. if(! empty($data['product_price'])){
  222. $insert = [];
  223. foreach ($data['product_price'] as $value){
  224. $insert[] = [
  225. 'product_id' => $model->id,
  226. 'basic_type_id' => $value['basic_type_id'],
  227. 'price' => $value['price'],
  228. 'crt_time' => $time,
  229. ];
  230. }
  231. ProductPriceDetail::insert($insert);
  232. }
  233. DB::commit();
  234. }catch (\Exception $exception){
  235. DB::rollBack();
  236. return [false,$exception->getMessage()];
  237. }
  238. return [true,''];
  239. }
  240. /**
  241. * 产品新增
  242. * @param $data
  243. * @param $user
  244. * @return array
  245. */
  246. public function productAdd($data,$user){
  247. list($status,$msg) = $this->productRule($data, $user);
  248. if(!$status) return [$status,$msg];
  249. try {
  250. DB::beginTransaction();
  251. $model = new Product();
  252. $model->product_category_id = $data['product_category_id'] ?? 0;
  253. $model->title = $data['title'];
  254. $model->code = $data['code'] ?? '';
  255. $model->size = $data['size'] ?? '';
  256. $model->unit = $data['unit'] ?? 0;
  257. $model->bar_code = $data['bar_code'] ?? '';
  258. $model->cost = $data['cost'] ?? 0;
  259. $model->retail_price = $data['retail_price'] ?? 0;
  260. $model->mark = $data['mark'] ?? '';
  261. $model->state = $data['state'] ?? 0;
  262. $model->crt_id = $user['id'];
  263. $model->depart_id = $data['depart_id'] ?? 0;
  264. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  265. $model->save();
  266. $time = time();
  267. if(! empty($data['introduction'])){
  268. $models = new ProductIntroduction();
  269. $models->product_id = $model->id;
  270. $models->introduction = $data['introduction'];
  271. $models->save();
  272. }
  273. if(! empty($data['img'])){
  274. $insert = [];
  275. foreach ($data['img'] as $value){
  276. $insert[] = [
  277. 'product_id' => $model->id,
  278. 'file' => $value['url'],
  279. 'type' => ProductInfo::type_one,
  280. 'name' => $value['name'],
  281. 'crt_time' => $time,
  282. ];
  283. }
  284. ProductInfo::insert($insert);
  285. }
  286. if(! empty($data['file'])){
  287. $insert = [];
  288. foreach ($data['file'] as $value){
  289. $insert[] = [
  290. 'product_id' => $model->id,
  291. 'file' => $value['url'],
  292. 'type' => ProductInfo::type_two,
  293. 'name' => $value['name'],
  294. 'crt_time' => $time,
  295. ];
  296. }
  297. ProductInfo::insert($insert);
  298. }
  299. if(! empty($data['depart'])){
  300. $insert = [];
  301. foreach ($data['depart'] as $value){
  302. $insert[] = [
  303. 'product_id' => $model->id,
  304. 'depart_id' => $value,
  305. 'type' => ProductRange::type_one,
  306. 'crt_time' => $time,
  307. ];
  308. }
  309. ProductRange::insert($insert);
  310. }
  311. if(! empty($data['employee'])){
  312. $insert = [];
  313. foreach ($data['employee'] as $value){
  314. $insert[] = [
  315. 'product_id' => $model->id,
  316. 'employee_id' => $value,
  317. 'type' => ProductRange::type_two,
  318. 'crt_time' => $time,
  319. ];
  320. }
  321. ProductRange::insert($insert);
  322. }
  323. if(! empty($data['product_price'])){
  324. $insert = [];
  325. foreach ($data['product_price'] as $value){
  326. $insert[] = [
  327. 'product_id' => $model->id,
  328. 'basic_type_id' => $value['basic_type_id'],
  329. 'price' => $value['price'],
  330. 'crt_time' => $time,
  331. ];
  332. }
  333. ProductPriceDetail::insert($insert);
  334. }
  335. DB::commit();
  336. }catch (\Exception $exception){
  337. DB::rollBack();
  338. return [false,$exception->getMessage()];
  339. }
  340. return [true,''];
  341. }
  342. /**
  343. * 产品删除
  344. * @param $data
  345. * @return array
  346. */
  347. public function productDel($data){
  348. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  349. try {
  350. DB::beginTransaction();
  351. $time = time();
  352. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  353. ProductIntroduction::where('product_id',$data['id'])
  354. ->where('del_time',0)
  355. ->update(['del_time' => $time]);
  356. ProductInfo::where('del_time',0)
  357. ->where('product_id',$data['id'])
  358. ->update(['del_time' => $time]);
  359. ProductRange::where('del_time',0)
  360. ->where('product_id',$data['id'])
  361. ->update(['del_time' => $time]);
  362. ProductPriceDetail::where('del_time',0)
  363. ->where('product_id',$data['id'])
  364. ->update(['del_time' => $time]);
  365. DB::commit();
  366. }catch (\Exception $exception){
  367. DB::rollBack();
  368. return [false,$exception->getMessage()];
  369. }
  370. return [true,''];
  371. }
  372. /**
  373. * 产品详情
  374. * @param $data
  375. * @param $user
  376. * @return array
  377. */
  378. public function productDetail($data,$user){
  379. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  380. $customer = Product::where('del_time',0)
  381. ->where('id',$data['id'])
  382. ->first();
  383. if(empty($customer)) return [false,'产品不存在或已被删除'];
  384. $customer = $customer->toArray();
  385. $category = ProductCategory::where('id',$customer['product_category_id'])
  386. ->value('title');
  387. $customer['product_category_title'] = $category;
  388. $customer['introduction'] = "";
  389. $in = ProductIntroduction::where('del_time',0)
  390. ->where('product_id',$data['id'])
  391. ->first();
  392. if(! empty($in)) $customer['introduction'] = $in->introduction;
  393. $detail = ProductPriceDetail::where('del_time',0)
  394. ->where('product_id',$data['id'])
  395. ->select('product_id','basic_type_id','price')
  396. ->get()->toArray();
  397. $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
  398. ->pluck('title','id')
  399. ->toArray();
  400. //是否总公司
  401. $is_main = EmployeeService::isMain($user['id']);
  402. $customer['product_price'] = [];
  403. //展示金额
  404. foreach ($detail as $value){
  405. if(! $is_main){
  406. $top_depart = $user['depart_top'][0] ?? [];
  407. if($top_depart['basic_type_id'] != $value['basic_type_id']) continue;
  408. }
  409. $customer['product_price'][] = [
  410. 'basic_type_id' => $value['basic_type_id'],
  411. 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
  412. 'price' => $value['price'],
  413. ];
  414. }
  415. //单位
  416. $title = BasicType::where('id',$customer['unit'])->value('title');
  417. $customer['unit_name'] = $title;
  418. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  419. $customer_info = ProductInfo::where('del_time',0)
  420. ->where('product_id',$customer['id'])
  421. ->select('id','product_id','file','type','name')
  422. ->get()->toArray();
  423. foreach ($customer_info as $value){
  424. $tmp = [
  425. 'url' => $value['file'],
  426. 'name' => $value['name'],
  427. ];
  428. if($value['type'] == ProductInfo::type_one){
  429. $customer['img'][] = $tmp;
  430. }elseif ($value['type'] == ProductInfo::type_two){
  431. $customer['file'][] = $tmp;
  432. }
  433. }
  434. $customer_range = ProductRange::where('del_time',0)
  435. ->where('product_id',$customer['id'])
  436. ->select('id','product_id','depart_id','type','employee_id')
  437. ->get()->toArray();
  438. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_range,'employee_id'))))
  439. ->pluck('emp_name','id')
  440. ->toArray();
  441. $depart_map = Depart::whereIn('id',array_unique(array_column($customer_range,'depart_id')))
  442. ->pluck('title','id')
  443. ->toArray();
  444. foreach ($customer_range as $value){
  445. if($value['type'] == ProductRange::type_one){
  446. $tmp = [
  447. 'id' => $value['depart_id'],
  448. 'name' => $depart_map[$value['depart_id']],
  449. ];
  450. $customer['depart'][] = $tmp;
  451. }elseif ($value['type'] == ProductRange::type_two){
  452. $tmp = [
  453. 'id' => $value['employee_id'],
  454. 'name' => $emp_map[$value['employee_id']] ?? '',
  455. ];
  456. $customer['employee'][] = $tmp;
  457. }
  458. }
  459. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  460. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  461. return [true, $customer];
  462. }
  463. /**
  464. * 产品列表
  465. * @param $data
  466. * @param $user
  467. * @return array
  468. */
  469. public function productList($data,$user){
  470. $model = new Product(['userData' => $user, 'search' => $data]);
  471. $model = $model->where('del_time',0)
  472. ->select('title','id','product_category_id','code','size','unit','bar_code','retail_price','cost','state','crt_id','crt_time','mark','depart_id','top_depart_id')
  473. ->orderby('id', 'desc');
  474. //getALL传入后无视设置范围
  475. if($user['id'] != Employee::SPECIAL_ADMIN ) {
  476. $user_id = $user['id'];
  477. $depart_id = $user['depart_range'];
  478. $product_id = ProductRange::where('del_time',0)
  479. ->where(function ($query) use($user_id, $depart_id) {
  480. $query->where('employee_id',$user_id)
  481. ->orWhereIn('depart_id', $depart_id);
  482. })->select('product_id')->get()
  483. ->toArray();
  484. $product_id = array_unique(array_column($product_id,'product_id'));
  485. //可见范围
  486. $model->when(! empty($sales_order_id), function ($query) use ($product_id) {
  487. return $query->orWhereIn('id', $product_id);
  488. });
  489. }
  490. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  491. if(isset($data['state'])) $model->where('state', $data['state']);
  492. $list = $this->limit($model,'',$data);
  493. $list = $this->fillData($list,$user);
  494. return [true, $list];
  495. }
  496. /**
  497. * 产品参数规则
  498. * @param $data
  499. * @param $is_add
  500. * @return array
  501. */
  502. public function productRule(&$data, $user, $is_add = true){
  503. if(empty($data['title'])) return [false,'产品名称不能为空'];
  504. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  505. if(empty($data['code'])) return [false,'产品编码不能为空'];
  506. if(empty($data['cost'])) return [false,'成本不能为空'];
  507. $res = $this->checkNumber($data['cost']);
  508. if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值'];
  509. if(empty($data['retail_price'])) return [false,'零售价不能为空'];
  510. $res = $this->checkNumber($data['retail_price']);
  511. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
  512. //所属部门 以及 顶级部门
  513. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  514. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  515. if($is_add){
  516. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  517. ->where('del_time',0)
  518. ->exists();
  519. }else{
  520. if(empty($data['id'])) return [false,'ID不能为空'];
  521. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}')")
  522. ->where('id','<>',$data['id'])
  523. ->where('del_time',0)
  524. ->exists();
  525. }
  526. if($bool) return [false,'产品名称或编码不能重复'];
  527. return [true, $data];
  528. }
  529. /**
  530. * 拼接数据
  531. * @param $data
  532. * @return array
  533. */
  534. public function fillData($data, $user){
  535. if(empty($data['data'])) return $data;
  536. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  537. ->pluck('emp_name','id')
  538. ->toArray();
  539. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  540. ->pluck('title','id')
  541. ->toArray();
  542. $detail = ProductPriceDetail::where('del_time',0)
  543. ->where('product_id',array_unique(array_column($data['data'],'id')))
  544. ->select('product_id','basic_type_id','price')
  545. ->get()->toArray();
  546. $basic_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'unit'),array_column($detail,'basic_type_id'))))
  547. ->pluck('title','id')
  548. ->toArray();
  549. $detail_map = [];
  550. foreach ($detail as $value){
  551. $detail_map[$value['product_id']] = $value;
  552. }
  553. //是否总公司
  554. $is_main = EmployeeService::isMain($user['id']);
  555. $top_depart = $user['depart_top'][0] ?? [];
  556. foreach ($data['data'] as $key => $value){
  557. $tmp = [];
  558. if(isset($detail_map[$value['id']])){
  559. $d = $detail_map[$value['id']];
  560. foreach ($d as $v){
  561. //不是总公司 不是公司设置的金额 就不展示
  562. if(! $is_main && ($top_depart['basic_type_id'] != $v['basic_type_id'])) continue;
  563. $tmp[] = [
  564. 'basic_type_id' => $value['basic_type_id'],
  565. 'basic_type_title' => $basic_map[$value['basic_type_id']] ?? '',
  566. 'price' => $value['price'],
  567. ];
  568. }
  569. }
  570. $data['data'][$key]['product_price'] = $tmp;
  571. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  572. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  573. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  574. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  575. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  576. }
  577. return $data;
  578. }
  579. }