ProductService.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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->build_fee = $data['build_fee'] ?? 0;
  213. $model->item_code = $data['item_code'] ?? "";
  214. $model->mark = $data['mark'] ?? '';
  215. $model->state = $data['state'] ?? 0;
  216. $model->product_attribute = $data['product_attribute'] ?? 0;
  217. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  218. $model->save();
  219. $time = time();
  220. ProductIntroduction::where('product_id',$data['id'])
  221. ->where('del_time',0)
  222. ->update(['del_time' => $time]);
  223. if(! empty($data['introduction'])){
  224. $models = new ProductIntroduction();
  225. $models->product_id = $model->id;
  226. $models->introduction = $data['introduction'];
  227. $models->save();
  228. }
  229. $old = ProductInfo::where('del_time',0)
  230. ->where('product_id',$data['id'])
  231. ->select('file')
  232. ->get()->toArray();
  233. $old = array_column($old,'file');
  234. ProductInfo::where('del_time',0)
  235. ->where('product_id',$data['id'])
  236. ->update(['del_time' => $time]);
  237. $new = [];
  238. if(! empty($data['img'])){
  239. $insert = [];
  240. foreach ($data['img'] as $value){
  241. $insert[] = [
  242. 'product_id' => $model->id,
  243. 'file' => $value['url'],
  244. 'type' => ProductInfo::type_one,
  245. 'name' => $value['name'],
  246. 'crt_time' => $time,
  247. ];
  248. if(in_array($value['url'], $old)) {
  249. foreach ($old as $o_k => $o_v){
  250. if($o_v == $value['url']) unset($old[$o_k]);
  251. }
  252. }else{
  253. $new[] = $value['url'];
  254. }
  255. }
  256. ProductInfo::insert($insert);
  257. }
  258. if(! empty($data['file'])){
  259. $insert = [];
  260. foreach ($data['file'] as $value){
  261. $insert[] = [
  262. 'product_id' => $model->id,
  263. 'file' => $value['url'],
  264. 'type' => ProductInfo::type_two,
  265. 'name' => $value['name'],
  266. 'crt_time' => $time,
  267. ];
  268. if(in_array($value['url'], $old)) {
  269. foreach ($old as $o_k => $o_v){
  270. if($o_v == $value['url']) unset($old[$o_k]);
  271. }
  272. }else{
  273. $new[] = $value['url'];
  274. }
  275. }
  276. ProductInfo::insert($insert);
  277. }
  278. ProductPriceDetail::where('del_time',0)
  279. ->where('product_id',$data['id'])
  280. ->update(['del_time' => $time]);
  281. if(! empty($data['product_price'])){
  282. $insert = [];
  283. foreach ($data['product_price'] as $value){
  284. $insert[] = [
  285. 'product_id' => $model->id,
  286. 'basic_type_id' => $value['basic_type_id'],
  287. 'price' => $value['price'] ?? 0,
  288. 'crt_time' => $time,
  289. ];
  290. }
  291. ProductPriceDetail::insert($insert);
  292. }
  293. DB::commit();
  294. }catch (\Exception $exception){
  295. DB::rollBack();
  296. return [false,$exception->getMessage()];
  297. }
  298. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  299. }
  300. /**
  301. * 产品新增
  302. * @param $data
  303. * @param $user
  304. * @return array
  305. */
  306. public function productAdd($data,$user){
  307. list($status,$msg) = $this->productRule($data, $user);
  308. if(!$status) return [$status,$msg];
  309. try {
  310. DB::beginTransaction();
  311. $model = new Product();
  312. $model->product_category_id = $data['product_category_id'] ?? 0;
  313. $model->product_category = $data['product_category'] ?? '';
  314. $model->title = $data['title'];
  315. $model->code = $data['code'] ?? '';
  316. $model->warranty_time = $data['warranty_time'] ?? 0;
  317. $model->install_time = $data['install_time'] ?? 0;
  318. $model->size = $data['size'] ?? '';
  319. $model->unit = $data['unit'] ?? 0;
  320. $model->bar_code = $data['bar_code'] ?? '';
  321. $model->cost = $data['cost'] ?? 0;
  322. $model->retail_price = $data['retail_price'] ?? 0;
  323. $model->build_fee = $data['build_fee'] ?? 0;
  324. $model->item_code = $data['item_code'] ?? "";
  325. $model->mark = $data['mark'] ?? '';
  326. $model->state = $data['state'] ?? 0;
  327. $model->product_attribute = $data['product_attribute'] ?? 0;
  328. $model->crt_id = $user['id'];
  329. $model->depart_id = $data['depart_id'] ?? 0;
  330. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  331. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  332. $model->save();
  333. $time = time();
  334. if(! empty($data['introduction'])){
  335. $models = new ProductIntroduction();
  336. $models->product_id = $model->id;
  337. $models->introduction = $data['introduction'];
  338. $models->save();
  339. }
  340. $new = [];
  341. if(! empty($data['img'])){
  342. $insert = [];
  343. foreach ($data['img'] as $value){
  344. $insert[] = [
  345. 'product_id' => $model->id,
  346. 'file' => $value['url'],
  347. 'type' => ProductInfo::type_one,
  348. 'name' => $value['name'],
  349. 'crt_time' => $time,
  350. ];
  351. if(! empty($value['url'])) $new[] = $value['url'];
  352. }
  353. ProductInfo::insert($insert);
  354. }
  355. if(! empty($data['file'])){
  356. $insert = [];
  357. foreach ($data['file'] as $value){
  358. $insert[] = [
  359. 'product_id' => $model->id,
  360. 'file' => $value['url'],
  361. 'type' => ProductInfo::type_two,
  362. 'name' => $value['name'],
  363. 'crt_time' => $time,
  364. ];
  365. if(! empty($value['url'])) $new[] = $value['url'];
  366. }
  367. ProductInfo::insert($insert);
  368. }
  369. if(! empty($data['product_price'])){
  370. $insert = [];
  371. foreach ($data['product_price'] as $value){
  372. $insert[] = [
  373. 'product_id' => $model->id,
  374. 'basic_type_id' => $value['basic_type_id'],
  375. 'price' => $value['price'] ?? 0,
  376. 'crt_time' => $time,
  377. ];
  378. }
  379. ProductPriceDetail::insert($insert);
  380. }
  381. DB::commit();
  382. }catch (\Exception $exception){
  383. DB::rollBack();
  384. return [false,$exception->getMessage()];
  385. }
  386. return [true, ['file' => ['new' => $new]]];
  387. }
  388. /**
  389. * 产品删除
  390. * @param $data
  391. * @return array
  392. */
  393. public function productDel($data){
  394. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  395. try {
  396. DB::beginTransaction();
  397. $time = time();
  398. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  399. ProductIntroduction::where('product_id',$data['id'])
  400. ->where('del_time',0)
  401. ->update(['del_time' => $time]);
  402. $old = ProductInfo::where('del_time',0)
  403. ->where('product_id',$data['id'])
  404. ->select('file')
  405. ->get()->toArray();
  406. $old = array_column($old,'file');
  407. ProductInfo::where('del_time',0)
  408. ->where('product_id',$data['id'])
  409. ->update(['del_time' => $time]);
  410. ProductPriceDetail::where('del_time',0)
  411. ->where('product_id',$data['id'])
  412. ->update(['del_time' => $time]);
  413. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  414. DB::commit();
  415. }catch (\Exception $exception){
  416. DB::rollBack();
  417. return [false,$exception->getMessage()];
  418. }
  419. return [true, ['file' => ['old' => $old]]];
  420. }
  421. /**
  422. * 产品详情
  423. * @param $data
  424. * @param $user
  425. * @return array
  426. */
  427. public function productDetail($data,$user){
  428. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  429. $customer = Product::where('del_time',0)
  430. ->where('id',$data['id'])
  431. ->first();
  432. if(empty($customer)) return [false,'产品不存在或已被删除'];
  433. $customer = $customer->toArray();
  434. $customer['product_attribute_title'] = Product::$product_attribute[$customer['product_attribute']] ?? "";
  435. $customer['is_use_title'] = Product::$is_use[$customer['is_use']] ?? "";
  436. $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
  437. $category = ProductCategory::where('id',$customer['product_category_id'])
  438. ->value('title');
  439. $customer['product_category_title'] = $category;
  440. $customer['introduction'] = "";
  441. $in = ProductIntroduction::where('del_time',0)
  442. ->where('product_id',$data['id'])
  443. ->first();
  444. if(! empty($in)) $customer['introduction'] = $in->introduction;
  445. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  446. $model = BasicType::TopClear($user,$data);
  447. $basic = $model->where('del_time',0)
  448. ->where('type',22)
  449. ->select('title','id','type')
  450. ->orderby('id', 'asc')->get()->toArray();
  451. $detail = ProductPriceDetail::where('del_time',0)
  452. ->where('product_id',$data['id'])
  453. ->select('product_id','basic_type_id','price')
  454. ->get()->toArray();
  455. $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
  456. ->pluck('title','id')
  457. ->toArray();
  458. $top_depart = $user['depart_top'][0] ?? [];
  459. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  460. $customer['product_price'] = [];
  461. //特殊功能按钮
  462. $special_button = $user['special_button'] ?? [];
  463. //成本
  464. $is_show_cost = 0;
  465. if(in_array(RoleMenuButton::special_two,$special_button)) $is_show_cost = 1;
  466. $customer['cost_show'] = $is_show_cost;
  467. //所有金额
  468. foreach ($basic as $value){
  469. $show = 0;
  470. if(in_array(RoleMenuButton::special_one,$special_button)){
  471. $show = 1;
  472. }else{
  473. if($top_depart['basic_type_id'] == $value['id']) $show = 1;
  474. }
  475. $customer['product_price'][$value['id']] = [
  476. 'basic_type_id' => $value['id'],
  477. 'basic_type_title' => $title_map[$value['id']] ?? '',
  478. 'price' => 0,
  479. 'is_show' => $show,
  480. ];
  481. }
  482. //展示金额
  483. foreach ($detail as $value){
  484. if(isset($customer['product_price'][$value['basic_type_id']])) $customer['product_price'][$value['basic_type_id']]['price'] = $value['price'];
  485. }
  486. $customer['product_price'] = array_values($customer['product_price']);
  487. //单位
  488. $title = BasicType::where('id',$customer['unit'])->value('title');
  489. $customer['unit_name'] = $title;
  490. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  491. $customer_info = ProductInfo::where('del_time',0)
  492. ->where('product_id',$customer['id'])
  493. ->select('id','product_id','file','type','name')
  494. ->get()->toArray();
  495. $fileUploadService = new FileUploadService();
  496. foreach ($customer_info as $value){
  497. $tmp = [
  498. 'url' => $value['file'],
  499. 'name' => $value['name'],
  500. 'show_url' => $fileUploadService->getFileShow($value['file']),
  501. ];
  502. if($value['type'] == ProductInfo::type_one){
  503. $customer['img'][] = $tmp;
  504. }elseif ($value['type'] == ProductInfo::type_two){
  505. $customer['file'][] = $tmp;
  506. }
  507. }
  508. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  509. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  510. //可见范围
  511. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  512. $customer['depart'] = $return[0] ?? [];
  513. $customer['employee'] = $return[1] ?? [];
  514. return [true, $customer];
  515. }
  516. public function productListIndex($data,$user){
  517. $model = Product::ProductClear($user,$data);
  518. $model = $model->where('del_time',0)
  519. ->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','build_fee','item_code')
  520. ->orderby('product_attribute', 'desc')
  521. ->orderby('id', 'desc');
  522. if(! empty($data['title_t'])) {
  523. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  524. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  525. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  526. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  527. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  528. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  529. }
  530. if(! empty($data['title'])) {
  531. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  532. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  533. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  534. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  535. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  536. }
  537. if(isset($data['state'])) $model->where('state', $data['state']);
  538. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  539. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  540. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  541. if(! empty($data['category'])){
  542. $id = $this->getProductCateGory($data['category']);
  543. $model->whereIn('product_category_id', $id);
  544. }
  545. if(! empty($data['product_category'])) {
  546. $product_category = ProductCategory::where('del_time',0)
  547. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  548. ->select('id')
  549. ->get()->toArray();
  550. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  551. }
  552. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  553. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  554. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  555. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  556. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  557. $model->where('crt_time','>=',$return[0]);
  558. $model->where('crt_time','<=',$return[1]);
  559. }
  560. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  561. $list = $this->limit($model,'',$data);
  562. $list = $this->fillData($list,$user,$data);
  563. return [true, $list];
  564. }
  565. /**
  566. * 产品列表
  567. * @param $data
  568. * @param $user
  569. * @return array
  570. */
  571. public function productList($data,$user){
  572. $model = Product::ProductClear($user,$data);
  573. $model = $model->where('del_time',0)
  574. ->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','build_fee','item_code')
  575. ->where('is_use', Product::is_use_one)
  576. ->orderby('product_attribute', 'desc')
  577. ->orderby('id', 'desc');
  578. if(! empty($data['title_t'])) {
  579. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  580. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  581. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  582. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  583. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  584. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  585. }
  586. if(! empty($data['title'])){
  587. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  588. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  589. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  590. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  591. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  592. }
  593. if(isset($data['state'])) $model->where('state', $data['state']);
  594. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  595. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  596. if(! empty($data['category'])){
  597. $id = $this->getProductCateGory($data['category']);
  598. $model->whereIn('product_category_id', $id);
  599. }
  600. if(! empty($data['product_category'])) {
  601. $product_category = ProductCategory::where('del_time',0)
  602. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  603. ->select('id')
  604. ->get()->toArray();
  605. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  606. }
  607. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  608. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  609. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  610. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  611. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  612. $model->where('crt_time','>=',$return[0]);
  613. $model->where('crt_time','<=',$return[1]);
  614. }
  615. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  616. $list = $this->limit($model,'',$data);
  617. $list = $this->fillData($list,$user,$data);
  618. return [true, $list];
  619. }
  620. public function productList2($data,$user){
  621. $model = ProductCategory::ProductClear($user,$data);
  622. $model = $model->where('del_time',0)
  623. ->select('title','id','parent_id')
  624. ->orderby('id','desc');
  625. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  626. $list = $model->get()->toArray();
  627. if(! empty($list)) {
  628. $productList = Product::where('del_time',0)
  629. ->whereIn('product_category_id',array_column($list,'id'))
  630. ->select('id','product_category_id','title','code')
  631. ->get()->toArray();
  632. $productMap = [];
  633. foreach ($productList as $value){
  634. $productMap[$value['product_category_id']][] = $value;
  635. }
  636. foreach ($list as $key => $value){
  637. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  638. }
  639. $list = $this->makeTree(0,$list);
  640. $list = $this->set_sort_circle($list);
  641. }
  642. return [200, $list];
  643. }
  644. /**
  645. * 产品参数规则
  646. * @param $data
  647. * @param $is_add
  648. * @return array
  649. */
  650. public function productRule(&$data, $user, $is_add = true){
  651. if(empty($data['title'])) return [false,'产品名称不能为空'];
  652. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  653. if(empty($data['code'])) return [false,'产品编码不能为空'];
  654. if(! isset($data['cost'])) return [false, '请填写成本'];
  655. if(! isset($data['retail_price'])) return [false, '请填写零售价'];
  656. $res = $this->checkNumber($data['cost']);
  657. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  658. $res = $this->checkNumber($data['retail_price']);
  659. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于等于0的数值'];
  660. if(! empty($data['product_price'])){
  661. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  662. ->pluck('title','id')
  663. ->toArray();
  664. foreach ($data['product_price'] as $value){
  665. if(! empty($value['price'])) {
  666. $tmp = $map[$value['basic_type_id']] ?? '';
  667. $res = $this->checkNumber($value['price']);
  668. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  669. }
  670. }
  671. }
  672. if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
  673. //所属部门 以及 顶级部门
  674. if(empty($data['depart_id'])) {
  675. $data['depart_id'] = $this->getDepart($user);
  676. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  677. }
  678. //总社id
  679. $top_depart_id = $user['head'] ?? [];
  680. $top_depart_id = $top_depart_id['id'] ?? 0;
  681. if($is_add){
  682. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  683. ->where('del_time',0)
  684. ->exists();
  685. }else{
  686. if(empty($data['id'])) return [false,'ID不能为空'];
  687. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  688. ->where('id','<>',$data['id'])
  689. ->where('del_time',0)
  690. ->exists();
  691. }
  692. if($bool) return [false,'产品编码不能重复'];
  693. return [true, $data];
  694. }
  695. /**
  696. * 拼接数据
  697. * @param $data
  698. * @return array
  699. */
  700. public function fillData($data, $user, $search){
  701. if(empty($data['data'])) return $data;
  702. $type = $search['type'] ?? 0;
  703. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  704. ->pluck('emp_name','id')
  705. ->toArray();
  706. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  707. ->select('title','id','is_edit_unit_price')
  708. ->get()
  709. ->toArray();
  710. $category = array_column($category,null,'id');
  711. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  712. ->orWhere('type',BasicType::type_22)
  713. ->pluck('title','id')
  714. ->toArray();
  715. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
  716. //产品使用价格
  717. $product_id = array_column($data['data'],'id');
  718. $detail_map = $this->getProductPrice($product_id, $type);
  719. //获取产品图片
  720. $img = $this->getProductImg($product_id);
  721. //当前门店
  722. $top_depart = $user['depart_top'][0] ?? [];
  723. //特殊功能按钮
  724. $special_button = $user['special_button'] ?? [];
  725. //库存
  726. $stock_map = [];
  727. if(! empty($search['storehouse_id'])){
  728. $stock = ProductInventoryService::getRealStock($product_id, $search['storehouse_id']);
  729. foreach ($stock as $value){
  730. $stock_map[$value['product_id']] = $value;
  731. }unset($stock);
  732. }
  733. //分社价格
  734. $product_fs = [];
  735. if(! empty($search['fs_price_get'])) $product_fs = $this->getProductPriceDetail(['id' => $product_id], $user);
  736. foreach ($data['data'] as $key => $value){
  737. $tmp = [];
  738. if(isset($detail_map[$value['id']])){
  739. $d = $detail_map[$value['id']];
  740. foreach ($d as $v){
  741. $is_show = 0;
  742. if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
  743. $is_use = 0;
  744. if($top_depart['basic_type_id'] == $v['basic_type_id']) $is_use = 1;
  745. $tmp[] = [
  746. 'basic_type_id' => $v['basic_type_id'],
  747. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  748. 'price' => $v['price'],
  749. 'is_show' => $is_show,
  750. 'is_use' => $is_use
  751. ];
  752. }
  753. }
  754. $data['data'][$key]['product_price'] = $tmp;
  755. $price_tmp = $product_fs[$value['id']] ?? [];
  756. foreach ($price_tmp as $t_k => $t){
  757. $data['data'][$key][$t_k] = $t;
  758. }
  759. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  760. $data['data'][$key]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
  761. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  762. $category_tmp = $category[$value['product_category_id']] ?? [];
  763. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  764. $data['data'][$key]['is_edit_unit_price'] = $category_tmp['is_edit_unit_price'] ?? 0;
  765. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  766. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  767. $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
  768. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  769. $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
  770. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  771. //库存
  772. $data['data'][$key]['stock'] = $stock_map[$value['id']] ?? (object)[];
  773. //成本
  774. $data['data'][$key]['cost_show'] = $value['cost'];
  775. }
  776. return $data;
  777. }
  778. public function getProductCateGory($category){
  779. $product_category = ProductCategory::where('del_time',0)
  780. ->select('id','parent_id')
  781. ->get()->toArray();
  782. $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
  783. return $result;
  784. }
  785. public function getProductImg($product_id = []){
  786. if(empty($product_id)) return [];
  787. $img = [];
  788. $customer_info = ProductInfo::where('del_time',0)
  789. ->whereIn('product_id',$product_id)
  790. ->where('type',ProductInfo::type_one)
  791. ->select('product_id','file','type','name')
  792. ->get()->toArray();
  793. $fileUploadService = new FileUploadService();
  794. foreach ($customer_info as $value){
  795. if(isset($img[$value['product_id']])) continue;
  796. $url = "";
  797. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  798. $img[$value['product_id']] = $url;
  799. }
  800. return $img;
  801. }
  802. public function batchUploadImg($data){
  803. if(empty($data['product_id'])) return [false, '请选择产品'];
  804. $time = time();
  805. try {
  806. DB::beginTransaction();
  807. $old = ProductInfo::where('del_time',0)
  808. ->where('type',ProductInfo::type_one)
  809. ->whereIn('product_id',$data['product_id'])
  810. ->select('file')
  811. ->get()->toArray();
  812. $old = array_column($old,'file');
  813. ProductInfo::where('del_time',0)
  814. ->where('type',ProductInfo::type_one)
  815. ->whereIn('product_id',$data['product_id'])
  816. ->update(['del_time' => $time]);
  817. $new['origin'] = $data['img_url'] ?? "";
  818. $new['img_list'] = [];
  819. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  820. $img = explode('.', $img);
  821. $insert = [];
  822. if(! empty($data['img_url'])){
  823. foreach ($data['product_id'] as $key => $value){
  824. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  825. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  826. $insert[] = [
  827. 'product_id' => $value,
  828. 'file' => $copy,
  829. 'type' => ProductInfo::type_one,
  830. 'name' => $data['img_name'] ?? "",
  831. 'crt_time' => $time,
  832. ];
  833. $new['img_list'][] = $copy;
  834. }
  835. if(! empty($insert)) ProductInfo::insert($insert);
  836. }
  837. DB::commit();
  838. }catch (\Throwable $exception){
  839. DB::rollBack();
  840. return [false, $exception->getMessage()];
  841. }
  842. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  843. }
  844. public function batchBuildFee($data){
  845. if(empty($data['product_id'])) return [false, '请选择产品'];
  846. if(! isset($data['build_fee'])) return [false, '请填写安装费'];
  847. $res = $this->checkNumber($data['build_fee']);
  848. if(! $res) return [false,'安装费请输入不超过两位小数并且大于等于0的数值'];
  849. try {
  850. DB::beginTransaction();
  851. Product::where('del_time',0)
  852. ->whereIn('id', $data['product_id'])
  853. ->update([
  854. 'build_fee' => $data['build_fee']
  855. ]);
  856. DB::commit();
  857. }catch (\Throwable $exception){
  858. DB::rollBack();
  859. return [false, $exception->getMessage()];
  860. }
  861. return [true, ''];
  862. }
  863. //获取产品字典
  864. public function getProductDetail($product_id = []){
  865. if(empty($product_id)) return [];
  866. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  867. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  868. ->pluck('is_edit_unit_price','id')
  869. ->toArray();
  870. foreach ($pro as $key => $value){
  871. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  872. }
  873. return array_column($pro,null,'id');
  874. }
  875. //获取产品使用价格
  876. public function getProductPrice($product_id = [], $type = 1){
  877. if(! is_array($product_id)) $product_id = [$product_id];
  878. //type 1 采购 2 合同 和 活动包
  879. $detail_map = [];
  880. $time = time();
  881. if($type == 1) {
  882. //供应商活动价格
  883. $activity = ProductActivityPrice::from('product_activity_price as a')
  884. ->join('product_activity as b','b.id','a.product_activity_id')
  885. ->where('a.del_time',0)
  886. ->whereIn('a.product_id',$product_id)
  887. ->where('a.start_time','<=',$time)
  888. ->where('a.end_time','>=',$time)
  889. ->where('b.type',ProductActivity::type_two)
  890. ->select('a.product_id','a.basic_type_id','a.price')
  891. ->get()->toArray();
  892. foreach ($activity as $value){
  893. $detail_map[$value['product_id']][] = $value;
  894. }
  895. //分社价 没有供应商活动价格使用分社价格
  896. $detail = ProductPriceDetail::where('del_time',0)
  897. ->whereIn('product_id',$product_id)
  898. ->select('product_id','basic_type_id','price')
  899. ->get()->toArray();
  900. foreach ($detail as $value){
  901. if(! isset($detail_map[$value['product_id']][$value['basic_type_id']])){
  902. $detail_map[$value['product_id']][$value['basic_type_id']] = $value;
  903. }
  904. }
  905. }else{
  906. //零售活动价格
  907. $activity = ProductActivityPrice::from('product_activity_price as a')
  908. ->join('product_activity as b','b.id','a.product_activity_id')
  909. ->where('a.del_time',0)
  910. ->whereIn('a.product_id',$product_id)
  911. ->where('a.start_time','<=',$time)
  912. ->where('a.end_time','>=',$time)
  913. ->where('b.type',ProductActivity::type_one)
  914. ->select('a.product_id','a.basic_type_id','a.price')
  915. ->get()->toArray();
  916. foreach ($activity as $value){
  917. $detail_map[$value['product_id']][] = $value;
  918. }
  919. }
  920. return $detail_map;
  921. }
  922. public function getProductPriceTmp($product_id = []){
  923. //type 0 采购 1 合同
  924. //分社价
  925. $detail = ProductPriceDetail::where('del_time',0)
  926. ->whereIn('product_id',$product_id)
  927. ->select('product_id','basic_type_id','price')
  928. ->get()->toArray();
  929. $detail_map = [];
  930. foreach ($detail as $value){
  931. $detail_map[$value['product_id']][] = $value;
  932. }
  933. $return = $detail_map;
  934. //活动价格
  935. $time = time();
  936. $activity = ProductActivityPrice::where('del_time',0)
  937. ->whereIn('product_id',$product_id)
  938. ->where('start_time','<=',$time)
  939. ->where('end_time','>=',$time)
  940. ->select('product_id','basic_type_id','price')
  941. ->get()->toArray();
  942. if(! empty($activity)){
  943. foreach ($activity as $value){
  944. if(! isset($detail_map[$value['product_id']])) {
  945. $return[$value['product_id']][] = $value;
  946. }else{
  947. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  948. if(! in_array($value['basic_type_id'], $basic_type)){
  949. $return[$value['product_id']][] = $value;
  950. continue;
  951. }
  952. foreach ($detail_map[$value['product_id']] as $key => $val){
  953. if($value['basic_type_id'] == $val['basic_type_id']) {
  954. //活动价格替换
  955. $return[$value['product_id']][$key]['price'] = $value['price'];
  956. }
  957. }
  958. }
  959. }
  960. }unset($detail_map);
  961. return $return;
  962. }
  963. //获取产品分社价格
  964. public function getProductPriceDetail($data, $user){
  965. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  966. $model = BasicType::TopClear($user,$data);
  967. $basic = $model->where('del_time',0)
  968. ->where('type',22)
  969. ->select('title','id','type')
  970. ->orderby('id', 'asc')->get()->toArray();
  971. $detail = ProductPriceDetail::where('del_time',0)
  972. ->whereIn('product_id',$data['id'])
  973. ->select('product_id','basic_type_id','price')
  974. ->get()->toArray();
  975. $product_price = [];
  976. //所有金额
  977. foreach ($basic as $value){
  978. $product_price[$value['id']] = "0";
  979. }
  980. $return = [];
  981. foreach ($data['id'] as $value){
  982. $return[$value] = $product_price;
  983. }
  984. //展示金额
  985. foreach ($detail as $value){
  986. if(isset($return[$value['product_id']][$value['basic_type_id']])) $return[$value['product_id']][$value['basic_type_id']] = $value['price'];
  987. }
  988. return $return;
  989. }
  990. }