Import.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 (trim($info_name) != trim($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. if(!empty($row[$i])){
  256. $item['phone'] = $row[$i];
  257. }
  258. break;
  259. case 16:
  260. if(!empty($row[$i])){
  261. $item['email'] = $row[$i];
  262. }
  263. break;
  264. case 25:
  265. $item['position'] = $row[$i];
  266. break;
  267. case 31:
  268. $item['fj_talent_level'] = $row[$i];
  269. break;
  270. case 32:
  271. $item['fj_talent_condition_text'] = $row[$i];
  272. break;
  273. case 33:
  274. $str = "认定时间:".$row[$i];
  275. break;
  276. case 34:
  277. $str .= ";省级人才证书编号:" . $row[$i];
  278. break;
  279. case 35:
  280. $str .= ";有效期:" . $row[$i];
  281. break;
  282. }
  283. }
  284. //$item['source'] = 1;
  285. $item['fj_talent_info'] = $str;
  286. $item["checkState"] = \app\common\api\TalentState::SCND_SAVE; //保存未提交
  287. $item["isImport"] = 1;//导入人才的标志
  288. $item['fjImport'] = 1;
  289. if (count($msg) >= 1) {
  290. $sheet->setCellValue('AL' . ($index + 2), '导入失败,失败原因:' . implode(";", $msg));
  291. } else {
  292. if ($action == 'update') {
  293. $item['updateTime'] = date("Y-m-d H:i:s", time());
  294. Db::table("new_talent_info")->where('id', $talent_info['id'])->update($item);
  295. $sheet->setCellValue('AL' . ($index + 2), '更新成功');
  296. } else {
  297. $item["highProcess"] = 5; //最高进度
  298. $item['createTime'] = date("Y-m-d H:i:s", time());
  299. Db::table("new_talent_info")->insert($item);
  300. $sheet->setCellValue('AL' . ($index + 2), '导入成功');
  301. }
  302. $log = TalentLogApi::system(1, $talent_info["id"],8,"直认数据导入",1);
  303. }
  304. }
  305. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  306. $writer->save($path . "/public/storage/" . $res['task_file']);
  307. 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())]);
  308. return true;
  309. } catch (\Exception $e) {
  310. 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())]);
  311. return false;
  312. }
  313. } else {
  314. return false;
  315. }
  316. }
  317. elseif ($data['type'] == 3){//泉州人才导入
  318. $id = $data['id'];
  319. $res = Db::table("new_talent_zhiren")->where('id', $id)->findOrEmpty();
  320. if ($res) {
  321. if ($res['task_status'] != -1) {
  322. return false;
  323. }
  324. try {
  325. $path = dirname(dirname(dirname(__FILE__)));
  326. $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($path ."/public/storage/" . $res['task_file']);
  327. $sheet = $spreadsheet->getSheet(0);
  328. $datas = $sheet->toArray();
  329. $datas = array_slice($datas, 1); //去标题
  330. $country = Db::table("sys_dict")->where("pid", 35)->column("code", "name");
  331. for ($index = 0; $index < count($datas); $index++) {
  332. $row = $datas[$index];
  333. $msg = [];
  334. $item = [];
  335. $enterprise_name = '';
  336. $enterprise_id = '';
  337. $action = 'insert';
  338. $str = '';
  339. for ($i = 0; $i < 28; $i++) {
  340. switch ($i) {
  341. case 0:
  342. if (empty($row[$i])) {
  343. array_push($msg, "机构名称不能为空");
  344. } else {
  345. $enterprise_name = $row[$i];
  346. }
  347. break;
  348. case 1:
  349. $info = Db::table("un_enterprise")->where('idCard', $row[$i])->where('delete',0)->findOrEmpty();
  350. if (!$info) {
  351. array_push($msg, "机构代码不存在,请检查");
  352. } else {
  353. $info_name = str_replace('(','(',$info['name']);
  354. $info_name = str_replace(')',')',$info_name);
  355. $enterprise_name = str_replace('(','(',$enterprise_name);
  356. $enterprise_name = str_replace(')',')',$enterprise_name);
  357. if (trim($info_name) != trim($enterprise_name)) {
  358. array_push($msg, "机构代码与企业名称无法对应,请检查");
  359. } else {
  360. $item['enterprise_id'] = $enterprise_id = $info['id'];
  361. }
  362. }
  363. break;
  364. case 2:
  365. if (empty($row[$i])) {
  366. array_push($msg, "人才姓名不能为空");
  367. } else {
  368. $item['name'] = $row[$i];
  369. }
  370. break;
  371. case 3:
  372. if($row[$i] == '女'){
  373. $item['sex'] = 2;
  374. }else{
  375. $item['sex'] = 1;
  376. }
  377. break;
  378. case 4:
  379. $item['nationality'] = array_key_exists($row[$i], $country) ? $country[$row[$i]] : 'other';
  380. break;
  381. case 5:
  382. if (in_array($row[$i], ['身份证', '通行证', '护照'])) {
  383. $key = array_search($row[$i], ['身份证', '通行证', '护照']);
  384. $item['card_type'] = $key + 1;
  385. } else {
  386. array_push($msg, "人才的证件类型不正确");
  387. }
  388. break;
  389. case 6:
  390. if (empty($row[$i])) {
  391. array_push($msg, "证件号码不能为空");
  392. } else {
  393. if (!isCreditNo($row[$i]) && $item['card_type'] == 1) {
  394. array_push($msg, "证件号码不正确");
  395. }
  396. $talent_info = Db::table("new_talent_info")->where("card_number", $row[$i])->findOrEmpty();
  397. if ($talent_info) {
  398. $action = 'update';
  399. }
  400. $item['card_number'] = $row[$i];
  401. }
  402. break;
  403. case 7:
  404. $item['birthday'] = $row[$i];
  405. break;
  406. case 9:
  407. $item['position'] = $row[$i];
  408. break;
  409. case 12:
  410. $item['professional'] = $row[$i];
  411. break;
  412. case 13:
  413. $item['pro_qua'] = $row[$i];
  414. break;
  415. case 16:
  416. if(!empty($row[$i])){
  417. $item['phone'] = $row[$i];
  418. }
  419. break;
  420. case 17:
  421. if(!empty($row[$i])){
  422. $item['email'] = $row[$i];
  423. }
  424. break;
  425. case 18:
  426. $item['qz_talent_level'] = $row[$i];
  427. break;
  428. case 19:
  429. $item['qz_talent_condition_text'] = $row[$i];
  430. break;
  431. case 25:
  432. $str = "人才证书号码:".$row[$i];
  433. break;
  434. case 26:
  435. $str .= ";发证日期:".$row[$i];
  436. break;
  437. case 27:
  438. $str .= ";人才证有效期:".$row[$i];
  439. break;
  440. }
  441. }
  442. //$item['source'] = 2;
  443. $item['qz_talent_info'] = $str;
  444. $item["checkState"] = \app\common\api\TalentState::SCND_SAVE; //保存未提交
  445. $item["highProcess"] = 5; //最高进度
  446. $item["isImport"] = 1;//导入人才的标志
  447. $item['qzImport'] = 1;
  448. if (count($msg) >= 1) {
  449. $sheet->setCellValue('AC' . ($index + 2), '导入失败,失败原因:' . implode(";", $msg));
  450. } else {
  451. if ($action == 'update') {
  452. $item['updateTime'] = date("Y-m-d H:i:s", time());
  453. Db::table("new_talent_info")->where('id', $talent_info['id'])->update($item);
  454. $sheet->setCellValue('AC' . ($index + 2), '更新成功');
  455. } else {
  456. $item['createTime'] = date("Y-m-d H:i:s", time());
  457. Db::table("new_talent_info")->insert($item);
  458. $sheet->setCellValue('AC' . ($index + 2), '导入成功');
  459. }
  460. $log = TalentLogApi::system(1, $talent_info["id"],8,"直认数据导入",1);
  461. }
  462. }
  463. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
  464. $writer->save($path . "/public/storage/" . $res['task_file']);
  465. 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())]);
  466. return true;
  467. } catch (\Exception $e) {
  468. 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())]);
  469. return false;
  470. }
  471. } else {
  472. return false;
  473. }
  474. }
  475. }
  476. }