Import.php 26 KB

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