customerSupplyRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = CustomerSupply::where('id',$data['id'])->first(); $model->title = $data['title'] ?? ''; $model->type = $data['type'] ?? 0; $model->status = $data['status'] ?? 0; $model->year = $data['year'] ?? 0; $model->first_scene = $data['first_scene'] ?? ""; $model->business = $data['business'] ?? ""; $model->connect_man = $data['connect_man'] ?? ""; $model->gender = $data['gender'] ?? ""; $model->interest = $data['interest'] ?? ""; $model->family = $data['family'] ?? ""; $model->ability = $data['ability'] ?? ""; $model->profession = $data['profession'] ?? ""; $model->mobile = $data['mobile'] ?? ""; $model->open = $data['open'] ?? ""; $model->attitude = $data['attitude'] ?? ""; $model->style = $data['style'] ?? ""; $model->save(); $time = time(); CustomerSupplyDetails::where('del_time',0) ->where('customer_supply_id', $model->id) ->update(['del_time' => $time]); $this->saveDetail($model->id, $time, $data); //更新绑定关系 (new WxEmployeeService())->updateWxEmployeeOfficial2($data['mobile']); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function customerSupplyAdd($data,$user){ list($status,$msg) = $this->customerSupplyRule($data, $user); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = new CustomerSupply(); $model->code = $this->generateCode($user['id']); $model->title = $data['title'] ?? ''; $model->type = $data['type'] ?? 0; $model->status = $data['status'] ?? 0; $model->year = $data['year'] ?? 0; $model->first_scene = $data['first_scene'] ?? ""; $model->business = $data['business'] ?? ""; $model->connect_man = $data['connect_man'] ?? ""; $model->gender = $data['gender'] ?? ""; $model->interest = $data['interest'] ?? ""; $model->family = $data['family'] ?? ""; $model->ability = $data['ability'] ?? ""; $model->profession = $data['profession'] ?? ""; $model->mobile = $data['mobile'] ?? ""; $model->open = $data['open'] ?? ""; $model->attitude = $data['attitude'] ?? ""; $model->style = $data['style'] ?? ""; $model->crt_id = $user['id']; $model->save(); $account = str_pad($model->id, 10, '0', STR_PAD_LEFT); CustomerSupply::where('id', $model->id) ->update(['account'=> $account, 'password' => $this->createPassword()]); $this->saveDetail($model->id, time(), $data); //更新绑定关系 (new WxEmployeeService())->updateWxEmployeeOfficial2($data['mobile']); $id = $model->id; DB::commit(); }catch (\Exception $exception){ DB::rollBack(); if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) { return [false, '网络波动,请重新操作!']; } return [false,$exception->getMessage()]; } return [true, ['id' => $id]]; } private function createPassword(){ return substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'), 0, 8); } private function saveDetail($id, $time, $data){ if(! empty($data['organization'])){ $unit = []; foreach ($data['organization'] as $value){ $unit[] = [ 'customer_supply_id' => $id, 'organization_id' => $value['organization_id'], 'position' => $value['position'], 'background' => $value['background'], 'leader' => $value['leader'], 'fg_leader' => $value['fg_leader'], 'relationship' => $value['relationship'], 'point_demand' => $value['point_demand'], 'values' => $value['values'], 'crt_time' => $time, ]; } if(! empty($unit)) CustomerSupplyDetails::insert($unit); } } private function getDetail($id){ $data = CustomerSupplyDetails::where('del_time',0) ->where('customer_supply_id', $id) ->get()->toArray(); $map = Organization::whereIn('id', array_column($data,'organization_id')) ->pluck('title','id') ->toArray(); $unit = []; foreach ($data as $value){ $title = $map[$value['organization_id']] ?? ""; $unit[] = [ 'organization_id' => $value['organization_id'], 'organization_title' => $title, 'position' => $value['position'], 'background' => $value['background'], 'leader' => $value['leader'], 'fg_leader' => $value['fg_leader'], 'relationship' => $value['relationship'], 'point_demand' => $value['point_demand'], 'values' => $value['values'], ]; } $detail = [ 'organization' => $unit, ]; foreach ($detail as $key => $value) { if (empty($value)) { $detail[$key] = (object)[]; // 转成 stdClass 对象 } } return $detail; } public function customerSupplyDel($data){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; try { DB::beginTransaction(); $time = time(); CustomerSupply::where('del_time',0) ->whereIn('id',$data['id']) ->update(['del_time' => $time]); CustomerSupplyDetails::where('del_time',0) ->where('customer_supply_id', $data['id']) ->update(['del_time' => $time]); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } public function customerSupplyDetail($data, $user){ if($this->isEmpty($data,'id')) return [false,'请选择数据!']; $customer = CustomerSupply::where('del_time',0) ->where('id',$data['id']) ->first(); if(empty($customer)) return [false,'人员不存在或已被删除']; $customer = $customer->toArray(); $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name'); $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): ''; $details = $this->getDetail($data['id']); $customer = array_merge($customer, $details); return [true, $customer]; } public function customerSupplyCommon($data,$user, $field = []){ if(empty($field)) $field = CustomerSupply::$field; $model = CustomerSupply::Clear($user,$data); $model = $model->where('del_time',0) ->select($field) ->orderby('id', 'desc'); if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%'); if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%'); if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%'); if(! empty($data['type'])) $model->where('type', $data['type']); if(! empty($data['id'])) $model->whereIn('id', $data['id']); if(! empty($data['crt_id'])) $model->whereIn('crt_id', $data['crt_id']); if(! empty($data['status'])) $model->where('status', $data['status']); if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) { $return = $this->changeDateToTimeStampAboutRange($data['crt_time']); $model->where('crt_time','>=',$return[0]); $model->where('crt_time','<=',$return[1]); } return $model; } public function customerSupplyList($data,$user){ $model = $this->customerSupplyCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list,$user,$data); return [true, $list]; } public function customerSupplyList2($data,$user){ $model = $this->customerSupplyCommon($data, $user); $list = $this->limit($model,'',$data); $list = $this->fillData($list,$user,$data); // 加上统计 $list['sum_array'] = $this->countStatus($data, $user); return [true, $list]; } public function countStatus($data, $user) { if(isset($data['status'])) unset($data['status']); $model = $this->customerSupplyCommon($data, $user); $all = (clone $model)->count(); // 统计各状态 $status1 = (clone $model)->where('status', 1)->count(); $status2 = (clone $model)->where('status', 2)->count(); $status3 = (clone $model)->where('status', 3)->count(); $status4 = (clone $model)->where('status', 4)->count(); $status5 = (clone $model)->where('status', 5)->count(); return [ 'total' => $all, 'status_1' => $status1, 'status_2' => $status2, 'status_3' => $status3, 'status_4' => $status4, 'status_5' => $status5, ]; } public function customerSupplyListForSearch($data,$user){ $model = $this->customerSupplyCommon($data, $user,['id']); $id = $model->get()->toArray(); $id = array_column($id,'id'); return [true, $id]; } public function customerSupplyRule(&$data, $user, $is_add = true){ if(empty($data['title'])) return [false, '名称不能为空']; if(empty($data['type'])) return [false, '人员类型不能为空']; if(! isset(CustomerSupply::$type_name[$data['type']])) return [false, '人员类型错误']; if(empty($data['mobile'])) return [false, '手机号码不能为空']; if(! $this->isValidPhone($data['mobile'])) return [false, '手机号码格式错误']; if(! empty($data['status']) && ! isset(CustomerSupply::$status_name[$data['status']])) return [false, '合作状态错误']; list($status, $msg) = $this->checkArrayRepeat($data['organization'],'organization_id','组织'); if(! $status) return [false, $msg]; if($is_add){ }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = CustomerSupply::where('id',$data['id']) ->where('del_time',0) ->exists(); if(! $bool) return [false, '人员不存在或已被删除']; } return [true, $data]; } public function fillData($data, $user, $search){ if(empty($data['data'])) return $data; $id = array_column($data['data'],'id'); $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id'))); $emp_2 = $this->getEmployeeMap($id,'detail'); $wx_map = $this->getWxBind($id); foreach ($data['data'] as $key => $value){ $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : ''; $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? ''; $data['data'][$key]['type_title'] = CustomerSupply::$type_name[$value['type']] ?? ''; $data['data'][$key]['status_title'] = CustomerSupply::$status_name[$value['status']] ?? ''; $e = $emp_2[$value['id']] ?? []; $data['data'][$key]['organization_title'] = $e['organization_title'] ?? ""; $tmp = $wx_map[$value['id']] ?? 0; $data['data'][$key]['is_wx_title'] = $tmp ? '是' : '否'; } return $data; } private function getWxBind($employee_id = []){ $appid = config("wx_msg.f_appid"); return WxEmployeeOfficial::whereIn('employee_id', $employee_id) ->where('type', WxEmployeeOfficial::login_type_one) ->where('appid', $appid) ->pluck('id', 'employee_id') ->toArray(); } public function getEmployeeMap($employee_ids, $type = ""){ if(empty($employee_ids)) return []; if(! is_array($employee_ids)) $employee_ids = [$employee_ids]; $result = CustomerSupply::whereIn('id', $employee_ids) ->select('title', 'id', 'code') ->get()->toArray(); if(! $type) return array_column($result,'title','id'); $details = CustomerSupplyDetails::from('customer_supply_details as a') ->leftJoin('organization as b','b.id','a.organization_id') ->where('a.del_time',0) ->whereIn('a.customer_supply_id',$employee_ids) ->select('a.organization_id','b.title','a.customer_supply_id') ->get()->toArray(); $details_map = []; foreach ($details as $value){ $details_map[$value['customer_supply_id']][] = [ 'organization_id' => $value['organization_id'], 'organization_title' => $value['title'], ]; } foreach ($result as $key => $value){ $organization = $details_map[$value['id']] ?? []; $string = ""; if(! empty($organization)){ $string = implode(',',array_column($organization,'organization_title')); } $result[$key]['organization_title'] = $string; } return array_column($result,null,'id'); } public function generateCode($crt_id) { return DB::transaction(function () use ($crt_id) { // 加行级锁 FOR UPDATE,避免并发重复 $maxCode = CustomerSupply::where('crt_id', $crt_id) ->lockForUpdate() ->max('code'); // 转数字 $num = intval($maxCode); // +1 $num++; // 小于 10000 → 保留 4 位前导零 if ($num < 10000) { $code = str_pad($num, 4, '0', STR_PAD_LEFT); } else { $code = (string)$num; // 大于等于10000则直接用数字 } return $code; }); } }