QuantizationService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. <?php
  2. namespace App\Service;
  3. use App\Model\CustomerSupply;
  4. use App\Model\Dimension;
  5. use App\Model\Employee;
  6. use App\Model\Quantization;
  7. use App\Model\QuantizationCreate;
  8. use App\Model\QuantizationCreateDetails;
  9. use App\Model\QuantizationDetails;
  10. use Illuminate\Support\Facades\DB;
  11. class QuantizationService extends Service
  12. {
  13. //量化档案-----------------------------------------------
  14. public function quantizationEdit($data,$user){
  15. list($status,$msg) = $this->quantizationRule($data, $user, false);
  16. if(!$status) return [$status,$msg];
  17. try {
  18. DB::beginTransaction();
  19. $model = Quantization::where('id',$data['id'])->first();
  20. $model->title = $data['title'] ?? '';
  21. $model->code = $data['code'] ?? '';
  22. $model->is_use = $data['is_use'] ?? 0;
  23. $model->save();
  24. $time = time();
  25. QuantizationDetails::where('del_time',0)
  26. ->where('quantization_id', $data['id'])
  27. ->update(['del_time' => $time]);
  28. $this->saveDetail($model->id, $time, $data);
  29. DB::commit();
  30. }catch (\Exception $exception){
  31. DB::rollBack();
  32. return [false,$exception->getMessage()];
  33. }
  34. return [true, ''];
  35. }
  36. public function quantizationAdd($data,$user){
  37. list($status,$msg) = $this->quantizationRule($data, $user);
  38. if(!$status) return [$status,$msg];
  39. try {
  40. DB::beginTransaction();
  41. $model = new Quantization();
  42. $model->title = $data['title'] ?? '';
  43. $model->code = $data['code'] ?? '';
  44. $model->is_use = $data['is_use'] ?? 0;
  45. $model->type = $data['type'] ?? 0;
  46. $model->crt_id = $user['id'];
  47. $model->save();
  48. $time = time();
  49. $this->saveDetail($model->id, $time, $data);
  50. DB::commit();
  51. }catch (\Exception $exception){
  52. DB::rollBack();
  53. return [false,$exception->getMessage()];
  54. }
  55. return [true, ''];
  56. }
  57. private function saveDetail($id, $time, $data){
  58. foreach ($data['details'] as $value) {
  59. // 插入父级
  60. $parent = QuantizationDetails::create([
  61. 'quantization_id' => $id,
  62. 'title' => $value['title'],
  63. 'score' => $value['score'],
  64. 'parent_id' => 0, // 顶层 parent_id = 0
  65. 'crt_time' => $time,
  66. ]);
  67. // 再处理子级
  68. if (! empty($value['details_son'])) {
  69. foreach ($value['details_son'] as $son) {
  70. $detailsSonData[] = [
  71. 'quantization_id' => $id,
  72. 'title' => $son['title'],
  73. 'score' => $son['score'],
  74. 'parent_id' => $parent->id, // 子级关联父级 id
  75. 'crt_time' => $time,
  76. ];
  77. }
  78. }
  79. }
  80. // 批量插入子级
  81. if (!empty($detailsSonData)) QuantizationDetails::insert($detailsSonData);
  82. }
  83. private function getDetail($id){
  84. // 查所有数据
  85. $all = QuantizationDetails::where('quantization_id', $id)
  86. ->where('del_time', 0)
  87. ->orderBy('id','asc')
  88. ->get()
  89. ->toArray();
  90. // 按 parent_id 分组
  91. $grouped = [];
  92. foreach ($all as $item) {
  93. $grouped[$item['parent_id']][] = $item;
  94. }
  95. $result = [];
  96. // 只取 parent_id = 0 的父级
  97. if (!empty($grouped[0])) {
  98. foreach ($grouped[0] as $parent) {
  99. $parent['details_son'] = $grouped[$parent['id']] ?? (object)[];
  100. $result[] = $parent;
  101. }
  102. }
  103. return $result;
  104. }
  105. public function quantizationDel($data){
  106. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  107. try {
  108. DB::beginTransaction();
  109. $time = time();
  110. Quantization::where('del_time',0)
  111. ->whereIn('id',$data['id'])
  112. ->update(['del_time' => $time]);
  113. QuantizationDetails::where('del_time',0)
  114. ->whereIn('quantization_id', $data['id'])
  115. ->update(['del_time' => $time]);
  116. DB::commit();
  117. }catch (\Exception $exception){
  118. DB::rollBack();
  119. return [false,$exception->getMessage()];
  120. }
  121. return [true, ''];
  122. }
  123. public function quantizationDetail($data,$user){
  124. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  125. $customer = Quantization::where('del_time',0)
  126. ->where('id',$data['id'])
  127. ->first();
  128. if(empty($customer)) return [false,'量化档案不存在或已被删除'];
  129. $customer = $customer->toArray();
  130. $details = $this->getDetail($customer['id']);
  131. $customer['details'] = $details;
  132. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  133. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  134. return [true, $customer];
  135. }
  136. public function quantizationCommon($data,$user, $field = []){
  137. if(empty($field)) $field = Quantization::$field;
  138. $model = Quantization::where('del_time',0)
  139. ->select($field)
  140. ->orderby('id', 'desc');
  141. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  142. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  143. if(! empty($data['type'])) $model->where('type', $data['type']);
  144. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  145. if(isset($data['is_use'])) $model->where('is_use', $data['is_use']);
  146. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  147. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  148. $model->where('crt_time','>=',$return[0]);
  149. $model->where('crt_time','<=',$return[1]);
  150. }
  151. return $model;
  152. }
  153. public function quantizationList($data,$user){
  154. $model = $this->quantizationCommon($data, $user);
  155. $list = $this->limit($model,'',$data);
  156. $list = $this->fillData($list,$user);
  157. return [true, $list];
  158. }
  159. public function quantizationRule(&$data, $user, $is_add = true){
  160. if(empty($data['type'])) return [false, '量化档案类型类型不能为空'];
  161. if(! isset(Quantization::$type_name[$data['type']])) return [false, '量化档案类型错误'];
  162. if(empty($data['title'])) return [false, '名称不能为空'];
  163. if(empty($data['code'])) return [false, '编码不能为空'];
  164. if(empty($data['details'])) return [false, '评价明细不能为空'];
  165. foreach ($data['details'] as $key => $value) {
  166. $rowNum = $key + 1;
  167. // 校验外层 title 不能为空
  168. if (empty($value['title'])) {
  169. return [false, "第{$rowNum}行的评价名称不能为空"];
  170. }
  171. // 校验外层 title 不重复
  172. if (!isset($titleSet)) $titleSet = [];
  173. if (in_array($value['title'], $titleSet)) {
  174. return [false, "第{$rowNum}行的评价名称 [{$value['title']}] 重复"];
  175. }
  176. $titleSet[] = $value['title'];
  177. // 校验维度总分
  178. if (empty($value['score']) || ! is_numeric($value['score']) || floatval($value['score']) <= 0.0) {
  179. return [false, "第{$rowNum}行的维度总分错误"];
  180. }
  181. if(empty($value['details_son'])) return [false, '维度明细不能为空'];
  182. // 校验明细中 title 不重复
  183. $sonTitleSet = [];
  184. foreach ($value['details_son'] as $sonKey => $val) {
  185. $sonRowNum = $sonKey + 1;
  186. if (empty($val['title'])) {
  187. return [false, "第{$rowNum}行下的第{$sonRowNum}个明细名称不能为空"];
  188. }
  189. if (in_array($val['title'], $sonTitleSet)) {
  190. return [false, "第{$rowNum}行下的明细名称 [{$val['title']}] 重复"];
  191. }
  192. if (empty($val['score']) || ! is_numeric($val['score']) || floatval($val['score']) <= 0.0) {
  193. return [false, "第{$rowNum}行下的第{$sonRowNum}个明细赋分错误"];
  194. }
  195. $sonTitleSet[] = $val['title'];
  196. }
  197. }
  198. $rate_total = floatval(array_sum(array_column($data['details'],'score')));
  199. if($rate_total != 100.0) return [false, '维度总分之和必须等于100'];
  200. if($is_add){
  201. $bool = Quantization::where('code',$data['code'])
  202. ->where('del_time',0)
  203. ->exists();
  204. }else{
  205. if(empty($data['id'])) return [false,'ID不能为空'];
  206. $bool = Quantization::where('code',$data['code'])
  207. ->where('id','<>',$data['id'])
  208. ->where('del_time',0)
  209. ->exists();
  210. }
  211. if($bool) return [false,'编码已存在'];
  212. return [true, $data];
  213. }
  214. public function fillData($data, $user){
  215. if(empty($data['data'])) return $data;
  216. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  217. foreach ($data['data'] as $key => $value){
  218. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  219. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  220. $data['data'][$key]['is_use_title'] = Quantization::$is_use_name[$value['is_use']] ?? '';
  221. $data['data'][$key]['type_title'] = Quantization::$type_name[$value['type']] ?? '';
  222. }
  223. return $data;
  224. }
  225. //量化档案-----------------------------------------------
  226. //量化创建-----------------------------------------------
  227. public function quantizationCreateEdit($data,$user){
  228. list($status,$msg) = $this->quantizationCreateRule($data, $user, false);
  229. if(!$status) return [$status,$msg];
  230. try {
  231. DB::beginTransaction();
  232. $model = QuantizationCreate::where('id',$data['id'])->first();
  233. $model->customer_supply_id = $data['customer_supply_id'];
  234. $model->quantization_id = $data['quantization_id'];
  235. $model->start_time = $data['start_time'] ?? 0;
  236. $model->end_time = $data['end_time'] ?? 0;
  237. $model->products = $data['products'] ?? '';
  238. $model->score = $data['score'] ?? 0;
  239. $model->save();
  240. $time = time();
  241. QuantizationCreateDetails::where('del_time',0)
  242. ->where('quantization_create_id', $data['id'])
  243. ->update(['del_time' => $time]);
  244. $this->saveDetail1($model->id, $time, $data);
  245. DB::commit();
  246. }catch (\Exception $exception){
  247. DB::rollBack();
  248. return [false,$exception->getMessage()];
  249. }
  250. return [true, ''];
  251. }
  252. public function quantizationCreateAdd($data,$user){
  253. list($status,$msg) = $this->quantizationCreateRule($data, $user);
  254. if(!$status) return [$status,$msg];
  255. try {
  256. DB::beginTransaction();
  257. $model = new QuantizationCreate();
  258. $model->customer_supply_id = $data['customer_supply_id'];
  259. $model->quantization_id = $data['quantization_id'];
  260. $model->start_time = $data['start_time'] ?? 0;
  261. $model->end_time = $data['end_time'] ?? 0;
  262. $model->products = $data['products'] ?? '';
  263. $model->score = $data['score'] ?? 0;
  264. $model->type = $data['type'] ?? 0;
  265. $model->crt_id = $user['id'];
  266. $model->save();
  267. $time = time();
  268. $this->saveDetail1($model->id, $time, $data);
  269. DB::commit();
  270. }catch (\Exception $exception){
  271. DB::rollBack();
  272. return [false,$exception->getMessage()];
  273. }
  274. return [true, ''];
  275. }
  276. private function saveDetail1($id, $time, $data){
  277. foreach ($data['details'] as $value) {
  278. // 插入父级
  279. $parent = QuantizationCreateDetails::create([
  280. 'quantization_create_id' => $id,
  281. 'title' => $value['title'],
  282. 'quantization_score' => $value['quantization_score'],
  283. 'parent_id' => 0, // 顶层 parent_id = 0
  284. 'crt_time' => $time,
  285. 'dimension_id' => $value['dimension_id'] ?? 0,
  286. 'score' => $value['score'] ?? 0,
  287. ]);
  288. // 再处理子级
  289. if (! empty($value['details_son'])) {
  290. foreach ($value['details_son'] as $son) {
  291. $detailsSonData[] = [
  292. 'quantization_create_id' => $id,
  293. 'title' => $son['title'],
  294. 'parent_id' => $parent->id, // 子级关联父级 id
  295. 'crt_time' => $time,
  296. 'dimension_id' => $son['dimension_id'] ?? 0,
  297. 'score' => $son['score'] ?? 0,
  298. 'rate' => $son['rate'] ?? 0,
  299. ];
  300. }
  301. }
  302. }
  303. // 批量插入子级
  304. if (!empty($detailsSonData)) QuantizationCreateDetails::insert($detailsSonData);
  305. }
  306. private function getDetail1($id){
  307. // 查所有数据
  308. $all = QuantizationCreateDetails::where('quantization_create_id', $id)
  309. ->where('del_time', 0)
  310. ->orderBy('id','asc')
  311. ->get()
  312. ->toArray();
  313. $map = Dimension::whereIn('id',array_unique(array_filter(array_column($all,'dimension_id'))))
  314. ->pluck('title','id')
  315. ->toArray();
  316. // 按 parent_id 分组
  317. $grouped = [];
  318. foreach ($all as $item) {
  319. $item['dimension_title'] = $map[$item['dimension_id']] ?? "";
  320. $grouped[$item['parent_id']][] = $item;
  321. }
  322. $result = [];
  323. // 只取 parent_id = 0 的父级
  324. if (!empty($grouped[0])) {
  325. foreach ($grouped[0] as $parent) {
  326. $parent['details_son'] = $grouped[$parent['id']] ?? (object)[];
  327. $result[] = $parent;
  328. }
  329. }
  330. return $result;
  331. }
  332. public function quantizationCreateDel($data){
  333. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  334. try {
  335. DB::beginTransaction();
  336. $time = time();
  337. QuantizationCreate::where('del_time',0)
  338. ->whereIn('id',$data['id'])
  339. ->update(['del_time' => $time]);
  340. QuantizationCreateDetails::where('del_time',0)
  341. ->whereIn('quantization_create_id', $data['id'])
  342. ->update(['del_time' => $time]);
  343. DB::commit();
  344. }catch (\Exception $exception){
  345. DB::rollBack();
  346. return [false,$exception->getMessage()];
  347. }
  348. return [true, ''];
  349. }
  350. public function quantizationCreateDetail($data,$user){
  351. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  352. $customer = QuantizationCreate::where('del_time',0)
  353. ->where('id',$data['id'])
  354. ->first();
  355. if(empty($customer)) return [false,'量化信息不存在或已被删除'];
  356. $customer = $customer->toArray();
  357. $emp_2 = (new CustomerSupplyService())->getEmployeeMap([$customer['customer_supply_id']],'detail');
  358. $e = $emp_2[$customer['customer_supply_id']] ?? [];
  359. $customer['organization_title'] = $e['organization_title'] ?? "";
  360. $customer['customer_supply_title'] = $e['title'] ?? "";
  361. $customer['customer_supply_code'] = $e['code'] ?? "";
  362. $customer['quantization_title'] = Quantization::where('id', $customer['quantization_id'])->value('title');
  363. $details = $this->getDetail1($customer['id']);
  364. $customer['details'] = $details;
  365. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  366. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  367. return [true, $customer];
  368. }
  369. public function quantizationCreateCommon($data,$user, $field = []){
  370. if(empty($data['type']) || ! isset(QuantizationCreate::$type_name[$data['type']])) return [false, '类型不存在或错误'];
  371. if(empty($field)) $field = QuantizationCreate::$field;
  372. $model = QuantizationCreate::Clear($user,$data);
  373. $model->where('del_time',0)
  374. ->select($field)
  375. ->orderby('id', 'desc');
  376. if(! empty($data['type'])) $model->where('type', $data['type']);
  377. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  378. if(! empty($data['customer_supply_title'])){
  379. list($status, $id) = (new CustomerSupplyService())->customerSupplyListForSearch(['title' => $data['customer_supply_title']], $user);
  380. $model->whereIn('customer_supply_id', $id);
  381. }
  382. if(! empty($data['customer_supply_code'])){
  383. list($status, $id) = (new CustomerSupplyService())->customerSupplyListForSearch(['code' => $data['customer_supply_code']], $user);
  384. $model->whereIn('customer_supply_id', $id);
  385. }
  386. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  387. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  388. $model->where('crt_time','>=',$return[0]);
  389. $model->where('crt_time','<=',$return[1]);
  390. }
  391. if(! empty($data['zq'][0]) && ! empty($data['zq'][1])) {
  392. $return = $this->changeDateToTimeStampAboutRange($data['zq']);
  393. $model->where('start_time','>=',$return[0]);
  394. $model->where('end_time','<=',$return[1]);
  395. }
  396. return [true, $model];
  397. }
  398. public function quantizationCreateList($data,$user){
  399. list($status,$model) = $this->quantizationCreateCommon($data, $user);
  400. if(! $status) return [false,$model];
  401. $list = $this->limit($model,'',$data);
  402. $list = $this->fillCreateData($list,$user);
  403. return [true, $list];
  404. }
  405. public function quantizationCreateRule(&$data, $user, $is_add = true){
  406. if(empty($data['type'])) return [false, '量化档案类型类型不能为空'];
  407. if(empty($data['quantization_id'])) return [false, '量化档案ID不能为空'];
  408. $q = Quantization::where('del_time',0)
  409. ->where('id', $data['quantization_id'])
  410. ->first();
  411. if(empty($q)) return [false, '引用的档案不存在或已被删除'];
  412. $q = $q->toArray();
  413. if(! isset(QuantizationCreate::$type_name[$data['type']])) return [false, '量化档案类型错误'];
  414. if($q['type'] != $data['type']) return [false, '引用档案类型与量化档案类型不一致'];
  415. if($data['type'] == QuantizationCreate::type_two){
  416. if(empty($data['start_time']) || empty($data['end_time'])) return [false, '周期不能为空'];
  417. $data['start_time'] = $this->changeDateToDate($data['start_time']);
  418. $data['end_time'] = $this->changeDateToDate($data['end_time']);
  419. }
  420. if(empty($data['customer_supply_id'])) return [false, '人员id不能为空'];
  421. $bool = CustomerSupply::where('del_time',0)
  422. ->where('id',$data['customer_supply_id'])
  423. ->exists();
  424. if(! $bool) return [false, '人员不存在或已被删除'];
  425. if(! isset($data['score'])) return [false, '分数不存在'];
  426. $res = $this->checkNumber($data['score']);
  427. if(! $res['valid']) return [false,'分数:' . $res['error']];
  428. $map = [];
  429. if(! empty($data['from_wx'])){
  430. $map = Dimension::where('del_time',0)
  431. ->where('is_use',Dimension::is_use_one)
  432. ->where('type',$data['type'])
  433. ->pluck('id','rate')
  434. ->toArray();
  435. }
  436. if(empty($data['details'])) return [false, '量化详情不能为空'];
  437. foreach ($data['details'] as $key => $value) {
  438. $rowNum = $key + 1;
  439. // 校验外层 title 不能为空
  440. if (empty($value['title'])) return [false, "第{$rowNum}行的评价名称不能为空"];
  441. // 校验维度总分
  442. if (empty($value['quantization_score']) || ! is_numeric($value['quantization_score']) || floatval($value['quantization_score']) <= 0.0) {
  443. return [false, "第{$rowNum}行的维度总分错误"];
  444. }
  445. if (empty($value['score']) || ! is_numeric($value['score']) || floatval($value['score']) <= 0.0) {
  446. return [false, "第{$rowNum}行的总得分错误"];
  447. }
  448. if(empty($value['details_son'])) return [false, '维度明细不能为空'];
  449. foreach ($value['details_son'] as $sonKey => $val) {
  450. $sonRowNum = $sonKey + 1;
  451. if (empty($val['title'])) return [false, "第{$rowNum}行下的第{$sonRowNum}个明细名称不能为空"];
  452. if (empty($val['score']) || ! is_numeric($val['score']) || floatval($val['score']) <= 0.0) {
  453. return [false, "第{$rowNum}行下的第{$sonRowNum}个明细的维度选项赋分不能为空"];
  454. }
  455. if(empty($data['from_wx'])){
  456. if (empty($val['dimension_id'])) return [false, "第{$rowNum}行下的第{$sonRowNum}个明细的维度选项不能为空"];
  457. }else{
  458. $dimension_id = $this->getDimensionIdByRate($map, $val['rate']);
  459. if(empty($dimension_id)) return [false, "第{$rowNum}行下的第{$sonRowNum}个明细的维度选项找不到对应档案"];
  460. $data['details'][$key]['details_son'][$sonKey]['dimension_id'] = $dimension_id;
  461. }
  462. if (empty($val['rate']) || ! is_numeric($val['rate']) || floatval($val['rate']) <= 0.0) {
  463. return [false, "第{$rowNum}行下的第{$sonRowNum}个明细的维度选项打分比例不能为空"];
  464. }
  465. }
  466. }
  467. $startTime = $data['start_time'] ?? 0;
  468. $endTime = $data['end_time'] ?? 0;
  469. if ($is_add) {
  470. if ($data['type'] == QuantizationCreate::type_one) {
  471. // 能力量化:只允许唯一一份
  472. $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
  473. ->where('type', QuantizationCreate::type_one)
  474. ->where('del_time', 0)
  475. ->exists();
  476. if ($bool) {
  477. return [false, '该人员已创建能力量化档案,请勿重复创建'];
  478. }
  479. } else {
  480. // 合作量化:检查时间区间重叠
  481. $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
  482. ->where('type', QuantizationCreate::type_two) // 建议限定 type,防止误匹配
  483. ->where('del_time', 0)
  484. ->where(function($query) use ($startTime, $endTime) {
  485. $query->where('start_time', '<=', $endTime)
  486. ->where('end_time', '>=', $startTime);
  487. })
  488. ->exists();
  489. if ($bool) {
  490. return [false, '该人员在该周期内已创建合作量化档案,请勿重复创建'];
  491. }
  492. }
  493. } else {
  494. // 编辑模式
  495. if (empty($data['id'])) return [false, 'ID不能为空'];
  496. $id = $data['id'];
  497. if ($data['type'] == QuantizationCreate::type_one) {
  498. $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
  499. ->where('type', QuantizationCreate::type_one)
  500. ->where('id', '<>', $id)
  501. ->where('del_time', 0)
  502. ->exists();
  503. if ($bool) return [false, '该人员已创建能力量化档案,请勿重复创建'];
  504. } else {
  505. $bool = QuantizationCreate::where('customer_supply_id', $data['customer_supply_id'])
  506. ->where('type', QuantizationCreate::type_two)
  507. ->where('del_time', 0)
  508. ->where(function($query) use ($startTime, $endTime) {
  509. $query->where('start_time', '<=', $endTime)
  510. ->where('end_time', '>=', $startTime);
  511. })
  512. ->where('id', '<>', $id)
  513. ->exists();
  514. if ($bool) return [false, '该人员在该周期内已创建合作量化档案,请勿重复创建'];
  515. }
  516. }
  517. return [true, $data];
  518. }
  519. function getDimensionIdByRate($map, $rate) {
  520. // 标准化输入为 "X.XX" 格式
  521. $key = number_format((float)$rate, 2, '.', '');
  522. return $map[$key] ?? null;
  523. }
  524. public function fillCreateData($data, $user){
  525. if(empty($data['data'])) return $data;
  526. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  527. $emp_2 = (new CustomerSupplyService())->getEmployeeMap(array_unique(array_column($data['data'],'customer_supply_id')),'detail');
  528. foreach ($data['data'] as $key => $value){
  529. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  530. $data['data'][$key]['start_time'] = $value['start_time'] ? date('Y-m-d',$value['start_time']) : '';
  531. $data['data'][$key]['end_time'] = $value['end_time'] ? date('Y-m-d',$value['end_time']) : '';
  532. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  533. $data['data'][$key]['type_title'] = QuantizationCreate::$type_name[$value['type']] ?? '';
  534. $e = $emp_2[$value['customer_supply_id']] ?? [];
  535. $data['data'][$key]['organization_title'] = $e['organization_title'] ?? "";
  536. $data['data'][$key]['customer_supply_title'] = $e['title'] ?? "";
  537. $data['data'][$key]['customer_supply_code'] = $e['code'] ?? "";
  538. $data['data'][$key]['result'] = $this->fillDataResult($value);
  539. }
  540. return $data;
  541. }
  542. private function fillDataResult($value)
  543. {
  544. $score = isset($value['score']) ? floatval($value['score']) : 0;
  545. $type = $value['type'] ?? null;
  546. if ($type == QuantizationCreate::type_one) {
  547. if ($score < 50) return '不合格';
  548. if ($score < 70) return '改善后评估';
  549. return '合格';
  550. }
  551. // 其他类型
  552. return $score < 60 ? '不合格' : '合格';
  553. }
  554. //量化创建-----------------------------------------------
  555. }