ProductActivityService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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\ProductPriceDetail;
  11. use App\Model\ProductRange;
  12. use Illuminate\Support\Facades\DB;
  13. class ProductActivityService extends Service
  14. {
  15. public function productEdit($data,$user){
  16. list($status,$msg) = $this->productRule($data, $user, false);
  17. if(!$status) return [$status,$msg];
  18. try {
  19. DB::beginTransaction();
  20. $model = Product::where('id',$data['id'])->first();
  21. $model->product_category_id = $data['product_category_id'] ?? 0;
  22. $model->title = $data['title'];
  23. $model->code = $data['code'] ?? '';
  24. $model->size = $data['size'] ?? '';
  25. $model->unit = $data['unit'] ?? 0;
  26. $model->bar_code = $data['bar_code'] ?? '';
  27. $model->cost = $data['cost'] ?? 0;
  28. $model->retail_price = $data['retail_price'] ?? 0;
  29. $model->mark = $data['mark'] ?? '';
  30. $model->state = $data['state'] ?? 0;
  31. $model->save();
  32. $time = time();
  33. ProductIntroduction::where('product_id',$data['id'])
  34. ->where('del_time',0)
  35. ->update(['del_time' => $time]);
  36. if(! empty($data['introduction'])){
  37. $models = new ProductIntroduction();
  38. $models->product_id = $model->id;
  39. $models->introduction = $data['introduction'];
  40. $models->save();
  41. }
  42. ProductInfo::where('del_time',0)
  43. ->where('product_id',$data['id'])
  44. ->update(['del_time' => $time]);
  45. if(! empty($data['img'])){
  46. $insert = [];
  47. foreach ($data['img'] as $value){
  48. $insert[] = [
  49. 'product_id' => $model->id,
  50. 'file' => $value['url'],
  51. 'type' => ProductInfo::type_one,
  52. 'name' => $value['name'],
  53. 'crt_time' => $time,
  54. ];
  55. }
  56. ProductInfo::insert($insert);
  57. }
  58. if(! empty($data['file'])){
  59. $insert = [];
  60. foreach ($data['file'] as $value){
  61. $insert[] = [
  62. 'product_id' => $model->id,
  63. 'file' => $value['url'],
  64. 'type' => ProductInfo::type_two,
  65. 'name' => $value['name'],
  66. 'crt_time' => $time,
  67. ];
  68. }
  69. ProductInfo::insert($insert);
  70. }
  71. ProductRange::where('del_time',0)
  72. ->where('product_id',$data['id'])
  73. ->update(['del_time' => $time]);
  74. if(! empty($data['depart'])){
  75. $insert = [];
  76. foreach ($data['depart'] as $value){
  77. $insert[] = [
  78. 'product_id' => $model->id,
  79. 'depart_id' => $value,
  80. 'type' => ProductRange::type_one,
  81. 'crt_time' => $time,
  82. ];
  83. }
  84. ProductRange::insert($insert);
  85. }
  86. if(! empty($data['employee'])){
  87. $insert = [];
  88. foreach ($data['employee'] as $value){
  89. $insert[] = [
  90. 'product_id' => $model->id,
  91. 'employee_id' => $value,
  92. 'type' => ProductRange::type_two,
  93. 'crt_time' => $time,
  94. ];
  95. }
  96. ProductRange::insert($insert);
  97. }
  98. ProductPriceDetail::where('del_time',0)
  99. ->where('product_id',$data['id'])
  100. ->update(['del_time' => $time]);
  101. if(! empty($data['product_price'])){
  102. $insert = [];
  103. foreach ($data['product_price'] as $value){
  104. $insert[] = [
  105. 'product_id' => $model->id,
  106. 'basic_type_id' => $value['basic_type_id'],
  107. 'price' => $value['price'],
  108. 'crt_time' => $time,
  109. ];
  110. }
  111. ProductPriceDetail::insert($insert);
  112. }
  113. DB::commit();
  114. }catch (\Exception $exception){
  115. DB::rollBack();
  116. return [false,$exception->getMessage()];
  117. }
  118. return [true,''];
  119. }
  120. public function productAdd($data,$user){
  121. list($status,$msg) = $this->productRule($data, $user);
  122. if(!$status) return [$status,$msg];
  123. try {
  124. DB::beginTransaction();
  125. $model = new Product();
  126. $model->product_category_id = $data['product_category_id'] ?? 0;
  127. $model->title = $data['title'];
  128. $model->code = $data['code'] ?? '';
  129. $model->size = $data['size'] ?? '';
  130. $model->unit = $data['unit'] ?? 0;
  131. $model->bar_code = $data['bar_code'] ?? '';
  132. $model->cost = $data['cost'] ?? 0;
  133. $model->retail_price = $data['retail_price'] ?? 0;
  134. $model->mark = $data['mark'] ?? '';
  135. $model->state = $data['state'] ?? 0;
  136. $model->crt_id = $user['id'];
  137. $model->depart_id = $data['depart_id'] ?? 0;
  138. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  139. $model->save();
  140. $time = time();
  141. if(! empty($data['introduction'])){
  142. $models = new ProductIntroduction();
  143. $models->product_id = $model->id;
  144. $models->introduction = $data['introduction'];
  145. $models->save();
  146. }
  147. if(! empty($data['img'])){
  148. $insert = [];
  149. foreach ($data['img'] as $value){
  150. $insert[] = [
  151. 'product_id' => $model->id,
  152. 'file' => $value['url'],
  153. 'type' => ProductInfo::type_one,
  154. 'name' => $value['name'],
  155. 'crt_time' => $time,
  156. ];
  157. }
  158. ProductInfo::insert($insert);
  159. }
  160. if(! empty($data['file'])){
  161. $insert = [];
  162. foreach ($data['file'] as $value){
  163. $insert[] = [
  164. 'product_id' => $model->id,
  165. 'file' => $value['url'],
  166. 'type' => ProductInfo::type_two,
  167. 'name' => $value['name'],
  168. 'crt_time' => $time,
  169. ];
  170. }
  171. ProductInfo::insert($insert);
  172. }
  173. if(! empty($data['depart'])){
  174. $insert = [];
  175. foreach ($data['depart'] as $value){
  176. $insert[] = [
  177. 'product_id' => $model->id,
  178. 'depart_id' => $value,
  179. 'type' => ProductRange::type_one,
  180. 'crt_time' => $time,
  181. ];
  182. }
  183. ProductRange::insert($insert);
  184. }
  185. if(! empty($data['employee'])){
  186. $insert = [];
  187. foreach ($data['employee'] as $value){
  188. $insert[] = [
  189. 'product_id' => $model->id,
  190. 'employee_id' => $value,
  191. 'type' => ProductRange::type_two,
  192. 'crt_time' => $time,
  193. ];
  194. }
  195. ProductRange::insert($insert);
  196. }
  197. if(! empty($data['product_price'])){
  198. $insert = [];
  199. foreach ($data['product_price'] as $value){
  200. $insert[] = [
  201. 'product_id' => $model->id,
  202. 'basic_type_id' => $value['basic_type_id'],
  203. 'price' => $value['price'],
  204. 'crt_time' => $time,
  205. ];
  206. }
  207. ProductPriceDetail::insert($insert);
  208. }
  209. DB::commit();
  210. }catch (\Exception $exception){
  211. DB::rollBack();
  212. return [false,$exception->getMessage()];
  213. }
  214. return [true,''];
  215. }
  216. public function productDel($data){
  217. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  218. try {
  219. DB::beginTransaction();
  220. $time = time();
  221. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  222. ProductIntroduction::where('product_id',$data['id'])
  223. ->where('del_time',0)
  224. ->update(['del_time' => $time]);
  225. ProductInfo::where('del_time',0)
  226. ->where('product_id',$data['id'])
  227. ->update(['del_time' => $time]);
  228. ProductRange::where('del_time',0)
  229. ->where('product_id',$data['id'])
  230. ->update(['del_time' => $time]);
  231. ProductPriceDetail::where('del_time',0)
  232. ->where('product_id',$data['id'])
  233. ->update(['del_time' => $time]);
  234. DB::commit();
  235. }catch (\Exception $exception){
  236. DB::rollBack();
  237. return [false,$exception->getMessage()];
  238. }
  239. return [true,''];
  240. }
  241. public function productDetail($data,$user){
  242. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  243. $customer = Product::where('del_time',0)
  244. ->where('id',$data['id'])
  245. ->first();
  246. if(empty($customer)) return [false,'产品不存在或已被删除'];
  247. $customer = $customer->toArray();
  248. $category = ProductCategory::where('id',$customer['product_category_id'])
  249. ->value('title');
  250. $customer['product_category_title'] = $category;
  251. $customer['introduction'] = "";
  252. $in = ProductIntroduction::where('del_time',0)
  253. ->where('product_id',$data['id'])
  254. ->first();
  255. if(! empty($in)) $customer['introduction'] = $in->introduction;
  256. $detail = ProductPriceDetail::where('del_time',0)
  257. ->where('product_id',$data['id'])
  258. ->select('product_id','basic_type_id','price')
  259. ->get()->toArray();
  260. $title_map = BasicType::whereIn('id',array_column($detail,'basic_type_id'))
  261. ->pluck('title','id')
  262. ->toArray();
  263. //是否总公司
  264. $is_main = $user['is_all_depart'];
  265. $top_depart = $user['depart_top'][0] ?? [];
  266. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  267. $customer['product_price'] = [];
  268. //展示金额
  269. foreach ($detail as $value){
  270. if(! $is_main && ($top_depart['basic_type_id'] != $value['basic_type_id'])) {
  271. $is_show = 0;
  272. }else{
  273. $is_show = 1;
  274. }
  275. $customer['product_price'][] = [
  276. 'basic_type_id' => $value['basic_type_id'],
  277. 'basic_type_title' => $title_map[$value['basic_type_id']] ?? '',
  278. 'price' => $value['price'],
  279. 'is_show' => $is_show,
  280. ];
  281. }
  282. //单位
  283. $title = BasicType::where('id',$customer['unit'])->value('title');
  284. $customer['unit_name'] = $title;
  285. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  286. $customer_info = ProductInfo::where('del_time',0)
  287. ->where('product_id',$customer['id'])
  288. ->select('id','product_id','file','type','name')
  289. ->get()->toArray();
  290. foreach ($customer_info as $value){
  291. $tmp = [
  292. 'url' => $value['file'],
  293. 'name' => $value['name'],
  294. ];
  295. if($value['type'] == ProductInfo::type_one){
  296. $customer['img'][] = $tmp;
  297. }elseif ($value['type'] == ProductInfo::type_two){
  298. $customer['file'][] = $tmp;
  299. }
  300. }
  301. $customer_range = ProductRange::where('del_time',0)
  302. ->where('product_id',$customer['id'])
  303. ->select('id','product_id','depart_id','type','employee_id')
  304. ->get()->toArray();
  305. $emp_map = Employee::whereIn('id',array_unique(array_merge_recursive([$customer['crt_id']],array_column($customer_range,'employee_id'))))
  306. ->pluck('emp_name','id')
  307. ->toArray();
  308. $depart_map = Depart::whereIn('id',array_unique(array_column($customer_range,'depart_id')))
  309. ->pluck('title','id')
  310. ->toArray();
  311. foreach ($customer_range as $value){
  312. if($value['type'] == ProductRange::type_one){
  313. $tmp = [
  314. 'id' => $value['depart_id'],
  315. 'name' => $depart_map[$value['depart_id']],
  316. ];
  317. $customer['depart'][] = $tmp;
  318. }elseif ($value['type'] == ProductRange::type_two){
  319. $tmp = [
  320. 'id' => $value['employee_id'],
  321. 'name' => $emp_map[$value['employee_id']] ?? '',
  322. ];
  323. $customer['employee'][] = $tmp;
  324. }
  325. }
  326. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  327. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  328. return [true, $customer];
  329. }
  330. public function productList($data,$user){
  331. $model = new Product(['userData' => $user, 'search' => $data]);
  332. $model = $model->where('del_time',0)
  333. ->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')
  334. ->orderby('id', 'desc');
  335. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  336. if(isset($data['state'])) $model->where('state', $data['state']);
  337. $list = $this->limit($model,'',$data);
  338. $list = $this->fillData($list,$user);
  339. return [true, $list];
  340. }
  341. public function productRule(&$data, $user, $is_add = true){
  342. if(empty($data['title'])) return [false,'产品名称不能为空'];
  343. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  344. if(empty($data['code'])) return [false,'产品编码不能为空'];
  345. if(empty($data['cost'])) return [false,'成本不能为空'];
  346. $res = $this->checkNumber($data['cost']);
  347. if(! $res) return [false,'成本请输入不超过两位小数并且大于0的数值'];
  348. if(empty($data['retail_price'])) return [false,'零售价不能为空'];
  349. $res = $this->checkNumber($data['retail_price']);
  350. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于0的数值'];
  351. if(! empty($data['product_price'])){
  352. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  353. ->pluck('title','id')
  354. ->toArray();
  355. foreach ($data['product_price'] as $value){
  356. if(! empty($value['price'])) {
  357. $tmp = $map[$value['basic_type_id']] ?? '';
  358. $res = $this->checkNumber($value['price']);
  359. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  360. }
  361. }
  362. }
  363. //所属部门 以及 顶级部门
  364. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  365. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  366. $top_depart = Depart::where('del_time',0)->where('parent_id',0)->where('is_main',1)->value('id');
  367. if($is_add){
  368. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND top_depart_id = {$data['top_depart_id']}")
  369. ->where('del_time',0)
  370. ->exists();
  371. }else{
  372. if(empty($data['id'])) return [false,'ID不能为空'];
  373. $top_depart_id = Product::where('id',$data['id'])->value('top_depart_id');
  374. $bool = Product::whereRaw("(binary code = '{$data['code']}' OR title = '{$data['title']}') AND top_depart_id = {$top_depart_id}")
  375. ->where('id','<>',$data['id'])
  376. ->where('del_time',0)
  377. ->exists();
  378. }
  379. if($bool) return [false,'产品名称或编码不能重复'];
  380. return [true, $data];
  381. }
  382. public function fillData($data, $user){
  383. if(empty($data['data'])) return $data;
  384. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  385. ->pluck('emp_name','id')
  386. ->toArray();
  387. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  388. ->pluck('title','id')
  389. ->toArray();
  390. $detail = ProductPriceDetail::where('del_time',0)
  391. ->whereIn('product_id',array_unique(array_column($data['data'],'id')))
  392. ->select('product_id','basic_type_id','price')
  393. ->get()->toArray();
  394. $basic_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($data['data'],'unit'),array_column($detail,'basic_type_id'))))
  395. ->pluck('title','id')
  396. ->toArray();
  397. $detail_map = [];
  398. foreach ($detail as $value){
  399. $detail_map[$value['product_id']][] = $value;
  400. }
  401. //是否总公司
  402. $is_main = $user['is_all_depart'];
  403. $top_depart = $user['depart_top'][0] ?? [];
  404. foreach ($data['data'] as $key => $value){
  405. $tmp = [];
  406. if(isset($detail_map[$value['id']])){
  407. $d = $detail_map[$value['id']];
  408. foreach ($d as $v){
  409. if(! $is_main && ($top_depart['basic_type_id'] != $v['basic_type_id'])) {
  410. $is_show = 0;
  411. }else{
  412. $is_show = 1;
  413. }
  414. if($top_depart['basic_type_id'] != $v['basic_type_id']) {
  415. $is_use = 0;
  416. }else{
  417. $is_use = 1;
  418. }
  419. $tmp[] = [
  420. 'basic_type_id' => $v['basic_type_id'],
  421. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  422. 'price' => $v['price'],
  423. 'is_show' => $is_show,
  424. 'is_use' => $is_use
  425. ];
  426. }
  427. }
  428. $data['data'][$key]['product_price'] = $tmp;
  429. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  430. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  431. $data['data'][$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  432. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  433. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  434. }
  435. return $data;
  436. }
  437. }