Import.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. namespace app\job;
  3. use app\common\api\BatchApi;
  4. use app\common\api\TalentLogApi;
  5. use app\common\model\TalentChecklog;
  6. use app\common\state\ProjectState;
  7. use think\queue\Job;
  8. use think\facade\Log;
  9. use think\facade\Db;
  10. use app\common\api\ChuanglanSmsApi;
  11. // 给类文件的命名空间起个别名
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. //Xlsx类 保存文件功能类
  14. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  15. class Import {
  16. public function fire(Job $job, $data) {
  17. if ($this->deal($data)) {
  18. Log::info(json_encode($data));
  19. $job->delete();
  20. return true;
  21. }
  22. Log::error(json_encode($data));
  23. if ($job->attempts() >= 3) {
  24. $job->delete();
  25. return false;
  26. }
  27. $job->release(10); //10秒后重试
  28. }
  29. /**
  30. * 处理业务逻辑
  31. * @param type $data
  32. * @return bool
  33. */
  34. public function deal($data): bool {
  35. if ($data['type'] == 1) {//企业导入
  36. $id = $data['id'];
  37. $res = Db::table("new_talent_zhiren")->where('id', $id)->findOrEmpty();
  38. if ($res) {
  39. if ($res['task_status'] != -1) {
  40. return false;
  41. }
  42. try {
  43. $path = dirname(dirname(dirname(__FILE__)));
  44. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path . "/public/storage/" . $res['task_file']);
  45. $sheet = $spreadsheet->getSheet(0);
  46. $datas = $sheet->toArray();
  47. $datas = array_slice($datas, 1); //去标题
  48. $street = Db::table("sys_dict")->where("pid", 1655)->column("code", "name");
  49. for ($index = 0; $index < count($datas); $index++) {
  50. $row = $datas[$index];
  51. $msg = [];
  52. $item = [];
  53. for ($i = 0; $i < 11; $i++) {
  54. switch ($i) {
  55. case 0:
  56. if (in_array($row[$i], ['企业用户', '民办非企业', '事业单位'])) {
  57. if ($row[$i] == '事业单位') {
  58. $item['special'] = 1;
  59. } else {
  60. $item['special'] = 0;
  61. }
  62. } else {
  63. array_push($msg, "导入的机构类型不正确");
  64. }
  65. break;
  66. case 1:
  67. if (empty($row[$i])) {
  68. array_push($msg, "机构名称不能为空");
  69. } else {
  70. $info = Db::table("un_enterprise")->where('name', $row[$i])->findOrEmpty();
  71. if ($info) {
  72. array_push($msg, "机构名称已存在,请检查");
  73. } else {
  74. $isMatched = preg_match('/^[\x{4e00}-\x{9fa5}\(\)()\da-zA-Z&]{2,50}$/u', $row[$i], $matches);
  75. if ($isMatched) {
  76. $item['name'] = $row[$i];
  77. } else {
  78. array_push($msg, "机构名称只能是中文或数字");
  79. }
  80. }
  81. }
  82. break;
  83. case 2:
  84. $info = Db::table("un_enterprise")->where('idCard', $row[$i])->where('delete',0)->findOrEmpty();
  85. if ($info) {
  86. array_push($msg, "机构代码已存在,请检查");
  87. } else {
  88. $isMatched = preg_match('/^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/', $row[$i], $matches);
  89. if ($isMatched) {
  90. $item['idCard'] = $row[$i];
  91. } else {
  92. array_push($msg, "组织机构代码证不正确");
  93. }
  94. }
  95. break;
  96. case 3:
  97. $item['agentName'] = $row[$i];
  98. break;
  99. case 4:
  100. $isMatched = preg_match('/^1[3456789]\d{9}$/', $row[$i], $matches);
  101. if ($isMatched) {
  102. $item['agentPhone'] = $row[$i];
  103. } else {
  104. array_push($msg, "联系电话不正确");
  105. }
  106. break;
  107. case 5:
  108. $item['street'] = array_key_exists($row[$i], $street) ? $street[$row[$i]] : '00000';
  109. break;
  110. case 6:
  111. $item['address'] = $row[$i];
  112. break;
  113. case 7:
  114. $item['ephone'] = $row[$i];
  115. break;
  116. case 8:
  117. $item['bankCard'] = $row[$i];
  118. break;
  119. case 9:
  120. $item['bank'] = $row[$i];
  121. break;
  122. case 10:
  123. $item['bankNetwork'] = $row[$i];
  124. break;
  125. }
  126. }
  127. if (count($msg) >= 1) {
  128. $sheet->setCellValue('L' . ($index + 2), '导入失败,失败原因:' . implode(";", $msg));
  129. } else {
  130. $item['id'] = getStringId();
  131. $item['type'] = 1;
  132. $item['source'] = 4;
  133. $item['username'] = $item['idCard'];
  134. $password = generate_password(8);
  135. $item['password'] = hash('md5', $password);
  136. $item['active'] = 1;
  137. $item['checkState'] = 3;
  138. $item['createTime'] = date("Y-m-d H:i:s", time());
  139. Db::table("un_enterprise")->insert($item);
  140. $sheet->setCellValue('L' . ($index + 2), '导入成功,初始账号为:' . $item['idCard'] . ',初始密码为:' . $password);
  141. $sender = new ChuanglanSmsApi();
  142. $sender->sendSMS($item['agentPhone'], "您好,您的机构信息仍需完善,请登录晋江市人才综合服务申报平台进行完善,您的用户名为:{$item['idCard']},密码为:{$password}。");
  143. TalentChecklog::create([
  144. 'id' => getStringId(),
  145. 'mainId' => $item['id'],
  146. 'type' => 10,
  147. 'typeFileId' => null,
  148. 'active' => 1,
  149. 'state' => 1,
  150. 'step' => 100,
  151. 'stateChange' => '直认导入',
  152. 'description' => '机构注册',
  153. 'createTime' => date("Y-m-d H:i:s", time()),
  154. 'createUser' => '系统'
  155. ]);
  156. }
  157. }
  158. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  159. $writer->save($path . "/public/storage/" . $res['task_file']);
  160. Db::table("new_talent_zhiren")->where('id', $id)->update(['task_result' => $res['task_file'], 'task_status' => 1, 'updateTime' => date("Y-m-d H:i:s", time())]);
  161. return true;
  162. } catch (\Exception $e) {
  163. var_dump($e->getMessage());
  164. Db::table("new_talent_zhiren")->where('id', $data['id'])->update(['task_result' => $e->getMessage(), 'status' => 0, 'updateTime' => date("Y-m-d H:i:s", time())]);
  165. return false;
  166. }
  167. } else {
  168. return false;
  169. }
  170. }
  171. elseif ($data['type'] == 2) {//省级人才导入
  172. $id = $data['id'];
  173. $res = Db::table("new_talent_zhiren")->where('id', $id)->findOrEmpty();
  174. if ($res) {
  175. if ($res['task_status'] != -1) {
  176. return false;
  177. }
  178. try {
  179. $path = dirname(dirname(dirname(__FILE__)));
  180. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path ."/public/storage/" . $res['task_file']);
  181. $sheet = $spreadsheet->getSheet(0);
  182. $datas = $sheet->toArray();
  183. $datas = array_slice($datas, 1); //去标题
  184. $country = Db::table("sys_dict")->where("pid", 35)->column("code", "name");
  185. $batch = BatchApi::getValidBatch(ProjectState::TALENT, 1);
  186. for ($index = 0; $index < count($datas); $index++) {
  187. $row = $datas[$index];
  188. $msg = [];
  189. $item = [];
  190. $enterprise_name = '';
  191. $enterprise_id = '';
  192. $action = 'insert';
  193. $str = "";
  194. for ($i = 0; $i < 37; $i++) {
  195. switch ($i) {
  196. case 0:
  197. if (empty($row[$i])) {
  198. array_push($msg, "机构名称不能为空");
  199. } else {
  200. $enterprise_name = $row[$i];
  201. }
  202. break;
  203. case 1:
  204. $info = Db::table("un_enterprise")->where('idCard', $row[$i])->where('delete',0)->findOrEmpty();
  205. if (!$info) {
  206. array_push($msg, "机构代码不存在,请检查");
  207. } else {
  208. $info_name = str_replace('(','(',$info['name']);
  209. $info_name = str_replace(')',')',$info_name);
  210. $enterprise_name = str_replace('(','(',$enterprise_name);
  211. $enterprise_name = str_replace(')',')',$enterprise_name);
  212. if (trim($info_name) != trim($enterprise_name)) {
  213. array_push($msg, "机构代码与企业名称无法对应,请检查");
  214. } else {
  215. $item['enterprise_id'] = $enterprise_id = $info['id'];
  216. }
  217. }
  218. break;
  219. case 5:
  220. if (empty($row[$i])) {
  221. array_push($msg, "人才姓名不能为空");
  222. } else {
  223. $item['name'] = $row[$i];
  224. }
  225. break;
  226. case 6:
  227. if (in_array($row[$i], ['身份证', '通行证', '护照'])) {
  228. $key = array_search($row[$i], ['身份证', '通行证', '护照']);
  229. $item['card_type'] = $key + 1;
  230. } else {
  231. array_push($msg, "人才的证件类型不正确");
  232. }
  233. break;
  234. case 7:
  235. if (empty($row[$i])) {
  236. array_push($msg, "证件号码不能为空");
  237. } else {
  238. if (!isCreditNo($row[$i]) && $item['card_type'] == 1) {
  239. array_push($msg, "证件号码不正确");
  240. }
  241. $talent_info = Db::table("new_talent_info")->where("card_number", $row[$i])->where('apply_year',$batch["batch"])->findOrEmpty();
  242. if ($talent_info) {
  243. $action = 'update';
  244. }
  245. $item['card_number'] = $row[$i];
  246. }
  247. break;
  248. case 8:
  249. if($row[$i] == '女'){
  250. $item['sex'] = 2;
  251. }else{
  252. $item['sex'] = 1;
  253. }
  254. break;
  255. // case 9:
  256. // $item['nationality'] = array_key_exists($row[$i], $country) ? $country[$row[$i]] : 'other';
  257. // break;
  258. case 15:
  259. if(!empty($row[$i])){
  260. $item['phone'] = $row[$i];
  261. }
  262. break;
  263. case 16:
  264. if(!empty($row[$i])){
  265. $item['email'] = $row[$i];
  266. }
  267. break;
  268. case 25:
  269. $item['position'] = $row[$i];
  270. break;
  271. case 31:
  272. $item['fj_talent_level'] = $row[$i];
  273. break;
  274. case 32:
  275. $item['fj_talent_condition_text'] = $row[$i];
  276. break;
  277. case 33:
  278. $str = "认定时间:".$row[$i];
  279. break;
  280. case 34:
  281. $str .= ";省级人才证书编号:" . $row[$i];
  282. break;
  283. case 35:
  284. $str .= ";有效期:" . $row[$i];
  285. break;
  286. }
  287. }
  288. //$item['source'] = 1;
  289. $item['fj_talent_info'] = $str;
  290. $item["checkState"] = \app\common\api\TalentState::SCND_SAVE; //保存未提交
  291. $item["isImport"] = 1;//导入人才的标志
  292. $item['fjImport'] = 1;
  293. if (count($msg) >= 1) {
  294. $sheet->setCellValue('AL' . ($index + 2), '导入失败,失败原因:' . implode(";", $msg));
  295. } else {
  296. if ($action == 'update') {
  297. $item['updateTime'] = date("Y-m-d H:i:s", time());
  298. Db::table("new_talent_info")->where('id', $talent_info['id'])->update($item);
  299. $sheet->setCellValue('AL' . ($index + 2), '更新成功');
  300. } else {
  301. $item['createTime'] = date("Y-m-d H:i:s", time());
  302. $item['apply_year'] = $batch["batch"];
  303. Db::table("new_talent_info")->insert($item);
  304. $sheet->setCellValue('AL' . ($index + 2), '导入成功');
  305. }
  306. $log = TalentLogApi::system(1, $talent_info["id"],8,"直认数据导入",1);
  307. }
  308. }
  309. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  310. $writer->save($path . "/public/storage/" . $res['task_file']);
  311. Db::table("new_talent_zhiren")->where('id', $id)->update(['task_result' => $res['task_file'], 'task_status' => 1, 'updateTime' => date("Y-m-d H:i:s", time())]);
  312. return true;
  313. } catch (\Exception $e) {
  314. var_dump($e->getMessage());
  315. Db::table("new_talent_zhiren")->where('id', $id)->update(['task_result' => $e->getMessage(), 'task_status' => 0, 'updateTime' => date("Y-m-d H:i:s", time())]);
  316. return false;
  317. }
  318. } else {
  319. return false;
  320. }
  321. }
  322. elseif ($data['type'] == 3){//泉州人才导入
  323. $id = $data['id'];
  324. $res = Db::table("new_talent_zhiren")->where('id', $id)->findOrEmpty();
  325. if ($res) {
  326. if ($res['task_status'] != -1) {
  327. return false;
  328. }
  329. try {
  330. $path = dirname(dirname(dirname(__FILE__)));
  331. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path ."/public/storage/" . $res['task_file']);
  332. $sheet = $spreadsheet->getSheet(0);
  333. $datas = $sheet->toArray();
  334. $datas = array_slice($datas, 1); //去标题
  335. $country = Db::table("sys_dict")->where("pid", 35)->column("code", "name");
  336. $batch = BatchApi::getValidBatch(ProjectState::TALENT, 1);
  337. for ($index = 0; $index < count($datas); $index++) {
  338. $row = $datas[$index];
  339. $msg = [];
  340. $item = [];
  341. $enterprise_name = '';
  342. $enterprise_id = '';
  343. $action = 'insert';
  344. $str = '';
  345. for ($i = 0; $i < 28; $i++) {
  346. switch ($i) {
  347. case 0:
  348. if (empty($row[$i])) {
  349. array_push($msg, "机构名称不能为空");
  350. } else {
  351. $enterprise_name = $row[$i];
  352. }
  353. break;
  354. case 1:
  355. $info = Db::table("un_enterprise")->where('idCard', $row[$i])->where('delete',0)->findOrEmpty();
  356. if (!$info) {
  357. array_push($msg, "机构代码不存在,请检查");
  358. } else {
  359. $info_name = str_replace('(','(',$info['name']);
  360. $info_name = str_replace(')',')',$info_name);
  361. $enterprise_name = str_replace('(','(',$enterprise_name);
  362. $enterprise_name = str_replace(')',')',$enterprise_name);
  363. if (trim($info_name) != trim($enterprise_name)) {
  364. array_push($msg, "机构代码与企业名称无法对应,请检查");
  365. } else {
  366. $item['enterprise_id'] = $enterprise_id = $info['id'];
  367. }
  368. }
  369. break;
  370. case 2:
  371. if (empty($row[$i])) {
  372. array_push($msg, "人才姓名不能为空");
  373. } else {
  374. $item['name'] = $row[$i];
  375. }
  376. break;
  377. case 3:
  378. if($row[$i] == '女'){
  379. $item['sex'] = 2;
  380. }else{
  381. $item['sex'] = 1;
  382. }
  383. break;
  384. // case 4:
  385. // $item['nationality'] = array_key_exists($row[$i], $country) ? $country[$row[$i]] : 'other';
  386. // break;
  387. case 5:
  388. if (in_array($row[$i], ['身份证', '通行证', '护照'])) {
  389. $key = array_search($row[$i], ['身份证', '通行证', '护照']);
  390. $item['card_type'] = $key + 1;
  391. } else {
  392. array_push($msg, "人才的证件类型不正确");
  393. }
  394. break;
  395. case 6:
  396. if (empty($row[$i])) {
  397. array_push($msg, "证件号码不能为空");
  398. } else {
  399. if (!isCreditNo($row[$i]) && $item['card_type'] == 1) {
  400. array_push($msg, "证件号码不正确");
  401. }
  402. $talent_info = Db::table("new_talent_info")->where("card_number", $row[$i])->where('apply_year',$batch["batch"])->where('delete',0)->findOrEmpty();
  403. if ($talent_info) {
  404. $action = 'update';
  405. }
  406. $item['card_number'] = $row[$i];
  407. }
  408. break;
  409. case 7:
  410. $item['birthday'] = $row[$i];
  411. break;
  412. case 9:
  413. $item['position'] = $row[$i];
  414. break;
  415. case 12:
  416. $item['professional'] = $row[$i];
  417. break;
  418. case 13:
  419. $item['pro_qua'] = $row[$i];
  420. break;
  421. case 16:
  422. if(!empty($row[$i])){
  423. $item['phone'] = $row[$i];
  424. }
  425. break;
  426. case 17:
  427. if(!empty($row[$i])){
  428. $item['email'] = $row[$i];
  429. }
  430. break;
  431. case 18:
  432. $item['qz_talent_level'] = $row[$i];
  433. break;
  434. case 19:
  435. $item['qz_talent_condition_text'] = $row[$i];
  436. break;
  437. case 25:
  438. $str = "人才证书号码:".$row[$i];
  439. break;
  440. case 26:
  441. $str .= ";发证日期:".$row[$i];
  442. break;
  443. case 27:
  444. $str .= ";人才证有效期:".$row[$i];
  445. break;
  446. }
  447. }
  448. //$item['source'] = 2;
  449. $item['qz_talent_info'] = $str;
  450. $item["checkState"] = \app\common\api\TalentState::SCND_SAVE; //保存未提交
  451. $item["isImport"] = 1;//导入人才的标志
  452. $item['qzImport'] = 1;
  453. if (count($msg) >= 1) {
  454. $sheet->setCellValue('AC' . ($index + 2), '导入失败,失败原因:' . implode(";", $msg));
  455. } else {
  456. if ($action == 'update') {
  457. $item['updateTime'] = date("Y-m-d H:i:s", time());
  458. Db::table("new_talent_info")->where('id', $talent_info['id'])->update($item);
  459. $sheet->setCellValue('AC' . ($index + 2), '更新成功');
  460. $log = TalentLogApi::system(1, $talent_info["id"],8,"直认数据导入",1);
  461. } else {
  462. $item['apply_year'] = $batch["batch"];
  463. $item['createTime'] = date("Y-m-d H:i:s", time());
  464. $talent_info_id = Db::table("new_talent_info")->insertGetId($item);
  465. $sheet->setCellValue('AC' . ($index + 2), '导入成功');
  466. $log = TalentLogApi::system(1, $talent_info_id,8,"直认数据导入",1);
  467. }
  468. }
  469. }
  470. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  471. $writer->save($path . "/public/storage/" . $res['task_file']);
  472. Db::table("new_talent_zhiren")->where('id', $id)->update(['task_result' => $res['task_file'], 'task_status' => 1, 'updateTime' => date("Y-m-d H:i:s", time())]);
  473. return true;
  474. } catch (\Exception $e) {
  475. var_dump($e->getMessage());
  476. Db::table("new_talent_zhiren")->where('id', $id)->update(['task_result' => $e->getMessage(), 'task_status' => 0, 'updateTime' => date("Y-m-d H:i:s", time())]);
  477. return false;
  478. }
  479. } else {
  480. return false;
  481. }
  482. }
  483. }
  484. }