CustomerSupplyService.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace App\Service;
  3. use App\Model\Employee;
  4. use App\Model\CustomerSupply;
  5. use App\Model\CustomerSupplyDetails;
  6. use App\Model\Organization;
  7. use Illuminate\Support\Facades\DB;
  8. class CustomerSupplyService extends Service
  9. {
  10. public function customerSupplyEdit($data,$user){
  11. list($status,$msg) = $this->customerSupplyRule($data, $user, false);
  12. if(!$status) return [$status,$msg];
  13. try {
  14. DB::beginTransaction();
  15. $model = CustomerSupply::where('id',$data['id'])->first();
  16. $model->code = $data['code'] ?? '';
  17. $model->title = $data['title'] ?? '';
  18. $model->type = $data['type'] ?? 0;
  19. $model->status = $data['status'] ?? 0;
  20. $model->year = $data['year'] ?? 0;
  21. $model->first_scene = $data['first_scene'] ?? "";
  22. $model->business = $data['business'] ?? "";
  23. $model->connect_man = $data['connect_man'] ?? "";
  24. $model->gender = $data['gender'] ?? "";
  25. $model->interest = $data['interest'] ?? "";
  26. $model->family = $data['family'] ?? "";
  27. $model->ability = $data['ability'] ?? "";
  28. $model->profession = $data['profession'] ?? "";
  29. $model->mobile = $data['mobile'] ?? "";
  30. $model->open = $data['open'] ?? "";
  31. $model->attitude = $data['attitude'] ?? "";
  32. $model->style = $data['style'] ?? "";
  33. $model->save();
  34. $time = time();
  35. CustomerSupplyDetails::where('del_time',0)
  36. ->where('customer_supply_id', $model->id)
  37. ->update(['del_time' => $time]);
  38. $this->saveDetail($model->id, $time, $data);
  39. DB::commit();
  40. }catch (\Exception $exception){
  41. DB::rollBack();
  42. return [false,$exception->getMessage()];
  43. }
  44. return [true, ''];
  45. }
  46. public function customerSupplyAdd($data,$user){
  47. list($status,$msg) = $this->customerSupplyRule($data, $user);
  48. if(!$status) return [$status,$msg];
  49. try {
  50. DB::beginTransaction();
  51. $model = new CustomerSupply();
  52. $model->code = $data['code'] ?? '';
  53. $model->title = $data['title'] ?? '';
  54. $model->type = $data['type'] ?? 0;
  55. $model->status = $data['status'] ?? 0;
  56. $model->year = $data['year'] ?? 0;
  57. $model->first_scene = $data['first_scene'] ?? "";
  58. $model->business = $data['business'] ?? "";
  59. $model->connect_man = $data['connect_man'] ?? "";
  60. $model->gender = $data['gender'] ?? "";
  61. $model->interest = $data['interest'] ?? "";
  62. $model->family = $data['family'] ?? "";
  63. $model->ability = $data['ability'] ?? "";
  64. $model->profession = $data['profession'] ?? "";
  65. $model->mobile = $data['mobile'] ?? "";
  66. $model->open = $data['open'] ?? "";
  67. $model->attitude = $data['attitude'] ?? "";
  68. $model->style = $data['style'] ?? "";
  69. $model->crt_id = $user['id'];
  70. $model->save();
  71. $account = str_pad($model->id, 10, '0', STR_PAD_LEFT);
  72. CustomerSupply::where('id', $model->id)
  73. ->update(['account'=> $account]);
  74. $this->saveDetail($model->id, time(), $data);
  75. DB::commit();
  76. }catch (\Exception $exception){
  77. DB::rollBack();
  78. return [false,$exception->getMessage()];
  79. }
  80. return [true, ''];
  81. }
  82. private function saveDetail($id, $time, $data){
  83. if(! empty($data['organization'])){
  84. $unit = [];
  85. foreach ($data['organization'] as $value){
  86. $unit[] = [
  87. 'customer_supply_id' => $id,
  88. 'organization_id' => $value['organization_id'],
  89. 'position' => $value['position'],
  90. 'background' => $value['background'],
  91. 'leader' => $value['leader'],
  92. 'fg_leader' => $value['fg_leader'],
  93. 'relationship' => $value['relationship'],
  94. 'point_demand' => $value['point_demand'],
  95. 'values' => $value['values'],
  96. 'crt_time' => $time,
  97. ];
  98. }
  99. if(! empty($unit)) CustomerSupplyDetails::insert($unit);
  100. }
  101. }
  102. private function getDetail($id){
  103. $data = CustomerSupplyDetails::where('del_time',0)
  104. ->where('customer_supply_id', $id)
  105. ->get()->toArray();
  106. $map = Organization::whereIn('id', array_column($data['organization_id']))
  107. ->pluck('title','id')
  108. ->toArray();
  109. $unit = [];
  110. foreach ($data as $value){
  111. $title = $map[$value['organization_id']] ?? "";
  112. $unit[] = [
  113. 'organization_id' => $value['organization_id'],
  114. 'organization_title' => $title,
  115. 'position' => $value['position'],
  116. 'background' => $value['background'],
  117. 'leader' => $value['leader'],
  118. 'fg_leader' => $value['fg_leader'],
  119. 'relationship' => $value['relationship'],
  120. 'point_demand' => $value['point_demand'],
  121. 'values' => $value['values'],
  122. ];
  123. }
  124. $detail = [
  125. 'organization' => $unit,
  126. ];
  127. foreach ($detail as $key => $value) {
  128. if (empty($value)) {
  129. $detail[$key] = (object)[]; // 转成 stdClass 对象
  130. }
  131. }
  132. return $detail;
  133. }
  134. public function customerSupplyDel($data){
  135. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  136. try {
  137. DB::beginTransaction();
  138. $time = time();
  139. CustomerSupply::where('del_time',0)
  140. ->whereIn('id',$data['id'])
  141. ->update(['del_time' => $time]);
  142. CustomerSupplyDetails::where('del_time',0)
  143. ->where('customer_supply_id', $data['id'])
  144. ->update(['del_time' => $time]);
  145. DB::commit();
  146. }catch (\Exception $exception){
  147. DB::rollBack();
  148. return [false,$exception->getMessage()];
  149. }
  150. return [true, ''];
  151. }
  152. public function customerSupplyDetail($data, $user){
  153. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  154. $customer = CustomerSupply::where('del_time',0)
  155. ->where('id',$data['id'])
  156. ->first();
  157. if(empty($customer)) return [false,'人员不存在或已被删除'];
  158. $customer = $customer->toArray();
  159. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  160. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  161. $details = $this->getDetail($data['id']);
  162. $customer = array_merge($customer, $details);
  163. return [true, $customer];
  164. }
  165. public function customerSupplyCommon($data,$user, $field = []){
  166. if(empty($field)) $field = CustomerSupply::$field;
  167. $model = CustomerSupply::Clear($user,$data);
  168. $model = $model->where('del_time',0)
  169. ->select($field)
  170. ->orderby('id', 'desc');
  171. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  172. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  173. if(! empty($data['type'])) $model->where('type', $data['type']);
  174. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  175. if(! empty($data['crt_id'])) $model->whereIn('crt_id', $data['crt_id']);
  176. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  177. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  178. $model->where('crt_time','>=',$return[0]);
  179. $model->where('crt_time','<=',$return[1]);
  180. }
  181. return $model;
  182. }
  183. public function customerSupplyList($data,$user){
  184. $model = $this->customerSupplyCommon($data, $user);
  185. $list = $this->limit($model,'',$data);
  186. $list = $this->fillData($list,$user,$data);
  187. return [true, $list];
  188. }
  189. public function customerSupplyRule(&$data, $user, $is_add = true){
  190. if(empty($data['code'])) return [false, '编码不能为空'];
  191. if(empty($data['title'])) return [false, '名称不能为空'];
  192. if(empty($data['type'])) return [false, '人员类型不能为空'];
  193. if(! isset(CustomerSupply::$type_name[$data['type']])) return [false, '人员类型错误'];
  194. if(! empty($data['status']) && ! isset(CustomerSupply::$status_name[$data['status']])) return [false, '合作状态错误'];
  195. list($status, $msg) = $this->checkArrayRepeat($data['organization'],'organization_id','组织');
  196. if(! $status) return [false, $msg];
  197. if($is_add){
  198. $bool = CustomerSupply::where('code',$data['code'])
  199. ->where('crt_id', $user['id'])
  200. ->where('del_time',0)
  201. ->exists();
  202. }else{
  203. if(empty($data['id'])) return [false,'ID不能为空'];
  204. $bool = CustomerSupply::where('code',$data['code'])
  205. ->where('crt_id', $user['id'])
  206. ->where('id','<>',$data['id'])
  207. ->where('del_time',0)
  208. ->exists();
  209. }
  210. if($bool) return [false, '编码已存在'];
  211. return [true, $data];
  212. }
  213. public function fillData($data, $user, $search){
  214. if(empty($data['data'])) return $data;
  215. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'id')));
  216. $emp_2 = $this->getEmployeeMap(array_column($data['data'],'id'),'detail');
  217. foreach ($data['data'] as $key => $value){
  218. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  219. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  220. $data['data'][$key]['type_title'] = CustomerSupply::$type_name[$value['type']] ?? '';
  221. $data['data'][$key]['status_title'] = CustomerSupply::$status_name[$data['status']] ?? '';
  222. $e = $emp_2[$value['id']] ?? [];
  223. $data['data'][$key]['organization_title'] = $e['organization_title'] ?? "";
  224. }
  225. return $data;
  226. }
  227. public function getEmployeeMap($employee_ids, $type = ""){
  228. if(empty($employee_ids)) return [];
  229. if(! is_array($employee_ids)) $employee_ids = [$employee_ids];
  230. $result = CustomerSupply::whereIn('id', $employee_ids)
  231. ->select('title', 'id', 'code')
  232. ->get()->toArray();
  233. if(! $type) return array_column($result,'title','id');
  234. $details = CustomerSupplyDetails::from('customer_supply_details as b')
  235. ->leftJoin('organization as b','b.id','a.organization_id')
  236. ->where('a.del_time',0)
  237. ->whereIn('a.customer_supply_id',$employee_ids)
  238. ->select('a.organization_id','a.title','a.customer_supply_id')
  239. ->get()->toArray();
  240. $details_map = [];
  241. foreach ($details as $value){
  242. $details_map[$value['customer_supply_id']][] = [
  243. 'organization_id' => $value['organization_id'],
  244. 'organization_title' => $value['organization_title'],
  245. ];
  246. }
  247. foreach ($result as $key => $value){
  248. $organization = $details_map[$value['id']] ?? [];
  249. $string = "";
  250. if(! empty($organization)){
  251. $string = implode(',',array_column($organization,'organization_title'));
  252. }
  253. $result[$key]['organization_title'] = $string;
  254. }
  255. return array_column($result,null,'id');
  256. }
  257. }