SupplierService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Service;
  3. use App\Model\SeeRange;
  4. use App\Model\Supplier;
  5. use App\Model\Employee;
  6. use Illuminate\Support\Facades\DB;
  7. class SupplierService extends Service
  8. {
  9. /**
  10. * 供应商编辑
  11. * @param $data
  12. * @param $user
  13. * @return array
  14. */
  15. public function customerEdit($data,$user){
  16. list($status,$msg) = $this->customerRule($data,$user, false);
  17. if(!$status) return [$status,$msg];
  18. try {
  19. DB::beginTransaction();
  20. $model = Supplier::where('id',$data['id'])->first();
  21. $model->title = $data['title'];
  22. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  23. $model->address2 = $data['address2'] ?? '';
  24. $model->mark = $data['mark'] ?? '';
  25. $model->is_main = $data['is_main'] ?? 0;
  26. $model->save();
  27. DB::commit();
  28. }catch (\Exception $exception){
  29. DB::rollBack();
  30. return [false,$exception->getMessage()];
  31. }
  32. return [true,''];
  33. }
  34. /**
  35. * 供应商新增
  36. * @param $data
  37. * @param $user
  38. * @return array
  39. */
  40. public function customerAdd($data,$user){
  41. list($status,$msg) = $this->customerRule($data,$user);
  42. if(!$status) return [$status,$msg];
  43. try {
  44. DB::beginTransaction();
  45. $model = new Supplier();
  46. $model->title = $data['title'];
  47. $model->mobile = $data['mobile'] ?? "";
  48. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  49. $model->address2 = $data['address2'] ?? '';
  50. $model->crt_id = $user['id'];
  51. $model->mark = $data['mark'] ?? '';
  52. $model->depart_id = $data['depart_id'] ?? 0;
  53. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  54. $model->is_main = $data['is_main'] ?? 0;
  55. $model->save();
  56. DB::commit();
  57. }catch (\Exception $exception){
  58. DB::rollBack();
  59. return [false,$exception->getMessage()];
  60. }
  61. return [true,''];
  62. }
  63. /**
  64. * 供应商删除
  65. * @param $data
  66. * @return array
  67. */
  68. public function customerDel($data){
  69. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  70. try {
  71. DB::beginTransaction();
  72. Supplier::where('id',$data['id'])->update([
  73. 'del_time'=> time()
  74. ]);
  75. (new RangeService())->RangeDelete($data['id'],SeeRange::type_nine);
  76. DB::commit();
  77. }catch (\Exception $exception){
  78. DB::rollBack();
  79. return [false,$exception->getMessage()];
  80. }
  81. return [true,''];
  82. }
  83. /**
  84. * 供应商详情
  85. * @param $data
  86. * @return array
  87. */
  88. public function customerDetail($data){
  89. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  90. $customer = Supplier::where('del_time',0)
  91. ->where('id',$data['id'])
  92. ->first();
  93. if(empty($customer)) return [false,'供应商不存在或已被删除'];
  94. $customer = $customer->toArray();
  95. $address = '';
  96. if(! empty($customer['address1'])) {
  97. $tmp = json_decode($customer['address1'],true);
  98. $customer['address1'] = $tmp;
  99. $tmp = implode(' ',$tmp);
  100. $tmp .= ' ' . $customer['address2'];
  101. $address = $tmp;
  102. }
  103. $customer['address'] = $address;
  104. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  105. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  106. //可见范围
  107. $return = (new RangeService())->RangeDetail($customer['id'],SeeRange::type_nine);
  108. $customer['depart'] = $return[0] ?? [];
  109. $customer['employee'] = $return[1] ?? [];
  110. return [true, $customer];
  111. }
  112. /**
  113. * 供应商列表
  114. * @param $data
  115. * @param $user
  116. * @return array
  117. */
  118. public function customerList($data,$user){
  119. $model = Supplier::Clear($user,$data);
  120. $model = $model->where('del_time',0)
  121. ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main','mark')
  122. ->orderby('id', 'asc');
  123. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  124. if(! empty($data['get_all'])) $model->orWhere('is_main',Supplier::is_main);
  125. $list = $this->limit($model,'',$data);
  126. $list = $this->fillData($list);
  127. return [true, $list];
  128. }
  129. /**
  130. * 供应商参数规则
  131. * @param $data
  132. * @param $is_add
  133. * @return array
  134. */
  135. public function customerRule(&$data, $user, $is_add = true){
  136. if(empty($data['title'])) return [false,'供应商名称不能为空'];
  137. //所属部门 以及 顶级部门
  138. if(empty($data['depart_id'])) {
  139. $data['depart_id'] = $this->getDepart($user);
  140. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  141. }
  142. if($is_add){
  143. $bool = Supplier::where('del_time',0)
  144. ->where('title',$data['title'])
  145. ->where('top_depart_id',$data['top_depart_id'])
  146. ->exists();
  147. }else{
  148. if(empty($data['id'])) return [false,'ID不能为空'];
  149. $bool = Supplier::where('del_time',0)
  150. ->where('id','<>',$data['id'])
  151. ->where('top_depart_id',$data['top_depart_id'])
  152. ->where('title',$data['title'])
  153. ->exists();
  154. }
  155. if($bool) return [false,'供应商名称不能重复'];
  156. return [true, ''];
  157. }
  158. /**
  159. * 拼接数据
  160. * @param $data
  161. * @return array
  162. */
  163. public function fillData($data){
  164. if(empty($data['data'])) return $data;
  165. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  166. ->pluck('emp_name','id')
  167. ->toArray();
  168. foreach ($data['data'] as $key => $value){
  169. $address = '';
  170. if(! empty($value['address1'])) {
  171. $tmp = json_decode($value['address1'],true);
  172. $tmp = implode(' ',$tmp);
  173. $tmp .= ' ' . $value['address2'];
  174. $address = $tmp;
  175. }
  176. $data['data'][$key]['address'] = $address;
  177. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  178. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  179. }
  180. return $data;
  181. }
  182. }