ProductService.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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->mark = $data['mark'] ?? '';
  213. $model->state = $data['state'] ?? 0;
  214. $model->product_attribute = $data['product_attribute'] ?? 0;
  215. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  216. $model->save();
  217. $time = time();
  218. ProductIntroduction::where('product_id',$data['id'])
  219. ->where('del_time',0)
  220. ->update(['del_time' => $time]);
  221. if(! empty($data['introduction'])){
  222. $models = new ProductIntroduction();
  223. $models->product_id = $model->id;
  224. $models->introduction = $data['introduction'];
  225. $models->save();
  226. }
  227. $old = ProductInfo::where('del_time',0)
  228. ->where('product_id',$data['id'])
  229. ->select('file')
  230. ->get()->toArray();
  231. $old = array_column($old,'file');
  232. ProductInfo::where('del_time',0)
  233. ->where('product_id',$data['id'])
  234. ->update(['del_time' => $time]);
  235. $new = [];
  236. if(! empty($data['img'])){
  237. $insert = [];
  238. foreach ($data['img'] as $value){
  239. $insert[] = [
  240. 'product_id' => $model->id,
  241. 'file' => $value['url'],
  242. 'type' => ProductInfo::type_one,
  243. 'name' => $value['name'],
  244. 'crt_time' => $time,
  245. ];
  246. if(in_array($value['url'], $old)) {
  247. foreach ($old as $o_k => $o_v){
  248. if($o_v == $value['url']) unset($old[$o_k]);
  249. }
  250. }else{
  251. $new[] = $value['url'];
  252. }
  253. }
  254. ProductInfo::insert($insert);
  255. }
  256. if(! empty($data['file'])){
  257. $insert = [];
  258. foreach ($data['file'] as $value){
  259. $insert[] = [
  260. 'product_id' => $model->id,
  261. 'file' => $value['url'],
  262. 'type' => ProductInfo::type_two,
  263. 'name' => $value['name'],
  264. 'crt_time' => $time,
  265. ];
  266. if(in_array($value['url'], $old)) {
  267. foreach ($old as $o_k => $o_v){
  268. if($o_v == $value['url']) unset($old[$o_k]);
  269. }
  270. }else{
  271. $new[] = $value['url'];
  272. }
  273. }
  274. ProductInfo::insert($insert);
  275. }
  276. ProductPriceDetail::where('del_time',0)
  277. ->where('product_id',$data['id'])
  278. ->update(['del_time' => $time]);
  279. if(! empty($data['product_price'])){
  280. $insert = [];
  281. foreach ($data['product_price'] as $value){
  282. $insert[] = [
  283. 'product_id' => $model->id,
  284. 'basic_type_id' => $value['basic_type_id'],
  285. 'price' => $value['price'] ?? 0,
  286. 'crt_time' => $time,
  287. ];
  288. }
  289. ProductPriceDetail::insert($insert);
  290. }
  291. DB::commit();
  292. }catch (\Exception $exception){
  293. DB::rollBack();
  294. return [false,$exception->getMessage()];
  295. }
  296. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  297. }
  298. /**
  299. * 产品新增
  300. * @param $data
  301. * @param $user
  302. * @return array
  303. */
  304. public function productAdd($data,$user){
  305. list($status,$msg) = $this->productRule($data, $user);
  306. if(!$status) return [$status,$msg];
  307. try {
  308. DB::beginTransaction();
  309. $model = new Product();
  310. $model->product_category_id = $data['product_category_id'] ?? 0;
  311. $model->product_category = $data['product_category'] ?? '';
  312. $model->title = $data['title'];
  313. $model->code = $data['code'] ?? '';
  314. $model->warranty_time = $data['warranty_time'] ?? 0;
  315. $model->install_time = $data['install_time'] ?? 0;
  316. $model->size = $data['size'] ?? '';
  317. $model->unit = $data['unit'] ?? 0;
  318. $model->bar_code = $data['bar_code'] ?? '';
  319. $model->cost = $data['cost'] ?? 0;
  320. $model->retail_price = $data['retail_price'] ?? 0;
  321. $model->mark = $data['mark'] ?? '';
  322. $model->state = $data['state'] ?? 0;
  323. $model->product_attribute = $data['product_attribute'] ?? 0;
  324. $model->crt_id = $user['id'];
  325. $model->depart_id = $data['depart_id'] ?? 0;
  326. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  327. $model->is_use = $data['is_use'] ?? Product::is_use_one;
  328. $model->save();
  329. $time = time();
  330. if(! empty($data['introduction'])){
  331. $models = new ProductIntroduction();
  332. $models->product_id = $model->id;
  333. $models->introduction = $data['introduction'];
  334. $models->save();
  335. }
  336. $new = [];
  337. if(! empty($data['img'])){
  338. $insert = [];
  339. foreach ($data['img'] as $value){
  340. $insert[] = [
  341. 'product_id' => $model->id,
  342. 'file' => $value['url'],
  343. 'type' => ProductInfo::type_one,
  344. 'name' => $value['name'],
  345. 'crt_time' => $time,
  346. ];
  347. if(! empty($value['url'])) $new[] = $value['url'];
  348. }
  349. ProductInfo::insert($insert);
  350. }
  351. if(! empty($data['file'])){
  352. $insert = [];
  353. foreach ($data['file'] as $value){
  354. $insert[] = [
  355. 'product_id' => $model->id,
  356. 'file' => $value['url'],
  357. 'type' => ProductInfo::type_two,
  358. 'name' => $value['name'],
  359. 'crt_time' => $time,
  360. ];
  361. if(! empty($value['url'])) $new[] = $value['url'];
  362. }
  363. ProductInfo::insert($insert);
  364. }
  365. if(! empty($data['product_price'])){
  366. $insert = [];
  367. foreach ($data['product_price'] as $value){
  368. $insert[] = [
  369. 'product_id' => $model->id,
  370. 'basic_type_id' => $value['basic_type_id'],
  371. 'price' => $value['price'] ?? 0,
  372. 'crt_time' => $time,
  373. ];
  374. }
  375. ProductPriceDetail::insert($insert);
  376. }
  377. DB::commit();
  378. }catch (\Exception $exception){
  379. DB::rollBack();
  380. return [false,$exception->getMessage()];
  381. }
  382. return [true, ['file' => ['new' => $new]]];
  383. }
  384. /**
  385. * 产品删除
  386. * @param $data
  387. * @return array
  388. */
  389. public function productDel($data){
  390. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  391. try {
  392. DB::beginTransaction();
  393. $time = time();
  394. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  395. ProductIntroduction::where('product_id',$data['id'])
  396. ->where('del_time',0)
  397. ->update(['del_time' => $time]);
  398. $old = ProductInfo::where('del_time',0)
  399. ->where('product_id',$data['id'])
  400. ->select('file')
  401. ->get()->toArray();
  402. $old = array_column($old,'file');
  403. ProductInfo::where('del_time',0)
  404. ->where('product_id',$data['id'])
  405. ->update(['del_time' => $time]);
  406. ProductPriceDetail::where('del_time',0)
  407. ->where('product_id',$data['id'])
  408. ->update(['del_time' => $time]);
  409. (new RangeService())->RangeDelete($data['id'],SeeRange::type_four);
  410. DB::commit();
  411. }catch (\Exception $exception){
  412. DB::rollBack();
  413. return [false,$exception->getMessage()];
  414. }
  415. return [true, ['file' => ['old' => $old]]];
  416. }
  417. /**
  418. * 产品详情
  419. * @param $data
  420. * @param $user
  421. * @return array
  422. */
  423. public function productDetail($data,$user){
  424. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  425. $customer = Product::where('del_time',0)
  426. ->where('id',$data['id'])
  427. ->first();
  428. if(empty($customer)) return [false,'产品不存在或已被删除'];
  429. $customer = $customer->toArray();
  430. $customer['product_attribute_title'] = Product::$product_attribute[$customer['product_attribute']] ?? "";
  431. $customer['is_use_title'] = Product::$is_use[$customer['is_use']] ?? "";
  432. $customer['product_category'] = ! empty($customer['product_category']) ? json_decode($customer['product_category'],true): [];
  433. $category = ProductCategory::where('id',$customer['product_category_id'])
  434. ->value('title');
  435. $customer['product_category_title'] = $category;
  436. $customer['introduction'] = "";
  437. $in = ProductIntroduction::where('del_time',0)
  438. ->where('product_id',$data['id'])
  439. ->first();
  440. if(! empty($in)) $customer['introduction'] = $in->introduction;
  441. $data['top_depart_id'] = $user['head']['id'] ?? 0;
  442. $model = BasicType::TopClear($user,$data);
  443. $basic = $model->where('del_time',0)
  444. ->where('type',22)
  445. ->select('title','id','type')
  446. ->orderby('id', 'asc')->get()->toArray();
  447. $detail = ProductPriceDetail::where('del_time',0)
  448. ->where('product_id',$data['id'])
  449. ->select('product_id','basic_type_id','price')
  450. ->get()->toArray();
  451. $title_map = BasicType::whereIn('id',array_unique(array_merge_recursive(array_column($detail,'basic_type_id'),array_column($basic,'id'))))
  452. ->pluck('title','id')
  453. ->toArray();
  454. //是否总公司
  455. $is_main = $user['is_all_depart'];
  456. $top_depart = $user['depart_top'][0] ?? [];
  457. $customer['is_edit'] = $customer['top_depart_id'] == $top_depart['depart_id'] ? 1 : 0;
  458. $customer['product_price'] = [];
  459. //特殊功能按钮
  460. $special_button = $user['special_button'] ?? [];
  461. //成本隐藏
  462. $price = "******";
  463. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $customer['cost'];
  464. $customer['cost_show'] = $price;
  465. //所有金额
  466. foreach ($basic as $value){
  467. $show = 0;
  468. if(in_array(RoleMenuButton::special_one,$special_button)){
  469. $show = 1;
  470. }else{
  471. if($top_depart['basic_type_id'] == $value['id']) $show = 1;
  472. }
  473. $customer['product_price'][$value['id']] = [
  474. 'basic_type_id' => $value['id'],
  475. 'basic_type_title' => $title_map[$value['id']] ?? '',
  476. 'price' => 0,
  477. 'is_show' => $show,
  478. ];
  479. }
  480. //展示金额
  481. foreach ($detail as $value){
  482. if(isset($customer['product_price'][$value['basic_type_id']])) $customer['product_price'][$value['basic_type_id']]['price'] = $value['price'];
  483. }
  484. $customer['product_price'] = array_values($customer['product_price']);
  485. //单位
  486. $title = BasicType::where('id',$customer['unit'])->value('title');
  487. $customer['unit_name'] = $title;
  488. $customer['img'] = $customer['file'] = $customer['depart'] = $customer['employee'] = [];
  489. $customer_info = ProductInfo::where('del_time',0)
  490. ->where('product_id',$customer['id'])
  491. ->select('id','product_id','file','type','name')
  492. ->get()->toArray();
  493. $fileUploadService = new FileUploadService();
  494. foreach ($customer_info as $value){
  495. $tmp = [
  496. 'url' => $value['file'],
  497. 'name' => $value['name'],
  498. 'show_url' => $fileUploadService->getFileShow($value['file']),
  499. ];
  500. if($value['type'] == ProductInfo::type_one){
  501. $customer['img'][] = $tmp;
  502. }elseif ($value['type'] == ProductInfo::type_two){
  503. $customer['file'][] = $tmp;
  504. }
  505. }
  506. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  507. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  508. //可见范围
  509. $return = (new RangeService())->RangeDetail($data['id'],SeeRange::type_four);
  510. $customer['depart'] = $return[0] ?? [];
  511. $customer['employee'] = $return[1] ?? [];
  512. return [true, $customer];
  513. }
  514. public function productListIndex($data,$user){
  515. $model = Product::ProductClear($user,$data);
  516. $model = $model->where('del_time',0)
  517. ->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')
  518. ->orderby('product_attribute', 'desc')
  519. ->orderby('id', 'desc');
  520. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  521. if(isset($data['state'])) $model->where('state', $data['state']);
  522. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  523. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  524. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  525. if(! empty($data['category'])){
  526. $id = $this->getProductCateGory($data['category']);
  527. $model->whereIn('product_category_id', $id);
  528. }
  529. if(! empty($data['product_category'])) {
  530. $product_category = ProductCategory::where('del_time',0)
  531. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  532. ->select('id')
  533. ->get()->toArray();
  534. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  535. }
  536. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  537. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  538. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  539. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  540. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  541. $model->where('crt_time','>=',$return[0]);
  542. $model->where('crt_time','<=',$return[1]);
  543. }
  544. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  545. $list = $this->limit($model,'',$data);
  546. $list = $this->fillData($list,$user,$data);
  547. return [true, $list];
  548. }
  549. /**
  550. * 产品列表
  551. * @param $data
  552. * @param $user
  553. * @return array
  554. */
  555. public function productList($data,$user){
  556. $model = Product::ProductClear($user,$data);
  557. $model = $model->where('del_time',0)
  558. ->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')
  559. ->where('is_use', Product::is_use_one)
  560. ->orderby('product_attribute', 'desc')
  561. ->orderby('id', 'desc');
  562. if(! empty($data['title_t'])) {
  563. $model->where('title', 'LIKE', '%'.$data['title_t'].'%')
  564. ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
  565. ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
  566. }
  567. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  568. if(isset($data['state'])) $model->where('state', $data['state']);
  569. if(isset($data['product_attribute'])) $model->where('product_attribute', $data['product_attribute']);
  570. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  571. if(! empty($data['category'])){
  572. $id = $this->getProductCateGory($data['category']);
  573. $model->whereIn('product_category_id', $id);
  574. }
  575. if(! empty($data['product_category'])) {
  576. $product_category = ProductCategory::where('del_time',0)
  577. ->where('title', 'LIKE', '%'.$data['product_category'].'%')
  578. ->select('id')
  579. ->get()->toArray();
  580. $model->whereIn('product_category_id',array_unique(array_column($product_category,'id')));
  581. }
  582. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  583. if(! empty($data['bar_code'])) $model->where('bar_code', 'LIKE', '%'.$data['bar_code'].'%');
  584. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  585. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  586. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  587. $model->where('crt_time','>=',$return[0]);
  588. $model->where('crt_time','<=',$return[1]);
  589. }
  590. if(! empty($data['product_id'])) $model->whereIn('id',$data['product_id']);
  591. $list = $this->limit($model,'',$data);
  592. $list = $this->fillData($list,$user,$data);
  593. return [true, $list];
  594. }
  595. public function productList2($data,$user){
  596. $model = ProductCategory::TopClear($user,$data);
  597. $model = $model->where('del_time',0)
  598. ->select('title','id','parent_id')
  599. ->orderby('id','desc');
  600. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  601. $list = $model->get()->toArray();
  602. if(! empty($list)) {
  603. $productList = Product::where('del_time',0)
  604. ->whereIn('product_category_id',array_column($list,'id'))
  605. ->select('id','product_category_id','title','code')
  606. ->get()->toArray();
  607. $productMap = [];
  608. foreach ($productList as $value){
  609. $productMap[$value['product_category_id']][] = $value;
  610. }
  611. foreach ($list as $key => $value){
  612. if(isset($productMap[$value['id']])) $list[$key]['product'] = $productMap[$value['id']];
  613. }
  614. $list = $this->makeTree(0,$list);
  615. $list = $this->set_sort_circle($list);
  616. }
  617. return [200, $list];
  618. }
  619. /**
  620. * 产品参数规则
  621. * @param $data
  622. * @param $is_add
  623. * @return array
  624. */
  625. public function productRule(&$data, $user, $is_add = true){
  626. if(empty($data['title'])) return [false,'产品名称不能为空'];
  627. if(empty($data['product_category_id'])) return [false,'产品分类不能为空'];
  628. if(empty($data['code'])) return [false,'产品编码不能为空'];
  629. if(! isset($data['cost'])) return [false, '请填写成本'];
  630. if(! isset($data['retail_price'])) return [false, '请填写零售价'];
  631. $res = $this->checkNumber($data['cost']);
  632. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  633. $res = $this->checkNumber($data['retail_price']);
  634. if(! $res) return [false,'零售价格请输入不超过两位小数并且大于等于0的数值'];
  635. if(! empty($data['product_price'])){
  636. $map = BasicType::whereIn('id',array_column($data['product_price'],'basic_type_id'))
  637. ->pluck('title','id')
  638. ->toArray();
  639. foreach ($data['product_price'] as $value){
  640. if(! empty($value['price'])) {
  641. $tmp = $map[$value['basic_type_id']] ?? '';
  642. $res = $this->checkNumber($value['price']);
  643. if(! $res) return [false, $tmp . '请输入不超过两位小数并且大于0的数值'];
  644. }
  645. }
  646. }
  647. if(! empty($data['product_category'])) $data['product_category'] = json_encode($data['product_category']);
  648. //所属部门 以及 顶级部门
  649. if(empty($data['depart_id'])) {
  650. $data['depart_id'] = $this->getDepart($user);
  651. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  652. }
  653. //总社id
  654. $top_depart_id = $user['head'] ?? [];
  655. $top_depart_id = $top_depart_id['id'] ?? 0;
  656. if($is_add){
  657. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  658. ->where('del_time',0)
  659. ->exists();
  660. }else{
  661. if(empty($data['id'])) return [false,'ID不能为空'];
  662. $bool = Product::whereRaw("(binary code = '{$data['code']}') AND (top_depart_id = {$data['top_depart_id']} OR top_depart_id = {$top_depart_id})")
  663. ->where('id','<>',$data['id'])
  664. ->where('del_time',0)
  665. ->exists();
  666. }
  667. if($bool) return [false,'产品编码不能重复'];
  668. return [true, $data];
  669. }
  670. /**
  671. * 拼接数据
  672. * @param $data
  673. * @return array
  674. */
  675. public function fillData($data, $user, $search){
  676. if(empty($data['data'])) return $data;
  677. $type = $search['type'] ?? 0;
  678. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  679. ->pluck('emp_name','id')
  680. ->toArray();
  681. $category = ProductCategory::whereIn('id',array_unique(array_column($data['data'],'product_category_id')))
  682. ->select('title','id','is_edit_unit_price')
  683. ->get()
  684. ->toArray();
  685. $category = array_column($category,null,'id');
  686. $basic_map = BasicType::whereIn('id',array_unique(array_column($data['data'],'unit')))
  687. ->orWhere('type',BasicType::type_22)
  688. ->pluck('title','id')
  689. ->toArray();
  690. $depart_map = Depart::whereIn('id',array_unique(array_column($data['data'],'top_depart_id')))->pluck('title','id')->toArray();
  691. //产品使用价格
  692. $product_id = array_column($data['data'],'id');
  693. $detail_map = $this->getProductPrice($product_id, $type);
  694. //获取产品图片
  695. $img = $this->getProductImg($product_id);
  696. //当前门店
  697. $top_depart = $user['depart_top'][0] ?? [];
  698. //特殊功能按钮
  699. $special_button = $user['special_button'] ?? [];
  700. foreach ($data['data'] as $key => $value){
  701. $tmp = [];
  702. if(isset($detail_map[$value['id']])){
  703. $d = $detail_map[$value['id']];
  704. foreach ($d as $v){
  705. $is_show = 0;
  706. if(in_array(RoleMenuButton::special_one,$special_button)) $is_show = 1;
  707. $is_use = 0;
  708. if($top_depart['basic_type_id'] == $v['basic_type_id']) $is_use = 1;
  709. $tmp[] = [
  710. 'basic_type_id' => $v['basic_type_id'],
  711. 'basic_type_title' => $basic_map[$v['basic_type_id']] ?? '',
  712. 'price' => $v['price'],
  713. 'is_show' => $is_show,
  714. 'is_use' => $is_use
  715. ];
  716. }
  717. }
  718. $data['data'][$key]['product_price'] = $tmp;
  719. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  720. $data['data'][$key]['is_use_title'] = Product::$is_use[$value['is_use']] ?? "";
  721. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  722. $category_tmp = $category[$value['product_category_id']] ?? [];
  723. $data['data'][$key]['product_category_name'] = $category_tmp['title'] ?? '';
  724. $data['data'][$key]['is_edit_unit_price'] = $category_tmp['is_edit_unit_price'] ?? 0;
  725. $data['data'][$key]['state_name'] = Product::$state[$value['state']] ?? '';
  726. $data['data'][$key]['unit_name'] = $basic_map[$value['unit']] ?? '';
  727. $data['data'][$key]['belong_to'] = $depart_map[$value['top_depart_id']] ?? '';
  728. $data['data'][$key]['product_attribute_title'] = Product::$product_attribute[$value['product_attribute']] ?? "";
  729. $data['data'][$key]['color'] = Product::$product_attribute_color[$value['product_attribute']] ?? [];
  730. $data['data'][$key]['img'] = $img[$value['id']] ?? "";
  731. //成本隐藏
  732. $price = "******";
  733. if(in_array(RoleMenuButton::special_two,$special_button)) $price = $value['cost'];
  734. $data['data'][$key]['cost_show'] = $price;
  735. }
  736. return $data;
  737. }
  738. public function getProductCateGory($category){
  739. $product_category = ProductCategory::where('del_time',0)
  740. ->select('id','parent_id')
  741. ->get()->toArray();
  742. $result = array_merge($this->getAllDescendants($product_category,$category),[$category]);
  743. return $result;
  744. }
  745. public function getProductImg($product_id = []){
  746. if(empty($product_id)) return [];
  747. $img = [];
  748. $customer_info = ProductInfo::where('del_time',0)
  749. ->whereIn('product_id',$product_id)
  750. ->where('type',ProductInfo::type_one)
  751. ->select('product_id','file','type','name')
  752. ->get()->toArray();
  753. $fileUploadService = new FileUploadService();
  754. foreach ($customer_info as $value){
  755. if(isset($img[$value['product_id']])) continue;
  756. $url = "";
  757. if(! empty($value['file'])) $url = $fileUploadService->getFileShow($value['file']);
  758. $img[$value['product_id']] = $url;
  759. }
  760. return $img;
  761. }
  762. public function batchUploadImg($data){
  763. if(empty($data['product_id'])) return [false, '请选择产品'];
  764. $time = time();
  765. try {
  766. DB::beginTransaction();
  767. $old = ProductInfo::where('del_time',0)
  768. ->where('type',ProductInfo::type_one)
  769. ->whereIn('product_id',$data['product_id'])
  770. ->select('file')
  771. ->get()->toArray();
  772. $old = array_column($old,'file');
  773. ProductInfo::where('del_time',0)
  774. ->where('type',ProductInfo::type_one)
  775. ->whereIn('product_id',$data['product_id'])
  776. ->update(['del_time' => $time]);
  777. $new['origin'] = $data['img_url'] ?? "";
  778. $new['img_list'] = [];
  779. $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
  780. $img = explode('.', $img);
  781. $insert = [];
  782. if(! empty($data['img_url'])){
  783. foreach ($data['product_id'] as $key => $value){
  784. $copy = $img[0] . 'C' . $key . '.' . $img[1];
  785. $copy = FileUploadService::string . FileUploadService::string2 . $copy;
  786. $insert[] = [
  787. 'product_id' => $value,
  788. 'file' => $copy,
  789. 'type' => ProductInfo::type_one,
  790. 'name' => $data['img_name'] ?? "",
  791. 'crt_time' => $time,
  792. ];
  793. $new['img_list'][] = $copy;
  794. }
  795. if(! empty($insert)) ProductInfo::insert($insert);
  796. }
  797. DB::commit();
  798. }catch (\Throwable $exception){
  799. DB::rollBack();
  800. return [false, $exception->getMessage()];
  801. }
  802. return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
  803. }
  804. //获取产品字典
  805. public function getProductDetail($product_id = []){
  806. if(empty($product_id)) return [];
  807. $pro = Product::whereIn('id', $product_id)->get()->toArray();
  808. $category = ProductCategory::whereIn('id',array_unique(array_column($pro,'product_category_id')))
  809. ->pluck('is_edit_unit_price','id')
  810. ->toArray();
  811. foreach ($pro as $key => $value){
  812. $pro[$key]['is_edit_unit_price'] = $category[$value['product_category_id']] ?? 0;
  813. }
  814. return array_column($pro,null,'id');
  815. }
  816. //获取产品使用价格
  817. public function getProductPrice($product_id = [], $type = 1){
  818. if(! is_array($product_id)) $product_id = [$product_id];
  819. //type 1 采购 2 合同 和 活动包
  820. $detail_map = [];
  821. $time = time();
  822. if($type == 1) {
  823. //供应商活动价格
  824. $activity = ProductActivityPrice::from('product_activity_price as a')
  825. ->join('product_activity as b','b.id','a.product_activity_id')
  826. ->where('a.del_time',0)
  827. ->whereIn('a.product_id',$product_id)
  828. ->where('a.start_time','<=',$time)
  829. ->where('a.end_time','>=',$time)
  830. ->where('b.type',ProductActivity::type_two)
  831. ->select('a.product_id','a.basic_type_id','a.price')
  832. ->get()->toArray();
  833. foreach ($activity as $value){
  834. $detail_map[$value['product_id']][] = $value;
  835. }
  836. //分社价 没有供应商活动价格使用分社价格
  837. $detail = ProductPriceDetail::where('del_time',0)
  838. ->whereIn('product_id',$product_id)
  839. ->select('product_id','basic_type_id','price')
  840. ->get()->toArray();
  841. foreach ($detail as $value){
  842. if(! isset($detail_map[$value['product_id']])){
  843. $detail_map[$value['product_id']][] = $value;
  844. }
  845. }
  846. }else{
  847. //零售活动价格
  848. $activity = ProductActivityPrice::from('product_activity_price as a')
  849. ->join('product_activity as b','b.id','a.product_activity_id')
  850. ->where('a.del_time',0)
  851. ->whereIn('a.product_id',$product_id)
  852. ->where('a.start_time','<=',$time)
  853. ->where('a.end_time','>=',$time)
  854. ->where('b.type',ProductActivity::type_one)
  855. ->select('a.product_id','a.basic_type_id','a.price')
  856. ->get()->toArray();
  857. foreach ($activity as $value){
  858. $detail_map[$value['product_id']][] = $value;
  859. }
  860. }
  861. return $detail_map;
  862. }
  863. public function getProductPriceTmp($product_id = []){
  864. //type 0 采购 1 合同
  865. //分社价
  866. $detail = ProductPriceDetail::where('del_time',0)
  867. ->whereIn('product_id',$product_id)
  868. ->select('product_id','basic_type_id','price')
  869. ->get()->toArray();
  870. $detail_map = [];
  871. foreach ($detail as $value){
  872. $detail_map[$value['product_id']][] = $value;
  873. }
  874. $return = $detail_map;
  875. //活动价格
  876. $time = time();
  877. $activity = ProductActivityPrice::where('del_time',0)
  878. ->whereIn('product_id',$product_id)
  879. ->where('start_time','<=',$time)
  880. ->where('end_time','>=',$time)
  881. ->select('product_id','basic_type_id','price')
  882. ->get()->toArray();
  883. if(! empty($activity)){
  884. foreach ($activity as $value){
  885. if(! isset($detail_map[$value['product_id']])) {
  886. $return[$value['product_id']][] = $value;
  887. }else{
  888. $basic_type = array_column($detail_map[$value['product_id']],'basic_type_id');
  889. if(! in_array($value['basic_type_id'], $basic_type)){
  890. $return[$value['product_id']][] = $value;
  891. continue;
  892. }
  893. foreach ($detail_map[$value['product_id']] as $key => $val){
  894. if($value['basic_type_id'] == $val['basic_type_id']) {
  895. //活动价格替换
  896. $return[$value['product_id']][$key]['price'] = $value['price'];
  897. }
  898. }
  899. }
  900. }
  901. }unset($detail_map);
  902. return $return;
  903. }
  904. }