ProductService.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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\ProductPriceDetail;
  13. use App\Model\Role;
  14. use App\Model\RoleMenuButton;
  15. use App\Model\SeeRange;
  16. use Illuminate\Support\Facades\DB;
  17. /**
  18. * 产品管理
  19. */
  20. class ProductService extends Service
  21. {
  22. public function setIsEditUnitPrice($data,$user){
  23. if($this->isEmpty($data,'id')) return [false,'请选择分类!'];
  24. if(! isset($data['is_edit_unit_price'])) return [false,'请选择受否允许修改采购单价!'];
  25. $is_edit_unit_price = $data['is_edit_unit_price'] > 0 ? 1 : 0;
  26. $all_id = $this->getProductCateGory($data['id']);
  27. ProductCategory::whereIn('id',$all_id)->update([
  28. 'is_edit_unit_price' => $is_edit_unit_price
  29. ]);
  30. return [true, ''];
  31. }
  32. /**
  33. * 产品分类编辑
  34. * @param $data
  35. * @param $user
  36. * @return array
  37. */
  38. public function productCategoryEdit($data,$user){
  39. list($status,$msg) = $this->productCategoryRule($data,$user,false);
  40. if(!$status) return [$status,$msg];
  41. $update = $msg['data'][0];
  42. $model = new ProductCategory();
  43. $model->where('id',$data['id'])->update($update);
  44. return [true,''];
  45. }
  46. /**
  47. * 产品分类新增
  48. * @param $data
  49. * @param $user
  50. * @return array
  51. */
  52. public function productCategoryAdd($data,$user){
  53. list($status,$msg) = $this->productCategoryRule($data,$user);
  54. if(!$status) return [$status,$msg];
  55. ProductCategory::insert($msg['data']);
  56. return [true,''];
  57. }
  58. /**
  59. * 产品分类删除
  60. * @param $data
  61. * @return array
  62. */
  63. public function productCategoryDel($data){
  64. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  65. $bool = Product::where('del_time',0)
  66. ->where('product_category_id',$data['id'])
  67. ->exists();
  68. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  69. try {
  70. DB::beginTransaction();
  71. ProductCategory::where('id',$data['id'])->update([
  72. 'del_time' => time()
  73. ]);
  74. DB::commit();
  75. }catch (\Exception $exception){
  76. DB::rollBack();
  77. return [false,$exception->getMessage()];
  78. }
  79. return [true,''];
  80. }
  81. /**
  82. * 产品分类列表
  83. * @param $data
  84. * @param $user
  85. * @return array
  86. */
  87. public function productCategoryList($data,$user){
  88. $model = ProductCategory::TopClear($user,$data);
  89. $model = $model->where('del_time',0)
  90. ->select('title','id','parent_id','is_edit_unit_price')
  91. ->orderby('id','asc');
  92. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  93. if(isset($data['is_edit_unit_price'])) $model->where('is_edit_unit_price', $data['is_edit_unit_price']);
  94. $list = $model->get()->toArray();
  95. foreach ($list as $key => $value){
  96. $list[$key]['is_edit_unit_price_title'] = ProductCategory::$is_edit_unit_price[$value['is_edit_unit_price']] ?? "";
  97. }
  98. $list_tree = $list;
  99. if(! empty($list_tree)) {
  100. $list_tree = $this->makeTree(0,$list_tree);
  101. $list_tree = $this->set_sort_circle($list_tree);
  102. }
  103. return [200, ['data' => $list,'tree' => $list_tree]];
  104. }
  105. public function productCategoryList2($data,$user){
  106. $head_top_depart_id = $user['head']['id'] ?? 0;
  107. $now_top_depart_id = $this->getDepart($user);
  108. //总社分类
  109. $head = ProductCategory::where('del_time',0)
  110. ->where('top_depart_id', $head_top_depart_id)
  111. ->select('title','id','parent_id')
  112. ->orderby('id','asc')
  113. ->get()->toArray();
  114. $head_tree = [];
  115. if(! empty($head)) {
  116. $head_tree = $this->makeTree(0,$head);
  117. $head_tree = $this->set_sort_circle($head_tree);
  118. }
  119. //我的
  120. $my_tree = [];
  121. if($head_top_depart_id != $now_top_depart_id){
  122. $model = ProductCategory::TopClear($user,$data);
  123. $model = $model->where('del_time',0)
  124. ->select('title','id','parent_id')
  125. ->orderby('id','asc');
  126. $list = $model->get()->toArray();
  127. if(! empty($list)) {
  128. $my_tree = $this->makeTree(0,$list);
  129. $my_tree = $this->set_sort_circle($my_tree);
  130. }
  131. }
  132. return [200, ['head_tree' => $head_tree, 'my_tree' => $my_tree]];
  133. }
  134. /**
  135. * 产品分类参数规则
  136. * @param $data
  137. * @param $is_add
  138. * @return array
  139. */
  140. public function productCategoryRule($data,$user, $is_add = true){
  141. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  142. //所属部门 以及 顶级部门
  143. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  144. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  145. $title = array_column($data['data'],'title');
  146. $title = array_map(function($val) {
  147. return $val !== null ? $val : 0;
  148. }, $title);
  149. $title_count = array_count_values($title);
  150. foreach ($title as $value){
  151. if(empty($value)) return [false,'名称不能为空!'];
  152. if($title_count[$value] > 1) return [false,'名称不能重复'];
  153. }
  154. foreach ($data['data'] as $key => $value){
  155. $data['data'][$key]['upd_time'] = time();
  156. if($is_add){
  157. $parent_id = 0;
  158. if(! empty($value['parent_id'])) {
  159. $bool = Product::where('del_time',0)
  160. ->where('product_category_id',$value['parent_id'])
  161. ->exists();
  162. if($bool) {
  163. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  164. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  165. }
  166. $parent_id = $value['parent_id'];
  167. }
  168. $data['data'][$key]['parent_id'] = $parent_id;
  169. $data['data'][$key]['crt_time'] = time();
  170. $data['data'][$key]['depart_id'] = $data['depart_id'];
  171. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  172. $bool = ProductCategory::where('title',$value['title'])
  173. ->where('top_depart_id',$data['top_depart_id'])
  174. ->where('del_time',0)
  175. ->exists();
  176. }else{
  177. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  178. $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id');
  179. $bool = ProductCategory::where('title',$value['title'])
  180. ->where('top_depart_id',$top_depart_id)
  181. ->where('id','<>',$data['id'])
  182. ->where('del_time',0)
  183. ->exists();
  184. }
  185. if($bool) return [false,'分类名称不能重复'];
  186. }
  187. return [true, $data];
  188. }
  189. /**
  190. * 产品编辑
  191. * @param $data
  192. * @param $user
  193. * @return array
  194. */
  195. public function productEdit($data,$user){
  196. list($status,$msg) = $this->productRule($data, $user, false);
  197. if(!$status) return [$status,$msg];
  198. try {
  199. DB::beginTransaction();
  200. $model = Product::where('id',$data['id'])->first();
  201. $model->product_category_id = $data['product_category_id'] ?? 0;
  202. $model->product_category = $data['product_category'] ?? '';
  203. $model->title = $data['title'];
  204. $model->code = $data['code'] ?? '';
  205. $model->warranty_time = $data['warranty_time'] ?? 0;
  206. $model->install_time = $data['install_time'] ?? 0;
  207. $model->size = $data['size'] ?? '';
  208. $model->unit = $data['unit'] ?? 0;
  209. $model->bar_code = $data['bar_code'] ?? '';
  210. $model->cost = $data['cost'] ?? 0;
  211. $model->retail_price = $data['retail_price'] ?? 0;
  212. $model->build_fee = $data['build_fee'] ?? 0;
  213. $model->mark = $data['mark'] ?? '';
  214. $model->state = $data['state'] ?? 0;
  215. $model->product_attribute = $data['product_attribute'] ?? 0;
  216. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  217. $model->save();
  218. $time = time();
  219. ProductIntroduction::where('product_id',$data['id'])
  220. ->where('del_time',0)
  221. ->update(['del_time' => $time]);
  222. if(! empty($data['introduction'])){
  223. $models = new ProductIntroduction();
  224. $models->product_id = $model->id;
  225. $models->introduction = $data['introduction'];
  226. $models->save();
  227. }
  228. $old = ProductInfo::where('del_time',0)
  229. ->where('product_id',$data['id'])
  230. ->select('file')
  231. ->get()->toArray();
  232. $old = array_column($old,'file');
  233. ProductInfo::where('del_time',0)
  234. ->where('product_id',$data['id'])
  235. ->update(['del_time' => $time]);
  236. $new = [];
  237. if(! empty($data['img'])){
  238. $insert = [];
  239. foreach ($data['img'] as $value){
  240. $insert[] = [
  241. 'product_id' => $model->id,
  242. 'file' => $value['url'],
  243. 'type' => ProductInfo::type_one,
  244. 'name' => $value['name'],
  245. 'crt_time' => $time,
  246. ];
  247. if(in_array($value['url'], $old)) {
  248. foreach ($old as $o_k => $o_v){
  249. if($o_v == $value['url']) unset($old[$o_k]);
  250. }
  251. }else{
  252. $new[] = $value['url'];
  253. }
  254. }
  255. ProductInfo::insert($insert);
  256. }
  257. if(! empty($data['file'])){
  258. $insert = [];
  259. foreach ($data['file'] as $value){
  260. $insert[] = [
  261. 'product_id' => $model->id,
  262. 'file' => $value['url'],
  263. 'type' => ProductInfo::type_two,
  264. 'name' => $value['name'],
  265. 'crt_time' => $time,
  266. ];
  267. if(in_array($value['url'], $old)) {
  268. foreach ($old as $o_k => $o_v){
  269. if($o_v == $value['url']) unset($old[$o_k]);
  270. }
  271. }else{
  272. $new[] = $value['url'];
  273. }
  274. }
  275. ProductInfo::insert($insert);
  276. }
  277. ProductPriceDetail::where('del_time',0)
  278. ->where('product_id',$data['id'])
  279. ->update(['del_time' => $time]);
  280. if(! empty($data['product_price'])){
  281. $insert = [];
  282. foreach ($data['product_price'] as $value){
  283. $insert[] = [
  284. 'product_id' => $model->id,
  285. 'basic_type_id' => $value['basic_type_id'],
  286. 'price' => $value['price'] ?? 0,
  287. 'crt_time' => $time,
  288. ];
  289. }
  290. ProductPriceDetail::insert($insert);
  291. }
  292. DB::commit();
  293. }catch (\Exception $exception){
  294. DB::rollBack();
  295. return [false,$exception->getMessage()];
  296. }
  297. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  298. }
  299. /**
  300. * 产品新增
  301. * @param $data
  302. * @param $user
  303. * @return array
  304. */
  305. public function productAdd($data,$user){
  306. list($status,$msg) = $this->productRule($data, $user);
  307. if(!$status) return [$status,$msg];
  308. try {
  309. DB::beginTransaction();
  310. $model = new Product();
  311. $model->product_category_id = $data['product_category_id'] ?? 0;
  312. $model->product_category = $data['product_category'] ?? '';
  313. $model->title = $data['title'];
  314. $model->code = $data['code'] ?? '';
  315. $model->warranty_time = $data['warranty_time'] ?? 0;
  316. $model->install_time = $data['install_time'] ?? 0;
  317. $model->size = $data['size'] ?? '';
  318. $model->unit = $data['unit'] ?? 0;
  319. $model->bar_code = $data['bar_code'] ?? '';
  320. $model->cost = $data['cost'] ?? 0;
  321. $model->retail_price = $data['retail_price'] ?? 0;
  322. $model->build_fee = $data['build_fee'] ?? 0;
  323. $model->mark = $data['mark'] ?? '';
  324. $model->state = $data['state'] ?? 0;
  325. $model->product_attribute = $data['product_attribute'] ?? 0;
  326. $model->crt_id = $user['id'];
  327. $model->depart_id = $data['depart_id'] ?? 0;
  328. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  329. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  330. $model->save();
  331. $time = time();
  332. if(! empty($data['introduction'])){
  333. $models = new ProductIntroduction();
  334. $models->product_id = $model->id;
  335. $models->introduction = $data['introduction'];
  336. $models->save();
  337. }
  338. $new = [];
  339. if(! empty($data['img'])){
  340. $insert = [];
  341. foreach ($data['img'] as $value){
  342. $insert[] = [
  343. 'product_id' => $model->id,
  344. 'file' => $value['url'],
  345. 'type' => ProductInfo::type_one,
  346. 'name' => $value['name'],
  347. 'crt_time' => $time,
  348. ];
  349. if(! empty($value['url'])) $new[] = $value['url'];
  350. }
  351. ProductInfo::insert($insert);
  352. }
  353. if(! empty($data['file'])){
  354. $insert = [];
  355. foreach ($data['file'] as $value){
  356. $insert[] = [
  357. 'product_id' => $model->id,
  358. 'file' => $value['url'],
  359. 'type' => ProductInfo::type_two,
  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['product_price'])){
  368. $insert = [];
  369. foreach ($data['product_price'] as $value){
  370. $insert[] = [
  371. 'product_id' => $model->id,
  372. 'basic_type_id' => $value['basic_type_id'],
  373. 'price' => $value['price'] ?? 0,
  374. 'crt_time' => $time,
  375. ];
  376. }
  377. ProductPriceDetail::insert($insert);
  378. }
  379. DB::commit();
  380. }catch (\Exception $exception){
  381. DB::rollBack();
  382. return [false,$exception->getMessage()];
  383. }
  384. return [true, ['file' => ['new' => $new]]];
  385. }
  386. /**
  387. * 产品删除
  388. * @param $data
  389. * @return array
  390. */
  391. public function productDel($data){
  392. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  393. try {
  394. DB::beginTransaction();
  395. $time = time();
  396. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  397. ProductIntroduction::where('product_id',$data['id'])
  398. ->where('del_time',0)
  399. ->update(['del_time' => $time]);
  400. $old = ProductInfo::where('del_time',0)
  401. ->where('product_id',$data['id'])
  402. ->select('file')
  403. ->get()->toArray();
  404. $old = array_column($old,'file');
  405. ProductInfo::where('del_time',0)
  406. ->where('product_id',$data['id'])
  407. ->update(['del_time' => $time]);
  408. ProductPriceDetail::where('del_time',0)
  409. ->where('product_id',$data['id'])
  410. ->update(['del_time' => $time]);
  411. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  412. DB::commit();
  413. }catch (\Exception $exception){
  414. DB::rollBack();
  415. return [false,$exception->getMessage()];
  416. }
  417. return [true, ['file' => ['old' => $old]]];
  418. }
  419. /**
  420. * 产品详情
  421. * @param $data
  422. * @param $user
  423. * @return array
  424. */
  425. public function productDetail($data,$user){
  426. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  427. $customer = Product::where('del_time',0)
  428. ->where('id',$data['id'])
  429. ->first();
  430. if(empty($customer)) return [false,'产品不存在或已被删除'];
  431. $customer = $customer->toArray();
  432. $customer['product_attribute_title'] = Product::$product_attribute[$customer['product_attribute']] ?? "";
  433. $customer['is_use_title'] = Product::$is_use[$customer['is_use']] ?? "";
  434. $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
  435. $category = ProductCategory::where('id',$customer['product_category_id'])
  436. ->value('title');
  437. $customer['product_category_title'] = $category;
  438. $customer['introduction'] = "";
  439. $in = ProductIntroduction::where('del_time',0)
  440. ->where('product_id',$data['id'])
  441. ->first();
  442. if(! empty($in)) $customer['introduction'] = $in->introduction;
  443. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  444. $model = BasicType::TopClear($user,$data);
  445. $basic = $model->where('del_time',0)
  446. ->where('type',22)
  447. ->select('title','id','type')
  448. ->orderby('id', 'asc')->get()->toArray();
  449. $detail = ProductPriceDetail::where('del_time',0)
  450. ->where('product_id',$data['id'])
  451. ->select('product_id','basic_type_id','price')
  452. ->get()->toArray();
  453. $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
  454. ->pluck('title','id')
  455. ->toArray();
  456. //是否总公司
  457. $is_main = $user['is_all_depart'];
  458. $top_depart = $user['depart_top'][0] ?? [];
  459. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  460. $customer['product_price'] = [];
  461. //特殊功能按钮
  462. $special_button = $user['special_button'] ?? [];
  463. //成本隐藏
  464. $price = "******";
  465. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $customer['cost'];
  466. $customer['cost_show'] = $price;
  467. //所有金额
  468. foreach ($basic as $value){
  469. $show = 0;
  470. if(in_array(RoleMenuButton::special_one,$special_button)){
  471. $show = 1;
  472. }else{
  473. if($top_depart['basic_type_id'] == $value['id']) $show = 1;
  474. }
  475. $customer['product_price'][$value['id']] = [
  476. 'basic_type_id' => $value['id'],
  477. 'basic_type_title' => $title_map[$value['id']] ?? '',
  478. 'price' => 0,
  479. 'is_show' => $show,
  480. ];
  481. }
  482. //展示金额
  483. foreach ($detail as $value){
  484. if(isset($customer['product_price'][$value['basic_type_id']])) $customer['product_price'][$value['basic_type_id']]['price'] = $value['price'];
  485. }
  486. $customer['product_price'] = array_values($customer['product_price']);
  487. //单位
  488. $title = BasicType::where('id',$customer['unit'])->value('title');
  489. $customer['unit_name'] = $title;
  490. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  491. $customer_info = ProductInfo::where('del_time',0)
  492. ->where('product_id',$customer['id'])
  493. ->select('id','product_id','file','type','name')
  494. ->get()->toArray();
  495. $fileUploadService = new FileUploadService();
  496. foreach ($customer_info as $value){
  497. $tmp = [
  498. 'url' => $value['file'],
  499. 'name' => $value['name'],
  500. 'show_url' => $fileUploadService->getFileShow($value['file']),
  501. ];
  502. if($value['type'] == ProductInfo::type_one){
  503. $customer['img'][] = $tmp;
  504. }elseif ($value['type'] == ProductInfo::type_two){
  505. $customer['file'][] = $tmp;
  506. }
  507. }
  508. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  509. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  510. //可见范围
  511. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  512. $customer['depart'] = $return[0] ?? [];
  513. $customer['employee'] = $return[1] ?? [];
  514. return [true, $customer];
  515. }
  516. public function productListIndex($data,$user){
  517. $model = Product::ProductClear($user,$data);
  518. $model = $model->where('del_time',0)
  519. ->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')
  520. ->orderby('product_attribute', 'desc')
  521. ->orderby('id', 'desc');
  522. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  523. if(isset($data['state'])) $model->where('state', $data['state']);
  524. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  525. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  526. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  527. if(! empty($data['category'])){
  528. $id = $this->getProductCateGory($data['category']);
  529. $model->whereIn('product_category_id', $id);
  530. }
  531. if(! empty($data['product_category'])) {
  532. $product_category = ProductCategory::where('del_time',0)
  533. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  534. ->select('id')
  535. ->get()->toArray();
  536. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  537. }
  538. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  539. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  540. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  541. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  542. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  543. $model->where('crt_time','>=',$return[0]);
  544. $model->where('crt_time','<=',$return[1]);
  545. }
  546. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  547. $list = $this->limit($model,'',$data);
  548. $list = $this->fillData($list,$user,$data);
  549. return [true, $list];
  550. }
  551. /**
  552. * 产品列表
  553. * @param $data
  554. * @param $user
  555. * @return array
  556. */
  557. public function productList($data,$user){
  558. $model = Product::ProductClear($user,$data);
  559. $model = $model->where('del_time',0)
  560. ->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')
  561. ->where('is_use', Product::is_use_one)
  562. ->orderby('product_attribute', 'desc')
  563. ->orderby('id', 'desc');
  564. if(! empty($data['title_t'])) {
  565. $model->where('title', 'LIKE', '%'.$data['title_t'].'%')
  566. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  567. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  568. }
  569. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  570. if(isset($data['state'])) $model->where('state', $data['state']);
  571. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  572. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  573. if(! empty($data['category'])){
  574. $id = $this->getProductCateGory($data['category']);
  575. $model->whereIn('product_category_id', $id);
  576. }
  577. if(! empty($data['product_category'])) {
  578. $product_category = ProductCategory::where('del_time',0)
  579. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  580. ->select('id')
  581. ->get()->toArray();
  582. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  583. }
  584. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  585. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  586. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  587. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  588. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  589. $model->where('crt_time','>=',$return[0]);
  590. $model->where('crt_time','<=',$return[1]);
  591. }
  592. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  593. $list = $this->limit($model,'',$data);
  594. $list = $this->fillData($list,$user,$data);
  595. return [true, $list];
  596. }
  597. public function productList2($data,$user){
  598. $model = ProductCategory::ProductClear($user,$data);
  599. $model = $model->where('del_time',0)
  600. ->select('title','id','parent_id')
  601. ->orderby('id','desc');
  602. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  603. $list = $model->get()->toArray();
  604. if(! empty($list)) {
  605. $productList = Product::where('del_time',0)
  606. ->whereIn('product_category_id',array_column($list,'id'))
  607. ->select('id','product_category_id','title','code')
  608. ->get()->toArray();
  609. $productMap = [];
  610. foreach ($productList as $value){
  611. $productMap[$value['product_category_id']][] = $value;
  612. }
  613. foreach ($list as $key => $value){
  614. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  615. }
  616. $list = $this->makeTree(0,$list);
  617. $list = $this->set_sort_circle($list);
  618. }
  619. return [200, $list];
  620. }
  621. /**
  622. * 产品参数规则
  623. * @param $data
  624. * @param $is_add
  625. * @return array
  626. */
  627. public function productRule(&$data, $user, $is_add = true){
  628. if(empty($data['title'])) return [false,'产品名称不能为空'];
  629. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  630. if(empty($data['code'])) return [false,'产品编码不能为空'];
  631. if(! isset($data['cost'])) return [false, '请填写成本'];
  632. if(! isset($data['retail_price'])) return [false, '请填写零售价'];
  633. $res = $this->checkNumber($data['cost']);
  634. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  635. $res = $this->checkNumber($data['retail_price']);
  636. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于等于0的数值'];
  637. if(! empty($data['product_price'])){
  638. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  639. ->pluck('title','id')
  640. ->toArray();
  641. foreach ($data['product_price'] as $value){
  642. if(! empty($value['price'])) {
  643. $tmp = $map[$value['basic_type_id']] ?? '';
  644. $res = $this->checkNumber($value['price']);
  645. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  646. }
  647. }
  648. }
  649. if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
  650. //所属部门 以及 顶级部门
  651. if(empty($data['depart_id'])) {
  652. $data['depart_id'] = $this->getDepart($user);
  653. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  654. }
  655. //总社id
  656. $top_depart_id = $user['head'] ?? [];
  657. $top_depart_id = $top_depart_id['id'] ?? 0;
  658. if($is_add){
  659. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  660. ->where('del_time',0)
  661. ->exists();
  662. }else{
  663. if(empty($data['id'])) return [false,'ID不能为空'];
  664. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  665. ->where('id','<>',$data['id'])
  666. ->where('del_time',0)
  667. ->exists();
  668. }
  669. if($bool) return [false,'产品编码不能重复'];
  670. return [true, $data];
  671. }
  672. /**
  673. * 拼接数据
  674. * @param $data
  675. * @return array
  676. */
  677. public function fillData($data, $user, $search){
  678. if(empty($data['data'])) return $data;
  679. $type = $search['type'] ?? 0;
  680. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  681. ->pluck('emp_name','id')
  682. ->toArray();
  683. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  684. ->select('title','id','is_edit_unit_price')
  685. ->get()
  686. ->toArray();
  687. $category = array_column($category,null,'id');
  688. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  689. ->orWhere('type',BasicType::type_22)
  690. ->pluck('title','id')
  691. ->toArray();
  692. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
  693. //产品使用价格
  694. $product_id = array_column($data['data'],'id');
  695. $detail_map = $this->getProductPrice($product_id, $type);
  696. //获取产品图片
  697. $img = $this->getProductImg($product_id);
  698. //当前门店
  699. $top_depart = $user['depart_top'][0] ?? [];
  700. //特殊功能按钮
  701. $special_button = $user['special_button'] ?? [];
  702. //库存
  703. $stock_map = [];
  704. if(! empty($search['storehouse_id'])){
  705. $stock = ProductInventoryService::getRealStock($product_id, $search['storehouse_id']);
  706. foreach ($stock as $value){
  707. $stock_map[$value['product_id']] = $value;
  708. }unset($stock);
  709. }
  710. foreach ($data['data'] as $key => $value){
  711. $tmp = [];
  712. if(isset($detail_map[$value['id']])){
  713. $d = $detail_map[$value['id']];
  714. foreach ($d as $v){
  715. $is_show = 0;
  716. if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
  717. $is_use = 0;
  718. if($top_depart['basic_type_id'] == $v['basic_type_id']) $is_use = 1;
  719. $tmp[] = [
  720. 'basic_type_id' => $v['basic_type_id'],
  721. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  722. 'price' => $v['price'],
  723. 'is_show' => $is_show,
  724. 'is_use' => $is_use
  725. ];
  726. }
  727. }
  728. $data['data'][$key]['product_price'] = $tmp;
  729. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  730. $data['data'][$key]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
  731. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  732. $category_tmp = $category[$value['product_category_id']] ?? [];
  733. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  734. $data['data'][$key]['is_edit_unit_price'] = $category_tmp['is_edit_unit_price'] ?? 0;
  735. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  736. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  737. $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
  738. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  739. $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
  740. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  741. //库存
  742. $data['data'][$key]['stock'] = $stock_map[$value['id']] ?? [];
  743. //成本隐藏
  744. $price = "******";
  745. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $value['cost'];
  746. $data['data'][$key]['cost_show'] = $price;
  747. }
  748. return $data;
  749. }
  750. public function getProductCateGory($category){
  751. $product_category = ProductCategory::where('del_time',0)
  752. ->select('id','parent_id')
  753. ->get()->toArray();
  754. $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
  755. return $result;
  756. }
  757. public function getProductImg($product_id = []){
  758. if(empty($product_id)) return [];
  759. $img = [];
  760. $customer_info = ProductInfo::where('del_time',0)
  761. ->whereIn('product_id',$product_id)
  762. ->where('type',ProductInfo::type_one)
  763. ->select('product_id','file','type','name')
  764. ->get()->toArray();
  765. $fileUploadService = new FileUploadService();
  766. foreach ($customer_info as $value){
  767. if(isset($img[$value['product_id']])) continue;
  768. $url = "";
  769. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  770. $img[$value['product_id']] = $url;
  771. }
  772. return $img;
  773. }
  774. public function batchUploadImg($data){
  775. if(empty($data['product_id'])) return [false, '请选择产品'];
  776. $time = time();
  777. try {
  778. DB::beginTransaction();
  779. $old = ProductInfo::where('del_time',0)
  780. ->where('type',ProductInfo::type_one)
  781. ->whereIn('product_id',$data['product_id'])
  782. ->select('file')
  783. ->get()->toArray();
  784. $old = array_column($old,'file');
  785. ProductInfo::where('del_time',0)
  786. ->where('type',ProductInfo::type_one)
  787. ->whereIn('product_id',$data['product_id'])
  788. ->update(['del_time' => $time]);
  789. $new['origin'] = $data['img_url'] ?? "";
  790. $new['img_list'] = [];
  791. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  792. $img = explode('.', $img);
  793. $insert = [];
  794. if(! empty($data['img_url'])){
  795. foreach ($data['product_id'] as $key => $value){
  796. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  797. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  798. $insert[] = [
  799. 'product_id' => $value,
  800. 'file' => $copy,
  801. 'type' => ProductInfo::type_one,
  802. 'name' => $data['img_name'] ?? "",
  803. 'crt_time' => $time,
  804. ];
  805. $new['img_list'][] = $copy;
  806. }
  807. if(! empty($insert)) ProductInfo::insert($insert);
  808. }
  809. DB::commit();
  810. }catch (\Throwable $exception){
  811. DB::rollBack();
  812. return [false, $exception->getMessage()];
  813. }
  814. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  815. }
  816. //获取产品字典
  817. public function getProductDetail($product_id = []){
  818. if(empty($product_id)) return [];
  819. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  820. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  821. ->pluck('is_edit_unit_price','id')
  822. ->toArray();
  823. foreach ($pro as $key => $value){
  824. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  825. }
  826. return array_column($pro,null,'id');
  827. }
  828. //获取产品使用价格
  829. public function getProductPrice($product_id = [], $type = 1){
  830. if(! is_array($product_id)) $product_id = [$product_id];
  831. //type 1 采购 2 合同 和 活动包
  832. $detail_map = [];
  833. $time = time();
  834. if($type == 1) {
  835. //供应商活动价格
  836. $activity = ProductActivityPrice::from('product_activity_price as a')
  837. ->join('product_activity as b','b.id','a.product_activity_id')
  838. ->where('a.del_time',0)
  839. ->whereIn('a.product_id',$product_id)
  840. ->where('a.start_time','<=',$time)
  841. ->where('a.end_time','>=',$time)
  842. ->where('b.type',ProductActivity::type_two)
  843. ->select('a.product_id','a.basic_type_id','a.price')
  844. ->get()->toArray();
  845. foreach ($activity as $value){
  846. $detail_map[$value['product_id']][] = $value;
  847. }
  848. //分社价 没有供应商活动价格使用分社价格
  849. $detail = ProductPriceDetail::where('del_time',0)
  850. ->whereIn('product_id',$product_id)
  851. ->select('product_id','basic_type_id','price')
  852. ->get()->toArray();
  853. foreach ($detail as $value){
  854. if(! isset($detail_map[$value['product_id']][$value['basic_type_id']])){
  855. $detail_map[$value['product_id']][$value['basic_type_id']] = $value;
  856. }
  857. }
  858. }else{
  859. //零售活动价格
  860. $activity = ProductActivityPrice::from('product_activity_price as a')
  861. ->join('product_activity as b','b.id','a.product_activity_id')
  862. ->where('a.del_time',0)
  863. ->whereIn('a.product_id',$product_id)
  864. ->where('a.start_time','<=',$time)
  865. ->where('a.end_time','>=',$time)
  866. ->where('b.type',ProductActivity::type_one)
  867. ->select('a.product_id','a.basic_type_id','a.price')
  868. ->get()->toArray();
  869. foreach ($activity as $value){
  870. $detail_map[$value['product_id']][] = $value;
  871. }
  872. }
  873. return $detail_map;
  874. }
  875. public function getProductPriceTmp($product_id = []){
  876. //type 0 采购 1 合同
  877. //分社价
  878. $detail = ProductPriceDetail::where('del_time',0)
  879. ->whereIn('product_id',$product_id)
  880. ->select('product_id','basic_type_id','price')
  881. ->get()->toArray();
  882. $detail_map = [];
  883. foreach ($detail as $value){
  884. $detail_map[$value['product_id']][] = $value;
  885. }
  886. $return = $detail_map;
  887. //活动价格
  888. $time = time();
  889. $activity = ProductActivityPrice::where('del_time',0)
  890. ->whereIn('product_id',$product_id)
  891. ->where('start_time','<=',$time)
  892. ->where('end_time','>=',$time)
  893. ->select('product_id','basic_type_id','price')
  894. ->get()->toArray();
  895. if(! empty($activity)){
  896. foreach ($activity as $value){
  897. if(! isset($detail_map[$value['product_id']])) {
  898. $return[$value['product_id']][] = $value;
  899. }else{
  900. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  901. if(! in_array($value['basic_type_id'], $basic_type)){
  902. $return[$value['product_id']][] = $value;
  903. continue;
  904. }
  905. foreach ($detail_map[$value['product_id']] as $key => $val){
  906. if($value['basic_type_id'] == $val['basic_type_id']) {
  907. //活动价格替换
  908. $return[$value['product_id']][$key]['price'] = $value['price'];
  909. }
  910. }
  911. }
  912. }
  913. }unset($detail_map);
  914. return $return;
  915. }
  916. }