customerSupplyRule($data, $user, false); if(!$status) return [$status,$msg]; try { DB::beginTransaction(); $model = CustomerSupply::where('id',$data['id'])->first(); $model->code = $data['code'] ?? ''; $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 = $data['code'] ?? ''; $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']); DB::commit(); }catch (\Exception $exception){ DB::rollBack(); return [false,$exception->getMessage()]; } return [true, '']; } 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['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['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 customerSupplyRule(&$data, $user, $is_add = true){ if(empty($data['code'])) return [false, '编码不能为空']; 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){ $bool = CustomerSupply::where('code',$data['code']) ->where('crt_id', $user['id']) ->where('del_time',0) ->exists(); }else{ if(empty($data['id'])) return [false,'ID不能为空']; $bool = CustomerSupply::where('code',$data['code']) ->where('crt_id', $user['id']) ->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; $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'id'))); $emp_2 = $this->getEmployeeMap(array_column($data['data'],'id'),'detail'); 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'] ?? ""; } return $data; } 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'); } }