Import.php 25 KB

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