ProductService.php 70 KB

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