SupplierService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Supplier;
  4. use App\Model\SupplierInfo;
  5. use App\Model\Depart;
  6. use App\Model\Employee;
  7. use Illuminate\Support\Facades\DB;
  8. class SupplierService extends Service
  9. {
  10. /**
  11. * 供应商编辑
  12. * @param $data
  13. * @param $user
  14. * @return array
  15. */
  16. public function customerEdit($data,$user){
  17. list($status,$msg) = $this->customerRule($data,$user, false);
  18. if(!$status) return [$status,$msg];
  19. try {
  20. DB::beginTransaction();
  21. $model = Supplier::where('id',$data['id'])->first();
  22. $model->title = $data['title'];
  23. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  24. $model->address2 = $data['address2'] ?? '';
  25. $model->mark = $data['mark'] ?? '';
  26. $model->is_main = $data['is_main'] ?? 0;
  27. $model->save();
  28. $time = time();
  29. SupplierInfo::where('del_time',0)
  30. ->where('supplier_id',$data['id'])
  31. ->update(['del_time' => $time]);
  32. if(! empty($data['employee'])){
  33. $insert = [];
  34. foreach ($data['employee'] as $value){
  35. $insert[] = [
  36. 'supplier_id' => $model->id,
  37. 'data_id' => $value,
  38. 'type' => SupplierInfo::type_two,
  39. 'crt_time' => $time,
  40. ];
  41. }
  42. SupplierInfo::insert($insert);
  43. }
  44. if(! empty($data['depart'])){
  45. $insert = [];
  46. foreach ($data['depart'] as $value){
  47. $insert[] = [
  48. 'supplier_id' => $model->id,
  49. 'data_id' => $value,
  50. 'type' => SupplierInfo::type_one,
  51. 'crt_time' => $time,
  52. ];
  53. }
  54. SupplierInfo::insert($insert);
  55. }
  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. * @param $user
  67. * @return array
  68. */
  69. public function customerAdd($data,$user){
  70. list($status,$msg) = $this->customerRule($data,$user);
  71. if(!$status) return [$status,$msg];
  72. try {
  73. DB::beginTransaction();
  74. $model = new Supplier();
  75. $model->title = $data['title'];
  76. $model->mobile = $data['mobile'] ?? "";
  77. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  78. $model->address2 = $data['address2'] ?? '';
  79. $model->crt_id = $user['id'];
  80. $model->mark = $data['mark'] ?? '';
  81. $model->depart_id = $data['depart_id'] ?? 0;
  82. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  83. $model->is_main = $data['is_main'] ?? 0;
  84. $model->save();
  85. $time = time();
  86. if(! empty($data['employee'])){
  87. $insert = [];
  88. foreach ($data['employee'] as $value){
  89. $insert[] = [
  90. 'supplier_id' => $model->id,
  91. 'data_id' => $value,
  92. 'type' => SupplierInfo::type_two,
  93. 'crt_time' => $time,
  94. ];
  95. }
  96. SupplierInfo::insert($insert);
  97. }
  98. if(! empty($data['depart'])){
  99. $insert = [];
  100. foreach ($data['depart'] as $value){
  101. $insert[] = [
  102. 'supplier_id' => $model->id,
  103. 'data_id' => $value,
  104. 'type' => SupplierInfo::type_one,
  105. 'crt_time' => $time,
  106. ];
  107. }
  108. SupplierInfo::insert($insert);
  109. }
  110. DB::commit();
  111. }catch (\Exception $exception){
  112. DB::rollBack();
  113. return [false,$exception->getMessage()];
  114. }
  115. return [true,''];
  116. }
  117. /**
  118. * 供应商删除
  119. * @param $data
  120. * @return array
  121. */
  122. public function customerDel($data){
  123. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  124. try {
  125. DB::beginTransaction();
  126. Supplier::where('id',$data['id'])->update([
  127. 'del_time'=> time()
  128. ]);
  129. SupplierInfo::where('del_time',0)
  130. ->where('supplier_id',$data['id'])
  131. ->update(['del_time' => time()]);
  132. DB::commit();
  133. }catch (\Exception $exception){
  134. DB::rollBack();
  135. return [false,$exception->getMessage()];
  136. }
  137. return [true,''];
  138. }
  139. /**
  140. * 供应商详情
  141. * @param $data
  142. * @return array
  143. */
  144. public function customerDetail($data){
  145. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  146. $customer = Supplier::where('del_time',0)
  147. ->where('id',$data['id'])
  148. ->first();
  149. if(empty($customer)) return [false,'供应商不存在或已被删除'];
  150. $customer = $customer->toArray();
  151. $address = '';
  152. if(! empty($customer['address1'])) {
  153. $tmp = json_decode($customer['address1'],true);
  154. $customer['address1'] = $tmp;
  155. $tmp = implode(' ',$tmp);
  156. $tmp .= ' ' . $customer['address2'];
  157. $address = $tmp;
  158. }
  159. $customer['address'] = $address;
  160. $customer_info = SupplierInfo::where('del_time',0)
  161. ->where('supplier_id',$customer['id'])
  162. ->get()->toArray();
  163. $emp_id = [];
  164. $emp_id[] = $customer['crt_id'];
  165. $depart = [];
  166. foreach ($customer_info as $value){
  167. if($value['type'] == SupplierInfo::type_two){
  168. $emp_id[] = $value['data_id'];
  169. }else{
  170. $depart[] = $value['data_id'];
  171. }
  172. }
  173. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  174. ->pluck('emp_name','id')
  175. ->toArray();
  176. $depart_map = Depart::whereIn('id',array_unique($depart))
  177. ->pluck('title','id')
  178. ->toArray();
  179. foreach ($customer_info as $value){
  180. if($value['type'] == SupplierInfo::type_one){
  181. $tmp = [
  182. 'id' => $value['data_id'],
  183. 'name' => $depart_map[$value['data_id']],
  184. ];
  185. $customer['depart'][] = $tmp;
  186. }elseif ($value['type'] == SupplierInfo::type_two){
  187. $tmp = [
  188. 'id' => $value['data_id'],
  189. 'name' => $emp_map[$value['data_id']] ?? '',
  190. ];
  191. $customer['employee'][] = $tmp;
  192. }
  193. }
  194. $customer['crt_name'] = $emp_map[$customer['crt_id']] ?? '';
  195. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  196. return [true, $customer];
  197. }
  198. /**
  199. * 供应商列表
  200. * @param $data
  201. * @param $user
  202. * @return array
  203. */
  204. public function customerList($data,$user){
  205. $model = new Supplier(['userData' => $user,'search'=> $data]);
  206. $model = $model->where('del_time',0)
  207. ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main','mark')
  208. ->orderby('id', 'asc');
  209. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  210. $list = $this->limit($model,'',$data);
  211. $list = $this->fillData($list);
  212. return [true, $list];
  213. }
  214. /**
  215. * 供应商参数规则
  216. * @param $data
  217. * @param $is_add
  218. * @return array
  219. */
  220. public function customerRule(&$data, $user, $is_add = true){
  221. if(empty($data['title'])) return [false,'供应商名称不能为空'];
  222. //所属部门 以及 顶级部门
  223. if(empty($data['depart_id'])) $data['depart_id'] = $this->getDepart($user);
  224. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  225. if($is_add){
  226. $bool = Supplier::where('del_time',0)
  227. ->where('title',$data['title'])
  228. ->where('top_depart_id',$data['top_depart_id'])
  229. ->exists();
  230. }else{
  231. if(empty($data['id'])) return [false,'ID不能为空'];
  232. $top_depart_id = Supplier::where('id',$data['id'])->value('top_depart_id');
  233. $bool = Supplier::where('del_time',0)
  234. ->where('id','<>',$data['id'])
  235. ->where('top_depart_id',$top_depart_id)
  236. ->where('title',$data['title'])
  237. ->exists();
  238. }
  239. if($bool) return [false,'供应商名称不能重复'];
  240. return [true, ''];
  241. }
  242. /**
  243. * 拼接数据
  244. * @param $data
  245. * @return array
  246. */
  247. public function fillData($data){
  248. if(empty($data['data'])) return $data;
  249. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  250. ->pluck('emp_name','id')
  251. ->toArray();
  252. foreach ($data['data'] as $key => $value){
  253. $address = '';
  254. if(! empty($value['address1'])) {
  255. $tmp = json_decode($value['address1'],true);
  256. $tmp = implode(' ',$tmp);
  257. $tmp .= ' ' . $value['address2'];
  258. $address = $tmp;
  259. }
  260. $data['data'][$key]['address'] = $address;
  261. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  262. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  263. }
  264. return $data;
  265. }
  266. }