ProductService.php 70 KB

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