CustomerSupplyService.php 15 KB

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