ProductService.php 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  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)){
  535. $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'];
  536. }
  537. $model = Product::ProductClear($user,$data);
  538. $model = $model->where('del_time',0)
  539. ->select($field)
  540. ->orderby('product_attribute', 'desc')
  541. ->orderby('id', 'desc');
  542. if(! empty($data['title_t'])) {
  543. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  544. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  545. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  546. $model->where(function ($query) use ($cleanTitle, $data) {
  547. $query->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  548. ->orWhere('code', 'LIKE', '%' . $data['title_t'] . '%')
  549. ->orWhere('size', 'LIKE', '%' . $data['title_t'] . '%');
  550. });
  551. }
  552. if(! empty($data['title'])) {
  553. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  554. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  555. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  556. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  557. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  558. }
  559. if(! empty($data['product_item_code_title'])){
  560. $item_code = ProductItemCodeMessage::where('del_time',0)
  561. ->where('title', 'LIKE', '%'.$data['product_item_code_title'].'%')
  562. ->select('item_code')
  563. ->get()->toArray();
  564. $model->whereIn('item_code', array_column($item_code,'item_code'));
  565. }
  566. if(isset($data['state'])) $model->where('state', $data['state']);
  567. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  568. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  569. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  570. if(! empty($data['category'])){
  571. $id = $this->getProductCateGory($data['category']);
  572. $model->whereIn('product_category_id', $id);
  573. }
  574. if(! empty($data['product_category'])) {
  575. $product_category = ProductCategory::where('del_time',0)
  576. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  577. ->select('id')
  578. ->get()->toArray();
  579. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  580. }
  581. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  582. if(! empty($data['item_code'])) $model->where('item_code', 'LIKE', '%'.$data['item_code'].'%');
  583. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  584. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  585. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  586. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  587. $model->where('crt_time','>=',$return[0]);
  588. $model->where('crt_time','<=',$return[1]);
  589. }
  590. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  591. return $model;
  592. }
  593. /**
  594. * 产品列表
  595. * @param $data
  596. * @param $user
  597. * @return array
  598. */
  599. public function productListIndex($data,$user){
  600. $model = $this->productCommon($data, $user);
  601. $list = $this->limit($model,'',$data);
  602. $list = $this->fillData($list,$user,$data);
  603. return [true, $list];
  604. }
  605. public function productList($data,$user){
  606. $model = Product::ProductClear($user,$data);
  607. $model = $model->where('del_time',0)
  608. ->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')
  609. ->where('is_use', Product::is_use_one)
  610. ->orderby('product_attribute', 'desc')
  611. ->orderby('id', 'desc');
  612. if(! empty($data['title_t'])) {
  613. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  614. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  615. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  616. $model->where(function ($query) use ($cleanTitle, $data) {
  617. $query->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  618. ->orWhere('code', 'LIKE', '%' . $data['title_t'] . '%')
  619. ->orWhere('size', 'LIKE', '%' . $data['title_t'] . '%');
  620. });
  621. }
  622. if(! empty($data['title'])){
  623. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  624. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  625. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  626. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  627. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  628. }
  629. if(isset($data['state'])) $model->where('state', $data['state']);
  630. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  631. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  632. if(! empty($data['category'])){
  633. $id = $this->getProductCateGory($data['category']);
  634. $model->whereIn('product_category_id', $id);
  635. }
  636. if(! empty($data['product_category'])) {
  637. $product_category = ProductCategory::where('del_time',0)
  638. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  639. ->select('id')
  640. ->get()->toArray();
  641. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  642. }
  643. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  644. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  645. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  646. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  647. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  648. $model->where('crt_time','>=',$return[0]);
  649. $model->where('crt_time','<=',$return[1]);
  650. }
  651. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  652. if(isset($data['is_head'])){
  653. if($data['is_head'] == 1){
  654. $head = $user['head']['id'] ?? 0;
  655. $model->where('top_depart_id', $head);
  656. }else{
  657. $top_depart_id = $user['depart_top'][0] ?? [];
  658. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  659. $model->where('top_depart_id', $top_depart_id);
  660. }
  661. }
  662. $list = $this->limit($model,'',$data);
  663. $list = $this->fillData($list,$user,$data);
  664. return [true, $list];
  665. }
  666. public function productList2($data,$user){
  667. $model = ProductCategory::ProductClear($user,$data);
  668. $model = $model->where('del_time',0)
  669. ->select('title','id','parent_id')
  670. ->orderby('id','desc');
  671. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  672. $list = $model->get()->toArray();
  673. if(! empty($list)) {
  674. $productList = Product::where('del_time',0)
  675. ->whereIn('product_category_id',array_column($list,'id'))
  676. ->select('id','product_category_id','title','code')
  677. ->get()->toArray();
  678. $productMap = [];
  679. foreach ($productList as $value){
  680. $productMap[$value['product_category_id']][] = $value;
  681. }
  682. foreach ($list as $key => $value){
  683. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  684. }
  685. $list = $this->makeTree(0,$list);
  686. $list = $this->set_sort_circle($list);
  687. }
  688. return [200, $list];
  689. }
  690. /**
  691. * 产品参数规则
  692. * @param $data
  693. * @param $is_add
  694. * @return array
  695. */
  696. public function productRule(&$data, $user, $is_add = true){
  697. if(empty($data['title'])) return [false,'产品名称不能为空'];
  698. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  699. if(empty($data['product_category'])) return [false,'产品分类树结构不能为空'];
  700. $data['product_category'] = json_encode($data['product_category']);
  701. if(empty($data['code'])) return [false,'产品编码不能为空'];
  702. if(! isset($data['cost'])) return [false, '请填写成本'];
  703. if(! isset($data['retail_price'])) return [false, '请填写零售价'];
  704. $res = $this->checkNumber($data['cost']);
  705. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  706. $res = $this->checkNumber($data['retail_price']);
  707. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于等于0的数值'];
  708. if(! empty($data['product_price'])){
  709. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  710. ->pluck('title','id')
  711. ->toArray();
  712. foreach ($data['product_price'] as $value){
  713. if(! empty($value['price'])) {
  714. $tmp = $map[$value['basic_type_id']] ?? '';
  715. $res = $this->checkNumber($value['price']);
  716. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  717. }
  718. }
  719. }
  720. //所属部门 以及 顶级部门
  721. if(empty($data['depart_id'])) {
  722. $data['depart_id'] = $this->getDepart($user);
  723. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  724. }
  725. //总社id
  726. $top_depart_id = $user['head'] ?? [];
  727. $top_depart_id = $top_depart_id['id'] ?? 0;
  728. if($is_add){
  729. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  730. ->where('del_time',0)
  731. ->exists();
  732. }else{
  733. if(empty($data['id'])) return [false,'ID不能为空'];
  734. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  735. ->where('id','<>',$data['id'])
  736. ->where('del_time',0)
  737. ->exists();
  738. }
  739. if($bool) return [false,'产品编码不能重复'];
  740. return [true, $data];
  741. }
  742. /**
  743. * 拼接数据
  744. * @param $data
  745. * @return array
  746. */
  747. public function fillData($data, $user, $search){
  748. if(empty($data['data'])) return $data;
  749. $type = $search['type'] ?? 0;
  750. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  751. ->pluck('emp_name','id')
  752. ->toArray();
  753. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  754. ->select('title','id','is_edit_unit_price')
  755. ->get()
  756. ->toArray();
  757. $category = array_column($category,null,'id');
  758. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  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_name'] = $basic_map[$value['unit']] ?? '';
  819. $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
  820. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  821. $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
  822. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  823. //库存
  824. $data['data'][$key]['stock'] = $stock_map[$value['id']] ?? (object)[];
  825. //成本
  826. $data['data'][$key]['cost_show'] = $value['cost'];
  827. $data['data'][$key]['product_item_code_title'] = $item_code_map[$value['item_code']] ?? "";
  828. }
  829. return $data;
  830. }
  831. public function getProductCateGory($category){
  832. $product_category = ProductCategory::where('del_time',0)
  833. ->select('id','parent_id')
  834. ->get()->toArray();
  835. $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
  836. return $result;
  837. }
  838. public function getProductImg($product_id = []){
  839. if(empty($product_id)) return [];
  840. $img = [];
  841. $customer_info = ProductInfo::where('del_time',0)
  842. ->whereIn('product_id',$product_id)
  843. ->where('type',ProductInfo::type_one)
  844. ->select('product_id','file','type','name')
  845. ->get()->toArray();
  846. $fileUploadService = new FileUploadService();
  847. foreach ($customer_info as $value){
  848. if(isset($img[$value['product_id']])) continue;
  849. $url = "";
  850. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  851. $img[$value['product_id']] = $url;
  852. }
  853. return $img;
  854. }
  855. public function getProductImgGroup($product_id = [],$search){
  856. if(empty($product_id)) return [];
  857. $img = [];
  858. $customer_info = ProductInfo::where('del_time',0)
  859. ->whereIn('product_id',$product_id)
  860. ->where('type',ProductInfo::type_one)
  861. ->select('product_id','file','type','name')
  862. ->get()->toArray();
  863. $fileUploadService = new FileUploadService();
  864. foreach ($customer_info as $value){
  865. if(empty($search['from_wechat_program'])){
  866. if(isset($img[$value['product_id']])) continue;
  867. $url = "";
  868. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  869. $img[$value['product_id']][] = $url;
  870. }else{
  871. $url = "";
  872. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  873. $img[$value['product_id']][] = $url;
  874. }
  875. }
  876. return $img;
  877. }
  878. public function batchUploadImg($data){
  879. if(empty($data['product_id'])) return [false, '请选择产品'];
  880. $time = time();
  881. try {
  882. DB::beginTransaction();
  883. $old = ProductInfo::where('del_time',0)
  884. ->where('type',ProductInfo::type_one)
  885. ->whereIn('product_id',$data['product_id'])
  886. ->select('file')
  887. ->get()->toArray();
  888. $old = array_column($old,'file');
  889. ProductInfo::where('del_time',0)
  890. ->where('type',ProductInfo::type_one)
  891. ->whereIn('product_id',$data['product_id'])
  892. ->update(['del_time' => $time]);
  893. $new['origin'] = $data['img_url'] ?? "";
  894. $new['img_list'] = [];
  895. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  896. $img = explode('.', $img);
  897. $insert = [];
  898. if(! empty($data['img_url'])){
  899. foreach ($data['product_id'] as $key => $value){
  900. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  901. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  902. $insert[] = [
  903. 'product_id' => $value,
  904. 'file' => $copy,
  905. 'type' => ProductInfo::type_one,
  906. 'name' => $data['img_name'] ?? "",
  907. 'crt_time' => $time,
  908. ];
  909. $new['img_list'][] = $copy;
  910. }
  911. if(! empty($insert)) ProductInfo::insert($insert);
  912. }
  913. DB::commit();
  914. }catch (\Throwable $exception){
  915. DB::rollBack();
  916. return [false, $exception->getMessage()];
  917. }
  918. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  919. }
  920. public function batchBuildFee($data){
  921. if(empty($data['product_id'])) return [false, '请选择产品'];
  922. if(! isset($data['build_fee'])) return [false, '请填写安装费'];
  923. $res = $this->checkNumber($data['build_fee']);
  924. if(! $res) return [false,'安装费请输入不超过两位小数并且大于等于0的数值'];
  925. try {
  926. DB::beginTransaction();
  927. Product::where('del_time',0)
  928. ->whereIn('id', $data['product_id'])
  929. ->update([
  930. 'build_fee' => $data['build_fee']
  931. ]);
  932. DB::commit();
  933. }catch (\Throwable $exception){
  934. DB::rollBack();
  935. return [false, $exception->getMessage()];
  936. }
  937. return [true, ''];
  938. }
  939. //获取产品字典
  940. public function getProductDetail($product_id = []){
  941. if(empty($product_id)) return [];
  942. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  943. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  944. ->pluck('is_edit_unit_price','id')
  945. ->toArray();
  946. foreach ($pro as $key => $value){
  947. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  948. }
  949. return array_column($pro,null,'id');
  950. }
  951. //获取产品使用价格
  952. public function getProductPrice($product_id = [], $type = 1, $basic_type_id_for = 0){
  953. if(! is_array($product_id)) $product_id = [$product_id];
  954. //type 1 采购 2 合同 和 活动包
  955. $detail_map = [];
  956. $time = time();
  957. if($type == 1) {
  958. //供应商活动价格
  959. $activity = ProductActivityPrice::from('product_activity_price as a')
  960. ->join('product_activity as b','b.id','a.product_activity_id')
  961. ->where('a.del_time',0)
  962. ->whereIn('a.product_id',$product_id)
  963. ->where('a.start_time','<=',$time)
  964. ->where('a.end_time','>=',$time)
  965. ->where('b.type',ProductActivity::type_two)
  966. ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
  967. return $query->where('a.basic_type_id',$basic_type_id_for);
  968. })
  969. ->select('a.product_id','a.basic_type_id','a.price')
  970. ->get()->toArray();
  971. foreach ($activity as $value){
  972. $detail_map[$value['product_id']][] = $value;
  973. }
  974. //分社价 没有供应商活动价格使用分社价格
  975. $detail = ProductPriceDetail::where('del_time',0)
  976. ->whereIn('product_id',$product_id)
  977. ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
  978. return $query->where('basic_type_id',$basic_type_id_for);
  979. })
  980. ->select('product_id','basic_type_id','price')
  981. ->get()->toArray();
  982. foreach ($detail as $value){
  983. if(! isset($detail_map[$value['product_id']][$value['basic_type_id']])){
  984. $detail_map[$value['product_id']][$value['basic_type_id']] = $value;
  985. }
  986. }
  987. }else{
  988. //零售活动价格
  989. $activity = ProductActivityPrice::from('product_activity_price as a')
  990. ->join('product_activity as b','b.id','a.product_activity_id')
  991. ->where('a.del_time',0)
  992. ->whereIn('a.product_id',$product_id)
  993. ->where('a.start_time','<=',$time)
  994. ->where('a.end_time','>=',$time)
  995. ->where('b.type',ProductActivity::type_one)
  996. ->select('a.product_id','a.basic_type_id','a.price')
  997. ->get()->toArray();
  998. foreach ($activity as $value){
  999. $detail_map[$value['product_id']][] = $value;
  1000. }
  1001. }
  1002. return $detail_map;
  1003. }
  1004. public function getProductPriceTmp($product_id = []){
  1005. //type 0 采购 1 合同
  1006. //分社价
  1007. $detail = ProductPriceDetail::where('del_time',0)
  1008. ->whereIn('product_id',$product_id)
  1009. ->select('product_id','basic_type_id','price')
  1010. ->get()->toArray();
  1011. $detail_map = [];
  1012. foreach ($detail as $value){
  1013. $detail_map[$value['product_id']][] = $value;
  1014. }
  1015. $return = $detail_map;
  1016. //活动价格
  1017. $time = time();
  1018. $activity = ProductActivityPrice::where('del_time',0)
  1019. ->whereIn('product_id',$product_id)
  1020. ->where('start_time','<=',$time)
  1021. ->where('end_time','>=',$time)
  1022. ->select('product_id','basic_type_id','price')
  1023. ->get()->toArray();
  1024. if(! empty($activity)){
  1025. foreach ($activity as $value){
  1026. if(! isset($detail_map[$value['product_id']])) {
  1027. $return[$value['product_id']][] = $value;
  1028. }else{
  1029. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  1030. if(! in_array($value['basic_type_id'], $basic_type)){
  1031. $return[$value['product_id']][] = $value;
  1032. continue;
  1033. }
  1034. foreach ($detail_map[$value['product_id']] as $key => $val){
  1035. if($value['basic_type_id'] == $val['basic_type_id']) {
  1036. //活动价格替换
  1037. $return[$value['product_id']][$key]['price'] = $value['price'];
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }unset($detail_map);
  1043. return $return;
  1044. }
  1045. //获取产品分社价格
  1046. public function getProductPriceDetail($data, $user){
  1047. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  1048. $model = BasicType::TopClear($user,$data);
  1049. $basic = $model->where('del_time',0)
  1050. ->where('type',22)
  1051. ->select('title','id','type')
  1052. ->orderby('id', 'asc')->get()->toArray();
  1053. $detail = ProductPriceDetail::where('del_time',0)
  1054. ->whereIn('product_id',$data['id'])
  1055. ->select('product_id','basic_type_id','price')
  1056. ->get()->toArray();
  1057. $product_price = [];
  1058. //所有金额
  1059. foreach ($basic as $value){
  1060. $product_price[$value['id']] = "0";
  1061. }
  1062. $return = [];
  1063. foreach ($data['id'] as $value){
  1064. $return[$value] = $product_price;
  1065. }
  1066. //展示金额
  1067. foreach ($detail as $value){
  1068. if(isset($return[$value['product_id']][$value['basic_type_id']])) $return[$value['product_id']][$value['basic_type_id']] = $value['price'];
  1069. }
  1070. return $return;
  1071. }
  1072. public function productGroupByList($data,$user){
  1073. $head_top_depart_id = $user['head']['id'] ?? 0;
  1074. $now_top_depart_id = $this->getDepart($user);
  1075. $p = ProductCategory::Special_for_p;
  1076. $model = Product::ProductClear($user,$data);
  1077. $model = $model->where('del_time',0)
  1078. ->where('item_code','<>',"")
  1079. ->where('is_use', Product::is_use_one)
  1080. ->when($head_top_depart_id != $now_top_depart_id, function ($query) use($p){
  1081. return $query->where('product_category','NOT LIKE', '%'.$p.'%');
  1082. })
  1083. ->select('title','id','product_category_id','product_category','retail_price','product_attribute','item_code','unit','size','code','bar_code')
  1084. ->groupBy('item_code')
  1085. ->orderByDesc('product_attribute')
  1086. ->orderByDesc('item_code');
  1087. // if(isset($data['from_wechat_program'])){
  1088. // //先根据大类分类排序 然后再根据子类分类排序 然后再根据产品属性排序
  1089. // $model = $model->orderByRaw("CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(product_category, ',', 1), '[', -1) AS UNSIGNED)")
  1090. // ->orderByRaw("CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(product_category, ',', 2), ',', -1) AS UNSIGNED)")
  1091. // ->orderByDesc('product_attribute')
  1092. // ->orderByDesc('item_code');
  1093. // }else{
  1094. // $model = $model->orderByDesc('product_attribute')
  1095. // ->orderByDesc('item_code');
  1096. // }
  1097. if(! empty($data['title_t'])) {
  1098. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1099. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  1100. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1101. $model->where(function ($query) use ($cleanTitle, $data) {
  1102. $query->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  1103. ->orWhere('code', 'LIKE', '%' . $data['title_t'] . '%')
  1104. ->orWhere('size', 'LIKE', '%' . $data['title_t'] . '%');
  1105. });
  1106. }
  1107. if(! empty($data['title'])){
  1108. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1109. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  1110. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1111. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  1112. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1113. }
  1114. if(isset($data['state'])) $model->where('state', $data['state']);
  1115. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  1116. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  1117. if(! empty($data['product_category_array_id'])) $model->whereIn('product_category_id', $data['product_category_array_id']);
  1118. if(! empty($data['category'])){
  1119. $id = $this->getProductCateGory($data['category']);
  1120. $model->whereIn('product_category_id', $id);
  1121. }
  1122. if(! empty($data['product_category'])) {
  1123. $product_category = ProductCategory::where('del_time',0)
  1124. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  1125. ->select('id')
  1126. ->get()->toArray();
  1127. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  1128. }
  1129. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  1130. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  1131. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  1132. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  1133. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1134. $model->where('crt_time','>=',$return[0]);
  1135. $model->where('crt_time','<=',$return[1]);
  1136. }
  1137. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  1138. $list = $this->limit($model,'',$data);
  1139. $list = $this->productGroupByListData($list,$user,$data);
  1140. return [true, $list];
  1141. }
  1142. public function productGroupByListData($data, $user, $search){
  1143. if(empty($data['data'])) return $data;
  1144. $type = $search['type'] ?? 1;
  1145. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  1146. ->select('title','id')
  1147. ->get()
  1148. ->toArray();
  1149. $category = array_column($category,null,'id');
  1150. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'], 'unit')))
  1151. ->OrWhere('type',BasicType::type_22)
  1152. ->pluck('title','id')
  1153. ->toArray();
  1154. //产品使用价格
  1155. $product_id = array_column($data['data'],'id');
  1156. $detail_map = $this->getProductPrice($product_id, $type);
  1157. //获取产品图片
  1158. $img = $this->getProductImgGroup($product_id,$search);
  1159. //当前门店
  1160. $top_depart = $user['depart_top'][0] ?? [];
  1161. //库存
  1162. $stock = ProductInventoryOfTop::where('del_time',0)
  1163. ->whereIn('product_id',$product_id)
  1164. ->pluck('number','product_id')
  1165. ->toArray();
  1166. //商品编码关联信息
  1167. $item_code_map = ProductItemCodeMessage::where('del_time',0)
  1168. ->whereIn('item_code', array_column($data['data'],'item_code'))
  1169. ->pluck('title','item_code')
  1170. ->toArray();
  1171. foreach ($data['data'] as $key => $value){
  1172. $arr = json_decode($value['product_category'], true);
  1173. $arr = array_flip($arr);
  1174. if (isset($arr[ProductCategory::Special_for_roll1]) || isset($arr[ProductCategory::Special_for_roll2])) {
  1175. $data['data'][$key]['is_roll'] = true;
  1176. $is_roll = true;
  1177. }else{
  1178. $data['data'][$key]['is_roll'] = false;
  1179. $is_roll = false;
  1180. }
  1181. $tmp = $img[$value['id']] ?? [];
  1182. $data['data'][$key]['img'] = $tmp[0] ?? "";
  1183. if(isset($tmp[0])) unset($tmp[0]);
  1184. $data['data'][$key]['img_detail'] = array_values($tmp);
  1185. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1186. $data['data'][$key]['product_price'] = $tmp;
  1187. $category_tmp = $category[$value['product_category_id']] ?? [];
  1188. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  1189. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1190. $data['data'][$key]['stock'] = $this->returnStock($stock, $value);
  1191. $data['data'][$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1192. $title = $value['title'];
  1193. if(! empty($item_code_map[$value['item_code']])) $title = $item_code_map[$value['item_code']];
  1194. $data['data'][$key]['title'] = $title;
  1195. //通用名称
  1196. $common_title = $item_code_map[$value['item_code']] ?? "";
  1197. //is_roll false 展示title true product_category_name
  1198. if($is_roll){
  1199. $category_tmp = $category[$value['product_category_id']] ?? [];
  1200. $category_title = $category_tmp['title'] ?? '';
  1201. if(! empty($common_title)) $category_title = $common_title;
  1202. $data['data'][$key]['product_category_name'] = $category_title;
  1203. }else{
  1204. $title = $value['title'];
  1205. if(! empty($common_title)) $title = $common_title;
  1206. $data['data'][$key]['title'] = $title;
  1207. }
  1208. }
  1209. return $data;
  1210. }
  1211. public function productGroupByListDetail($data, $user){
  1212. if(empty($data['item_code'])) return [false, '商品编码不能为空'];
  1213. $type = $data['type'] ?? 1;
  1214. //获取相同itemCode的产品
  1215. $product = $this->getProductByItemCode([$data['item_code']], $user);
  1216. $category = ProductCategory::whereIn('id',array_unique(array_column($product,'product_category_id')))
  1217. ->pluck('title','id')
  1218. ->toArray();
  1219. $basic_map = BasicType::whereIn('id',array_unique(array_column($product, 'unit')))
  1220. ->OrWhere('type',BasicType::type_22)
  1221. ->pluck('title','id')
  1222. ->toArray();
  1223. //产品使用价格
  1224. $product_id = array_column($product,'id');
  1225. $detail_map = $this->getProductPrice($product_id, $type);
  1226. //获取产品图片
  1227. $img = $this->getProductImg($product_id);
  1228. //当前门店
  1229. $top_depart = $user['depart_top'][0] ?? [];
  1230. $stock = ProductInventoryOfTop::where('del_time',0)
  1231. ->whereIn('product_id',$product_id)
  1232. ->pluck('number','product_id')
  1233. ->toArray();
  1234. foreach ($product as $key => $value){
  1235. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1236. $product[$key]['product_price'] = $tmp;
  1237. $product[$key]['img'] = $img[$value['id']] ?? "";
  1238. $product[$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  1239. $product[$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1240. $product[$key]['stock'] = $this->returnStock($stock, $value);
  1241. $product[$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1242. $arr = json_decode($value['product_category'], true);
  1243. $arr = array_flip($arr);
  1244. if (isset($arr[ProductCategory::Special_for_roll1]) || isset($arr[ProductCategory::Special_for_roll2])) {
  1245. $product[$key]['is_roll'] = true;
  1246. }else{
  1247. $product[$key]['is_roll'] = false;
  1248. }
  1249. }
  1250. return [true, $product];
  1251. }
  1252. public function productUpdateProductBasicInfo($data){
  1253. $time = time();
  1254. list($status, $msg) = $this->productUpdateProductBasicInfoRule($data,$time);
  1255. if(! $status) return [false, $msg];
  1256. //产品id 主表更新信息 子表插入信息 子表是否参与更新
  1257. list($product_id,$update,$insert_detail,$update_detail_bool) = $msg;
  1258. // dd($product_id,$update,$insert_detail,$update_detail_bool);
  1259. try {
  1260. DB::beginTransaction();
  1261. if(! empty($update)){
  1262. foreach ($product_id as $p_id){
  1263. Product::where('id',$p_id)
  1264. ->update($update);
  1265. }
  1266. }
  1267. if($update_detail_bool){
  1268. ProductPriceDetail::where('del_time',0)
  1269. ->whereIn('product_id',$product_id)
  1270. ->update(['del_time' => $time]);
  1271. if(! empty($insert_detail)) ProductPriceDetail::insert($insert_detail);
  1272. }
  1273. DB::commit();
  1274. }catch (\Throwable $exception){
  1275. DB::rollBack();
  1276. return [false, $exception->getMessage()];
  1277. }
  1278. return [true, ''];
  1279. }
  1280. public function productUpdateProductBasicInfoRule($data,$time){
  1281. if(empty($data['product_id'])) return [false, '产品ID不能为空'];
  1282. $product = Product::where('del_time',0)
  1283. ->whereIn('id',$data['product_id'])
  1284. ->select('id')
  1285. ->get()->toArray();
  1286. if(empty($product)) return [false, '产品不存在或已被删除'];
  1287. $product = array_column($product,'id');
  1288. // 产品分类 单位 成本 各分社价 商品编码 产品属性
  1289. $update = $update_detail = [];
  1290. if(! empty($data['product_category_id'])){
  1291. $update['product_category_id'] = $data['product_category_id'];
  1292. if(empty($data['product_category'])) return [false,'产品分类树结构不能为空'];
  1293. $product_category = json_encode($data['product_category']);
  1294. $update['product_category'] = $product_category;
  1295. }
  1296. if(Arr::has($data,'unit')) $update['unit'] = $data['unit'] ?? 0;
  1297. if(Arr::has($data,'cost')){
  1298. $cost = $data['cost'] ?? 0;
  1299. $res = $this->checkNumber($cost);
  1300. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  1301. $update['cost'] = $cost;
  1302. }
  1303. if(Arr::has($data,'item_code')) $update['item_code'] = $data['item_code'] ?? "";
  1304. if(Arr::has($data,'product_attribute')) $update['product_attribute'] = $data['product_attribute'] ?? 0;
  1305. if(! empty($data['product_price'])){
  1306. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  1307. ->pluck('title','id')
  1308. ->toArray();
  1309. foreach ($data['product_price'] as $value){
  1310. $price = $value['price'] ?? 0;
  1311. $tmp = $map[$value['basic_type_id']] ?? '';
  1312. if(! $tmp) return [false, '分社价格错误'];
  1313. $res = $this->checkNumber($price);
  1314. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  1315. $update_detail[] = [
  1316. 'product_id' => 0,
  1317. 'basic_type_id' => $value['basic_type_id'],
  1318. 'price' => $price,
  1319. 'crt_time' => $time,
  1320. ];
  1321. }
  1322. }
  1323. if(empty($update) && empty($update_detail)) return [false, '暂无更新'];
  1324. $update_detail_bool = false;
  1325. //详情信息
  1326. $insert_detail = [];
  1327. if(! empty($update_detail)){
  1328. $update_detail_bool = true;
  1329. foreach ($product as $p_id){
  1330. foreach ($update_detail as $detail){
  1331. $detail['product_id'] = $p_id;
  1332. $insert_detail[] = $detail;
  1333. }
  1334. }
  1335. }
  1336. return [true, [$product, $update, $insert_detail, $update_detail_bool]];
  1337. }
  1338. public function productUpdateByItemCode($data){
  1339. if(empty($data['item_code'])) return [false, "商品编码不能为空"];
  1340. $title = $data['title'] ?? '';
  1341. try {
  1342. DB::beginTransaction();
  1343. $time = time();
  1344. ProductItemCodeMessage::where('del_time',0)
  1345. ->where('item_code', $data['item_code'])
  1346. ->update(['del_time' => $time]);
  1347. ProductItemCodeMessage::insert([
  1348. 'item_code' => $data['item_code'],
  1349. 'title' => $title,
  1350. ]);
  1351. DB::commit();
  1352. }catch (\Throwable $exception){
  1353. DB::rollBack();
  1354. return [false, $exception->getMessage()];
  1355. }
  1356. return [true, ''];
  1357. }
  1358. public function getProductByItemCode($item_code,$user){
  1359. if(empty($item_code)) return [];
  1360. $model = Product::ProductClear($user,[]);
  1361. $product = $model->where('del_time',0)
  1362. ->whereIn('item_code', $item_code)
  1363. ->where('is_use', Product::is_use_one)
  1364. ->select('title','id','product_category_id','retail_price','product_attribute','item_code','size','unit','code','bar_code','product_category')
  1365. ->orderby('product_attribute', 'desc')
  1366. ->get()->toArray();
  1367. return $product;
  1368. }
  1369. public function fillProductPrice($detail_map, $value, $top_depart,$basic_map = []){
  1370. $tmp = [];
  1371. if(isset($detail_map[$value['id']])){
  1372. $d = $detail_map[$value['id']];
  1373. foreach ($d as $v){
  1374. if($top_depart['basic_type_id'] != $v['basic_type_id']) continue;
  1375. $tmp = [
  1376. 'basic_type_id' => $v['basic_type_id'],
  1377. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  1378. 'price' => $v['price']
  1379. ];
  1380. }
  1381. }
  1382. if(empty($tmp)) $tmp = (object)$tmp;
  1383. return $tmp;
  1384. }
  1385. public function updateTopStock($data, $user){
  1386. $limit_key = "updateTopStockFromYy";
  1387. // list($status, $msg) = $this->limitingSendRequestBackg($limit_key);
  1388. // if(! $status) return [false, '正在获取T9杭州总仓库存货库存,请勿重复操作'];
  1389. //总店
  1390. $head = $user['head']['id'] ?? 0;
  1391. $storehouse_id = Storehouse::where('top_depart_id', $head)->where('del_time',0)->value('id') ?? 0;
  1392. $service = new U8ServerService(true);
  1393. if(! empty($service->error)) return [false, $service->error];
  1394. $time = time();
  1395. DB::table('product')
  1396. ->where('del_time', 0)
  1397. ->where('top_depart_id', $head)
  1398. ->select('id','code')
  1399. ->orderBy('id')
  1400. ->chunk(200, function ($data) use($head,$storehouse_id,$time,$service){
  1401. $data = Collect($data)->map(function ($object) {
  1402. return (array)$object;
  1403. })->toArray();
  1404. $map = array_column($data,'id','code');
  1405. $id = array_unique(array_column($data,'id'));
  1406. $code = array_unique(array_column($data,'code'));
  1407. list($status, $msg) = $service->getStock($code, "001");
  1408. if(! $status) return [false, $msg];
  1409. // $post = [
  1410. // 'urlFromT9' => 'getStock',
  1411. // 'code' => $code,
  1412. // 'warehouse' => '001',
  1413. // ];
  1414. // $post = json_encode($post);
  1415. // $header = ['Content-Type:application/json'];
  1416. // list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1417. // if($msg['code'] != 200) return [false, $msg['msg']];
  1418. // $msg = $msg['data'] ?? [];
  1419. $insert = [];
  1420. foreach ($msg as $value){
  1421. $insert[] = [
  1422. 'product_id' => $map[$value['product_no']] ?? 0,
  1423. 'storehouse_id' => $storehouse_id,
  1424. 'top_depart_id' => $head,
  1425. 'number' => $value['number'],
  1426. 'crt_time' => $time,
  1427. ];
  1428. }
  1429. ProductInventoryOfTop::where('del_time',0)
  1430. ->whereIn('product_id',$id)
  1431. ->update(['del_time' => $time]);
  1432. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1433. });
  1434. return [true, ''];
  1435. try{
  1436. $job = ProcessDataJob::dispatch(['head' => $head, 'storehouse_id' => $storehouse_id, 'type' => U8Job::three,'limit_key' => $limit_key])->onQueue(U8Job::job3);
  1437. if(! $job) {
  1438. $this->dellimitingSendRequestBackg($limit_key);
  1439. return [false,'T9杭州总仓库存货库存任务没有进入队列!'];
  1440. }
  1441. }catch (\Throwable $e){
  1442. $this->dellimitingSendRequestBackg($limit_key);
  1443. return [false,$e->getMessage()];
  1444. }
  1445. return [true, 'T9杭州总仓库存货库存任务正在后台执行,请等待'];
  1446. }
  1447. private function returnStock($stock, $value){
  1448. if(isset($stock[$value['id']])){
  1449. $number = $stock[$value['id']];
  1450. if($number <= 0){
  1451. $stock = "售罄";
  1452. } elseif($number <= 15){
  1453. $stock = " 库存紧张";
  1454. }else{
  1455. $stock = " 库存充足";
  1456. }
  1457. }else{
  1458. $stock = "联系客服";
  1459. }
  1460. return $stock;
  1461. }
  1462. public function writeStock($data){
  1463. try {
  1464. $head = $data['head'];
  1465. $storehouse_id = $data['storehouse_id'];
  1466. $limit_key = $data['limit_key'];
  1467. DB::enableQueryLog();
  1468. $service = new U8ServerService(true);
  1469. if(! empty($service->error)) return [false, $service->error];
  1470. $time = time();
  1471. DB::table('product')
  1472. ->where('del_time', 0)
  1473. ->where('top_depart_id', $head)
  1474. ->select('id','code')
  1475. ->orderBy('id')
  1476. ->chunk(200, function ($data) use($head,$storehouse_id,$time,$service){
  1477. $data = Collect($data)->map(function ($object) {
  1478. return (array)$object;
  1479. })->toArray();
  1480. $map = array_column($data,'id','code');
  1481. $id = array_unique(array_column($data,'id'));
  1482. $code = array_unique(array_column($data,'code'));
  1483. list($status, $msg) = $service->getStock($code, "001");
  1484. if(! $status) return [false, $msg];
  1485. // $post = [
  1486. // 'urlFromT9' => 'getStock',
  1487. // 'code' => $code,
  1488. // 'warehouse' => '001',
  1489. // ];
  1490. // $post = json_encode($post);
  1491. // $header = ['Content-Type:application/json'];
  1492. // list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1493. // if($msg['code'] != 200) return [false, $msg['msg']];
  1494. // $msg = $msg['data'] ?? [];
  1495. $insert = [];
  1496. foreach ($msg as $value){
  1497. $insert[] = [
  1498. 'product_id' => $map[$value['product_no']] ?? 0,
  1499. 'storehouse_id' => $storehouse_id,
  1500. 'top_depart_id' => $head,
  1501. 'number' => $value['number'],
  1502. 'crt_time' => $time,
  1503. ];
  1504. }
  1505. ProductInventoryOfTop::where('del_time',0)
  1506. ->whereIn('product_id',$id)
  1507. ->update(['del_time' => $time]);
  1508. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1509. });
  1510. DB::commit();
  1511. $this->dellimitingSendRequestBackg($limit_key);
  1512. }catch (\Throwable $exception){
  1513. DB::rollBack();
  1514. $this->dellimitingSendRequestBackg($limit_key);
  1515. return [false, $exception->getMessage()];
  1516. }
  1517. return [true, ''];
  1518. }
  1519. }