ProductService.php 61 KB

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