ProductService.php 23 KB

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