CustomerSupplyService.php 12 KB

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