CustomerSupplyService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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->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. //更新绑定关系
  40. (new WxEmployeeService())->updateWxEmployeeOfficial2($data['mobile']);
  41. DB::commit();
  42. }catch (\Exception $exception){
  43. DB::rollBack();
  44. return [false,$exception->getMessage()];
  45. }
  46. return [true, ''];
  47. }
  48. public function customerSupplyAdd($data,$user){
  49. list($status,$msg) = $this->customerSupplyRule($data, $user);
  50. if(!$status) return [$status,$msg];
  51. try {
  52. DB::beginTransaction();
  53. $model = new CustomerSupply();
  54. $model->code = $this->generateCode($user['id']);
  55. $model->title = $data['title'] ?? '';
  56. $model->type = $data['type'] ?? 0;
  57. $model->status = $data['status'] ?? 0;
  58. $model->year = $data['year'] ?? 0;
  59. $model->first_scene = $data['first_scene'] ?? "";
  60. $model->business = $data['business'] ?? "";
  61. $model->connect_man = $data['connect_man'] ?? "";
  62. $model->gender = $data['gender'] ?? "";
  63. $model->interest = $data['interest'] ?? "";
  64. $model->family = $data['family'] ?? "";
  65. $model->ability = $data['ability'] ?? "";
  66. $model->profession = $data['profession'] ?? "";
  67. $model->mobile = $data['mobile'] ?? "";
  68. $model->open = $data['open'] ?? "";
  69. $model->attitude = $data['attitude'] ?? "";
  70. $model->style = $data['style'] ?? "";
  71. $model->crt_id = $user['id'];
  72. $model->save();
  73. $account = str_pad($model->id, 10, '0', STR_PAD_LEFT);
  74. CustomerSupply::where('id', $model->id)
  75. ->update(['account'=> $account, 'password' => $this->createPassword()]);
  76. $this->saveDetail($model->id, time(), $data);
  77. //更新绑定关系
  78. (new WxEmployeeService())->updateWxEmployeeOfficial2($data['mobile']);
  79. DB::commit();
  80. }catch (\Exception $exception){
  81. DB::rollBack();
  82. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  83. return [false, '网络波动,请重新操作!'];
  84. }
  85. return [false,$exception->getMessage()];
  86. }
  87. return [true, ''];
  88. }
  89. private function createPassword(){
  90. return substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'), 0, 8);
  91. }
  92. private function saveDetail($id, $time, $data){
  93. if(! empty($data['organization'])){
  94. $unit = [];
  95. foreach ($data['organization'] as $value){
  96. $unit[] = [
  97. 'customer_supply_id' => $id,
  98. 'organization_id' => $value['organization_id'],
  99. 'position' => $value['position'],
  100. 'background' => $value['background'],
  101. 'leader' => $value['leader'],
  102. 'fg_leader' => $value['fg_leader'],
  103. 'relationship' => $value['relationship'],
  104. 'point_demand' => $value['point_demand'],
  105. 'values' => $value['values'],
  106. 'crt_time' => $time,
  107. ];
  108. }
  109. if(! empty($unit)) CustomerSupplyDetails::insert($unit);
  110. }
  111. }
  112. private function getDetail($id){
  113. $data = CustomerSupplyDetails::where('del_time',0)
  114. ->where('customer_supply_id', $id)
  115. ->get()->toArray();
  116. $map = Organization::whereIn('id', array_column($data,'organization_id'))
  117. ->pluck('title','id')
  118. ->toArray();
  119. $unit = [];
  120. foreach ($data as $value){
  121. $title = $map[$value['organization_id']] ?? "";
  122. $unit[] = [
  123. 'organization_id' => $value['organization_id'],
  124. 'organization_title' => $title,
  125. 'position' => $value['position'],
  126. 'background' => $value['background'],
  127. 'leader' => $value['leader'],
  128. 'fg_leader' => $value['fg_leader'],
  129. 'relationship' => $value['relationship'],
  130. 'point_demand' => $value['point_demand'],
  131. 'values' => $value['values'],
  132. ];
  133. }
  134. $detail = [
  135. 'organization' => $unit,
  136. ];
  137. foreach ($detail as $key => $value) {
  138. if (empty($value)) {
  139. $detail[$key] = (object)[]; // 转成 stdClass 对象
  140. }
  141. }
  142. return $detail;
  143. }
  144. public function customerSupplyDel($data){
  145. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  146. try {
  147. DB::beginTransaction();
  148. $time = time();
  149. CustomerSupply::where('del_time',0)
  150. ->whereIn('id',$data['id'])
  151. ->update(['del_time' => $time]);
  152. CustomerSupplyDetails::where('del_time',0)
  153. ->where('customer_supply_id', $data['id'])
  154. ->update(['del_time' => $time]);
  155. DB::commit();
  156. }catch (\Exception $exception){
  157. DB::rollBack();
  158. return [false,$exception->getMessage()];
  159. }
  160. return [true, ''];
  161. }
  162. public function customerSupplyDetail($data, $user){
  163. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  164. $customer = CustomerSupply::where('del_time',0)
  165. ->where('id',$data['id'])
  166. ->first();
  167. if(empty($customer)) return [false,'人员不存在或已被删除'];
  168. $customer = $customer->toArray();
  169. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  170. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  171. $details = $this->getDetail($data['id']);
  172. $customer = array_merge($customer, $details);
  173. return [true, $customer];
  174. }
  175. public function customerSupplyCommon($data,$user, $field = []){
  176. if(empty($field)) $field = CustomerSupply::$field;
  177. $model = CustomerSupply::Clear($user,$data);
  178. $model = $model->where('del_time',0)
  179. ->select($field)
  180. ->orderby('id', 'desc');
  181. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  182. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  183. if(! empty($data['type'])) $model->where('type', $data['type']);
  184. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  185. if(! empty($data['crt_id'])) $model->whereIn('crt_id', $data['crt_id']);
  186. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  187. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  188. $model->where('crt_time','>=',$return[0]);
  189. $model->where('crt_time','<=',$return[1]);
  190. }
  191. return $model;
  192. }
  193. public function customerSupplyList($data,$user){
  194. $model = $this->customerSupplyCommon($data, $user);
  195. $list = $this->limit($model,'',$data);
  196. $list = $this->fillData($list,$user,$data);
  197. return [true, $list];
  198. }
  199. public function customerSupplyList2($data,$user){
  200. $model = $this->customerSupplyCommon($data, $user);
  201. $list = $this->limit($model,'',$data);
  202. $list = $this->fillData($list,$user,$data);
  203. // 加上统计
  204. $list['sum_array'] = $this->countStatus($data, $user);
  205. return [true, $list];
  206. }
  207. public function countStatus($data, $user)
  208. {
  209. $model = $this->customerSupplyCommon($data, $user);
  210. // 统计各状态
  211. $status1 = (clone $model)->where('status', 1)->count();
  212. $status2 = (clone $model)->where('status', 2)->count();
  213. $status3 = (clone $model)->where('status', 3)->count();
  214. $status4 = (clone $model)->where('status', 4)->count();
  215. $status5 = (clone $model)->where('status', 5)->count();
  216. return [
  217. 'status_1' => $status1,
  218. 'status_2' => $status2,
  219. 'status_3' => $status3,
  220. 'status_4' => $status4,
  221. 'status_5' => $status5,
  222. ];
  223. }
  224. public function customerSupplyListForSearch($data,$user){
  225. $model = $this->customerSupplyCommon($data, $user,['id']);
  226. $id = $model->get()->toArray();
  227. $id = array_column($id,'id');
  228. return [true, $id];
  229. }
  230. public function customerSupplyRule(&$data, $user, $is_add = true){
  231. if(empty($data['title'])) return [false, '名称不能为空'];
  232. if(empty($data['type'])) return [false, '人员类型不能为空'];
  233. if(! isset(CustomerSupply::$type_name[$data['type']])) return [false, '人员类型错误'];
  234. if(empty($data['mobile'])) return [false, '手机号码不能为空'];
  235. if(! $this->isValidPhone($data['mobile'])) return [false, '手机号码格式错误'];
  236. if(! empty($data['status']) && ! isset(CustomerSupply::$status_name[$data['status']])) return [false, '合作状态错误'];
  237. list($status, $msg) = $this->checkArrayRepeat($data['organization'],'organization_id','组织');
  238. if(! $status) return [false, $msg];
  239. if($is_add){
  240. }else{
  241. if(empty($data['id'])) return [false,'ID不能为空'];
  242. $bool = CustomerSupply::where('id',$data['id'])
  243. ->where('del_time',0)
  244. ->exists();
  245. if($bool) return [false, '人员不存在或已被删除'];
  246. }
  247. return [true, $data];
  248. }
  249. public function fillData($data, $user, $search){
  250. if(empty($data['data'])) return $data;
  251. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  252. $emp_2 = $this->getEmployeeMap(array_column($data['data'],'id'),'detail');
  253. foreach ($data['data'] as $key => $value){
  254. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  255. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  256. $data['data'][$key]['type_title'] = CustomerSupply::$type_name[$value['type']] ?? '';
  257. $data['data'][$key]['status_title'] = CustomerSupply::$status_name[$value['status']] ?? '';
  258. $e = $emp_2[$value['id']] ?? [];
  259. $data['data'][$key]['organization_title'] = $e['organization_title'] ?? "";
  260. }
  261. return $data;
  262. }
  263. public function getEmployeeMap($employee_ids, $type = ""){
  264. if(empty($employee_ids)) return [];
  265. if(! is_array($employee_ids)) $employee_ids = [$employee_ids];
  266. $result = CustomerSupply::whereIn('id', $employee_ids)
  267. ->select('title', 'id', 'code')
  268. ->get()->toArray();
  269. if(! $type) return array_column($result,'title','id');
  270. $details = CustomerSupplyDetails::from('customer_supply_details as a')
  271. ->leftJoin('organization as b','b.id','a.organization_id')
  272. ->where('a.del_time',0)
  273. ->whereIn('a.customer_supply_id',$employee_ids)
  274. ->select('a.organization_id','b.title','a.customer_supply_id')
  275. ->get()->toArray();
  276. $details_map = [];
  277. foreach ($details as $value){
  278. $details_map[$value['customer_supply_id']][] = [
  279. 'organization_id' => $value['organization_id'],
  280. 'organization_title' => $value['title'],
  281. ];
  282. }
  283. foreach ($result as $key => $value){
  284. $organization = $details_map[$value['id']] ?? [];
  285. $string = "";
  286. if(! empty($organization)){
  287. $string = implode(',',array_column($organization,'organization_title'));
  288. }
  289. $result[$key]['organization_title'] = $string;
  290. }
  291. return array_column($result,null,'id');
  292. }
  293. public function generateCode($crt_id)
  294. {
  295. return DB::transaction(function () use ($crt_id) {
  296. // 加行级锁 FOR UPDATE,避免并发重复
  297. $maxCode = CustomerSupply::where('crt_id', $crt_id)
  298. ->lockForUpdate()
  299. ->max('code');
  300. // 转数字
  301. $num = intval($maxCode);
  302. // +1
  303. $num++;
  304. // 小于 10000 → 保留 4 位前导零
  305. if ($num < 10000) {
  306. $code = str_pad($num, 4, '0', STR_PAD_LEFT);
  307. } else {
  308. $code = (string)$num; // 大于等于10000则直接用数字
  309. }
  310. return $code;
  311. });
  312. }
  313. }