| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635 |
- <?php
- namespace App\Service;
- use App\Model\CustomerSupply;
- use App\Model\Dimension;
- use App\Model\Employee;
- use App\Model\Quantization;
- use App\Model\QuantizationCreate;
- use App\Model\QuantizationCreateDetails;
- use App\Model\QuantizationDetails;
- use Illuminate\Support\Facades\DB;
- class QuantizationService extends Service
- {
- //量化档案-----------------------------------------------
- public function quantizationEdit($data,$user){
- list($status,$msg) = $this->quantizationRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = Quantization::where('id',$data['id'])->first();
- $model->title = $data['title'] ?? '';
- $model->code = $data['code'] ?? '';
- $model->is_use = $data['is_use'] ?? 0;
- $model->save();
- $time = time();
- QuantizationDetails::where('del_time',0)
- ->where('quantization_id', $data['id'])
- ->update(['del_time' => $time]);
- $this->saveDetail($model->id, $time, $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function quantizationAdd($data,$user){
- list($status,$msg) = $this->quantizationRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new Quantization();
- $model->title = $data['title'] ?? '';
- $model->code = $data['code'] ?? '';
- $model->is_use = $data['is_use'] ?? 0;
- $model->type = $data['type'] ?? 0;
- $model->crt_id = $user['id'];
- $model->save();
- $time = time();
- $this->saveDetail($model->id, $time, $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- private function saveDetail($id, $time, $data){
- foreach ($data['details'] as $value) {
- // 插入父级
- $parent = QuantizationDetails::create([
- 'quantization_id' => $id,
- 'title' => $value['title'],
- 'rate' => $value['rate'],
- 'parent_id' => 0, // 顶层 parent_id = 0
- 'crt_time' => $time,
- ]);
- // 再处理子级
- if (! empty($value['details_son'])) {
- foreach ($value['details_son'] as $son) {
- $detailsSonData[] = [
- 'quantization_id' => $id,
- 'title' => $son['title'],
- 'parent_id' => $parent->id, // 子级关联父级 id
- 'crt_time' => $time,
- ];
- }
- }
- }
- // 批量插入子级
- if (!empty($detailsSonData)) QuantizationDetails::insert($detailsSonData);
- }
- private function getDetail($id){
- // 查所有数据
- $all = QuantizationDetails::where('quantization_id', $id)
- ->where('del_time', 0)
- ->orderBy('id','asc')
- ->get()
- ->toArray();
- // 按 parent_id 分组
- $grouped = [];
- foreach ($all as $item) {
- $grouped[$item['parent_id']][] = $item;
- }
- $result = [];
- // 只取 parent_id = 0 的父级
- if (!empty($grouped[0])) {
- foreach ($grouped[0] as $parent) {
- $parent['details_son'] = $grouped[$parent['id']] ?? (object)[];
- $result[] = $parent;
- }
- }
- return $result;
- }
- public function quantizationDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- Quantization::where('del_time',0)
- ->whereIn('id',$data['id'])
- ->update(['del_time' => $time]);
- QuantizationDetails::where('del_time',0)
- ->whereIn('quantization_id', $data['id'])
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function quantizationDetail($data,$user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = Quantization::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'量化档案不存在或已被删除'];
- $customer = $customer->toArray();
- $details = $this->getDetail($customer['id']);
- $customer['details'] = $details;
- $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']): '';
- return [true, $customer];
- }
- public function quantizationCommon($data,$user, $field = []){
- if(empty($field)) $field = Quantization::$field;
- $model = Quantization::where('del_time',0)
- ->select($field)
- ->orderby('id', 'desc');
- if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
- if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
- if(! empty($data['type'])) $model->where('type', $data['type']);
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
- if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
- 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 quantizationList($data,$user){
- $model = $this->quantizationCommon($data, $user);
- $list = $this->limit($model,'',$data);
- $list = $this->fillData($list,$user);
- return [true, $list];
- }
- public function quantizationRule(&$data, $user, $is_add = true){
- if(empty($data['type'])) return [false, '量化档案类型类型不能为空'];
- if(! isset(Quantization::$type_name[$data['type']])) return [false, '量化档案类型错误'];
- if(empty($data['title'])) return [false, '名称不能为空'];
- if(empty($data['code'])) return [false, '编码不能为空'];
- if(empty($data['details'])) return [false, '评价明细不能为空'];
- foreach ($data['details'] as $key => $value) {
- $rowNum = $key + 1;
- // 校验外层 title 不能为空
- if (empty($value['title'])) {
- return [false, "第{$rowNum}行的评价名称不能为空"];
- }
- // 校验外层 title 不重复
- if (!isset($titleSet)) $titleSet = [];
- if (in_array($value['title'], $titleSet)) {
- return [false, "第{$rowNum}行的评价名称 [{$value['title']}] 重复"];
- }
- $titleSet[] = $value['title'];
- // 校验占比
- if (empty($value['rate']) || ! is_numeric($value['rate']) || floatval($value['rate']) <= 0.0) {
- return [false, "第{$rowNum}行的占比填写错误"];
- }
- // 明细不能为空
- if (! empty($value['details_son'])) {
- // 校验明细中 title 不重复
- $sonTitleSet = [];
- foreach ($value['details_son'] as $sonKey => $val) {
- $sonRowNum = $sonKey + 1;
- if (empty($val['title'])) {
- return [false, "第{$rowNum}行下的第{$sonRowNum}个明细名称不能为空"];
- }
- if (in_array($val['title'], $sonTitleSet)) {
- return [false, "第{$rowNum}行下的明细名称 [{$val['title']}] 重复"];
- }
- $sonTitleSet[] = $val['title'];
- }
- }
- }
- $rate_total = floatval(array_sum(array_column($data['details'],'rate')));
- if($rate_total != 100.0) return [false, '占比之和必须等于100'];
- if($is_add){
- $bool = Quantization::where('code',$data['code'])
- ->where('del_time',0)
- ->exists();
- }else{
- if(empty($data['id'])) return [false,'ID不能为空'];
- $bool = Quantization::where('code',$data['code'])
- ->where('id','<>',$data['id'])
- ->where('del_time',0)
- ->exists();
- }
- if($bool) return [false,'编码已存在'];
- return [true, $data];
- }
- public function fillData($data, $user){
- if(empty($data['data'])) return $data;
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_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]['is_use_title'] = Quantization::$is_use_name[$value['is_use']] ?? '';
- $data['data'][$key]['type_title'] = Quantization::$type_name[$value['type']] ?? '';
- }
- return $data;
- }
- //量化档案-----------------------------------------------
- //量化创建-----------------------------------------------
- public function quantizationCreateEdit($data,$user){
- list($status,$msg) = $this->quantizationCreateRule($data, $user, false);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = QuantizationCreate::where('id',$data['id'])->first();
- $model->customer_supply_id = $data['customer_supply_id'];
- $model->quantization_id = $data['quantization_id'];
- $model->start_time = $data['start_time'] ?? 0;
- $model->end_time = $data['end_time'] ?? 0;
- $model->products = $data['products'] ?? '';
- $model->score = $data['score'] ?? 0;
- $model->save();
- $time = time();
- QuantizationCreateDetails::where('del_time',0)
- ->where('quantization_create_id', $data['id'])
- ->update(['del_time' => $time]);
- $this->saveDetail1($model->id, $time, $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function quantizationCreateAdd($data,$user){
- list($status,$msg) = $this->quantizationCreateRule($data, $user);
- if(!$status) return [$status,$msg];
- try {
- DB::beginTransaction();
- $model = new QuantizationCreate();
- $model->customer_supply_id = $data['customer_supply_id'];
- $model->quantization_id = $data['quantization_id'];
- $model->start_time = $data['start_time'] ?? 0;
- $model->end_time = $data['end_time'] ?? 0;
- $model->products = $data['products'] ?? '';
- $model->score = $data['score'] ?? 0;
- $model->type = $data['type'] ?? 0;
- $model->crt_id = $user['id'];
- $model->save();
- $time = time();
- $this->saveDetail1($model->id, $time, $data);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- private function saveDetail1($id, $time, $data){
- foreach ($data['details'] as $value) {
- // 插入父级
- $parent = QuantizationCreateDetails::create([
- 'quantization_create_id' => $id,
- 'title' => $value['title'],
- 'rate' => $value['rate'],
- 'parent_id' => 0, // 顶层 parent_id = 0
- 'crt_time' => $time,
- 'dimension_id' => $value['dimension_id'] ?? 0,
- 'score' => $value['score'] ?? 0,
- ]);
- // 再处理子级
- if (! empty($value['details_son'])) {
- foreach ($value['details_son'] as $son) {
- $detailsSonData[] = [
- 'quantization_create_id' => $id,
- 'title' => $son['title'],
- 'parent_id' => $parent->id, // 子级关联父级 id
- 'crt_time' => $time,
- 'dimension_id' => $son['dimension_id'] ?? 0,
- 'score' => $son['score'] ?? 0,
- ];
- }
- }
- }
- // 批量插入子级
- if (!empty($detailsSonData)) QuantizationCreateDetails::insert($detailsSonData);
- }
- private function getDetail1($id){
- // 查所有数据
- $all = QuantizationCreateDetails::where('quantization_create_id', $id)
- ->where('del_time', 0)
- ->orderBy('id','asc')
- ->get()
- ->toArray();
- $map = Dimension::whereIn('id',array_unique(array_filter(array_column($all,'dimension_id'))))
- ->pluck('title','id')
- ->toArray();
- // 按 parent_id 分组
- $grouped = [];
- foreach ($all as $item) {
- $item['dimension_title'] = $map[$item['dimension_id']] ?? "";
- $grouped[$item['parent_id']][] = $item;
- }
- $result = [];
- // 只取 parent_id = 0 的父级
- if (!empty($grouped[0])) {
- foreach ($grouped[0] as $parent) {
- $parent['details_son'] = $grouped[$parent['id']] ?? (object)[];
- $result[] = $parent;
- }
- }
- return $result;
- }
- public function quantizationCreateDel($data){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- try {
- DB::beginTransaction();
- $time = time();
- QuantizationCreate::where('del_time',0)
- ->whereIn('id',$data['id'])
- ->update(['del_time' => $time]);
- QuantizationCreateDetails::where('del_time',0)
- ->whereIn('quantization_create_id', $data['id'])
- ->update(['del_time' => $time]);
- DB::commit();
- }catch (\Exception $exception){
- DB::rollBack();
- return [false,$exception->getMessage()];
- }
- return [true, ''];
- }
- public function quantizationCreateDetail($data,$user){
- if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
- $customer = QuantizationCreate::where('del_time',0)
- ->where('id',$data['id'])
- ->first();
- if(empty($customer)) return [false,'量化信息不存在或已被删除'];
- $customer = $customer->toArray();
- $e = $emp_2[$customer['customer_supply_id']] ?? [];
- $customer['organization_title'] = $e['organization_title'] ?? "";
- $customer['customer_supply_title'] = $e['title'] ?? "";
- $customer['customer_supply_code'] = $e['code'] ?? "";
- $customer['quantization_title'] = Quantization::where('id', $customer['quantization_id'])->value('title');
- $details = $this->getDetail1($customer['id']);
- $customer['details'] = $details;
- $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']): '';
- return [true, $customer];
- }
- public function quantizationCreateCommon($data,$user, $field = []){
- if(empty($data['type']) || ! isset(QuantizationCreate::$type_name[$data['type']])) return [false, '类型不存在或错误'];
- if(empty($field)) $field = QuantizationCreate::$field;
- $model = QuantizationCreate::Clear($user,$data);
- $model->where('del_time',0)
- ->select($field)
- ->orderby('id', 'desc');
- if(! empty($data['type'])) $model->where('type', $data['type']);
- if(! empty($data['id'])) $model->whereIn('id', $data['id']);
- if(! empty($data['customer_supply_title'])){
- list($status, $id) = (new DeviceService())->customerSupplyListForSearch(['title' => $data['customer_supply_title']], $user);
- $model->whereIn('customer_supply_id', $id);
- }
- if(! empty($data['customer_supply_code'])){
- list($status, $id) = (new DeviceService())->customerSupplyListForSearch(['code' => $data['customer_supply_code']], $user);
- $model->whereIn('customer_supply_id', $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]);
- }
- if(! empty($data['zq'][0]) && ! empty($data['zq'][1])) {
- $return = $this->changeDateToTimeStampAboutRange($data['zq']);
- $model->where('start_time','>=',$return[0]);
- $model->where('end_time','<=',$return[1]);
- }
- return [true, $model];
- }
- public function quantizationCreateList($data,$user){
- list($status,$model) = $this->quantizationCreateCommon($data, $user);
- if(! $status) return [false,$model];
- $list = $this->limit($model,'',$data);
- $list = $this->fillCreateData($list,$user);
- return [true, $list];
- }
- public function quantizationCreateRule(&$data, $user, $is_add = true){
- if(empty($data['type'])) return [false, '量化档案类型类型不能为空'];
- if(empty($data['quantization_id'])) return [false, '量化档案ID不能为空'];
- $q = Quantization::where('del_time',0)
- ->where('id', $data['quantization_id'])
- ->first();
- if(empty($q)) return [false, '引用的档案不存在或已被删除'];
- $q = $q->toArray();
- if(! isset(QuantizationCreate::$type_name[$data['type']])) return [false, '量化档案类型错误'];
- if($q['type'] != $data['type']) return [false, '引用档案类型与量化档案类型不一致'];
- if($data['type'] == QuantizationCreate::type_two){
- if(empty($data['start_time']) || empty($data['end_time'])) return [false, '周期不能为空'];
- $data['start_time'] = $this->changeDateToDate($data['start_time']);
- $data['end_time'] = $this->changeDateToDate($data['end_time']);
- }
- if(empty($data['customer_supply_id'])) return [false, '人员id不能为空'];
- $bool = CustomerSupply::where('del_time',0)
- ->where('id',$data['customer_supply_id'])
- ->exists();
- if(! $bool) return [false, '人员不存在或已被删除'];
- if(! isset($data['score'])) return [false, '分数不存在'];
- $res = $this->checkNumber($data['score']);
- if(! $res['valid']) return [false,'分数:' . $res['error']];
- if(empty($data['details'])) return [false, '量化详情不能为空'];
- foreach ($data['details'] as $key => $value) {
- $rowNum = $key + 1;
- // 校验外层 title 不能为空
- if (empty($value['title'])) return [false, "第{$rowNum}行的评价名称不能为空"];
- // 校验占比
- if (empty($value['rate']) || ! is_numeric($value['rate']) || floatval($value['rate']) <= 0.0) {
- return [false, "第{$rowNum}行的占比错误"];
- }
- // 明细不能为空
- if (! empty($value['details_son'])) {
- foreach ($value['details_son'] as $sonKey => $val) {
- $sonRowNum = $sonKey + 1;
- if (empty($val['title'])) return [false, "第{$rowNum}行下的第{$sonRowNum}个明细名称不能为空"];
- if (empty($val['dimension_id'])) return [false, "第{$rowNum}行下的第{$sonRowNum}个明细的维度选项不能为空"];
- }
- }else{
- if(empty($value['dimension_id'])) return [false, "第{$rowNum}行的维度选项不能为空"];
- }
- }
- $startTime = $data['start_time'] ?? 0;
- $endTime = $data['end_time'] ?? 0;
- if ($is_add) {
- if ($data['type'] == QuantizationCreate::type_one) {
- // 能力量化:只允许唯一一份
- $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
- ->where('type', QuantizationCreate::type_one)
- ->where('del_time', 0)
- ->exists();
- if ($bool) {
- return [false, '该人员已创建能力量化档案,请勿重复创建'];
- }
- } else {
- // 合作量化:检查时间区间重叠
- $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
- ->where('type', QuantizationCreate::type_two) // 建议限定 type,防止误匹配
- ->where('del_time', 0)
- ->where(function($query) use ($startTime, $endTime) {
- $query->where('start_time', '<=', $endTime)
- ->where('end_time', '>=', $startTime);
- })
- ->exists();
- if ($bool) {
- return [false, '该人员在该周期内已创建合作量化档案,请勿重复创建'];
- }
- }
- } else {
- // 编辑模式
- if (empty($data['id'])) return [false, 'ID不能为空'];
- $id = $data['id'];
- if ($data['type'] == QuantizationCreate::type_one) {
- $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
- ->where('type', QuantizationCreate::type_one)
- ->where('id', '<>', $id)
- ->where('del_time', 0)
- ->exists();
- if ($bool) return [false, '该人员已创建能力量化档案,请勿重复创建'];
- } else {
- $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
- ->where('type', QuantizationCreate::type_two)
- ->where('del_time', 0)
- ->where(function($query) use ($startTime, $endTime) {
- $query->where('start_time', '<=', $endTime)
- ->where('end_time', '>=', $startTime);
- })
- ->where('id', '<>', $id)
- ->exists();
- if ($bool) return [false, '该人员在该周期内已创建合作量化档案,请勿重复创建'];
- }
- }
- return [true, $data];
- }
- public function fillCreateData($data, $user){
- if(empty($data['data'])) return $data;
- $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_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]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
- $data['data'][$key]['end_time'] = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
- $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
- $data['data'][$key]['type_title'] = QuantizationCreate::$type_name[$value['type']] ?? '';
- $e = $emp_2[$value['customer_supply_id']] ?? [];
- $data['data'][$key]['organization_title'] = $e['organization_title'] ?? "";
- $data['data'][$key]['customer_supply_title'] = $e['title'] ?? "";
- $data['data'][$key]['customer_supply_code'] = $e['code'] ?? "";
- $data['data'][$key]['result'] = $this->fillDataResult($value);
- }
- return $data;
- }
- private function fillDataResult($value)
- {
- $score = isset($value['score']) ? floatval($value['score']) : 0;
- $type = $value['type'] ?? null;
- if ($type == QuantizationCreate::type_one) {
- if ($score < 50) return '不合格';
- if ($score < 70) return '改善后评估';
- return '合格';
- }
- // 其他类型
- return $score < 60 ? '不合格' : '合格';
- }
- //量化创建-----------------------------------------------
- }
|