ProductService.php 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  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 batchUploadImg($data){
  833. if(empty($data['product_id'])) return [false, '请选择产品'];
  834. $time = time();
  835. try {
  836. DB::beginTransaction();
  837. $old = ProductInfo::where('del_time',0)
  838. ->where('type',ProductInfo::type_one)
  839. ->whereIn('product_id',$data['product_id'])
  840. ->select('file')
  841. ->get()->toArray();
  842. $old = array_column($old,'file');
  843. ProductInfo::where('del_time',0)
  844. ->where('type',ProductInfo::type_one)
  845. ->whereIn('product_id',$data['product_id'])
  846. ->update(['del_time' => $time]);
  847. $new['origin'] = $data['img_url'] ?? "";
  848. $new['img_list'] = [];
  849. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  850. $img = explode('.', $img);
  851. $insert = [];
  852. if(! empty($data['img_url'])){
  853. foreach ($data['product_id'] as $key => $value){
  854. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  855. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  856. $insert[] = [
  857. 'product_id' => $value,
  858. 'file' => $copy,
  859. 'type' => ProductInfo::type_one,
  860. 'name' => $data['img_name'] ?? "",
  861. 'crt_time' => $time,
  862. ];
  863. $new['img_list'][] = $copy;
  864. }
  865. if(! empty($insert)) ProductInfo::insert($insert);
  866. }
  867. DB::commit();
  868. }catch (\Throwable $exception){
  869. DB::rollBack();
  870. return [false, $exception->getMessage()];
  871. }
  872. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  873. }
  874. public function batchBuildFee($data){
  875. if(empty($data['product_id'])) return [false, '请选择产品'];
  876. if(! isset($data['build_fee'])) return [false, '请填写安装费'];
  877. $res = $this->checkNumber($data['build_fee']);
  878. if(! $res) return [false,'安装费请输入不超过两位小数并且大于等于0的数值'];
  879. try {
  880. DB::beginTransaction();
  881. Product::where('del_time',0)
  882. ->whereIn('id', $data['product_id'])
  883. ->update([
  884. 'build_fee' => $data['build_fee']
  885. ]);
  886. DB::commit();
  887. }catch (\Throwable $exception){
  888. DB::rollBack();
  889. return [false, $exception->getMessage()];
  890. }
  891. return [true, ''];
  892. }
  893. //获取产品字典
  894. public function getProductDetail($product_id = []){
  895. if(empty($product_id)) return [];
  896. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  897. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  898. ->pluck('is_edit_unit_price','id')
  899. ->toArray();
  900. foreach ($pro as $key => $value){
  901. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  902. }
  903. return array_column($pro,null,'id');
  904. }
  905. //获取产品使用价格
  906. public function getProductPrice($product_id = [], $type = 1, $basic_type_id_for = 0){
  907. if(! is_array($product_id)) $product_id = [$product_id];
  908. //type 1 采购 2 合同 和 活动包
  909. $detail_map = [];
  910. $time = time();
  911. if($type == 1) {
  912. //供应商活动价格
  913. $activity = ProductActivityPrice::from('product_activity_price as a')
  914. ->join('product_activity as b','b.id','a.product_activity_id')
  915. ->where('a.del_time',0)
  916. ->whereIn('a.product_id',$product_id)
  917. ->where('a.start_time','<=',$time)
  918. ->where('a.end_time','>=',$time)
  919. ->where('b.type',ProductActivity::type_two)
  920. ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
  921. return $query->where('a.basic_type_id',$basic_type_id_for);
  922. })
  923. ->select('a.product_id','a.basic_type_id','a.price')
  924. ->get()->toArray();
  925. foreach ($activity as $value){
  926. $detail_map[$value['product_id']][] = $value;
  927. }
  928. //分社价 没有供应商活动价格使用分社价格
  929. $detail = ProductPriceDetail::where('del_time',0)
  930. ->whereIn('product_id',$product_id)
  931. ->when(! empty($basic_type_id_for), function ($query) use ($basic_type_id_for) {
  932. return $query->where('basic_type_id',$basic_type_id_for);
  933. })
  934. ->select('product_id','basic_type_id','price')
  935. ->get()->toArray();
  936. foreach ($detail as $value){
  937. if(! isset($detail_map[$value['product_id']][$value['basic_type_id']])){
  938. $detail_map[$value['product_id']][$value['basic_type_id']] = $value;
  939. }
  940. }
  941. }else{
  942. //零售活动价格
  943. $activity = ProductActivityPrice::from('product_activity_price as a')
  944. ->join('product_activity as b','b.id','a.product_activity_id')
  945. ->where('a.del_time',0)
  946. ->whereIn('a.product_id',$product_id)
  947. ->where('a.start_time','<=',$time)
  948. ->where('a.end_time','>=',$time)
  949. ->where('b.type',ProductActivity::type_one)
  950. ->select('a.product_id','a.basic_type_id','a.price')
  951. ->get()->toArray();
  952. foreach ($activity as $value){
  953. $detail_map[$value['product_id']][] = $value;
  954. }
  955. }
  956. return $detail_map;
  957. }
  958. public function getProductPriceTmp($product_id = []){
  959. //type 0 采购 1 合同
  960. //分社价
  961. $detail = ProductPriceDetail::where('del_time',0)
  962. ->whereIn('product_id',$product_id)
  963. ->select('product_id','basic_type_id','price')
  964. ->get()->toArray();
  965. $detail_map = [];
  966. foreach ($detail as $value){
  967. $detail_map[$value['product_id']][] = $value;
  968. }
  969. $return = $detail_map;
  970. //活动价格
  971. $time = time();
  972. $activity = ProductActivityPrice::where('del_time',0)
  973. ->whereIn('product_id',$product_id)
  974. ->where('start_time','<=',$time)
  975. ->where('end_time','>=',$time)
  976. ->select('product_id','basic_type_id','price')
  977. ->get()->toArray();
  978. if(! empty($activity)){
  979. foreach ($activity as $value){
  980. if(! isset($detail_map[$value['product_id']])) {
  981. $return[$value['product_id']][] = $value;
  982. }else{
  983. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  984. if(! in_array($value['basic_type_id'], $basic_type)){
  985. $return[$value['product_id']][] = $value;
  986. continue;
  987. }
  988. foreach ($detail_map[$value['product_id']] as $key => $val){
  989. if($value['basic_type_id'] == $val['basic_type_id']) {
  990. //活动价格替换
  991. $return[$value['product_id']][$key]['price'] = $value['price'];
  992. }
  993. }
  994. }
  995. }
  996. }unset($detail_map);
  997. return $return;
  998. }
  999. //获取产品分社价格
  1000. public function getProductPriceDetail($data, $user){
  1001. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  1002. $model = BasicType::TopClear($user,$data);
  1003. $basic = $model->where('del_time',0)
  1004. ->where('type',22)
  1005. ->select('title','id','type')
  1006. ->orderby('id', 'asc')->get()->toArray();
  1007. $detail = ProductPriceDetail::where('del_time',0)
  1008. ->whereIn('product_id',$data['id'])
  1009. ->select('product_id','basic_type_id','price')
  1010. ->get()->toArray();
  1011. $product_price = [];
  1012. //所有金额
  1013. foreach ($basic as $value){
  1014. $product_price[$value['id']] = "0";
  1015. }
  1016. $return = [];
  1017. foreach ($data['id'] as $value){
  1018. $return[$value] = $product_price;
  1019. }
  1020. //展示金额
  1021. foreach ($detail as $value){
  1022. if(isset($return[$value['product_id']][$value['basic_type_id']])) $return[$value['product_id']][$value['basic_type_id']] = $value['price'];
  1023. }
  1024. return $return;
  1025. }
  1026. public function productGroupByList($data,$user){
  1027. $model = Product::ProductClear($user,$data);
  1028. $model = $model->where('del_time',0)
  1029. ->where('item_code','<>',"")
  1030. ->select('title','id','product_category_id','product_category','retail_price','product_attribute','item_code','unit','size','code','bar_code')
  1031. ->where('is_use', Product::is_use_one)
  1032. ->groupBy('item_code')
  1033. ->orderByDesc('product_attribute')
  1034. ->orderByDesc('item_code');
  1035. // if(isset($data['from_wechat_program'])){
  1036. // //先根据大类分类排序 然后再根据子类分类排序 然后再根据产品属性排序
  1037. // $model = $model->orderByRaw("CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(product_category, ',', 1), '[', -1) AS UNSIGNED)")
  1038. // ->orderByRaw("CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(product_category, ',', 2), ',', -1) AS UNSIGNED)")
  1039. // ->orderByDesc('product_attribute')
  1040. // ->orderByDesc('item_code');
  1041. // }else{
  1042. // $model = $model->orderByDesc('product_attribute')
  1043. // ->orderByDesc('item_code');
  1044. // }
  1045. if(! empty($data['title_t'])) {
  1046. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1047. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
  1048. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1049. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
  1050. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  1051. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  1052. }
  1053. if(! empty($data['title'])){
  1054. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  1055. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  1056. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  1057. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  1058. // $model->where('title', 'LIKE', '%'.$data['title'].'%');
  1059. }
  1060. if(isset($data['state'])) $model->where('state', $data['state']);
  1061. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  1062. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  1063. if(! empty($data['product_category_array_id'])) $model->whereIn('product_category_id', $data['product_category_array_id']);
  1064. if(! empty($data['category'])){
  1065. $id = $this->getProductCateGory($data['category']);
  1066. $model->whereIn('product_category_id', $id);
  1067. }
  1068. if(! empty($data['product_category'])) {
  1069. $product_category = ProductCategory::where('del_time',0)
  1070. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  1071. ->select('id')
  1072. ->get()->toArray();
  1073. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  1074. }
  1075. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  1076. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  1077. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  1078. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  1079. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  1080. $model->where('crt_time','>=',$return[0]);
  1081. $model->where('crt_time','<=',$return[1]);
  1082. }
  1083. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  1084. $list = $this->limit($model,'',$data);
  1085. $list = $this->productGroupByListData($list,$user,$data);
  1086. return [true, $list];
  1087. }
  1088. public function productGroupByListData($data, $user, $search){
  1089. if(empty($data['data'])) return $data;
  1090. $type = $search['type'] ?? 1;
  1091. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  1092. ->select('title','id')
  1093. ->get()
  1094. ->toArray();
  1095. $category = array_column($category,null,'id');
  1096. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'], 'unit')))
  1097. ->OrWhere('type',BasicType::type_22)
  1098. ->pluck('title','id')
  1099. ->toArray();
  1100. //产品使用价格
  1101. $product_id = array_column($data['data'],'id');
  1102. $detail_map = $this->getProductPrice($product_id, $type);
  1103. //获取产品图片
  1104. $img = $this->getProductImg($product_id);
  1105. //当前门店
  1106. $top_depart = $user['depart_top'][0] ?? [];
  1107. $stock = ProductInventoryOfTop::where('del_time',0)
  1108. ->whereIn('product_id',$product_id)
  1109. ->pluck('number','product_id')
  1110. ->toArray();
  1111. foreach ($data['data'] as $key => $value){
  1112. $arr = json_decode($value['product_category'], true);
  1113. $arr = array_flip($arr);
  1114. if (isset($arr[112])) {
  1115. $data['data'][$key]['is_roll'] = true;
  1116. }else{
  1117. $data['data'][$key]['is_roll'] = false;
  1118. }
  1119. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  1120. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1121. $data['data'][$key]['product_price'] = $tmp;
  1122. $category_tmp = $category[$value['product_category_id']] ?? [];
  1123. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  1124. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1125. $data['data'][$key]['stock'] = $this->returnStock($stock, $value);
  1126. $data['data'][$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1127. }
  1128. return $data;
  1129. }
  1130. public function productGroupByListDetail($data, $user){
  1131. if(empty($data['item_code'])) return [false, '商品编码不能为空'];
  1132. $type = $data['type'] ?? 1;
  1133. //获取相同itemCode的产品
  1134. $product = $this->getProductByItemCode([$data['item_code']], $user);
  1135. $category = ProductCategory::whereIn('id',array_unique(array_column($product,'product_category_id')))
  1136. ->pluck('title','id')
  1137. ->toArray();
  1138. $basic_map = BasicType::whereIn('id',array_unique(array_column($product, 'unit')))
  1139. ->OrWhere('type',BasicType::type_22)
  1140. ->pluck('title','id')
  1141. ->toArray();
  1142. //产品使用价格
  1143. $product_id = array_column($product,'id');
  1144. $detail_map = $this->getProductPrice($product_id, $type);
  1145. //获取产品图片
  1146. $img = $this->getProductImg($product_id);
  1147. //当前门店
  1148. $top_depart = $user['depart_top'][0] ?? [];
  1149. $stock = ProductInventoryOfTop::where('del_time',0)
  1150. ->whereIn('product_id',$product_id)
  1151. ->pluck('number','product_id')
  1152. ->toArray();
  1153. foreach ($product as $key => $value){
  1154. $tmp = $this->fillProductPrice($detail_map, $value, $top_depart,$basic_map);
  1155. $product[$key]['product_price'] = $tmp;
  1156. $product[$key]['img'] = $img[$value['id']] ?? "";
  1157. $product[$key]['product_category_name'] = $category[$value['product_category_id']] ?? '';
  1158. $product[$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  1159. $product[$key]['stock'] = $this->returnStock($stock, $value);
  1160. $product[$key]['unit_title'] = $basic_map[$value['unit']] ?? "";
  1161. $arr = json_decode($value['product_category'], true);
  1162. $arr = array_flip($arr);
  1163. if (isset($arr[112])) {
  1164. $product[$key]['is_roll'] = true;
  1165. }else{
  1166. $product[$key]['is_roll'] = false;
  1167. }
  1168. }
  1169. return [true, $product];
  1170. }
  1171. public function getProductByItemCode($item_code,$user){
  1172. if(empty($item_code)) return [];
  1173. $model = Product::ProductClear($user,[]);
  1174. $product = $model->where('del_time',0)
  1175. ->whereIn('item_code', $item_code)
  1176. ->where('is_use', Product::is_use_one)
  1177. ->select('title','id','product_category_id','retail_price','product_attribute','item_code','size','unit','code','bar_code','product_category')
  1178. ->orderby('product_attribute', 'desc')
  1179. ->get()->toArray();
  1180. return $product;
  1181. }
  1182. public function fillProductPrice($detail_map, $value, $top_depart,$basic_map = []){
  1183. $tmp = [];
  1184. if(isset($detail_map[$value['id']])){
  1185. $d = $detail_map[$value['id']];
  1186. foreach ($d as $v){
  1187. if($top_depart['basic_type_id'] != $v['basic_type_id']) continue;
  1188. $tmp = [
  1189. 'basic_type_id' => $v['basic_type_id'],
  1190. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  1191. 'price' => $v['price']
  1192. ];
  1193. }
  1194. }
  1195. if(empty($tmp)) $tmp = (object)$tmp;
  1196. return $tmp;
  1197. }
  1198. public function updateTopStock($data, $user){
  1199. $limit_key = "updateTopStockFromYy";
  1200. // list($status, $msg) = $this->limitingSendRequestBackg($limit_key);
  1201. // if(! $status) return [false, '正在获取T9杭州总仓库存货库存,请勿重复操作'];
  1202. //总店
  1203. $head = $user['head']['id'] ?? 0;
  1204. $storehouse_id = Storehouse::where('top_depart_id', $head)->where('del_time',0)->value('id') ?? 0;
  1205. $time = time();
  1206. DB::table('product')
  1207. ->where('del_time', 0)
  1208. ->where('top_depart_id', $head)
  1209. ->select('id','code')
  1210. ->orderBy('id')
  1211. ->chunk(200, function ($data) use($head,$storehouse_id,$time){
  1212. $data = Collect($data)->map(function ($object) {
  1213. return (array)$object;
  1214. })->toArray();
  1215. $map = array_column($data,'id','code');
  1216. $id = array_unique(array_column($data,'id'));
  1217. $code = array_unique(array_column($data,'code'));
  1218. $post = [
  1219. 'urlFromT9' => 'getStock',
  1220. 'code' => $code,
  1221. 'warehouse' => '001',
  1222. ];
  1223. $post = json_encode($post);
  1224. $header = ['Content-Type:application/json'];
  1225. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1226. if($msg['code'] != 200) return [false, $msg['msg']];
  1227. $msg = $msg['data'] ?? [];
  1228. $insert = [];
  1229. foreach ($msg as $value){
  1230. $insert[] = [
  1231. 'product_id' => $map[$value['product_no']] ?? 0,
  1232. 'storehouse_id' => $storehouse_id,
  1233. 'top_depart_id' => $head,
  1234. 'number' => $value['number'],
  1235. 'crt_time' => $time,
  1236. ];
  1237. }
  1238. ProductInventoryOfTop::where('del_time',0)
  1239. ->whereIn('product_id',$id)
  1240. ->update(['del_time' => $time]);
  1241. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1242. });
  1243. return [true, ''];
  1244. try{
  1245. $job = ProcessDataJob::dispatch(['head' => $head, 'storehouse_id' => $storehouse_id, 'type' => U8Job::three,'limit_key' => $limit_key])->onQueue(U8Job::job3);
  1246. if(! $job) {
  1247. $this->dellimitingSendRequestBackg($limit_key);
  1248. return [false,'T9杭州总仓库存货库存任务没有进入队列!'];
  1249. }
  1250. }catch (\Throwable $e){
  1251. $this->dellimitingSendRequestBackg($limit_key);
  1252. return [false,$e->getMessage()];
  1253. }
  1254. return [true, 'T9杭州总仓库存货库存任务正在后台执行,请等待'];
  1255. }
  1256. private function returnStock($stock, $value){
  1257. if(isset($stock[$value['id']])){
  1258. $number = $stock[$value['id']];
  1259. if($number <= 0){
  1260. $stock = "售罄";
  1261. } elseif($number <= 15){
  1262. $stock = " 库存紧张";
  1263. }else{
  1264. $stock = " 库存充足";
  1265. }
  1266. }else{
  1267. $stock = "联系客服";
  1268. }
  1269. return $stock;
  1270. }
  1271. public function writeStock($data){
  1272. try {
  1273. $head = $data['head'];
  1274. $storehouse_id = $data['storehouse_id'];
  1275. $limit_key = $data['limit_key'];
  1276. DB::enableQueryLog();
  1277. $service = new U8ServerService(true);
  1278. if(! empty($service->error)) return [false, $service->error];
  1279. $time = time();
  1280. DB::table('product')
  1281. ->where('del_time', 0)
  1282. ->where('top_depart_id', $head)
  1283. ->select('id','code')
  1284. ->orderBy('id')
  1285. ->chunk(200, function ($data) use($head,$storehouse_id,$time){
  1286. $data = Collect($data)->map(function ($object) {
  1287. return (array)$object;
  1288. })->toArray();
  1289. $map = array_column($data,'id','code');
  1290. $id = array_unique(array_column($data,'id'));
  1291. $code = array_unique(array_column($data,'code'));
  1292. $post = [
  1293. 'urlFromT9' => 'getStock',
  1294. 'code' => $code,
  1295. 'warehouse' => '001',
  1296. ];
  1297. $post = json_encode($post);
  1298. $header = ['Content-Type:application/json'];
  1299. list($status, $msg) = $this->post_helper("https://workapi.qingyaokeji.com/api/updateTopStock",$post, $header);
  1300. if($msg['code'] != 200) return [false, $msg['msg']];
  1301. $msg = $msg['data'] ?? [];
  1302. $insert = [];
  1303. foreach ($msg as $value){
  1304. $insert[] = [
  1305. 'product_id' => $map[$value['product_no']] ?? 0,
  1306. 'storehouse_id' => $storehouse_id,
  1307. 'top_depart_id' => $head,
  1308. 'number' => $value['number'],
  1309. 'crt_time' => $time,
  1310. ];
  1311. }
  1312. ProductInventoryOfTop::where('del_time',0)
  1313. ->whereIn('product_id',$id)
  1314. ->update(['del_time' => $time]);
  1315. if(! empty($insert)) ProductInventoryOfTop::insert($insert);
  1316. });
  1317. DB::commit();
  1318. $this->dellimitingSendRequestBackg($limit_key);
  1319. }catch (\Throwable $exception){
  1320. DB::rollBack();
  1321. $this->dellimitingSendRequestBackg($limit_key);
  1322. return [false, $exception->getMessage()];
  1323. }
  1324. return [true, ''];
  1325. }
  1326. public function post_helper($url, $data, $header = [], $timeout = 20){
  1327. $ch = curl_init();
  1328. curl_setopt($ch, CURLOPT_URL, $url);
  1329. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1330. curl_setopt($ch, CURLOPT_ENCODING, '');
  1331. curl_setopt($ch, CURLOPT_POST, 1);
  1332. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  1333. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  1334. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1335. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1336. if(!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1337. $r = curl_exec($ch);
  1338. if ($r === false) {
  1339. // 获取错误号
  1340. $errorNumber = curl_errno($ch);
  1341. // 获取错误信息
  1342. $errorMessage = curl_error($ch);
  1343. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  1344. return [false, $message];
  1345. }
  1346. curl_close($ch);
  1347. return [true, json_decode($r, true)];
  1348. }
  1349. }