ProductService.php 55 KB

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