ProductService.php 72 KB

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