ProductService.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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\ProductActivity;
  8. use App\Model\ProductActivityPrice;
  9. use App\Model\ProductCategory;
  10. use App\Model\ProductInfo;
  11. use App\Model\ProductIntroduction;
  12. use App\Model\ProductPriceDetail;
  13. use App\Model\Role;
  14. use App\Model\RoleMenuButton;
  15. use App\Model\SeeRange;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 产品管理
  19. */
  20. class ProductService extends Service
  21. {
  22. public function setIsEditUnitPrice($data,$user){
  23. if($this->isEmpty($data,'id')) return [false,'请选择分类!'];
  24. if(! isset($data['is_edit_unit_price'])) return [false,'请选择受否允许修改采购单价!'];
  25. $is_edit_unit_price = $data['is_edit_unit_price'] > 0 ? 1 : 0;
  26. $all_id = $this->getProductCateGory($data['id']);
  27. ProductCategory::whereIn('id',$all_id)->update([
  28. 'is_edit_unit_price' => $is_edit_unit_price
  29. ]);
  30. return [true, ''];
  31. }
  32. /**
  33. * 产品分类编辑
  34. * @param $data
  35. * @param $user
  36. * @return array
  37. */
  38. public function productCategoryEdit($data,$user){
  39. list($status,$msg) = $this->productCategoryRule($data,$user,false);
  40. if(!$status) return [$status,$msg];
  41. $update = $msg['data'][0];
  42. $model = new ProductCategory();
  43. $model->where('id',$data['id'])->update($update);
  44. return [true,''];
  45. }
  46. /**
  47. * 产品分类新增
  48. * @param $data
  49. * @param $user
  50. * @return array
  51. */
  52. public function productCategoryAdd($data,$user){
  53. list($status,$msg) = $this->productCategoryRule($data,$user);
  54. if(!$status) return [$status,$msg];
  55. ProductCategory::insert($msg['data']);
  56. return [true,''];
  57. }
  58. /**
  59. * 产品分类删除
  60. * @param $data
  61. * @return array
  62. */
  63. public function productCategoryDel($data){
  64. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  65. $bool = Product::where('del_time',0)
  66. ->where('product_category_id',$data['id'])
  67. ->exists();
  68. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  69. try {
  70. DB::beginTransaction();
  71. ProductCategory::where('id',$data['id'])->update([
  72. 'del_time' => time()
  73. ]);
  74. DB::commit();
  75. }catch (\Exception $exception){
  76. DB::rollBack();
  77. return [false,$exception->getMessage()];
  78. }
  79. return [true,''];
  80. }
  81. /**
  82. * 产品分类列表
  83. * @param $data
  84. * @param $user
  85. * @return array
  86. */
  87. public function productCategoryList($data,$user){
  88. $model = ProductCategory::TopClear($user,$data);
  89. $model = $model->where('del_time',0)
  90. ->select('title','id','parent_id','is_edit_unit_price')
  91. ->orderby('id','asc');
  92. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  93. if(isset($data['is_edit_unit_price'])) $model->where('is_edit_unit_price', $data['is_edit_unit_price']);
  94. $list = $model->get()->toArray();
  95. foreach ($list as $key => $value){
  96. $list[$key]['is_edit_unit_price_title'] = ProductCategory::$is_edit_unit_price[$value['is_edit_unit_price']] ?? "";
  97. }
  98. $list_tree = $list;
  99. if(! empty($list_tree)) {
  100. $list_tree = $this->makeTree(0,$list_tree);
  101. $list_tree = $this->set_sort_circle($list_tree);
  102. }
  103. return [200, ['data' => $list,'tree' => $list_tree]];
  104. }
  105. public function productCategoryList2($data,$user){
  106. $head_top_depart_id = $user['head']['id'] ?? 0;
  107. $now_top_depart_id = $this->getDepart($user);
  108. //总社分类
  109. $head = ProductCategory::where('del_time',0)
  110. ->where('top_depart_id', $head_top_depart_id)
  111. ->select('title','id','parent_id')
  112. ->orderby('id','asc')
  113. ->get()->toArray();
  114. $head_tree = [];
  115. if(! empty($head)) {
  116. $head_tree = $this->makeTree(0,$head);
  117. $head_tree = $this->set_sort_circle($head_tree);
  118. }
  119. //我的
  120. $my_tree = [];
  121. if($head_top_depart_id != $now_top_depart_id){
  122. $model = ProductCategory::TopClear($user,$data);
  123. $model = $model->where('del_time',0)
  124. ->select('title','id','parent_id')
  125. ->orderby('id','asc');
  126. $list = $model->get()->toArray();
  127. if(! empty($list)) {
  128. $my_tree = $this->makeTree(0,$list);
  129. $my_tree = $this->set_sort_circle($my_tree);
  130. }
  131. }
  132. return [200, ['head_tree' => $head_tree, 'my_tree' => $my_tree]];
  133. }
  134. /**
  135. * 产品分类参数规则
  136. * @param $data
  137. * @param $is_add
  138. * @return array
  139. */
  140. public function productCategoryRule($data,$user, $is_add = true){
  141. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  142. //所属部门 以及 顶级部门
  143. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  144. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  145. $title = array_column($data['data'],'title');
  146. $title = array_map(function($val) {
  147. return $val !== null ? $val : 0;
  148. }, $title);
  149. $title_count = array_count_values($title);
  150. foreach ($title as $value){
  151. if(empty($value)) return [false,'名称不能为空!'];
  152. if($title_count[$value] > 1) return [false,'名称不能重复'];
  153. }
  154. foreach ($data['data'] as $key => $value){
  155. $data['data'][$key]['upd_time'] = time();
  156. if($is_add){
  157. $parent_id = 0;
  158. if(! empty($value['parent_id'])) {
  159. $bool = Product::where('del_time',0)
  160. ->where('product_category_id',$value['parent_id'])
  161. ->exists();
  162. if($bool) {
  163. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  164. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  165. }
  166. $parent_id = $value['parent_id'];
  167. }
  168. $data['data'][$key]['parent_id'] = $parent_id;
  169. $data['data'][$key]['crt_time'] = time();
  170. $data['data'][$key]['depart_id'] = $data['depart_id'];
  171. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  172. $bool = ProductCategory::where('title',$value['title'])
  173. ->where('top_depart_id',$data['top_depart_id'])
  174. ->where('del_time',0)
  175. ->exists();
  176. }else{
  177. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  178. $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id');
  179. $bool = ProductCategory::where('title',$value['title'])
  180. ->where('top_depart_id',$top_depart_id)
  181. ->where('id','<>',$data['id'])
  182. ->where('del_time',0)
  183. ->exists();
  184. }
  185. if($bool) return [false,'分类名称不能重复'];
  186. }
  187. return [true, $data];
  188. }
  189. /**
  190. * 产品编辑
  191. * @param $data
  192. * @param $user
  193. * @return array
  194. */
  195. public function productEdit($data,$user){
  196. list($status,$msg) = $this->productRule($data, $user, false);
  197. if(!$status) return [$status,$msg];
  198. try {
  199. DB::beginTransaction();
  200. $model = Product::where('id',$data['id'])->first();
  201. $model->product_category_id = $data['product_category_id'] ?? 0;
  202. $model->product_category = $data['product_category'] ?? '';
  203. $model->title = $data['title'];
  204. $model->code = $data['code'] ?? '';
  205. $model->warranty_time = $data['warranty_time'] ?? 0;
  206. $model->install_time = $data['install_time'] ?? 0;
  207. $model->size = $data['size'] ?? '';
  208. $model->unit = $data['unit'] ?? 0;
  209. $model->bar_code = $data['bar_code'] ?? '';
  210. $model->cost = $data['cost'] ?? 0;
  211. $model->retail_price = $data['retail_price'] ?? 0;
  212. $model->mark = $data['mark'] ?? '';
  213. $model->state = $data['state'] ?? 0;
  214. $model->product_attribute = $data['product_attribute'] ?? 0;
  215. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  216. $model->save();
  217. $time = time();
  218. ProductIntroduction::where('product_id',$data['id'])
  219. ->where('del_time',0)
  220. ->update(['del_time' => $time]);
  221. if(! empty($data['introduction'])){
  222. $models = new ProductIntroduction();
  223. $models->product_id = $model->id;
  224. $models->introduction = $data['introduction'];
  225. $models->save();
  226. }
  227. $old = ProductInfo::where('del_time',0)
  228. ->where('product_id',$data['id'])
  229. ->select('file')
  230. ->get()->toArray();
  231. $old = array_column($old,'file');
  232. ProductInfo::where('del_time',0)
  233. ->where('product_id',$data['id'])
  234. ->update(['del_time' => $time]);
  235. $new = [];
  236. if(! empty($data['img'])){
  237. $insert = [];
  238. foreach ($data['img'] as $value){
  239. $insert[] = [
  240. 'product_id' => $model->id,
  241. 'file' => $value['url'],
  242. 'type' => ProductInfo::type_one,
  243. 'name' => $value['name'],
  244. 'crt_time' => $time,
  245. ];
  246. if(in_array($value['url'], $old)) {
  247. foreach ($old as $o_k => $o_v){
  248. if($o_v == $value['url']) unset($old[$o_k]);
  249. }
  250. }else{
  251. $new[] = $value['url'];
  252. }
  253. }
  254. ProductInfo::insert($insert);
  255. }
  256. if(! empty($data['file'])){
  257. $insert = [];
  258. foreach ($data['file'] as $value){
  259. $insert[] = [
  260. 'product_id' => $model->id,
  261. 'file' => $value['url'],
  262. 'type' => ProductInfo::type_two,
  263. 'name' => $value['name'],
  264. 'crt_time' => $time,
  265. ];
  266. if(in_array($value['url'], $old)) {
  267. foreach ($old as $o_k => $o_v){
  268. if($o_v == $value['url']) unset($old[$o_k]);
  269. }
  270. }else{
  271. $new[] = $value['url'];
  272. }
  273. }
  274. ProductInfo::insert($insert);
  275. }
  276. ProductPriceDetail::where('del_time',0)
  277. ->where('product_id',$data['id'])
  278. ->update(['del_time' => $time]);
  279. if(! empty($data['product_price'])){
  280. $insert = [];
  281. foreach ($data['product_price'] as $value){
  282. $insert[] = [
  283. 'product_id' => $model->id,
  284. 'basic_type_id' => $value['basic_type_id'],
  285. 'price' => $value['price'] ?? 0,
  286. 'crt_time' => $time,
  287. ];
  288. }
  289. ProductPriceDetail::insert($insert);
  290. }
  291. DB::commit();
  292. }catch (\Exception $exception){
  293. DB::rollBack();
  294. return [false,$exception->getMessage()];
  295. }
  296. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  297. }
  298. /**
  299. * 产品新增
  300. * @param $data
  301. * @param $user
  302. * @return array
  303. */
  304. public function productAdd($data,$user){
  305. list($status,$msg) = $this->productRule($data, $user);
  306. if(!$status) return [$status,$msg];
  307. try {
  308. DB::beginTransaction();
  309. $model = new Product();
  310. $model->product_category_id = $data['product_category_id'] ?? 0;
  311. $model->product_category = $data['product_category'] ?? '';
  312. $model->title = $data['title'];
  313. $model->code = $data['code'] ?? '';
  314. $model->warranty_time = $data['warranty_time'] ?? 0;
  315. $model->install_time = $data['install_time'] ?? 0;
  316. $model->size = $data['size'] ?? '';
  317. $model->unit = $data['unit'] ?? 0;
  318. $model->bar_code = $data['bar_code'] ?? '';
  319. $model->cost = $data['cost'] ?? 0;
  320. $model->retail_price = $data['retail_price'] ?? 0;
  321. $model->mark = $data['mark'] ?? '';
  322. $model->state = $data['state'] ?? 0;
  323. $model->product_attribute = $data['product_attribute'] ?? 0;
  324. $model->crt_id = $user['id'];
  325. $model->depart_id = $data['depart_id'] ?? 0;
  326. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  327. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  328. $model->save();
  329. $time = time();
  330. if(! empty($data['introduction'])){
  331. $models = new ProductIntroduction();
  332. $models->product_id = $model->id;
  333. $models->introduction = $data['introduction'];
  334. $models->save();
  335. }
  336. $new = [];
  337. if(! empty($data['img'])){
  338. $insert = [];
  339. foreach ($data['img'] as $value){
  340. $insert[] = [
  341. 'product_id' => $model->id,
  342. 'file' => $value['url'],
  343. 'type' => ProductInfo::type_one,
  344. 'name' => $value['name'],
  345. 'crt_time' => $time,
  346. ];
  347. if(! empty($value['url'])) $new[] = $value['url'];
  348. }
  349. ProductInfo::insert($insert);
  350. }
  351. if(! empty($data['file'])){
  352. $insert = [];
  353. foreach ($data['file'] as $value){
  354. $insert[] = [
  355. 'product_id' => $model->id,
  356. 'file' => $value['url'],
  357. 'type' => ProductInfo::type_two,
  358. 'name' => $value['name'],
  359. 'crt_time' => $time,
  360. ];
  361. if(! empty($value['url'])) $new[] = $value['url'];
  362. }
  363. ProductInfo::insert($insert);
  364. }
  365. if(! empty($data['product_price'])){
  366. $insert = [];
  367. foreach ($data['product_price'] as $value){
  368. $insert[] = [
  369. 'product_id' => $model->id,
  370. 'basic_type_id' => $value['basic_type_id'],
  371. 'price' => $value['price'] ?? 0,
  372. 'crt_time' => $time,
  373. ];
  374. }
  375. ProductPriceDetail::insert($insert);
  376. }
  377. DB::commit();
  378. }catch (\Exception $exception){
  379. DB::rollBack();
  380. return [false,$exception->getMessage()];
  381. }
  382. return [true, ['file' => ['new' => $new]]];
  383. }
  384. /**
  385. * 产品删除
  386. * @param $data
  387. * @return array
  388. */
  389. public function productDel($data){
  390. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  391. try {
  392. DB::beginTransaction();
  393. $time = time();
  394. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  395. ProductIntroduction::where('product_id',$data['id'])
  396. ->where('del_time',0)
  397. ->update(['del_time' => $time]);
  398. $old = ProductInfo::where('del_time',0)
  399. ->where('product_id',$data['id'])
  400. ->select('file')
  401. ->get()->toArray();
  402. $old = array_column($old,'file');
  403. ProductInfo::where('del_time',0)
  404. ->where('product_id',$data['id'])
  405. ->update(['del_time' => $time]);
  406. ProductPriceDetail::where('del_time',0)
  407. ->where('product_id',$data['id'])
  408. ->update(['del_time' => $time]);
  409. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  410. DB::commit();
  411. }catch (\Exception $exception){
  412. DB::rollBack();
  413. return [false,$exception->getMessage()];
  414. }
  415. return [true, ['file' => ['old' => $old]]];
  416. }
  417. /**
  418. * 产品详情
  419. * @param $data
  420. * @param $user
  421. * @return array
  422. */
  423. public function productDetail($data,$user){
  424. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  425. $customer = Product::where('del_time',0)
  426. ->where('id',$data['id'])
  427. ->first();
  428. if(empty($customer)) return [false,'产品不存在或已被删除'];
  429. $customer = $customer->toArray();
  430. $customer['product_attribute_title'] = Product::$product_attribute[$customer['product_attribute']] ?? "";
  431. $customer['is_use_title'] = Product::$is_use[$customer['is_use']] ?? "";
  432. $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
  433. $category = ProductCategory::where('id',$customer['product_category_id'])
  434. ->value('title');
  435. $customer['product_category_title'] = $category;
  436. $customer['introduction'] = "";
  437. $in = ProductIntroduction::where('del_time',0)
  438. ->where('product_id',$data['id'])
  439. ->first();
  440. if(! empty($in)) $customer['introduction'] = $in->introduction;
  441. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  442. $model = BasicType::TopClear($user,$data);
  443. $basic = $model->where('del_time',0)
  444. ->where('type',22)
  445. ->select('title','id','type')
  446. ->orderby('id', 'asc')->get()->toArray();
  447. $detail = ProductPriceDetail::where('del_time',0)
  448. ->where('product_id',$data['id'])
  449. ->select('product_id','basic_type_id','price')
  450. ->get()->toArray();
  451. $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
  452. ->pluck('title','id')
  453. ->toArray();
  454. //是否总公司
  455. $is_main = $user['is_all_depart'];
  456. $top_depart = $user['depart_top'][0] ?? [];
  457. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  458. $customer['product_price'] = [];
  459. //特殊功能按钮
  460. $special_button = $user['special_button'] ?? [];
  461. //成本隐藏
  462. $price = "******";
  463. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $customer['cost'];
  464. $customer['cost_show'] = $price;
  465. //所有金额
  466. foreach ($basic as $value){
  467. $show = 0;
  468. if(in_array(RoleMenuButton::special_one,$special_button)){
  469. $show = 1;
  470. }else{
  471. if($top_depart['basic_type_id'] == $value['id']) $show = 1;
  472. }
  473. $customer['product_price'][$value['id']] = [
  474. 'basic_type_id' => $value['id'],
  475. 'basic_type_title' => $title_map[$value['id']] ?? '',
  476. 'price' => 0,
  477. 'is_show' => $show,
  478. ];
  479. }
  480. //展示金额
  481. foreach ($detail as $value){
  482. if(isset($customer['product_price'][$value['basic_type_id']])) $customer['product_price'][$value['basic_type_id']]['price'] = $value['price'];
  483. }
  484. $customer['product_price'] = array_values($customer['product_price']);
  485. //单位
  486. $title = BasicType::where('id',$customer['unit'])->value('title');
  487. $customer['unit_name'] = $title;
  488. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  489. $customer_info = ProductInfo::where('del_time',0)
  490. ->where('product_id',$customer['id'])
  491. ->select('id','product_id','file','type','name')
  492. ->get()->toArray();
  493. $fileUploadService = new FileUploadService();
  494. foreach ($customer_info as $value){
  495. $tmp = [
  496. 'url' => $value['file'],
  497. 'name' => $value['name'],
  498. 'show_url' => $fileUploadService->getFileShow($value['file']),
  499. ];
  500. if($value['type'] == ProductInfo::type_one){
  501. $customer['img'][] = $tmp;
  502. }elseif ($value['type'] == ProductInfo::type_two){
  503. $customer['file'][] = $tmp;
  504. }
  505. }
  506. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  507. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  508. //可见范围
  509. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  510. $customer['depart'] = $return[0] ?? [];
  511. $customer['employee'] = $return[1] ?? [];
  512. return [true, $customer];
  513. }
  514. public function productListIndex($data,$user){
  515. $model = Product::ProductClear($user,$data);
  516. $model = $model->where('del_time',0)
  517. ->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','install_time','product_attribute','is_use')
  518. ->orderby('product_attribute', 'desc')
  519. ->orderby('id', 'desc');
  520. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  521. if(isset($data['state'])) $model->where('state', $data['state']);
  522. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  523. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  524. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  525. if(! empty($data['category'])){
  526. $id = $this->getProductCateGory($data['category']);
  527. $model->whereIn('product_category_id', $id);
  528. }
  529. if(! empty($data['product_category'])) {
  530. $product_category = ProductCategory::where('del_time',0)
  531. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  532. ->select('id')
  533. ->get()->toArray();
  534. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  535. }
  536. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  537. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  538. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  539. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  540. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  541. $model->where('crt_time','>=',$return[0]);
  542. $model->where('crt_time','<=',$return[1]);
  543. }
  544. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  545. $list = $this->limit($model,'',$data);
  546. $list = $this->fillData($list,$user,$data);
  547. return [true, $list];
  548. }
  549. /**
  550. * 产品列表
  551. * @param $data
  552. * @param $user
  553. * @return array
  554. */
  555. public function productList($data,$user){
  556. $model = Product::ProductClear($user,$data);
  557. $model = $model->where('del_time',0)
  558. ->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','install_time','product_attribute','is_use')
  559. ->where('is_use', Product::is_use_one)
  560. ->orderby('product_attribute', 'desc')
  561. ->orderby('id', 'desc');
  562. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  563. if(isset($data['state'])) $model->where('state', $data['state']);
  564. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  565. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  566. if(! empty($data['category'])){
  567. $id = $this->getProductCateGory($data['category']);
  568. $model->whereIn('product_category_id', $id);
  569. }
  570. if(! empty($data['product_category'])) {
  571. $product_category = ProductCategory::where('del_time',0)
  572. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  573. ->select('id')
  574. ->get()->toArray();
  575. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  576. }
  577. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  578. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  579. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  580. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  581. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  582. $model->where('crt_time','>=',$return[0]);
  583. $model->where('crt_time','<=',$return[1]);
  584. }
  585. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  586. $list = $this->limit($model,'',$data);
  587. $list = $this->fillData($list,$user,$data);
  588. return [true, $list];
  589. }
  590. public function productList2($data,$user){
  591. $model = ProductCategory::TopClear($user,$data);
  592. $model = $model->where('del_time',0)
  593. ->select('title','id','parent_id')
  594. ->orderby('id','desc');
  595. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  596. $list = $model->get()->toArray();
  597. if(! empty($list)) {
  598. $productList = Product::where('del_time',0)
  599. ->whereIn('product_category_id',array_column($list,'id'))
  600. ->select('id','product_category_id','title','code')
  601. ->get()->toArray();
  602. $productMap = [];
  603. foreach ($productList as $value){
  604. $productMap[$value['product_category_id']][] = $value;
  605. }
  606. foreach ($list as $key => $value){
  607. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  608. }
  609. $list = $this->makeTree(0,$list);
  610. $list = $this->set_sort_circle($list);
  611. }
  612. return [200, $list];
  613. }
  614. /**
  615. * 产品参数规则
  616. * @param $data
  617. * @param $is_add
  618. * @return array
  619. */
  620. public function productRule(&$data, $user, $is_add = true){
  621. if(empty($data['title'])) return [false,'产品名称不能为空'];
  622. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  623. if(empty($data['code'])) return [false,'产品编码不能为空'];
  624. if(! isset($data['cost'])) return [false, '请填写成本'];
  625. if(! isset($data['retail_price'])) return [false, '请填写零售价'];
  626. $res = $this->checkNumber($data['cost']);
  627. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  628. $res = $this->checkNumber($data['retail_price']);
  629. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于等于0的数值'];
  630. if(! empty($data['product_price'])){
  631. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  632. ->pluck('title','id')
  633. ->toArray();
  634. foreach ($data['product_price'] as $value){
  635. if(! empty($value['price'])) {
  636. $tmp = $map[$value['basic_type_id']] ?? '';
  637. $res = $this->checkNumber($value['price']);
  638. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  639. }
  640. }
  641. }
  642. if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
  643. //所属部门 以及 顶级部门
  644. if(empty($data['depart_id'])) {
  645. $data['depart_id'] = $this->getDepart($user);
  646. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  647. }
  648. //总社id
  649. $top_depart_id = $user['head'] ?? [];
  650. $top_depart_id = $top_depart_id['id'] ?? 0;
  651. if($is_add){
  652. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  653. ->where('del_time',0)
  654. ->exists();
  655. }else{
  656. if(empty($data['id'])) return [false,'ID不能为空'];
  657. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  658. ->where('id','<>',$data['id'])
  659. ->where('del_time',0)
  660. ->exists();
  661. }
  662. if($bool) return [false,'产品编码不能重复'];
  663. return [true, $data];
  664. }
  665. /**
  666. * 拼接数据
  667. * @param $data
  668. * @return array
  669. */
  670. public function fillData($data, $user, $search){
  671. if(empty($data['data'])) return $data;
  672. $type = $search['type'] ?? 0;
  673. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  674. ->pluck('emp_name','id')
  675. ->toArray();
  676. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  677. ->select('title','id','is_edit_unit_price')
  678. ->get()
  679. ->toArray();
  680. $category = array_column($category,null,'id');
  681. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  682. ->orWhere('type',BasicType::type_22)
  683. ->pluck('title','id')
  684. ->toArray();
  685. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
  686. //产品使用价格
  687. $product_id = array_column($data['data'],'id');
  688. $detail_map = $this->getProductPrice($product_id, $type);
  689. //获取产品图片
  690. $img = $this->getProductImg($product_id);
  691. //当前门店
  692. $top_depart = $user['depart_top'][0] ?? [];
  693. //特殊功能按钮
  694. $special_button = $user['special_button'] ?? [];
  695. foreach ($data['data'] as $key => $value){
  696. $tmp = [];
  697. if(isset($detail_map[$value['id']])){
  698. $d = $detail_map[$value['id']];
  699. foreach ($d as $v){
  700. $is_show = 0;
  701. if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
  702. $is_use = 0;
  703. if($top_depart['basic_type_id'] == $v['basic_type_id']) $is_use = 1;
  704. $tmp[] = [
  705. 'basic_type_id' => $v['basic_type_id'],
  706. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  707. 'price' => $v['price'],
  708. 'is_show' => $is_show,
  709. 'is_use' => $is_use
  710. ];
  711. }
  712. }
  713. $data['data'][$key]['product_price'] = $tmp;
  714. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  715. $data['data'][$key]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
  716. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  717. $category_tmp = $category[$value['product_category_id']] ?? [];
  718. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  719. $data['data'][$key]['is_edit_unit_price'] = $category_tmp['is_edit_unit_price'] ?? 0;
  720. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  721. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  722. $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
  723. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  724. $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
  725. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  726. //成本隐藏
  727. $price = "******";
  728. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $value['cost'];
  729. $data['data'][$key]['cost_show'] = $price;
  730. }
  731. return $data;
  732. }
  733. public function getProductCateGory($category){
  734. $product_category = ProductCategory::where('del_time',0)
  735. ->select('id','parent_id')
  736. ->get()->toArray();
  737. $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
  738. return $result;
  739. }
  740. public function getProductImg($product_id = []){
  741. if(empty($product_id)) return [];
  742. $img = [];
  743. $customer_info = ProductInfo::where('del_time',0)
  744. ->whereIn('product_id',$product_id)
  745. ->where('type',ProductInfo::type_one)
  746. ->select('product_id','file','type','name')
  747. ->get()->toArray();
  748. $fileUploadService = new FileUploadService();
  749. foreach ($customer_info as $value){
  750. if(isset($img[$value['product_id']])) continue;
  751. $url = "";
  752. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  753. $img[$value['product_id']] = $url;
  754. }
  755. return $img;
  756. }
  757. public function batchUploadImg($data){
  758. if(empty($data['product_id'])) return [false, '请选择产品'];
  759. $time = time();
  760. try {
  761. DB::beginTransaction();
  762. $old = ProductInfo::where('del_time',0)
  763. ->where('type',ProductInfo::type_one)
  764. ->whereIn('product_id',$data['product_id'])
  765. ->select('file')
  766. ->get()->toArray();
  767. $old = array_column($old,'file');
  768. ProductInfo::where('del_time',0)
  769. ->where('type',ProductInfo::type_one)
  770. ->whereIn('product_id',$data['product_id'])
  771. ->update(['del_time' => $time]);
  772. $new['origin'] = $data['img_url'] ?? "";
  773. $new['img_list'] = [];
  774. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  775. $img = explode('.', $img);
  776. $insert = [];
  777. if(! empty($data['img_url'])){
  778. foreach ($data['product_id'] as $key => $value){
  779. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  780. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  781. $insert[] = [
  782. 'product_id' => $value,
  783. 'file' => $copy,
  784. 'type' => ProductInfo::type_one,
  785. 'name' => $data['img_name'] ?? "",
  786. 'crt_time' => $time,
  787. ];
  788. $new['img_list'][] = $copy;
  789. }
  790. if(! empty($insert)) ProductInfo::insert($insert);
  791. }
  792. DB::commit();
  793. }catch (\Throwable $exception){
  794. DB::rollBack();
  795. return [false, $exception->getMessage()];
  796. }
  797. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  798. }
  799. //获取产品字典
  800. public function getProductDetail($product_id = []){
  801. if(empty($product_id)) return [];
  802. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  803. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  804. ->pluck('is_edit_unit_price','id')
  805. ->toArray();
  806. foreach ($pro as $key => $value){
  807. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  808. }
  809. return array_column($pro,null,'id');
  810. }
  811. //获取产品使用价格
  812. public function getProductPrice($product_id = [], $type = 1){
  813. if(! is_array($product_id)) $product_id = [$product_id];
  814. //type 1 采购 2 合同 和 活动包
  815. $detail_map = [];
  816. $time = time();
  817. if($type == 1) {
  818. //供应商活动价格
  819. $activity = ProductActivityPrice::from('product_activity_price as a')
  820. ->join('product_activity as b','b.id','a.product_activity_id')
  821. ->where('a.del_time',0)
  822. ->whereIn('a.product_id',$product_id)
  823. ->where('a.start_time','<=',$time)
  824. ->where('a.end_time','>=',$time)
  825. ->where('b.type',ProductActivity::type_two)
  826. ->select('a.product_id','a.basic_type_id','a.price')
  827. ->get()->toArray();
  828. foreach ($activity as $value){
  829. $detail_map[$value['product_id']][] = $value;
  830. }
  831. //分社价 没有供应商活动价格使用分社价格
  832. $detail = ProductPriceDetail::where('del_time',0)
  833. ->whereIn('product_id',$product_id)
  834. ->select('product_id','basic_type_id','price')
  835. ->get()->toArray();
  836. foreach ($detail as $value){
  837. if(! isset($detail_map[$value['product_id']])){
  838. $detail_map[$value['product_id']][] = $value;
  839. }
  840. }
  841. }else{
  842. //零售活动价格
  843. $activity = ProductActivityPrice::from('product_activity_price as a')
  844. ->join('product_activity as b','b.id','a.product_activity_id')
  845. ->where('a.del_time',0)
  846. ->whereIn('a.product_id',$product_id)
  847. ->where('a.start_time','<=',$time)
  848. ->where('a.end_time','>=',$time)
  849. ->where('b.type',ProductActivity::type_one)
  850. ->select('a.product_id','a.basic_type_id','a.price')
  851. ->get()->toArray();
  852. foreach ($activity as $value){
  853. $detail_map[$value['product_id']][] = $value;
  854. }
  855. }
  856. return $detail_map;
  857. }
  858. public function getProductPriceTmp($product_id = []){
  859. //type 0 采购 1 合同
  860. //分社价
  861. $detail = ProductPriceDetail::where('del_time',0)
  862. ->whereIn('product_id',$product_id)
  863. ->select('product_id','basic_type_id','price')
  864. ->get()->toArray();
  865. $detail_map = [];
  866. foreach ($detail as $value){
  867. $detail_map[$value['product_id']][] = $value;
  868. }
  869. $return = $detail_map;
  870. //活动价格
  871. $time = time();
  872. $activity = ProductActivityPrice::where('del_time',0)
  873. ->whereIn('product_id',$product_id)
  874. ->where('start_time','<=',$time)
  875. ->where('end_time','>=',$time)
  876. ->select('product_id','basic_type_id','price')
  877. ->get()->toArray();
  878. if(! empty($activity)){
  879. foreach ($activity as $value){
  880. if(! isset($detail_map[$value['product_id']])) {
  881. $return[$value['product_id']][] = $value;
  882. }else{
  883. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  884. if(! in_array($value['basic_type_id'], $basic_type)){
  885. $return[$value['product_id']][] = $value;
  886. continue;
  887. }
  888. foreach ($detail_map[$value['product_id']] as $key => $val){
  889. if($value['basic_type_id'] == $val['basic_type_id']) {
  890. //活动价格替换
  891. $return[$value['product_id']][$key]['price'] = $value['price'];
  892. }
  893. }
  894. }
  895. }
  896. }unset($detail_map);
  897. return $return;
  898. }
  899. }