CustomerSupplyService.php 15 KB

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