ProductService.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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 productListIndex($data,$user){
  521. $model = Product::ProductClear($user,$data);
  522. $model = $model->where('del_time',0)
  523. ->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')
  524. ->orderby('product_attribute', 'desc')
  525. ->orderby('id', 'desc');
  526. if(! empty($data['title_t'])) {
  527. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  528. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  529. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  530. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  531. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  532. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  533. }
  534. if(! empty($data['title'])) {
  535. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  536. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  537. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  538. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  539. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  540. }
  541. if(isset($data['state'])) $model->where('state', $data['state']);
  542. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  543. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  544. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  545. if(! empty($data['category'])){
  546. $id = $this->getProductCateGory($data['category']);
  547. $model->whereIn('product_category_id', $id);
  548. }
  549. if(! empty($data['product_category'])) {
  550. $product_category = ProductCategory::where('del_time',0)
  551. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  552. ->select('id')
  553. ->get()->toArray();
  554. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  555. }
  556. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  557. if(! empty($data['item_code'])) $model->where('item_code', 'LIKE', '%'.$data['item_code'].'%');
  558. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  559. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  560. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  561. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  562. $model->where('crt_time','>=',$return[0]);
  563. $model->where('crt_time','<=',$return[1]);
  564. }
  565. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  566. $list = $this->limit($model,'',$data);
  567. $list = $this->fillData($list,$user,$data);
  568. return [true, $list];
  569. }
  570. /**
  571. * 产品列表
  572. * @param $data
  573. * @param $user
  574. * @return array
  575. */
  576. public function productList($data,$user){
  577. $model = Product::ProductClear($user,$data);
  578. $model = $model->where('del_time',0)
  579. ->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')
  580. ->where('is_use', Product::is_use_one)
  581. ->orderby('product_attribute', 'desc')
  582. ->orderby('id', 'desc');
  583. if(! empty($data['title_t'])) {
  584. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  585. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  586. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  587. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  588. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  589. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  590. }
  591. if(! empty($data['title'])){
  592. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  593. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  594. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  595. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  596. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  597. }
  598. if(isset($data['state'])) $model->where('state', $data['state']);
  599. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  600. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  601. if(! empty($data['category'])){
  602. $id = $this->getProductCateGory($data['category']);
  603. $model->whereIn('product_category_id', $id);
  604. }
  605. if(! empty($data['product_category'])) {
  606. $product_category = ProductCategory::where('del_time',0)
  607. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  608. ->select('id')
  609. ->get()->toArray();
  610. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  611. }
  612. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  613. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  614. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  615. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  616. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  617. $model->where('crt_time','>=',$return[0]);
  618. $model->where('crt_time','<=',$return[1]);
  619. }
  620. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  621. $list = $this->limit($model,'',$data);
  622. $list = $this->fillData($list,$user,$data);
  623. return [true, $list];
  624. }
  625. public function productList2($data,$user){
  626. $model = ProductCategory::ProductClear($user,$data);
  627. $model = $model->where('del_time',0)
  628. ->select('title','id','parent_id')
  629. ->orderby('id','desc');
  630. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  631. $list = $model->get()->toArray();
  632. if(! empty($list)) {
  633. $productList = Product::where('del_time',0)
  634. ->whereIn('product_category_id',array_column($list,'id'))
  635. ->select('id','product_category_id','title','code')
  636. ->get()->toArray();
  637. $productMap = [];
  638. foreach ($productList as $value){
  639. $productMap[$value['product_category_id']][] = $value;
  640. }
  641. foreach ($list as $key => $value){
  642. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  643. }
  644. $list = $this->makeTree(0,$list);
  645. $list = $this->set_sort_circle($list);
  646. }
  647. return [200, $list];
  648. }
  649. /**
  650. * 产品参数规则
  651. * @param $data
  652. * @param $is_add
  653. * @return array
  654. */
  655. public function productRule(&$data, $user, $is_add = true){
  656. if(empty($data['title'])) return [false,'产品名称不能为空'];
  657. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  658. if(empty($data['code'])) return [false,'产品编码不能为空'];
  659. if(! isset($data['cost'])) return [false, '请填写成本'];
  660. if(! isset($data['retail_price'])) return [false, '请填写零售价'];
  661. $res = $this->checkNumber($data['cost']);
  662. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  663. $res = $this->checkNumber($data['retail_price']);
  664. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于等于0的数值'];
  665. if(! empty($data['product_price'])){
  666. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  667. ->pluck('title','id')
  668. ->toArray();
  669. foreach ($data['product_price'] as $value){
  670. if(! empty($value['price'])) {
  671. $tmp = $map[$value['basic_type_id']] ?? '';
  672. $res = $this->checkNumber($value['price']);
  673. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  674. }
  675. }
  676. }
  677. if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
  678. //所属部门 以及 顶级部门
  679. if(empty($data['depart_id'])) {
  680. $data['depart_id'] = $this->getDepart($user);
  681. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  682. }
  683. //总社id
  684. $top_depart_id = $user['head'] ?? [];
  685. $top_depart_id = $top_depart_id['id'] ?? 0;
  686. if($is_add){
  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('del_time',0)
  689. ->exists();
  690. }else{
  691. if(empty($data['id'])) return [false,'ID不能为空'];
  692. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  693. ->where('id','<>',$data['id'])
  694. ->where('del_time',0)
  695. ->exists();
  696. }
  697. if($bool) return [false,'产品编码不能重复'];
  698. return [true, $data];
  699. }
  700. /**
  701. * 拼接数据
  702. * @param $data
  703. * @return array
  704. */
  705. public function fillData($data, $user, $search){
  706. if(empty($data['data'])) return $data;
  707. $type = $search['type'] ?? 0;
  708. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  709. ->pluck('emp_name','id')
  710. ->toArray();
  711. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  712. ->select('title','id','is_edit_unit_price')
  713. ->get()
  714. ->toArray();
  715. $category = array_column($category,null,'id');
  716. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  717. ->orWhere('type',BasicType::type_22)
  718. ->pluck('title','id')
  719. ->toArray();
  720. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
  721. //产品使用价格
  722. $product_id = array_column($data['data'],'id');
  723. $detail_map = $this->getProductPrice($product_id, $type);
  724. //获取产品图片
  725. $img = $this->getProductImg($product_id);
  726. //当前门店
  727. $top_depart = $user['depart_top'][0] ?? [];
  728. //特殊功能按钮
  729. $special_button = $user['special_button'] ?? [];
  730. //库存
  731. $stock_map = [];
  732. if(! empty($search['storehouse_id'])){
  733. $stock = ProductInventoryService::getRealStock($product_id, $search['storehouse_id']);
  734. foreach ($stock as $value){
  735. $stock_map[$value['product_id']] = $value;
  736. }unset($stock);
  737. }
  738. //分社价格
  739. $product_fs = [];
  740. if(! empty($search['fs_price_get'])) $product_fs = $this->getProductPriceDetail(['id' => $product_id], $user);
  741. foreach ($data['data'] as $key => $value){
  742. $tmp = [];
  743. if(isset($detail_map[$value['id']])){
  744. $d = $detail_map[$value['id']];
  745. foreach ($d as $v){
  746. $is_show = 0;
  747. if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
  748. $is_use = 0;
  749. if($top_depart['basic_type_id'] == $v['basic_type_id']) $is_use = 1;
  750. $tmp[] = [
  751. 'basic_type_id' => $v['basic_type_id'],
  752. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  753. 'price' => $v['price'],
  754. 'is_show' => $is_show,
  755. 'is_use' => $is_use
  756. ];
  757. }
  758. }
  759. $data['data'][$key]['product_price'] = $tmp;
  760. $price_tmp = $product_fs[$value['id']] ?? [];
  761. foreach ($price_tmp as $t_k => $t){
  762. $data['data'][$key][$t_k] = $t;
  763. }
  764. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  765. $data['data'][$key]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
  766. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  767. $category_tmp = $category[$value['product_category_id']] ?? [];
  768. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  769. $data['data'][$key]['is_edit_unit_price'] = $category_tmp['is_edit_unit_price'] ?? 0;
  770. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  771. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  772. $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
  773. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  774. $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
  775. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  776. //库存
  777. $data['data'][$key]['stock'] = $stock_map[$value['id']] ?? (object)[];
  778. //成本
  779. $data['data'][$key]['cost_show'] = $value['cost'];
  780. }
  781. return $data;
  782. }
  783. public function getProductCateGory($category){
  784. $product_category = ProductCategory::where('del_time',0)
  785. ->select('id','parent_id')
  786. ->get()->toArray();
  787. $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
  788. return $result;
  789. }
  790. public function getProductImg($product_id = []){
  791. if(empty($product_id)) return [];
  792. $img = [];
  793. $customer_info = ProductInfo::where('del_time',0)
  794. ->whereIn('product_id',$product_id)
  795. ->where('type',ProductInfo::type_one)
  796. ->select('product_id','file','type','name')
  797. ->get()->toArray();
  798. $fileUploadService = new FileUploadService();
  799. foreach ($customer_info as $value){
  800. if(isset($img[$value['product_id']])) continue;
  801. $url = "";
  802. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  803. $img[$value['product_id']] = $url;
  804. }
  805. return $img;
  806. }
  807. public function batchUploadImg($data){
  808. if(empty($data['product_id'])) return [false, '请选择产品'];
  809. $time = time();
  810. try {
  811. DB::beginTransaction();
  812. $old = ProductInfo::where('del_time',0)
  813. ->where('type',ProductInfo::type_one)
  814. ->whereIn('product_id',$data['product_id'])
  815. ->select('file')
  816. ->get()->toArray();
  817. $old = array_column($old,'file');
  818. ProductInfo::where('del_time',0)
  819. ->where('type',ProductInfo::type_one)
  820. ->whereIn('product_id',$data['product_id'])
  821. ->update(['del_time' => $time]);
  822. $new['origin'] = $data['img_url'] ?? "";
  823. $new['img_list'] = [];
  824. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  825. $img = explode('.', $img);
  826. $insert = [];
  827. if(! empty($data['img_url'])){
  828. foreach ($data['product_id'] as $key => $value){
  829. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  830. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  831. $insert[] = [
  832. 'product_id' => $value,
  833. 'file' => $copy,
  834. 'type' => ProductInfo::type_one,
  835. 'name' => $data['img_name'] ?? "",
  836. 'crt_time' => $time,
  837. ];
  838. $new['img_list'][] = $copy;
  839. }
  840. if(! empty($insert)) ProductInfo::insert($insert);
  841. }
  842. DB::commit();
  843. }catch (\Throwable $exception){
  844. DB::rollBack();
  845. return [false, $exception->getMessage()];
  846. }
  847. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  848. }
  849. public function batchBuildFee($data){
  850. if(empty($data['product_id'])) return [false, '请选择产品'];
  851. if(! isset($data['build_fee'])) return [false, '请填写安装费'];
  852. $res = $this->checkNumber($data['build_fee']);
  853. if(! $res) return [false,'安装费请输入不超过两位小数并且大于等于0的数值'];
  854. try {
  855. DB::beginTransaction();
  856. Product::where('del_time',0)
  857. ->whereIn('id', $data['product_id'])
  858. ->update([
  859. 'build_fee' => $data['build_fee']
  860. ]);
  861. DB::commit();
  862. }catch (\Throwable $exception){
  863. DB::rollBack();
  864. return [false, $exception->getMessage()];
  865. }
  866. return [true, ''];
  867. }
  868. //获取产品字典
  869. public function getProductDetail($product_id = []){
  870. if(empty($product_id)) return [];
  871. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  872. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  873. ->pluck('is_edit_unit_price','id')
  874. ->toArray();
  875. foreach ($pro as $key => $value){
  876. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  877. }
  878. return array_column($pro,null,'id');
  879. }
  880. //获取产品使用价格
  881. public function getProductPrice($product_id = [], $type = 1){
  882. if(! is_array($product_id)) $product_id = [$product_id];
  883. //type 1 采购 2 合同 和 活动包
  884. $detail_map = [];
  885. $time = time();
  886. if($type == 1) {
  887. //供应商活动价格
  888. $activity = ProductActivityPrice::from('product_activity_price as a')
  889. ->join('product_activity as b','b.id','a.product_activity_id')
  890. ->where('a.del_time',0)
  891. ->whereIn('a.product_id',$product_id)
  892. ->where('a.start_time','<=',$time)
  893. ->where('a.end_time','>=',$time)
  894. ->where('b.type',ProductActivity::type_two)
  895. ->select('a.product_id','a.basic_type_id','a.price')
  896. ->get()->toArray();
  897. foreach ($activity as $value){
  898. $detail_map[$value['product_id']][] = $value;
  899. }
  900. //分社价 没有供应商活动价格使用分社价格
  901. $detail = ProductPriceDetail::where('del_time',0)
  902. ->whereIn('product_id',$product_id)
  903. ->select('product_id','basic_type_id','price')
  904. ->get()->toArray();
  905. foreach ($detail as $value){
  906. if(! isset($detail_map[$value['product_id']][$value['basic_type_id']])){
  907. $detail_map[$value['product_id']][$value['basic_type_id']] = $value;
  908. }
  909. }
  910. }else{
  911. //零售活动价格
  912. $activity = ProductActivityPrice::from('product_activity_price as a')
  913. ->join('product_activity as b','b.id','a.product_activity_id')
  914. ->where('a.del_time',0)
  915. ->whereIn('a.product_id',$product_id)
  916. ->where('a.start_time','<=',$time)
  917. ->where('a.end_time','>=',$time)
  918. ->where('b.type',ProductActivity::type_one)
  919. ->select('a.product_id','a.basic_type_id','a.price')
  920. ->get()->toArray();
  921. foreach ($activity as $value){
  922. $detail_map[$value['product_id']][] = $value;
  923. }
  924. }
  925. return $detail_map;
  926. }
  927. public function getProductPriceTmp($product_id = []){
  928. //type 0 采购 1 合同
  929. //分社价
  930. $detail = ProductPriceDetail::where('del_time',0)
  931. ->whereIn('product_id',$product_id)
  932. ->select('product_id','basic_type_id','price')
  933. ->get()->toArray();
  934. $detail_map = [];
  935. foreach ($detail as $value){
  936. $detail_map[$value['product_id']][] = $value;
  937. }
  938. $return = $detail_map;
  939. //活动价格
  940. $time = time();
  941. $activity = ProductActivityPrice::where('del_time',0)
  942. ->whereIn('product_id',$product_id)
  943. ->where('start_time','<=',$time)
  944. ->where('end_time','>=',$time)
  945. ->select('product_id','basic_type_id','price')
  946. ->get()->toArray();
  947. if(! empty($activity)){
  948. foreach ($activity as $value){
  949. if(! isset($detail_map[$value['product_id']])) {
  950. $return[$value['product_id']][] = $value;
  951. }else{
  952. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  953. if(! in_array($value['basic_type_id'], $basic_type)){
  954. $return[$value['product_id']][] = $value;
  955. continue;
  956. }
  957. foreach ($detail_map[$value['product_id']] as $key => $val){
  958. if($value['basic_type_id'] == $val['basic_type_id']) {
  959. //活动价格替换
  960. $return[$value['product_id']][$key]['price'] = $value['price'];
  961. }
  962. }
  963. }
  964. }
  965. }unset($detail_map);
  966. return $return;
  967. }
  968. //获取产品分社价格
  969. public function getProductPriceDetail($data, $user){
  970. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  971. $model = BasicType::TopClear($user,$data);
  972. $basic = $model->where('del_time',0)
  973. ->where('type',22)
  974. ->select('title','id','type')
  975. ->orderby('id', 'asc')->get()->toArray();
  976. $detail = ProductPriceDetail::where('del_time',0)
  977. ->whereIn('product_id',$data['id'])
  978. ->select('product_id','basic_type_id','price')
  979. ->get()->toArray();
  980. $product_price = [];
  981. //所有金额
  982. foreach ($basic as $value){
  983. $product_price[$value['id']] = "0";
  984. }
  985. $return = [];
  986. foreach ($data['id'] as $value){
  987. $return[$value] = $product_price;
  988. }
  989. //展示金额
  990. foreach ($detail as $value){
  991. if(isset($return[$value['product_id']][$value['basic_type_id']])) $return[$value['product_id']][$value['basic_type_id']] = $value['price'];
  992. }
  993. return $return;
  994. }
  995. public function productGroupByList($data,$user){
  996. $model = Product::ProductClear($user,$data);
  997. $model = $model->where('del_time',0)
  998. ->where('item_code','<>',"")
  999. ->select('title','id','product_category_id','product_category','retail_price','product_attribute','item_code','unit','size','code','bar_code')
  1000. ->where('is_use', Product::is_use_one)
  1001. ->groupBy('item_code')
  1002. ->orderby('product_attribute', 'desc');
  1003. if(! empty($data['title_t'])) {
  1004. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1005. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  1006. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1007. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  1008. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  1009. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  1010. }
  1011. if(! empty($data['title'])){
  1012. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1013. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  1014. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1015. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  1016. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1017. }
  1018. if(isset($data['state'])) $model->where('state', $data['state']);
  1019. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  1020. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  1021. if(! empty($data['category'])){
  1022. $id = $this->getProductCateGory($data['category']);
  1023. $model->whereIn('product_category_id', $id);
  1024. }
  1025. if(! empty($data['product_category'])) {
  1026. $product_category = ProductCategory::where('del_time',0)
  1027. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  1028. ->select('id')
  1029. ->get()->toArray();
  1030. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  1031. }
  1032. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  1033. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  1034. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  1035. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  1036. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1037. $model->where('crt_time','>=',$return[0]);
  1038. $model->where('crt_time','<=',$return[1]);
  1039. }
  1040. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  1041. $list = $this->limit($model,'',$data);
  1042. $list = $this->productGroupByListData($list,$user,$data);
  1043. return [true, $list];
  1044. }
  1045. public function productGroupByListData($data, $user, $search){
  1046. if(empty($data['data'])) return $data;
  1047. $type = $search['type'] ?? 1;
  1048. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  1049. ->select('title','id')
  1050. ->get()
  1051. ->toArray();
  1052. $category = array_column($category,null,'id');
  1053. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'], 'unit')))
  1054. ->OrWhere('type',BasicType::type_22)
  1055. ->pluck('title','id')
  1056. ->toArray();
  1057. //产品使用价格
  1058. $product_id = array_column($data['data'],'id');
  1059. $detail_map = $this->getProductPrice($product_id, $type);
  1060. //获取产品图片
  1061. $img = $this->getProductImg($product_id);
  1062. //当前门店
  1063. $top_depart = $user['depart_top'][0] ?? [];
  1064. $stock = ProductInventoryOfTop::where('del_time',0)
  1065. ->whereIn('product_id',$product_id)
  1066. ->pluck('number','product_id')
  1067. ->toArray();
  1068. foreach ($data['data'] as $key => $value){
  1069. $arr = json_decode($value['product_category'], true);
  1070. $arr = array_flip($arr);
  1071. if (isset($arr[112])) {
  1072. $data['data'][$key]['is_roll'] = true;
  1073. }else{
  1074. $data['data'][$key]['is_roll'] = false;
  1075. }
  1076. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  1077. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1078. $data['data'][$key]['product_price'] = $tmp;
  1079. $category_tmp = $category[$value['product_category_id']] ?? [];
  1080. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  1081. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1082. $data['data'][$key]['stock'] = $this->returnStock($stock, $value);
  1083. $data['data'][$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1084. }
  1085. return $data;
  1086. }
  1087. public function productGroupByListDetail($data, $user){
  1088. if(empty($data['item_code'])) return [false, '商品编码不能为空'];
  1089. $type = $data['type'] ?? 1;
  1090. //获取相同itemCode的产品
  1091. $product = $this->getProductByItemCode([$data['item_code']], $user);
  1092. $category = ProductCategory::whereIn('id',array_unique(array_column($product,'product_category_id')))
  1093. ->pluck('title','id')
  1094. ->toArray();
  1095. $basic_map = BasicType::whereIn('id',array_unique(array_column($product, 'unit')))
  1096. ->OrWhere('type',BasicType::type_22)
  1097. ->pluck('title','id')
  1098. ->toArray();
  1099. //产品使用价格
  1100. $product_id = array_column($product,'id');
  1101. $detail_map = $this->getProductPrice($product_id, $type);
  1102. //获取产品图片
  1103. $img = $this->getProductImg($product_id);
  1104. //当前门店
  1105. $top_depart = $user['depart_top'][0] ?? [];
  1106. $stock = ProductInventoryOfTop::where('del_time',0)
  1107. ->whereIn('product_id',$product_id)
  1108. ->pluck('number','product_id')
  1109. ->toArray();
  1110. foreach ($product as $key => $value){
  1111. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1112. $product[$key]['product_price'] = $tmp;
  1113. $product[$key]['img'] = $img[$value['id']] ?? "";
  1114. $product[$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  1115. $product[$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1116. $product[$key]['stock'] = $this->returnStock($stock, $value);
  1117. $product[$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1118. $arr = json_decode($value['product_category'], true);
  1119. $arr = array_flip($arr);
  1120. if (isset($arr[112])) {
  1121. $product[$key]['is_roll'] = true;
  1122. }else{
  1123. $product[$key]['is_roll'] = false;
  1124. }
  1125. }
  1126. return [true, $product];
  1127. }
  1128. public function getProductByItemCode($item_code,$user){
  1129. if(empty($item_code)) return [];
  1130. $model = Product::ProductClear($user,[]);
  1131. $product = $model->where('del_time',0)
  1132. ->whereIn('item_code', $item_code)
  1133. ->where('is_use', Product::is_use_one)
  1134. ->select('title','id','product_category_id','retail_price','product_attribute','item_code','size','unit','code','bar_code','product_category')
  1135. ->orderby('product_attribute', 'desc')
  1136. ->get()->toArray();
  1137. return $product;
  1138. }
  1139. public function fillProductPrice($detail_map, $value, $top_depart,$basic_map){
  1140. $tmp = [];
  1141. if(isset($detail_map[$value['id']])){
  1142. $d = $detail_map[$value['id']];
  1143. foreach ($d as $v){
  1144. if($top_depart['basic_type_id'] != $v['basic_type_id']) continue;
  1145. $tmp = [
  1146. 'basic_type_id' => $v['basic_type_id'],
  1147. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  1148. 'price' => $v['price']
  1149. ];
  1150. }
  1151. }
  1152. if(empty($tmp)) $tmp = (object)$tmp;
  1153. return $tmp;
  1154. }
  1155. public function updateTopStock($data, $user){
  1156. $limit_key = "updateTopStockFromYy";
  1157. // list($status, $msg) = $this->limitingSendRequestBackg($limit_key);
  1158. // if(! $status) return [false, '正在获取T9杭州总仓库存货库存,请勿重复操作'];
  1159. //总店
  1160. $head = $user['head']['id'] ?? 0;
  1161. $storehouse_id = Storehouse::where('top_depart_id', $head)->where('del_time',0)->value('id') ?? 0;
  1162. $time = time();
  1163. DB::table('product')
  1164. ->where('del_time', 0)
  1165. ->where('top_depart_id', $head)
  1166. ->select('id','code')
  1167. ->orderBy('id')
  1168. ->chunk(200, function ($data) use($head,$storehouse_id,$time){
  1169. $data = Collect($data)->map(function ($object) {
  1170. return (array)$object;
  1171. })->toArray();
  1172. $map = array_column($data,'id','code');
  1173. $id = array_unique(array_column($data,'id'));
  1174. $code = array_unique(array_column($data,'code'));
  1175. $post = [
  1176. 'urlFromT9' => 'getStock',
  1177. 'code' => $code,
  1178. 'warehouse' => '001',
  1179. ];
  1180. $post = json_encode($post);
  1181. $header = ['Content-Type:application/json'];
  1182. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1183. if($msg['code'] != 200) return [false, $msg['msg']];
  1184. $msg = $msg['data'] ?? [];
  1185. $insert = [];
  1186. foreach ($msg as $value){
  1187. $insert[] = [
  1188. 'product_id' => $map[$value['product_no']] ?? 0,
  1189. 'storehouse_id' => $storehouse_id,
  1190. 'top_depart_id' => $head,
  1191. 'number' => $value['number'],
  1192. 'crt_time' => $time,
  1193. ];
  1194. }
  1195. ProductInventoryOfTop::where('del_time',0)
  1196. ->whereIn('product_id',$id)
  1197. ->update(['del_time' => $time]);
  1198. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1199. });
  1200. return [true, ''];
  1201. try{
  1202. $job = ProcessDataJob::dispatch(['head' => $head, 'storehouse_id' => $storehouse_id, 'type' => U8Job::three,'limit_key' => $limit_key])->onQueue(U8Job::job3);
  1203. if(! $job) {
  1204. $this->dellimitingSendRequestBackg($limit_key);
  1205. return [false,'T9杭州总仓库存货库存任务没有进入队列!'];
  1206. }
  1207. }catch (\Throwable $e){
  1208. $this->dellimitingSendRequestBackg($limit_key);
  1209. return [false,$e->getMessage()];
  1210. }
  1211. return [true, 'T9杭州总仓库存货库存任务正在后台执行,请等待'];
  1212. }
  1213. private function returnStock($stock, $value){
  1214. if(isset($stock[$value['id']])){
  1215. $number = $stock[$value['id']];
  1216. if($number <= 0){
  1217. $stock = "售罄";
  1218. } elseif($number <= 15){
  1219. $stock = " 库存紧张";
  1220. }else{
  1221. $stock = " 库存充足";
  1222. }
  1223. }else{
  1224. $stock = "联系客服";
  1225. }
  1226. return $stock;
  1227. }
  1228. public function writeStock($data){
  1229. try {
  1230. $head = $data['head'];
  1231. $storehouse_id = $data['storehouse_id'];
  1232. $limit_key = $data['limit_key'];
  1233. DB::enableQueryLog();
  1234. $service = new U8ServerService(true);
  1235. if(! empty($service->error)) return [false, $service->error];
  1236. $time = time();
  1237. DB::table('product')
  1238. ->where('del_time', 0)
  1239. ->where('top_depart_id', $head)
  1240. ->select('id','code')
  1241. ->orderBy('id')
  1242. ->chunk(200, function ($data) use($head,$storehouse_id,$time){
  1243. $data = Collect($data)->map(function ($object) {
  1244. return (array)$object;
  1245. })->toArray();
  1246. $map = array_column($data,'id','code');
  1247. $id = array_unique(array_column($data,'id'));
  1248. $code = array_unique(array_column($data,'code'));
  1249. $post = [
  1250. 'urlFromT9' => 'getStock',
  1251. 'code' => $code,
  1252. 'warehouse' => '001',
  1253. ];
  1254. $post = json_encode($post);
  1255. $header = ['Content-Type:application/json'];
  1256. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1257. if($msg['code'] != 200) return [false, $msg['msg']];
  1258. $msg = $msg['data'] ?? [];
  1259. $insert = [];
  1260. foreach ($msg as $value){
  1261. $insert[] = [
  1262. 'product_id' => $map[$value['product_no']] ?? 0,
  1263. 'storehouse_id' => $storehouse_id,
  1264. 'top_depart_id' => $head,
  1265. 'number' => $value['number'],
  1266. 'crt_time' => $time,
  1267. ];
  1268. }
  1269. ProductInventoryOfTop::where('del_time',0)
  1270. ->whereIn('product_id',$id)
  1271. ->update(['del_time' => $time]);
  1272. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1273. });
  1274. DB::commit();
  1275. $this->dellimitingSendRequestBackg($limit_key);
  1276. }catch (\Throwable $exception){
  1277. DB::rollBack();
  1278. $this->dellimitingSendRequestBackg($limit_key);
  1279. return [false, $exception->getMessage()];
  1280. }
  1281. return [true, ''];
  1282. }
  1283. public function post_helper($url, $data, $header = [], $timeout = 20){
  1284. $ch = curl_init();
  1285. curl_setopt($ch, CURLOPT_URL, $url);
  1286. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1287. curl_setopt($ch, CURLOPT_ENCODING, '');
  1288. curl_setopt($ch, CURLOPT_POST, 1);
  1289. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  1290. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  1291. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1292. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1293. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1294. $r = curl_exec($ch);
  1295. if ($r === false) {
  1296. // 获取错误号
  1297. $errorNumber = curl_errno($ch);
  1298. // 获取错误信息
  1299. $errorMessage = curl_error($ch);
  1300. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  1301. return [false, $message];
  1302. }
  1303. curl_close($ch);
  1304. return [true, json_decode($r, true)];
  1305. }
  1306. }