ProductService.php 64 KB

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