ProductService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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\ProductItemCodeMessage;
  15. use App\Model\ProductPriceDetail;
  16. use App\Model\Role;
  17. use App\Model\RoleMenuButton;
  18. use App\Model\SeeRange;
  19. use App\Model\Storehouse;
  20. use App\Model\U8Job;
  21. use Illuminate\Support\Arr;
  22. use Illuminate\Support\Facades\DB;
  23. /**
  24. * 产品管理
  25. */
  26. class ProductService extends Service
  27. {
  28. /**
  29. * 产品分类编辑
  30. * @param $data
  31. * @param $user
  32. * @return array
  33. */
  34. public function productCategoryEdit($data,$user){
  35. list($status,$msg) = $this->productCategoryRule($data,$user,false);
  36. if(!$status) return [$status,$msg];
  37. $update = $msg['data'][0];
  38. $model = new ProductCategory();
  39. $model->where('id',$data['id'])->update($update);
  40. return [true,''];
  41. }
  42. /**
  43. * 产品分类新增
  44. * @param $data
  45. * @param $user
  46. * @return array
  47. */
  48. public function productCategoryAdd($data,$user){
  49. list($status,$msg) = $this->productCategoryRule($data,$user);
  50. if(!$status) return [$status,$msg];
  51. ProductCategory::insert($msg['data']);
  52. return [true,''];
  53. }
  54. /**
  55. * 产品分类删除
  56. * @param $data
  57. * @return array
  58. */
  59. public function productCategoryDel($data){
  60. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  61. $bool = Product::where('del_time',0)
  62. ->where('product_category_id',$data['id'])
  63. ->exists();
  64. if($bool) return [false,'产品分类下已添加产品,操作失败'];
  65. try {
  66. DB::beginTransaction();
  67. ProductCategory::where('id',$data['id'])->update([
  68. 'del_time' => time()
  69. ]);
  70. DB::commit();
  71. }catch (\Exception $exception){
  72. DB::rollBack();
  73. return [false,$exception->getMessage()];
  74. }
  75. return [true,''];
  76. }
  77. /**
  78. * 产品分类列表
  79. * @param $data
  80. * @param $user
  81. * @return array
  82. */
  83. public function productCategoryList($data,$user){
  84. $model = ProductCategory::leftJoin('product_category_orderby as a', function ($join) {
  85. $join->on('product_category.id', '=', 'a.category_id')
  86. ->where('a.del_time',0);
  87. })
  88. ->where('product_category.del_time',0)
  89. ->select('product_category.title','product_category.id','product_category.parent_id','product_category.is_edit_unit_price')
  90. ->orderByRaw('IF(a.sort IS NULL, product_category.id, a.sort) ASC'); // 排序逻辑:优先按 a.sort 排序,没有关联时按主表 id 排序
  91. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  92. if(isset($data['is_edit_unit_price'])) $model->where('is_edit_unit_price', $data['is_edit_unit_price']);
  93. $list = $model->get()->toArray();
  94. foreach ($list as $key => $value){
  95. $list[$key]['is_edit_unit_price_title'] = ProductCategory::$is_edit_unit_price[$value['is_edit_unit_price']] ?? "";
  96. }
  97. $list_tree = $list;
  98. if(! empty($list_tree)) {
  99. $list_tree = $this->makeTree(0,$list_tree);
  100. $list_tree = $this->set_sort_circle($list_tree);
  101. }
  102. return [200, ['data' => $list,'tree' => $list_tree]];
  103. }
  104. /**
  105. * 产品分类参数规则
  106. * @param $data
  107. * @param $is_add
  108. * @return array
  109. */
  110. public function productCategoryRule($data,$user, $is_add = true){
  111. if($this->isEmpty($data,'data')) return [false,'数据不能为空!'];
  112. $title = array_column($data['data'],'title');
  113. $title = array_map(function($val) {
  114. return $val !== null ? $val : 0;
  115. }, $title);
  116. $title_count = array_count_values($title);
  117. foreach ($title as $value){
  118. if(empty($value)) return [false,'名称不能为空!'];
  119. if($title_count[$value] > 1) return [false,'名称不能重复'];
  120. }
  121. foreach ($data['data'] as $key => $value){
  122. $data['data'][$key]['upd_time'] = time();
  123. if($is_add){
  124. $parent_id = 0;
  125. if(! empty($value['parent_id'])) {
  126. $bool = Product::where('del_time',0)
  127. ->where('product_category_id',$value['parent_id'])
  128. ->exists();
  129. if($bool) {
  130. $title = ProductCategory::where('id',$value['parent_id'])->select('title')->value('title');
  131. return [false,'产品分类:'. $title .'下已添加产品,不允许添加子分类'];
  132. }
  133. $parent_id = $value['parent_id'];
  134. }
  135. $data['data'][$key]['parent_id'] = $parent_id;
  136. $data['data'][$key]['crt_time'] = time();
  137. $data['data'][$key]['depart_id'] = $data['depart_id'];
  138. $data['data'][$key]['top_depart_id'] = $data['top_depart_id'];
  139. $bool = ProductCategory::where('title',$value['title'])
  140. ->where('top_depart_id',$data['top_depart_id'])
  141. ->where('del_time',0)
  142. ->exists();
  143. }else{
  144. if($this->isEmpty($data,'id')) return [false,'id不能为空!'];
  145. $top_depart_id = ProductCategory::where('id',$data['id'])->value('top_depart_id');
  146. $bool = ProductCategory::where('title',$value['title'])
  147. ->where('id','<>',$data['id'])
  148. ->where('del_time',0)
  149. ->exists();
  150. }
  151. if($bool) return [false,'分类名称不能重复'];
  152. }
  153. return [true, $data];
  154. }
  155. /**
  156. * 产品编辑
  157. * @param $data
  158. * @param $user
  159. * @return array
  160. */
  161. public function productEdit($data,$user){
  162. list($status,$msg) = $this->productRule($data, $user, false);
  163. if(!$status) return [$status,$msg];
  164. try {
  165. DB::beginTransaction();
  166. $model = Product::where('id',$data['id'])->first();
  167. $model->category = $data['category'] ?? '';
  168. $model->title = $data['title'];
  169. $model->code = $data['code'] ?? '';
  170. $model->size = $data['size'] ?? '';
  171. $model->unit = $data['unit'] ?? 0;
  172. $model->business_cost = $data['business_cost'] ?? 0;
  173. $model->cost = $data['cost'] ?? 0;
  174. $model->write_off_price = $data['write_off_price'] ?? 0;
  175. $model->major_client_settlement_price = $data['major_client_settlement_price'] ?? 0;
  176. $model->return_change_price = $data['return_change_price'] ?? "";
  177. $model->mark = $data['mark'] ?? '';
  178. $model->save();
  179. DB::commit();
  180. }catch (\Exception $exception){
  181. DB::rollBack();
  182. return [false,$exception->getMessage()];
  183. }
  184. return [true, ''];
  185. }
  186. /**
  187. * 产品新增
  188. * @param $data
  189. * @param $user
  190. * @return array
  191. */
  192. public function productAdd($data,$user){
  193. list($status,$msg) = $this->productRule($data, $user);
  194. if(!$status) return [$status,$msg];
  195. try {
  196. DB::beginTransaction();
  197. $model = new Product();
  198. $model->category = $data['category'] ?? '';
  199. $model->title = $data['title'];
  200. $model->code = $data['code'] ?? '';
  201. $model->size = $data['size'] ?? '';
  202. $model->unit = $data['unit'] ?? 0;
  203. $model->business_cost = $data['business_cost'] ?? 0;
  204. $model->cost = $data['cost'] ?? 0;
  205. $model->write_off_price = $data['write_off_price'] ?? 0;
  206. $model->major_client_settlement_price = $data['major_client_settlement_price'] ?? 0;
  207. $model->return_change_price = $data['return_change_price'] ?? "";
  208. $model->mark = $data['mark'] ?? '';
  209. $model->crt_id = $user['id'];
  210. $model->save();
  211. DB::commit();
  212. }catch (\Exception $exception){
  213. DB::rollBack();
  214. return [false,$exception->getMessage()];
  215. }
  216. return [true, ''];
  217. }
  218. /**
  219. * 产品删除
  220. * @param $data
  221. * @return array
  222. */
  223. public function productDel($data){
  224. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  225. try {
  226. DB::beginTransaction();
  227. $time = time();
  228. Product::where('del_time',0)->where('id',$data['id'])->update(['del_time' => $time]);
  229. DB::commit();
  230. }catch (\Exception $exception){
  231. DB::rollBack();
  232. return [false,$exception->getMessage()];
  233. }
  234. return [true, ['file' => ['old' => $old]]];
  235. }
  236. /**
  237. * 产品详情
  238. * @param $data
  239. * @param $user
  240. * @return array
  241. */
  242. public function productDetail($data,$user){
  243. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  244. $customer = Product::where('del_time',0)
  245. ->where('id',$data['id'])
  246. ->first();
  247. if(empty($customer)) return [false,'产品不存在或已被删除'];
  248. $customer = $customer->toArray();
  249. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  250. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  251. return [true, $customer];
  252. }
  253. public function productCommon($data,$user, $field = []){
  254. if(empty($field)) $field = Product::$field;
  255. $model = Product::where('del_time',0)
  256. ->select($field)
  257. ->orderby('code', 'asc');
  258. if(! empty($data['title'])) {
  259. // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
  260. $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title']));
  261. // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
  262. $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%']);
  263. }
  264. if(! empty($data['product_category_id'])) $model->where('product_category_id', $data['product_category_id']);
  265. if(! empty($data['category'])) $model->where('code', 'LIKE', '%'.$data['category'].'%');
  266. if(! empty($data['size'])) $model->where('size', 'LIKE', '%'.$data['size'].'%');
  267. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  268. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  269. $model->where('crt_time','>=',$return[0]);
  270. $model->where('crt_time','<=',$return[1]);
  271. }
  272. return $model;
  273. }
  274. /**
  275. * 产品列表
  276. * @param $data
  277. * @param $user
  278. * @return array
  279. */
  280. public function productList($data,$user){
  281. $model = $this->productCommon($data, $user);
  282. $list = $this->limit($model,'',$data);
  283. $list = $this->fillData($list,$user,$data);
  284. return [true, $list];
  285. }
  286. /**
  287. * 产品参数规则
  288. * @param $data
  289. * @param $is_add
  290. * @return array
  291. */
  292. public function productRule(&$data, $user, $is_add = true){
  293. if(empty($data['code'])) return [false,'产品编码不能为空'];
  294. if(empty($data['title'])) return [false,'产品名称不能为空'];
  295. if(! isset($data['cost'])) return [false, '请填写成本'];
  296. if(! isset($data['business_cost'])) return [false, '请填写业务成本'];
  297. $res = $this->checkNumber($data['cost']);
  298. if(! $res) return [false,'成本请输入不超过两位小数并且大于等于0的数值'];
  299. $res = $this->checkNumber($data['business_cost']);
  300. if(! $res) return [false,'业务成本请输入不超过两位小数并且大于等于0的数值'];
  301. if(! empty($data['write_off_price'])){
  302. $res = $this->checkNumber($data['write_off_price']);
  303. if(! $res) return [false,'核销单价请输入不超过两位小数并且大于等于0的数值'];
  304. }
  305. if(! empty($data['major_client_settlement_price'])){
  306. $res = $this->checkNumber($data['major_client_settlement_price']);
  307. if(! $res) return [false,'大客户结算单价请输入不超过两位小数并且大于等于0的数值'];
  308. }
  309. if(! empty($data['return_change_price'])){
  310. $res = $this->checkNumber($data['return_change_price']);
  311. if(! $res) return [false,'退货损耗单价请输入不超过两位小数并且大于等于0的数值'];
  312. }
  313. if($is_add){
  314. $bool = Product::where('code', $data['code'])
  315. ->where('del_time',0)
  316. ->exists();
  317. }else{
  318. if(empty($data['id'])) return [false,'ID不能为空'];
  319. $bool = Product::where('code', $data['code'])
  320. ->where('id','<>',$data['id'])
  321. ->where('del_time',0)
  322. ->exists();
  323. }
  324. if($bool) return [false,'产品编码不能重复'];
  325. return [true, $data];
  326. }
  327. /**
  328. * 拼接数据
  329. * @param $data
  330. * @return array
  331. */
  332. public function fillData($data, $user, $search){
  333. if(empty($data['data'])) return $data;
  334. foreach ($data['data'] as $key => $value){
  335. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  336. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  337. }
  338. return $data;
  339. }
  340. }