ProductService.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. <?php
  2. namespace App\Service;
  3. use App\Jobs\ProcessDataJob;
  4. use App\Model\BasicType;
  5. use App\Model\Depart;
  6. use App\Model\Employee;
  7. use App\Model\Product;
  8. use App\Model\ProductActivity;
  9. use App\Model\ProductActivityPrice;
  10. use App\Model\ProductCategory;
  11. use App\Model\ProductInfo;
  12. use App\Model\ProductIntroduction;
  13. use App\Model\ProductInventoryOfTop;
  14. use App\Model\ProductPriceDetail;
  15. use App\Model\Role;
  16. use App\Model\RoleMenuButton;
  17. use App\Model\SeeRange;
  18. use App\Model\Storehouse;
  19. use App\Model\U8Job;
  20. use Illuminate\Support\Facades\DB;
  21. /**
  22. * 产品管理
  23. */
  24. class ProductService extends Service
  25. {
  26. public function setIsEditUnitPrice($data,$user){
  27. if($this->isEmpty($data,'id')) return [false,'请选择分类!'];
  28. if(! isset($data['is_edit_unit_price'])) return [false,'请选择受否允许修改采购单价!'];
  29. $is_edit_unit_price = $data['is_edit_unit_price'] > 0 ? 1 : 0;
  30. $all_id = $this->getProductCateGory($data['id']);
  31. ProductCategory::whereIn('id',$all_id)->update([
  32. 'is_edit_unit_price' => $is_edit_unit_price
  33. ]);
  34. return [true, ''];
  35. }
  36. /**
  37. * 产品分类编辑
  38. * @param $data
  39. * @param $user
  40. * @return array
  41. */
  42. public function productCategoryEdit($data,$user){
  43. list($status,$msg) = $this->productCategoryRule($data,$user,false);
  44. if(!$status) return [$status,$msg];
  45. $update = $msg['data'][0];
  46. $model = new ProductCategory();
  47. $model->where('id',$data['id'])->update($update);
  48. return [true,''];
  49. }
  50. /**
  51. * 产品分类新增
  52. * @param $data
  53. * @param $user
  54. * @return array
  55. */
  56. public function productCategoryAdd($data,$user){
  57. list($status,$msg) = $this->productCategoryRule($data,$user);
  58. if(!$status) return [$status,$msg];
  59. ProductCategory::insert($msg['data']);
  60. return [true,''];
  61. }
  62. /**
  63. * 产品分类删除
  64. * @param $data
  65. * @return array
  66. */
  67. public function productCategoryDel($data){
  68. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  69. $bool = Product::where('del_time',0)
  70. ->where('product_category_id',$data['id'])
  71. ->exists();
  72. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  73. try {
  74. DB::beginTransaction();
  75. ProductCategory::where('id',$data['id'])->update([
  76. 'del_time' => time()
  77. ]);
  78. DB::commit();
  79. }catch (\Exception $exception){
  80. DB::rollBack();
  81. return [false,$exception->getMessage()];
  82. }
  83. return [true,''];
  84. }
  85. /**
  86. * 产品分类列表
  87. * @param $data
  88. * @param $user
  89. * @return array
  90. */
  91. public function productCategoryList($data,$user){
  92. $model = ProductCategory::TopClear($user,$data);
  93. $model = $model->where('del_time',0)
  94. ->select('title','id','parent_id','is_edit_unit_price')
  95. ->orderby('id','asc');
  96. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  97. if(isset($data['is_edit_unit_price'])) $model->where('is_edit_unit_price', $data['is_edit_unit_price']);
  98. $list = $model->get()->toArray();
  99. foreach ($list as $key => $value){
  100. $list[$key]['is_edit_unit_price_title'] = ProductCategory::$is_edit_unit_price[$value['is_edit_unit_price']] ?? "";
  101. }
  102. $list_tree = $list;
  103. if(! empty($list_tree)) {
  104. $list_tree = $this->makeTree(0,$list_tree);
  105. $list_tree = $this->set_sort_circle($list_tree);
  106. }
  107. return [200, ['data' => $list,'tree' => $list_tree]];
  108. }
  109. public function productCategoryList2($data,$user){
  110. $head_top_depart_id = $user['head']['id'] ?? 0;
  111. $now_top_depart_id = $this->getDepart($user);
  112. //总社分类
  113. $head = ProductCategory::where('del_time',0)
  114. ->where('top_depart_id', $head_top_depart_id)
  115. ->select('title','id','parent_id')
  116. ->orderby('id','asc')
  117. ->get()->toArray();
  118. $head_tree = [];
  119. if(! empty($head)) {
  120. $head_tree = $this->makeTree(0,$head);
  121. $head_tree = $this->set_sort_circle($head_tree);
  122. }
  123. //我的
  124. $my_tree = [];
  125. if($head_top_depart_id != $now_top_depart_id){
  126. $model = ProductCategory::TopClear($user,$data);
  127. $model = $model->where('del_time',0)
  128. ->select('title','id','parent_id')
  129. ->orderby('id','asc');
  130. $list = $model->get()->toArray();
  131. if(! empty($list)) {
  132. $my_tree = $this->makeTree(0,$list);
  133. $my_tree = $this->set_sort_circle($my_tree);
  134. }
  135. }
  136. return [200, ['head_tree' => $head_tree, 'my_tree' => $my_tree]];
  137. }
  138. /**
  139. * 产品分类参数规则
  140. * @param $data
  141. * @param $is_add
  142. * @return array
  143. */
  144. public function productCategoryRule($data,$user, $is_add = true){
  145. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  146. //所属部门 以及 顶级部门
  147. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  148. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  149. $title = array_column($data['data'],'title');
  150. $title = array_map(function($val) {
  151. return $val !== null ? $val : 0;
  152. }, $title);
  153. $title_count = array_count_values($title);
  154. foreach ($title as $value){
  155. if(empty($value)) return [false,'名称不能为空!'];
  156. if($title_count[$value] > 1) return [false,'名称不能重复'];
  157. }
  158. foreach ($data['data'] as $key => $value){
  159. $data['data'][$key]['upd_time'] = time();
  160. if($is_add){
  161. $parent_id = 0;
  162. if(! empty($value['parent_id'])) {
  163. $bool = Product::where('del_time',0)
  164. ->where('product_category_id',$value['parent_id'])
  165. ->exists();
  166. if($bool) {
  167. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  168. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  169. }
  170. $parent_id = $value['parent_id'];
  171. }
  172. $data['data'][$key]['parent_id'] = $parent_id;
  173. $data['data'][$key]['crt_time'] = time();
  174. $data['data'][$key]['depart_id'] = $data['depart_id'];
  175. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  176. $bool = ProductCategory::where('title',$value['title'])
  177. ->where('top_depart_id',$data['top_depart_id'])
  178. ->where('del_time',0)
  179. ->exists();
  180. }else{
  181. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  182. $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id');
  183. $bool = ProductCategory::where('title',$value['title'])
  184. ->where('top_depart_id',$top_depart_id)
  185. ->where('id','<>',$data['id'])
  186. ->where('del_time',0)
  187. ->exists();
  188. }
  189. if($bool) return [false,'分类名称不能重复'];
  190. }
  191. return [true, $data];
  192. }
  193. /**
  194. * 产品编辑
  195. * @param $data
  196. * @param $user
  197. * @return array
  198. */
  199. public function productEdit($data,$user){
  200. list($status,$msg) = $this->productRule($data, $user, false);
  201. if(!$status) return [$status,$msg];
  202. try {
  203. DB::beginTransaction();
  204. $model = Product::where('id',$data['id'])->first();
  205. $model->product_category_id = $data['product_category_id'] ?? 0;
  206. $model->product_category = $data['product_category'] ?? '';
  207. $model->title = $data['title'];
  208. $model->code = $data['code'] ?? '';
  209. $model->warranty_time = $data['warranty_time'] ?? 0;
  210. $model->install_time = $data['install_time'] ?? 0;
  211. $model->size = $data['size'] ?? '';
  212. $model->unit = $data['unit'] ?? 0;
  213. $model->bar_code = $data['bar_code'] ?? '';
  214. $model->cost = $data['cost'] ?? 0;
  215. $model->retail_price = $data['retail_price'] ?? 0;
  216. $model->build_fee = $data['build_fee'] ?? 0;
  217. $model->item_code = $data['item_code'] ?? "";
  218. $model->mark = $data['mark'] ?? '';
  219. $model->state = $data['state'] ?? 0;
  220. $model->product_attribute = $data['product_attribute'] ?? 0;
  221. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  222. $model->save();
  223. $time = time();
  224. ProductIntroduction::where('product_id',$data['id'])
  225. ->where('del_time',0)
  226. ->update(['del_time' => $time]);
  227. if(! empty($data['introduction'])){
  228. $models = new ProductIntroduction();
  229. $models->product_id = $model->id;
  230. $models->introduction = $data['introduction'];
  231. $models->save();
  232. }
  233. $old = ProductInfo::where('del_time',0)
  234. ->where('product_id',$data['id'])
  235. ->select('file')
  236. ->get()->toArray();
  237. $old = array_column($old,'file');
  238. ProductInfo::where('del_time',0)
  239. ->where('product_id',$data['id'])
  240. ->update(['del_time' => $time]);
  241. $new = [];
  242. if(! empty($data['img'])){
  243. $insert = [];
  244. foreach ($data['img'] as $value){
  245. $insert[] = [
  246. 'product_id' => $model->id,
  247. 'file' => $value['url'],
  248. 'type' => ProductInfo::type_one,
  249. 'name' => $value['name'],
  250. 'crt_time' => $time,
  251. ];
  252. if(in_array($value['url'], $old)) {
  253. foreach ($old as $o_k => $o_v){
  254. if($o_v == $value['url']) unset($old[$o_k]);
  255. }
  256. }else{
  257. $new[] = $value['url'];
  258. }
  259. }
  260. ProductInfo::insert($insert);
  261. }
  262. if(! empty($data['file'])){
  263. $insert = [];
  264. foreach ($data['file'] as $value){
  265. $insert[] = [
  266. 'product_id' => $model->id,
  267. 'file' => $value['url'],
  268. 'type' => ProductInfo::type_two,
  269. 'name' => $value['name'],
  270. 'crt_time' => $time,
  271. ];
  272. if(in_array($value['url'], $old)) {
  273. foreach ($old as $o_k => $o_v){
  274. if($o_v == $value['url']) unset($old[$o_k]);
  275. }
  276. }else{
  277. $new[] = $value['url'];
  278. }
  279. }
  280. ProductInfo::insert($insert);
  281. }
  282. ProductPriceDetail::where('del_time',0)
  283. ->where('product_id',$data['id'])
  284. ->update(['del_time' => $time]);
  285. if(! empty($data['product_price'])){
  286. $insert = [];
  287. foreach ($data['product_price'] as $value){
  288. $insert[] = [
  289. 'product_id' => $model->id,
  290. 'basic_type_id' => $value['basic_type_id'],
  291. 'price' => $value['price'] ?? 0,
  292. 'crt_time' => $time,
  293. ];
  294. }
  295. ProductPriceDetail::insert($insert);
  296. }
  297. DB::commit();
  298. }catch (\Exception $exception){
  299. DB::rollBack();
  300. return [false,$exception->getMessage()];
  301. }
  302. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  303. }
  304. /**
  305. * 产品新增
  306. * @param $data
  307. * @param $user
  308. * @return array
  309. */
  310. public function productAdd($data,$user){
  311. list($status,$msg) = $this->productRule($data, $user);
  312. if(!$status) return [$status,$msg];
  313. try {
  314. DB::beginTransaction();
  315. $model = new Product();
  316. $model->product_category_id = $data['product_category_id'] ?? 0;
  317. $model->product_category = $data['product_category'] ?? '';
  318. $model->title = $data['title'];
  319. $model->code = $data['code'] ?? '';
  320. $model->warranty_time = $data['warranty_time'] ?? 0;
  321. $model->install_time = $data['install_time'] ?? 0;
  322. $model->size = $data['size'] ?? '';
  323. $model->unit = $data['unit'] ?? 0;
  324. $model->bar_code = $data['bar_code'] ?? '';
  325. $model->cost = $data['cost'] ?? 0;
  326. $model->retail_price = $data['retail_price'] ?? 0;
  327. $model->build_fee = $data['build_fee'] ?? 0;
  328. $model->item_code = $data['item_code'] ?? "";
  329. $model->mark = $data['mark'] ?? '';
  330. $model->state = $data['state'] ?? 0;
  331. $model->product_attribute = $data['product_attribute'] ?? 0;
  332. $model->crt_id = $user['id'];
  333. $model->depart_id = $data['depart_id'] ?? 0;
  334. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  335. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  336. $model->save();
  337. $time = time();
  338. if(! empty($data['introduction'])){
  339. $models = new ProductIntroduction();
  340. $models->product_id = $model->id;
  341. $models->introduction = $data['introduction'];
  342. $models->save();
  343. }
  344. $new = [];
  345. if(! empty($data['img'])){
  346. $insert = [];
  347. foreach ($data['img'] as $value){
  348. $insert[] = [
  349. 'product_id' => $model->id,
  350. 'file' => $value['url'],
  351. 'type' => ProductInfo::type_one,
  352. 'name' => $value['name'],
  353. 'crt_time' => $time,
  354. ];
  355. if(! empty($value['url'])) $new[] = $value['url'];
  356. }
  357. ProductInfo::insert($insert);
  358. }
  359. if(! empty($data['file'])){
  360. $insert = [];
  361. foreach ($data['file'] as $value){
  362. $insert[] = [
  363. 'product_id' => $model->id,
  364. 'file' => $value['url'],
  365. 'type' => ProductInfo::type_two,
  366. 'name' => $value['name'],
  367. 'crt_time' => $time,
  368. ];
  369. if(! empty($value['url'])) $new[] = $value['url'];
  370. }
  371. ProductInfo::insert($insert);
  372. }
  373. if(! empty($data['product_price'])){
  374. $insert = [];
  375. foreach ($data['product_price'] as $value){
  376. $insert[] = [
  377. 'product_id' => $model->id,
  378. 'basic_type_id' => $value['basic_type_id'],
  379. 'price' => $value['price'] ?? 0,
  380. 'crt_time' => $time,
  381. ];
  382. }
  383. ProductPriceDetail::insert($insert);
  384. }
  385. DB::commit();
  386. }catch (\Exception $exception){
  387. DB::rollBack();
  388. return [false,$exception->getMessage()];
  389. }
  390. return [true, ['file' => ['new' => $new]]];
  391. }
  392. /**
  393. * 产品删除
  394. * @param $data
  395. * @return array
  396. */
  397. public function productDel($data){
  398. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  399. try {
  400. DB::beginTransaction();
  401. $time = time();
  402. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  403. ProductIntroduction::where('product_id',$data['id'])
  404. ->where('del_time',0)
  405. ->update(['del_time' => $time]);
  406. $old = ProductInfo::where('del_time',0)
  407. ->where('product_id',$data['id'])
  408. ->select('file')
  409. ->get()->toArray();
  410. $old = array_column($old,'file');
  411. ProductInfo::where('del_time',0)
  412. ->where('product_id',$data['id'])
  413. ->update(['del_time' => $time]);
  414. ProductPriceDetail::where('del_time',0)
  415. ->where('product_id',$data['id'])
  416. ->update(['del_time' => $time]);
  417. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  418. DB::commit();
  419. }catch (\Exception $exception){
  420. DB::rollBack();
  421. return [false,$exception->getMessage()];
  422. }
  423. return [true, ['file' => ['old' => $old]]];
  424. }
  425. /**
  426. * 产品详情
  427. * @param $data
  428. * @param $user
  429. * @return array
  430. */
  431. public function productDetail($data,$user){
  432. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  433. $customer = Product::where('del_time',0)
  434. ->where('id',$data['id'])
  435. ->first();
  436. if(empty($customer)) return [false,'产品不存在或已被删除'];
  437. $customer = $customer->toArray();
  438. $customer['product_attribute_title'] = Product::$product_attribute[$customer['product_attribute']] ?? "";
  439. $customer['is_use_title'] = Product::$is_use[$customer['is_use']] ?? "";
  440. $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
  441. $category = ProductCategory::where('id',$customer['product_category_id'])
  442. ->value('title');
  443. $customer['product_category_title'] = $category;
  444. $customer['introduction'] = "";
  445. $in = ProductIntroduction::where('del_time',0)
  446. ->where('product_id',$data['id'])
  447. ->first();
  448. if(! empty($in)) $customer['introduction'] = $in->introduction;
  449. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  450. $model = BasicType::TopClear($user,$data);
  451. $basic = $model->where('del_time',0)
  452. ->where('type',22)
  453. ->select('title','id','type')
  454. ->orderby('id', 'asc')->get()->toArray();
  455. $detail = ProductPriceDetail::where('del_time',0)
  456. ->where('product_id',$data['id'])
  457. ->select('product_id','basic_type_id','price')
  458. ->get()->toArray();
  459. $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
  460. ->pluck('title','id')
  461. ->toArray();
  462. $top_depart = $user['depart_top'][0] ?? [];
  463. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  464. $customer['product_price'] = [];
  465. //特殊功能按钮
  466. $special_button = $user['special_button'] ?? [];
  467. //成本
  468. $is_show_cost = 0;
  469. if(in_array(RoleMenuButton::special_two,$special_button)) $is_show_cost = 1;
  470. $customer['cost_show'] = $is_show_cost;
  471. //所有金额
  472. foreach ($basic as $value){
  473. $show = 0;
  474. if(in_array(RoleMenuButton::special_one,$special_button)){
  475. $show = 1;
  476. }else{
  477. if($top_depart['basic_type_id'] == $value['id']) $show = 1;
  478. }
  479. $customer['product_price'][$value['id']] = [
  480. 'basic_type_id' => $value['id'],
  481. 'basic_type_title' => $title_map[$value['id']] ?? '',
  482. 'price' => 0,
  483. 'is_show' => $show,
  484. ];
  485. }
  486. //展示金额
  487. foreach ($detail as $value){
  488. if(isset($customer['product_price'][$value['basic_type_id']])) $customer['product_price'][$value['basic_type_id']]['price'] = $value['price'];
  489. }
  490. $customer['product_price'] = array_values($customer['product_price']);
  491. //单位
  492. $title = BasicType::where('id',$customer['unit'])->value('title');
  493. $customer['unit_name'] = $title;
  494. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  495. $customer_info = ProductInfo::where('del_time',0)
  496. ->where('product_id',$customer['id'])
  497. ->select('id','product_id','file','type','name')
  498. ->get()->toArray();
  499. $fileUploadService = new FileUploadService();
  500. foreach ($customer_info as $value){
  501. $tmp = [
  502. 'url' => $value['file'],
  503. 'name' => $value['name'],
  504. 'show_url' => $fileUploadService->getFileShow($value['file']),
  505. ];
  506. if($value['type'] == ProductInfo::type_one){
  507. $customer['img'][] = $tmp;
  508. }elseif ($value['type'] == ProductInfo::type_two){
  509. $customer['file'][] = $tmp;
  510. }
  511. }
  512. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  513. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  514. //可见范围
  515. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  516. $customer['depart'] = $return[0] ?? [];
  517. $customer['employee'] = $return[1] ?? [];
  518. return [true, $customer];
  519. }
  520. public function productCommon($data,$user, $field = []){
  521. if(empty($field)){
  522. $field = ['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'];
  523. }
  524. $model = Product::ProductClear($user,$data);
  525. $model = $model->where('del_time',0)
  526. ->select($field)
  527. ->orderby('product_attribute', 'desc')
  528. ->orderby('id', 'desc');
  529. if(! empty($data['title_t'])) {
  530. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  531. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  532. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  533. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  534. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  535. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  536. }
  537. if(! empty($data['title'])) {
  538. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  539. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  540. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  541. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  542. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  543. }
  544. if(isset($data['state'])) $model->where('state', $data['state']);
  545. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  546. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  547. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  548. if(! empty($data['category'])){
  549. $id = $this->getProductCateGory($data['category']);
  550. $model->whereIn('product_category_id', $id);
  551. }
  552. if(! empty($data['product_category'])) {
  553. $product_category = ProductCategory::where('del_time',0)
  554. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  555. ->select('id')
  556. ->get()->toArray();
  557. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  558. }
  559. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  560. if(! empty($data['item_code'])) $model->where('item_code', 'LIKE', '%'.$data['item_code'].'%');
  561. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  562. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  563. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  564. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  565. $model->where('crt_time','>=',$return[0]);
  566. $model->where('crt_time','<=',$return[1]);
  567. }
  568. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  569. return $model;
  570. }
  571. /**
  572. * 产品列表
  573. * @param $data
  574. * @param $user
  575. * @return array
  576. */
  577. public function productListIndex($data,$user){
  578. $model = $this->productCommon($data, $user);
  579. $list = $this->limit($model,'',$data);
  580. $list = $this->fillData($list,$user,$data);
  581. return [true, $list];
  582. }
  583. public function productList($data,$user){
  584. $model = Product::ProductClear($user,$data);
  585. $model = $model->where('del_time',0)
  586. ->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')
  587. ->where('is_use', Product::is_use_one)
  588. ->orderby('product_attribute', 'desc')
  589. ->orderby('id', 'desc');
  590. if(! empty($data['title_t'])) {
  591. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  592. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  593. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  594. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  595. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  596. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  597. }
  598. if(! empty($data['title'])){
  599. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  600. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  601. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  602. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  603. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  604. }
  605. if(isset($data['state'])) $model->where('state', $data['state']);
  606. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  607. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  608. if(! empty($data['category'])){
  609. $id = $this->getProductCateGory($data['category']);
  610. $model->whereIn('product_category_id', $id);
  611. }
  612. if(! empty($data['product_category'])) {
  613. $product_category = ProductCategory::where('del_time',0)
  614. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  615. ->select('id')
  616. ->get()->toArray();
  617. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  618. }
  619. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  620. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  621. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  622. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  623. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  624. $model->where('crt_time','>=',$return[0]);
  625. $model->where('crt_time','<=',$return[1]);
  626. }
  627. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  628. $list = $this->limit($model,'',$data);
  629. $list = $this->fillData($list,$user,$data);
  630. return [true, $list];
  631. }
  632. public function productList2($data,$user){
  633. $model = ProductCategory::ProductClear($user,$data);
  634. $model = $model->where('del_time',0)
  635. ->select('title','id','parent_id')
  636. ->orderby('id','desc');
  637. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  638. $list = $model->get()->toArray();
  639. if(! empty($list)) {
  640. $productList = Product::where('del_time',0)
  641. ->whereIn('product_category_id',array_column($list,'id'))
  642. ->select('id','product_category_id','title','code')
  643. ->get()->toArray();
  644. $productMap = [];
  645. foreach ($productList as $value){
  646. $productMap[$value['product_category_id']][] = $value;
  647. }
  648. foreach ($list as $key => $value){
  649. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  650. }
  651. $list = $this->makeTree(0,$list);
  652. $list = $this->set_sort_circle($list);
  653. }
  654. return [200, $list];
  655. }
  656. /**
  657. * 产品参数规则
  658. * @param $data
  659. * @param $is_add
  660. * @return array
  661. */
  662. public function productRule(&$data, $user, $is_add = true){
  663. if(empty($data['title'])) return [false,'产品名称不能为空'];
  664. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  665. if(empty($data['code'])) return [false,'产品编码不能为空'];
  666. if(! isset($data['cost'])) return [false, '请填写成本'];
  667. if(! isset($data['retail_price'])) return [false, '请填写零售价'];
  668. $res = $this->checkNumber($data['cost']);
  669. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  670. $res = $this->checkNumber($data['retail_price']);
  671. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于等于0的数值'];
  672. if(! empty($data['product_price'])){
  673. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  674. ->pluck('title','id')
  675. ->toArray();
  676. foreach ($data['product_price'] as $value){
  677. if(! empty($value['price'])) {
  678. $tmp = $map[$value['basic_type_id']] ?? '';
  679. $res = $this->checkNumber($value['price']);
  680. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  681. }
  682. }
  683. }
  684. if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
  685. //所属部门 以及 顶级部门
  686. if(empty($data['depart_id'])) {
  687. $data['depart_id'] = $this->getDepart($user);
  688. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  689. }
  690. //总社id
  691. $top_depart_id = $user['head'] ?? [];
  692. $top_depart_id = $top_depart_id['id'] ?? 0;
  693. if($is_add){
  694. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  695. ->where('del_time',0)
  696. ->exists();
  697. }else{
  698. if(empty($data['id'])) return [false,'ID不能为空'];
  699. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  700. ->where('id','<>',$data['id'])
  701. ->where('del_time',0)
  702. ->exists();
  703. }
  704. if($bool) return [false,'产品编码不能重复'];
  705. return [true, $data];
  706. }
  707. /**
  708. * 拼接数据
  709. * @param $data
  710. * @return array
  711. */
  712. public function fillData($data, $user, $search){
  713. if(empty($data['data'])) return $data;
  714. $type = $search['type'] ?? 0;
  715. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  716. ->pluck('emp_name','id')
  717. ->toArray();
  718. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  719. ->select('title','id','is_edit_unit_price')
  720. ->get()
  721. ->toArray();
  722. $category = array_column($category,null,'id');
  723. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  724. ->orWhere('type',BasicType::type_22)
  725. ->pluck('title','id')
  726. ->toArray();
  727. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
  728. //产品使用价格
  729. $product_id = array_column($data['data'],'id');
  730. $detail_map = $this->getProductPrice($product_id, $type);
  731. //获取产品图片
  732. $img = $this->getProductImg($product_id);
  733. //当前门店
  734. $top_depart = $user['depart_top'][0] ?? [];
  735. //特殊功能按钮
  736. $special_button = $user['special_button'] ?? [];
  737. //库存
  738. $stock_map = [];
  739. if(! empty($search['storehouse_id'])){
  740. $stock = ProductInventoryService::getRealStock($product_id, $search['storehouse_id']);
  741. foreach ($stock as $value){
  742. $stock_map[$value['product_id']] = $value;
  743. }unset($stock);
  744. }
  745. //分社价格
  746. $product_fs = [];
  747. if(! empty($search['fs_price_get'])) $product_fs = $this->getProductPriceDetail(['id' => $product_id], $user);
  748. foreach ($data['data'] as $key => $value){
  749. $tmp = [];
  750. if(isset($detail_map[$value['id']])){
  751. $d = $detail_map[$value['id']];
  752. foreach ($d as $v){
  753. $is_show = 0;
  754. if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
  755. $is_use = 0;
  756. if($top_depart['basic_type_id'] == $v['basic_type_id']) $is_use = 1;
  757. $tmp[] = [
  758. 'basic_type_id' => $v['basic_type_id'],
  759. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  760. 'price' => $v['price'],
  761. 'is_show' => $is_show,
  762. 'is_use' => $is_use
  763. ];
  764. }
  765. }
  766. $data['data'][$key]['product_price'] = $tmp;
  767. $price_tmp = $product_fs[$value['id']] ?? [];
  768. foreach ($price_tmp as $t_k => $t){
  769. $data['data'][$key][$t_k] = $t;
  770. }
  771. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  772. $data['data'][$key]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
  773. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  774. $category_tmp = $category[$value['product_category_id']] ?? [];
  775. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  776. $data['data'][$key]['is_edit_unit_price'] = $category_tmp['is_edit_unit_price'] ?? 0;
  777. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  778. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  779. $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
  780. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  781. $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
  782. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  783. //库存
  784. $data['data'][$key]['stock'] = $stock_map[$value['id']] ?? (object)[];
  785. //成本
  786. $data['data'][$key]['cost_show'] = $value['cost'];
  787. }
  788. return $data;
  789. }
  790. public function getProductCateGory($category){
  791. $product_category = ProductCategory::where('del_time',0)
  792. ->select('id','parent_id')
  793. ->get()->toArray();
  794. $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
  795. return $result;
  796. }
  797. public function getProductImg($product_id = []){
  798. if(empty($product_id)) return [];
  799. $img = [];
  800. $customer_info = ProductInfo::where('del_time',0)
  801. ->whereIn('product_id',$product_id)
  802. ->where('type',ProductInfo::type_one)
  803. ->select('product_id','file','type','name')
  804. ->get()->toArray();
  805. $fileUploadService = new FileUploadService();
  806. foreach ($customer_info as $value){
  807. if(isset($img[$value['product_id']])) continue;
  808. $url = "";
  809. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  810. $img[$value['product_id']] = $url;
  811. }
  812. return $img;
  813. }
  814. public function batchUploadImg($data){
  815. if(empty($data['product_id'])) return [false, '请选择产品'];
  816. $time = time();
  817. try {
  818. DB::beginTransaction();
  819. $old = ProductInfo::where('del_time',0)
  820. ->where('type',ProductInfo::type_one)
  821. ->whereIn('product_id',$data['product_id'])
  822. ->select('file')
  823. ->get()->toArray();
  824. $old = array_column($old,'file');
  825. ProductInfo::where('del_time',0)
  826. ->where('type',ProductInfo::type_one)
  827. ->whereIn('product_id',$data['product_id'])
  828. ->update(['del_time' => $time]);
  829. $new['origin'] = $data['img_url'] ?? "";
  830. $new['img_list'] = [];
  831. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  832. $img = explode('.', $img);
  833. $insert = [];
  834. if(! empty($data['img_url'])){
  835. foreach ($data['product_id'] as $key => $value){
  836. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  837. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  838. $insert[] = [
  839. 'product_id' => $value,
  840. 'file' => $copy,
  841. 'type' => ProductInfo::type_one,
  842. 'name' => $data['img_name'] ?? "",
  843. 'crt_time' => $time,
  844. ];
  845. $new['img_list'][] = $copy;
  846. }
  847. if(! empty($insert)) ProductInfo::insert($insert);
  848. }
  849. DB::commit();
  850. }catch (\Throwable $exception){
  851. DB::rollBack();
  852. return [false, $exception->getMessage()];
  853. }
  854. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  855. }
  856. public function batchBuildFee($data){
  857. if(empty($data['product_id'])) return [false, '请选择产品'];
  858. if(! isset($data['build_fee'])) return [false, '请填写安装费'];
  859. $res = $this->checkNumber($data['build_fee']);
  860. if(! $res) return [false,'安装费请输入不超过两位小数并且大于等于0的数值'];
  861. try {
  862. DB::beginTransaction();
  863. Product::where('del_time',0)
  864. ->whereIn('id', $data['product_id'])
  865. ->update([
  866. 'build_fee' => $data['build_fee']
  867. ]);
  868. DB::commit();
  869. }catch (\Throwable $exception){
  870. DB::rollBack();
  871. return [false, $exception->getMessage()];
  872. }
  873. return [true, ''];
  874. }
  875. //获取产品字典
  876. public function getProductDetail($product_id = []){
  877. if(empty($product_id)) return [];
  878. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  879. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  880. ->pluck('is_edit_unit_price','id')
  881. ->toArray();
  882. foreach ($pro as $key => $value){
  883. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  884. }
  885. return array_column($pro,null,'id');
  886. }
  887. //获取产品使用价格
  888. public function getProductPrice($product_id = [], $type = 1){
  889. if(! is_array($product_id)) $product_id = [$product_id];
  890. //type 1 采购 2 合同 和 活动包
  891. $detail_map = [];
  892. $time = time();
  893. if($type == 1) {
  894. //供应商活动价格
  895. $activity = ProductActivityPrice::from('product_activity_price as a')
  896. ->join('product_activity as b','b.id','a.product_activity_id')
  897. ->where('a.del_time',0)
  898. ->whereIn('a.product_id',$product_id)
  899. ->where('a.start_time','<=',$time)
  900. ->where('a.end_time','>=',$time)
  901. ->where('b.type',ProductActivity::type_two)
  902. ->select('a.product_id','a.basic_type_id','a.price')
  903. ->get()->toArray();
  904. foreach ($activity as $value){
  905. $detail_map[$value['product_id']][] = $value;
  906. }
  907. //分社价 没有供应商活动价格使用分社价格
  908. $detail = ProductPriceDetail::where('del_time',0)
  909. ->whereIn('product_id',$product_id)
  910. ->select('product_id','basic_type_id','price')
  911. ->get()->toArray();
  912. foreach ($detail as $value){
  913. if(! isset($detail_map[$value['product_id']][$value['basic_type_id']])){
  914. $detail_map[$value['product_id']][$value['basic_type_id']] = $value;
  915. }
  916. }
  917. }else{
  918. //零售活动价格
  919. $activity = ProductActivityPrice::from('product_activity_price as a')
  920. ->join('product_activity as b','b.id','a.product_activity_id')
  921. ->where('a.del_time',0)
  922. ->whereIn('a.product_id',$product_id)
  923. ->where('a.start_time','<=',$time)
  924. ->where('a.end_time','>=',$time)
  925. ->where('b.type',ProductActivity::type_one)
  926. ->select('a.product_id','a.basic_type_id','a.price')
  927. ->get()->toArray();
  928. foreach ($activity as $value){
  929. $detail_map[$value['product_id']][] = $value;
  930. }
  931. }
  932. return $detail_map;
  933. }
  934. public function getProductPriceTmp($product_id = []){
  935. //type 0 采购 1 合同
  936. //分社价
  937. $detail = ProductPriceDetail::where('del_time',0)
  938. ->whereIn('product_id',$product_id)
  939. ->select('product_id','basic_type_id','price')
  940. ->get()->toArray();
  941. $detail_map = [];
  942. foreach ($detail as $value){
  943. $detail_map[$value['product_id']][] = $value;
  944. }
  945. $return = $detail_map;
  946. //活动价格
  947. $time = time();
  948. $activity = ProductActivityPrice::where('del_time',0)
  949. ->whereIn('product_id',$product_id)
  950. ->where('start_time','<=',$time)
  951. ->where('end_time','>=',$time)
  952. ->select('product_id','basic_type_id','price')
  953. ->get()->toArray();
  954. if(! empty($activity)){
  955. foreach ($activity as $value){
  956. if(! isset($detail_map[$value['product_id']])) {
  957. $return[$value['product_id']][] = $value;
  958. }else{
  959. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  960. if(! in_array($value['basic_type_id'], $basic_type)){
  961. $return[$value['product_id']][] = $value;
  962. continue;
  963. }
  964. foreach ($detail_map[$value['product_id']] as $key => $val){
  965. if($value['basic_type_id'] == $val['basic_type_id']) {
  966. //活动价格替换
  967. $return[$value['product_id']][$key]['price'] = $value['price'];
  968. }
  969. }
  970. }
  971. }
  972. }unset($detail_map);
  973. return $return;
  974. }
  975. //获取产品分社价格
  976. public function getProductPriceDetail($data, $user){
  977. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  978. $model = BasicType::TopClear($user,$data);
  979. $basic = $model->where('del_time',0)
  980. ->where('type',22)
  981. ->select('title','id','type')
  982. ->orderby('id', 'asc')->get()->toArray();
  983. $detail = ProductPriceDetail::where('del_time',0)
  984. ->whereIn('product_id',$data['id'])
  985. ->select('product_id','basic_type_id','price')
  986. ->get()->toArray();
  987. $product_price = [];
  988. //所有金额
  989. foreach ($basic as $value){
  990. $product_price[$value['id']] = "0";
  991. }
  992. $return = [];
  993. foreach ($data['id'] as $value){
  994. $return[$value] = $product_price;
  995. }
  996. //展示金额
  997. foreach ($detail as $value){
  998. if(isset($return[$value['product_id']][$value['basic_type_id']])) $return[$value['product_id']][$value['basic_type_id']] = $value['price'];
  999. }
  1000. return $return;
  1001. }
  1002. public function productGroupByList($data,$user){
  1003. $model = Product::ProductClear($user,$data);
  1004. $model = $model->where('del_time',0)
  1005. ->where('item_code','<>',"")
  1006. ->select('title','id','product_category_id','product_category','retail_price','product_attribute','item_code','unit','size','code','bar_code')
  1007. ->where('is_use', Product::is_use_one)
  1008. ->groupBy('item_code')
  1009. ->orderby('product_attribute', 'desc');
  1010. if(! empty($data['title_t'])) {
  1011. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1012. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  1013. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1014. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  1015. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  1016. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  1017. }
  1018. if(! empty($data['title'])){
  1019. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1020. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  1021. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1022. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  1023. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1024. }
  1025. if(isset($data['state'])) $model->where('state', $data['state']);
  1026. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  1027. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  1028. if(! empty($data['category'])){
  1029. $id = $this->getProductCateGory($data['category']);
  1030. $model->whereIn('product_category_id', $id);
  1031. }
  1032. if(! empty($data['product_category'])) {
  1033. $product_category = ProductCategory::where('del_time',0)
  1034. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  1035. ->select('id')
  1036. ->get()->toArray();
  1037. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  1038. }
  1039. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  1040. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  1041. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  1042. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  1043. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1044. $model->where('crt_time','>=',$return[0]);
  1045. $model->where('crt_time','<=',$return[1]);
  1046. }
  1047. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  1048. $list = $this->limit($model,'',$data);
  1049. $list = $this->productGroupByListData($list,$user,$data);
  1050. return [true, $list];
  1051. }
  1052. public function productGroupByListData($data, $user, $search){
  1053. if(empty($data['data'])) return $data;
  1054. $type = $search['type'] ?? 1;
  1055. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  1056. ->select('title','id')
  1057. ->get()
  1058. ->toArray();
  1059. $category = array_column($category,null,'id');
  1060. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'], 'unit')))
  1061. ->OrWhere('type',BasicType::type_22)
  1062. ->pluck('title','id')
  1063. ->toArray();
  1064. //产品使用价格
  1065. $product_id = array_column($data['data'],'id');
  1066. $detail_map = $this->getProductPrice($product_id, $type);
  1067. //获取产品图片
  1068. $img = $this->getProductImg($product_id);
  1069. //当前门店
  1070. $top_depart = $user['depart_top'][0] ?? [];
  1071. $stock = ProductInventoryOfTop::where('del_time',0)
  1072. ->whereIn('product_id',$product_id)
  1073. ->pluck('number','product_id')
  1074. ->toArray();
  1075. foreach ($data['data'] as $key => $value){
  1076. $arr = json_decode($value['product_category'], true);
  1077. $arr = array_flip($arr);
  1078. if (isset($arr[112])) {
  1079. $data['data'][$key]['is_roll'] = true;
  1080. }else{
  1081. $data['data'][$key]['is_roll'] = false;
  1082. }
  1083. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  1084. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1085. $data['data'][$key]['product_price'] = $tmp;
  1086. $category_tmp = $category[$value['product_category_id']] ?? [];
  1087. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  1088. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1089. $data['data'][$key]['stock'] = $this->returnStock($stock, $value);
  1090. $data['data'][$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1091. }
  1092. return $data;
  1093. }
  1094. public function productGroupByListDetail($data, $user){
  1095. if(empty($data['item_code'])) return [false, '商品编码不能为空'];
  1096. $type = $data['type'] ?? 1;
  1097. //获取相同itemCode的产品
  1098. $product = $this->getProductByItemCode([$data['item_code']], $user);
  1099. $category = ProductCategory::whereIn('id',array_unique(array_column($product,'product_category_id')))
  1100. ->pluck('title','id')
  1101. ->toArray();
  1102. $basic_map = BasicType::whereIn('id',array_unique(array_column($product, 'unit')))
  1103. ->OrWhere('type',BasicType::type_22)
  1104. ->pluck('title','id')
  1105. ->toArray();
  1106. //产品使用价格
  1107. $product_id = array_column($product,'id');
  1108. $detail_map = $this->getProductPrice($product_id, $type);
  1109. //获取产品图片
  1110. $img = $this->getProductImg($product_id);
  1111. //当前门店
  1112. $top_depart = $user['depart_top'][0] ?? [];
  1113. $stock = ProductInventoryOfTop::where('del_time',0)
  1114. ->whereIn('product_id',$product_id)
  1115. ->pluck('number','product_id')
  1116. ->toArray();
  1117. foreach ($product as $key => $value){
  1118. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1119. $product[$key]['product_price'] = $tmp;
  1120. $product[$key]['img'] = $img[$value['id']] ?? "";
  1121. $product[$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  1122. $product[$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1123. $product[$key]['stock'] = $this->returnStock($stock, $value);
  1124. $product[$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1125. $arr = json_decode($value['product_category'], true);
  1126. $arr = array_flip($arr);
  1127. if (isset($arr[112])) {
  1128. $product[$key]['is_roll'] = true;
  1129. }else{
  1130. $product[$key]['is_roll'] = false;
  1131. }
  1132. }
  1133. return [true, $product];
  1134. }
  1135. public function getProductByItemCode($item_code,$user){
  1136. if(empty($item_code)) return [];
  1137. $model = Product::ProductClear($user,[]);
  1138. $product = $model->where('del_time',0)
  1139. ->whereIn('item_code', $item_code)
  1140. ->where('is_use', Product::is_use_one)
  1141. ->select('title','id','product_category_id','retail_price','product_attribute','item_code','size','unit','code','bar_code','product_category')
  1142. ->orderby('product_attribute', 'desc')
  1143. ->get()->toArray();
  1144. return $product;
  1145. }
  1146. public function fillProductPrice($detail_map, $value, $top_depart,$basic_map){
  1147. $tmp = [];
  1148. if(isset($detail_map[$value['id']])){
  1149. $d = $detail_map[$value['id']];
  1150. foreach ($d as $v){
  1151. if($top_depart['basic_type_id'] != $v['basic_type_id']) continue;
  1152. $tmp = [
  1153. 'basic_type_id' => $v['basic_type_id'],
  1154. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  1155. 'price' => $v['price']
  1156. ];
  1157. }
  1158. }
  1159. if(empty($tmp)) $tmp = (object)$tmp;
  1160. return $tmp;
  1161. }
  1162. public function updateTopStock($data, $user){
  1163. $limit_key = "updateTopStockFromYy";
  1164. // list($status, $msg) = $this->limitingSendRequestBackg($limit_key);
  1165. // if(! $status) return [false, '正在获取T9杭州总仓库存货库存,请勿重复操作'];
  1166. //总店
  1167. $head = $user['head']['id'] ?? 0;
  1168. $storehouse_id = Storehouse::where('top_depart_id', $head)->where('del_time',0)->value('id') ?? 0;
  1169. $time = time();
  1170. DB::table('product')
  1171. ->where('del_time', 0)
  1172. ->where('top_depart_id', $head)
  1173. ->select('id','code')
  1174. ->orderBy('id')
  1175. ->chunk(200, function ($data) use($head,$storehouse_id,$time){
  1176. $data = Collect($data)->map(function ($object) {
  1177. return (array)$object;
  1178. })->toArray();
  1179. $map = array_column($data,'id','code');
  1180. $id = array_unique(array_column($data,'id'));
  1181. $code = array_unique(array_column($data,'code'));
  1182. $post = [
  1183. 'urlFromT9' => 'getStock',
  1184. 'code' => $code,
  1185. 'warehouse' => '001',
  1186. ];
  1187. $post = json_encode($post);
  1188. $header = ['Content-Type:application/json'];
  1189. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1190. if($msg['code'] != 200) return [false, $msg['msg']];
  1191. $msg = $msg['data'] ?? [];
  1192. $insert = [];
  1193. foreach ($msg as $value){
  1194. $insert[] = [
  1195. 'product_id' => $map[$value['product_no']] ?? 0,
  1196. 'storehouse_id' => $storehouse_id,
  1197. 'top_depart_id' => $head,
  1198. 'number' => $value['number'],
  1199. 'crt_time' => $time,
  1200. ];
  1201. }
  1202. ProductInventoryOfTop::where('del_time',0)
  1203. ->whereIn('product_id',$id)
  1204. ->update(['del_time' => $time]);
  1205. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1206. });
  1207. return [true, ''];
  1208. try{
  1209. $job = ProcessDataJob::dispatch(['head' => $head, 'storehouse_id' => $storehouse_id, 'type' => U8Job::three,'limit_key' => $limit_key])->onQueue(U8Job::job3);
  1210. if(! $job) {
  1211. $this->dellimitingSendRequestBackg($limit_key);
  1212. return [false,'T9杭州总仓库存货库存任务没有进入队列!'];
  1213. }
  1214. }catch (\Throwable $e){
  1215. $this->dellimitingSendRequestBackg($limit_key);
  1216. return [false,$e->getMessage()];
  1217. }
  1218. return [true, 'T9杭州总仓库存货库存任务正在后台执行,请等待'];
  1219. }
  1220. private function returnStock($stock, $value){
  1221. if(isset($stock[$value['id']])){
  1222. $number = $stock[$value['id']];
  1223. if($number <= 0){
  1224. $stock = "售罄";
  1225. } elseif($number <= 15){
  1226. $stock = " 库存紧张";
  1227. }else{
  1228. $stock = " 库存充足";
  1229. }
  1230. }else{
  1231. $stock = "联系客服";
  1232. }
  1233. return $stock;
  1234. }
  1235. public function writeStock($data){
  1236. try {
  1237. $head = $data['head'];
  1238. $storehouse_id = $data['storehouse_id'];
  1239. $limit_key = $data['limit_key'];
  1240. DB::enableQueryLog();
  1241. $service = new U8ServerService(true);
  1242. if(! empty($service->error)) return [false, $service->error];
  1243. $time = time();
  1244. DB::table('product')
  1245. ->where('del_time', 0)
  1246. ->where('top_depart_id', $head)
  1247. ->select('id','code')
  1248. ->orderBy('id')
  1249. ->chunk(200, function ($data) use($head,$storehouse_id,$time){
  1250. $data = Collect($data)->map(function ($object) {
  1251. return (array)$object;
  1252. })->toArray();
  1253. $map = array_column($data,'id','code');
  1254. $id = array_unique(array_column($data,'id'));
  1255. $code = array_unique(array_column($data,'code'));
  1256. $post = [
  1257. 'urlFromT9' => 'getStock',
  1258. 'code' => $code,
  1259. 'warehouse' => '001',
  1260. ];
  1261. $post = json_encode($post);
  1262. $header = ['Content-Type:application/json'];
  1263. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1264. if($msg['code'] != 200) return [false, $msg['msg']];
  1265. $msg = $msg['data'] ?? [];
  1266. $insert = [];
  1267. foreach ($msg as $value){
  1268. $insert[] = [
  1269. 'product_id' => $map[$value['product_no']] ?? 0,
  1270. 'storehouse_id' => $storehouse_id,
  1271. 'top_depart_id' => $head,
  1272. 'number' => $value['number'],
  1273. 'crt_time' => $time,
  1274. ];
  1275. }
  1276. ProductInventoryOfTop::where('del_time',0)
  1277. ->whereIn('product_id',$id)
  1278. ->update(['del_time' => $time]);
  1279. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1280. });
  1281. DB::commit();
  1282. $this->dellimitingSendRequestBackg($limit_key);
  1283. }catch (\Throwable $exception){
  1284. DB::rollBack();
  1285. $this->dellimitingSendRequestBackg($limit_key);
  1286. return [false, $exception->getMessage()];
  1287. }
  1288. return [true, ''];
  1289. }
  1290. public function post_helper($url, $data, $header = [], $timeout = 20){
  1291. $ch = curl_init();
  1292. curl_setopt($ch, CURLOPT_URL, $url);
  1293. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1294. curl_setopt($ch, CURLOPT_ENCODING, '');
  1295. curl_setopt($ch, CURLOPT_POST, 1);
  1296. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  1297. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  1298. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1299. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1300. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1301. $r = curl_exec($ch);
  1302. if ($r === false) {
  1303. // 获取错误号
  1304. $errorNumber = curl_errno($ch);
  1305. // 获取错误信息
  1306. $errorMessage = curl_error($ch);
  1307. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  1308. return [false, $message];
  1309. }
  1310. curl_close($ch);
  1311. return [true, json_decode($r, true)];
  1312. }
  1313. }