Commit b903250a0161e4dc54b4896a1a3dacf60b644030
1 parent
447098e4
1.从金蝶人事库更新人员数据逻辑优化
Showing
6 changed files
with
173 additions
and
476 deletions
src/main/java/com/bsth/StartCommand.java
| 1 | 1 | package com.bsth; |
| 2 | 2 | |
| 3 | 3 | |
| 4 | -import com.bsth.thread.PersonnelUpdateThrad; | |
| 4 | +import com.bsth.thread.PersonnelUpdateThread; | |
| 5 | 5 | import com.bsth.util.DateUtils; |
| 6 | 6 | |
| 7 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
| ... | ... | @@ -12,8 +12,6 @@ import org.slf4j.LoggerFactory; |
| 12 | 12 | |
| 13 | 13 | import java.util.concurrent.Executors; |
| 14 | 14 | import java.util.concurrent.ScheduledExecutorService; |
| 15 | -import java.util.concurrent.TimeUnit; | |
| 16 | - | |
| 17 | 15 | |
| 18 | 16 | /** |
| 19 | 17 | * 随应用启动运行 |
| ... | ... | @@ -29,13 +27,11 @@ public class StartCommand implements CommandLineRunner{ |
| 29 | 27 | private static long timeDiff; |
| 30 | 28 | |
| 31 | 29 | @Autowired |
| 32 | - PersonnelUpdateThrad personnelUpdateThrad; | |
| 30 | + PersonnelUpdateThread personnelUpdateThread; | |
| 33 | 31 | |
| 34 | 32 | static { |
| 35 | 33 | // 中午12:00 |
| 36 | 34 | timeDiff = (DateUtils.getTimestamp() + 1000 * 60 * 60 * 24) - System.currentTimeMillis(); |
| 37 | -// if (timeDiff < 0) | |
| 38 | -// timeDiff += (1000 * 60 * 60 * 24); | |
| 39 | 35 | } |
| 40 | 36 | |
| 41 | 37 | @Override |
| ... | ... | @@ -43,9 +39,8 @@ public class StartCommand implements CommandLineRunner{ |
| 43 | 39 | |
| 44 | 40 | try { |
| 45 | 41 | log.info("在:"+timeDiff / 1000 / 60 + "分钟后开始同步数据"); |
| 46 | - personnelUpdateThrad.run(); | |
| 47 | - mainServices.scheduleAtFixedRate(personnelUpdateThrad, timeDiff / 1000, 60 * 60 * 24, TimeUnit.SECONDS);//timeDiff / 1000 | |
| 48 | -// mainServices.scheduleAtFixedRate(personnelUpdateThrad, 1, 60*60, TimeUnit.SECONDS);//timeDiff / 1000 | |
| 42 | + personnelUpdateThread.run(); | |
| 43 | + //mainServices.scheduleAtFixedRate(personnelUpdateThread, timeDiff / 1000, 60 * 60 * 24, TimeUnit.SECONDS);//timeDiff / 1000 | |
| 49 | 44 | } catch (Exception e) { |
| 50 | 45 | e.printStackTrace(); |
| 51 | 46 | } | ... | ... |
src/main/java/com/bsth/handler/PersonnelUpdateHandler.java
0 → 100644
| 1 | +package com.bsth.handler; | |
| 2 | + | |
| 3 | +import com.alibaba.fastjson.JSONArray; | |
| 4 | +import com.alibaba.fastjson.JSONObject; | |
| 5 | +import com.bsth.entity.Personnel; | |
| 6 | +import com.bsth.repository.PersonnelRepository; | |
| 7 | +import com.bsth.service.PersonnelService; | |
| 8 | +import com.bsth.util.XmlToJson; | |
| 9 | +import com.bsth.wsdl.PJDataSerivce; | |
| 10 | +import com.bsth.wsdl.PJDataSerivceSoap; | |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; | |
| 12 | +import org.slf4j.Logger; | |
| 13 | +import org.slf4j.LoggerFactory; | |
| 14 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 15 | +import org.springframework.jdbc.core.BatchPreparedStatementSetter; | |
| 16 | +import org.springframework.jdbc.core.BeanPropertyRowMapper; | |
| 17 | +import org.springframework.jdbc.core.JdbcTemplate; | |
| 18 | +import org.springframework.jdbc.datasource.DataSourceTransactionManager; | |
| 19 | +import org.springframework.scheduling.annotation.EnableScheduling; | |
| 20 | +import org.springframework.scheduling.annotation.Scheduled; | |
| 21 | +import org.springframework.stereotype.Component; | |
| 22 | +import org.springframework.transaction.TransactionDefinition; | |
| 23 | +import org.springframework.transaction.TransactionStatus; | |
| 24 | +import org.springframework.transaction.support.DefaultTransactionDefinition; | |
| 25 | + | |
| 26 | +import javax.xml.namespace.QName; | |
| 27 | +import java.net.URL; | |
| 28 | +import java.sql.Connection; | |
| 29 | +import java.sql.PreparedStatement; | |
| 30 | +import java.sql.ResultSet; | |
| 31 | +import java.sql.SQLException; | |
| 32 | +import java.util.ArrayList; | |
| 33 | +import java.util.HashMap; | |
| 34 | +import java.util.Iterator; | |
| 35 | +import java.util.List; | |
| 36 | +import java.util.Map; | |
| 37 | + | |
| 38 | + | |
| 39 | +@EnableScheduling | |
| 40 | +@Component | |
| 41 | +public class PersonnelUpdateHandler { | |
| 42 | + | |
| 43 | + @Autowired | |
| 44 | + PersonnelService personnelService; | |
| 45 | + | |
| 46 | + @Autowired | |
| 47 | + PersonnelRepository repository; | |
| 48 | + | |
| 49 | + @Autowired | |
| 50 | + JdbcTemplate jdbcTemplate; | |
| 51 | + | |
| 52 | + Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 53 | + | |
| 54 | + private static final QName SERVICE_NAME = new QName("http://www.pd-transport.com/", "PJDataSerivce"); | |
| 55 | + | |
| 56 | + private static Map<String, String> gzname2code = new HashMap<>(); | |
| 57 | + | |
| 58 | + static { | |
| 59 | + gzname2code.put("路线驾驶员","1"); | |
| 60 | + gzname2code.put("团客车驾驶员","1"); | |
| 61 | + gzname2code.put("交通车驾驶员","1"); | |
| 62 | + gzname2code.put("调度员","2"); | |
| 63 | + gzname2code.put("乘务员","3"); | |
| 64 | + gzname2code.put("机务员/质检员","3"); | |
| 65 | + gzname2code.put("其他服务人员","14"); | |
| 66 | + } | |
| 67 | + | |
| 68 | + @Scheduled(cron = "0 0 4-23 * * *") | |
| 69 | + public void sync(String s) { | |
| 70 | + final Map<String, String> unit2company = new HashMap<>(4); | |
| 71 | + unit2company.put("KD01.0110", "55-上南公司"); | |
| 72 | + unit2company.put("KD01.0111", "22-金高公司"); | |
| 73 | + unit2company.put("KD01.0112", "26-南汇公司"); | |
| 74 | + unit2company.put("KD01.0113", "05-杨高公司"); | |
| 75 | + final Map<String, String> unit2bcompany = new HashMap<>(); | |
| 76 | + unit2bcompany.put("杨高公交分公司", "5-杨高分公司"); | |
| 77 | + unit2bcompany.put("杨高周浦分公司", "6-周浦分公司"); | |
| 78 | + unit2bcompany.put("杨高川沙分公司", "1-川沙分公司"); | |
| 79 | + unit2bcompany.put("杨高金桥分公司", "2-金桥分公司"); | |
| 80 | + unit2bcompany.put("上南六分公司", "3-上南六分公司"); | |
| 81 | + unit2bcompany.put("上南三分公司", "2-上南三分公司"); | |
| 82 | + unit2bcompany.put("上南一分公司", "4-上南一分公司"); | |
| 83 | + unit2bcompany.put("上南二分公司", "1-上南二分公司"); | |
| 84 | + unit2bcompany.put("金高二分公司", "2-二分公司"); | |
| 85 | + unit2bcompany.put("金高四分公司", "1-四分公司"); | |
| 86 | + unit2bcompany.put("金高三分公司", "3-三分公司"); | |
| 87 | + unit2bcompany.put("金高一分公司", "5-一分公司"); | |
| 88 | + unit2bcompany.put("南汇一分公司", "1-南汇一分"); | |
| 89 | + unit2bcompany.put("南汇二分公司", "2-南汇二分"); | |
| 90 | + unit2bcompany.put("南汇临港三分公司", "3-南汇三分"); | |
| 91 | + unit2bcompany.put("南汇六分公司", "6-南汇六分"); | |
| 92 | + unit2bcompany.put("南汇五分公司", "7-南汇五分"); | |
| 93 | + | |
| 94 | + PJDataSerivce ss = new PJDataSerivce(PJDataSerivce.WSDL_LOCATION, SERVICE_NAME); | |
| 95 | + PJDataSerivceSoap port = ss.getPJDataSerivceSoap12(); | |
| 96 | + //编程式事务 | |
| 97 | + DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 98 | + DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 99 | + def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 100 | + TransactionStatus status = tran.getTransaction(def); | |
| 101 | + try { | |
| 102 | + logger.info("人事数据同步 开始"); | |
| 103 | + List<Personnel> personnels = jdbcTemplate.query("select * from bsth_c_personnel", BeanPropertyRowMapper.newInstance(Personnel.class)); | |
| 104 | + logger.info("personnel 原数据:" + new ObjectMapper().writeValueAsString(personnels)); | |
| 105 | + // KD01.0110上南 KD01.0111金高 KD01.0112南汇 KD01.0113杨高 | |
| 106 | + jdbcTemplate.execute("delete from bsth_c_employee"); | |
| 107 | + for (int i = 0;i < 4;i++) { | |
| 108 | + final String unintCode = "KD01.011" + i; | |
| 109 | + String employeeXml = port.getEmployee(unintCode, ""); | |
| 110 | + logger.info(unintCode + ":" + employeeXml); | |
| 111 | + JSONObject json = XmlToJson.xml2Json(employeeXml); | |
| 112 | + final JSONArray emps = (JSONArray)json.get("Emp"); | |
| 113 | + jdbcTemplate.batchUpdate("insert into bsth_c_employee values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() { | |
| 114 | + @Override | |
| 115 | + public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 116 | + JSONObject emp = emps.getJSONObject(i); | |
| 117 | + String jobCode = emp.getString("Code"), company = unit2company.get(unintCode), positName = emp.getString("PositName"), bcompany = unit2bcompany.get(emp.getString("HRMS_USERFIELD_45")); | |
| 118 | + String[] jobCodeSplit = jobCode.split("-"), companySplit = company.split("-"), bcompanySplit = bcompany == null ? null : bcompany.split("-"); | |
| 119 | + | |
| 120 | + ps.setString(1, jobCode); | |
| 121 | + ps.setString(2, companySplit[0] + "-" + jobCodeSplit[1]); | |
| 122 | + ps.setString(3, jobCodeSplit[1]); | |
| 123 | + ps.setString(4, emp.getString("EmployeeName")); | |
| 124 | + ps.setString(5, gzname2code.get(positName) == null ? "14" : gzname2code.get(positName)); | |
| 125 | + ps.setString(6, companySplit[0]); | |
| 126 | + ps.setString(7, companySplit[1]); | |
| 127 | + ps.setString(8, emp.getString("IDCardID") == null ? "" : emp.getString("IDCardID")); | |
| 128 | + ps.setString(9, emp.getString("EnterDeptDate")); | |
| 129 | + ps.setString(10, emp.getString("EnterGroupDate")); | |
| 130 | + ps.setString(11, emp.getString("InpositionDate")); | |
| 131 | + ps.setString(12, bcompanySplit == null ? null : bcompanySplit[0]); | |
| 132 | + ps.setString(13, bcompanySplit == null ? null : bcompanySplit[1]); | |
| 133 | + } | |
| 134 | + | |
| 135 | + @Override | |
| 136 | + public int getBatchSize() { | |
| 137 | + return emps.size(); | |
| 138 | + } | |
| 139 | + }); | |
| 140 | + } | |
| 141 | + // 根据工号关联更新姓名、公司、工种、身份证号 | |
| 142 | + int rows = jdbcTemplate.update("update bsth_c_personnel a inner join bsth_c_employee b on a.job_code = b.job_code1 set a.personnel_name = b.employee_name,a.personnel_type = b.job_type,a.company = b.company_name,a.company_code = b.company_code,a.card = b.id_card,a.branche_company_code = b.branche_company_code,a.branche_company = b.branche_company,a.jd_codeori = b.job_code,a.job_codeori = b.job_code2 where a.destroy = 0 and a.locked = 0"); | |
| 143 | + logger.info(String.format("根据工号关联更新姓名、公司、工种、身份证号,记录数%d",rows)); | |
| 144 | + // 未匹配上工号的数据 用身份证号关联更新工号、姓名、公司、工种 | |
| 145 | + rows = jdbcTemplate.update("update bsth_c_personnel a inner join bsth_c_employee b on a.card = b.id_card set a.job_code = b.job_code1,a.personnel_name = b.employee_name,a.personnel_type = b.job_type,a.company = b.company_name,a.company_code = b.company_code,a.branche_company_code = b.branche_company_code,a.branche_company = b.branche_company,a.jd_codeori = b.job_code,a.job_codeori = b.job_code2 where a.destroy = 0 and a.job_code <> b.job_code1 and a.locked = 0"); | |
| 146 | + logger.info(String.format("根据身份证号关联更新工号、姓名、公司、工种,记录数%d",rows)); | |
| 147 | + // 可能会出现数据异常情况 如一个是最新的工号无身份证号,一个是老的工号有身份证号,最后更新的结果会造成工号和身份证号重复 | |
| 148 | + // 将更早创建的人员信息更新为停用 | |
| 149 | + rows = jdbcTemplate.update("update bsth_c_personnel x INNER JOIN (select card,min(id) id from bsth_c_personnel a where destroy = 0 and LENGTH(a.card) > 1 and a.locked = 0 GROUP BY card having count(*) > 1) y on x.id = y.id set destroy = 1"); | |
| 150 | + logger.info(String.format("同一个人,将更早创建的人员停用,记录数%d",rows)); | |
| 151 | + // 仍未匹配上的调度数据作为停用人员 | |
| 152 | + rows = jdbcTemplate.update("update bsth_c_personnel a left join bsth_c_employee b on a.job_code = b.job_code1 set a.destroy = 1 where a.destroy = 0 and b.job_code1 is null and a.locked = 0"); | |
| 153 | + logger.info(String.format("调度系统未匹配数据标记为停用,记录数%d",rows)); | |
| 154 | + // 仍未匹配上的金蝶数据作为新人员 | |
| 155 | + rows = jdbcTemplate.update("insert into bsth_c_personnel (job_code,personnel_name,personnel_type,company,company_code,job_codeori,branche_company_code,branche_company,jd_codeori) select b.job_code1,b.employee_name,b.job_type,b.company_name,b.company_code,b.job_code2,b.branche_company_code,b.branche_company,b.job_code from bsth_c_employee b left join bsth_c_personnel a on b.job_code1 = a.job_code where a.job_code is null"); | |
| 156 | + logger.info(String.format("金蝶未匹配数据插入,记录数%d",rows)); | |
| 157 | + // 赋值分公司编码1 不然无法查询 | |
| 158 | + rows = jdbcTemplate.update("update bsth_c_personnel set branche_company_code = '1' where branche_company_code is null and locked = 0"); | |
| 159 | + logger.info(String.format("分公司代码更新为1,记录数%d",rows)); | |
| 160 | + logger.info(String.format("临港公交的人员全部启用,记录数%d",rows)); | |
| 161 | + logger.info("人事数据同步 结束"); | |
| 162 | + tran.commit(status); | |
| 163 | + } catch (Exception e) { | |
| 164 | + tran.rollback(status); | |
| 165 | + logger.error("", e); | |
| 166 | + } | |
| 167 | + } | |
| 168 | +} | ... | ... |
src/main/java/com/bsth/handler/personnelUpdateHandler.java deleted
100644 → 0
| 1 | -package com.bsth.handler; | |
| 2 | - | |
| 3 | -import com.alibaba.fastjson.JSONArray; | |
| 4 | -import com.alibaba.fastjson.JSONObject; | |
| 5 | -import com.bsth.entity.Personnel; | |
| 6 | -import com.bsth.repository.PersonnelRepository; | |
| 7 | -import com.bsth.service.PersonnelService; | |
| 8 | -import com.bsth.util.DBUtils_Personnel; | |
| 9 | -import com.bsth.util.XmlToJson; | |
| 10 | -import com.bsth.wsdl.PJDataSerivce; | |
| 11 | -import com.bsth.wsdl.PJDataSerivceSoap; | |
| 12 | -import com.fasterxml.jackson.databind.ObjectMapper; | |
| 13 | -import org.hibernate.collection.internal.PersistentBag; | |
| 14 | -import org.joda.time.DateTime; | |
| 15 | -import org.slf4j.Logger; | |
| 16 | -import org.slf4j.LoggerFactory; | |
| 17 | -import org.springframework.beans.factory.annotation.Autowired; | |
| 18 | -import org.springframework.jdbc.core.BatchPreparedStatementSetter; | |
| 19 | -import org.springframework.jdbc.core.BeanPropertyRowMapper; | |
| 20 | -import org.springframework.jdbc.core.JdbcTemplate; | |
| 21 | -import org.springframework.jdbc.datasource.DataSourceTransactionManager; | |
| 22 | -import org.springframework.stereotype.Component; | |
| 23 | -import org.springframework.transaction.TransactionDefinition; | |
| 24 | -import org.springframework.transaction.TransactionStatus; | |
| 25 | -import org.springframework.transaction.support.DefaultTransactionDefinition; | |
| 26 | - | |
| 27 | -import javax.xml.namespace.QName; | |
| 28 | -import java.net.URL; | |
| 29 | -import java.sql.Connection; | |
| 30 | -import java.sql.PreparedStatement; | |
| 31 | -import java.sql.ResultSet; | |
| 32 | -import java.sql.SQLException; | |
| 33 | -import java.util.ArrayList; | |
| 34 | -import java.util.HashMap; | |
| 35 | -import java.util.Iterator; | |
| 36 | -import java.util.List; | |
| 37 | -import java.util.Map; | |
| 38 | - | |
| 39 | - | |
| 40 | -@Component | |
| 41 | -public class personnelUpdateHandler { | |
| 42 | - | |
| 43 | - @Autowired | |
| 44 | - PersonnelService personnelService; | |
| 45 | - | |
| 46 | - @Autowired | |
| 47 | - PersonnelRepository repository; | |
| 48 | - | |
| 49 | - @Autowired | |
| 50 | - JdbcTemplate jdbcTemplate; | |
| 51 | - | |
| 52 | - Logger logger = LoggerFactory.getLogger(this.getClass()); | |
| 53 | - | |
| 54 | - private static final QName SERVICE_NAME = new QName("http://www.pd-transport.com/", "PJDataSerivce"); | |
| 55 | - | |
| 56 | - private static Map<String, String> gzname2code = new HashMap<>(); | |
| 57 | - | |
| 58 | - static { | |
| 59 | - gzname2code.put("路线驾驶员","1"); | |
| 60 | - gzname2code.put("团客车驾驶员","1"); | |
| 61 | - gzname2code.put("交通车驾驶员","1"); | |
| 62 | - gzname2code.put("调度员","2"); | |
| 63 | - gzname2code.put("乘务员","3"); | |
| 64 | - gzname2code.put("机务员/质检员","3"); | |
| 65 | - gzname2code.put("其他服务人员","14"); | |
| 66 | - } | |
| 67 | - | |
| 68 | - public void sync() { | |
| 69 | - | |
| 70 | - URL wsdlURL = PJDataSerivce.WSDL_LOCATION; | |
| 71 | - | |
| 72 | - PJDataSerivce ss = new PJDataSerivce(wsdlURL, SERVICE_NAME); | |
| 73 | - PJDataSerivceSoap port = ss.getPJDataSerivceSoap12(); | |
| 74 | - | |
| 75 | - | |
| 76 | - | |
| 77 | - | |
| 78 | - try { | |
| 79 | - Connection conn ; | |
| 80 | - PreparedStatement ps; | |
| 81 | - ResultSet rs = null ; | |
| 82 | - DateTime dt = DateTime.now(); | |
| 83 | - String rq = dt.toString("yyyy-MM-dd"); | |
| 84 | - logger.info("同步人员数据: " + rq); | |
| 85 | - System.out.println("Invoking getEmployee..."); | |
| 86 | - String _getEmployee_idCard = ""; | |
| 87 | - /*String unintCode_sn = "KD01.0110"; | |
| 88 | - String _getEmployee_sn = port.getEmployee(unintCode_sn, _getEmployee_idCard); | |
| 89 | - String unintCode_jg = "KD01.0111"; | |
| 90 | - String _getEmployee_jg = port.getEmployee(unintCode_jg, _getEmployee_idCard); | |
| 91 | - String unintCode_yg = "KD01.0113"; | |
| 92 | - String _getEmployee_yg = port.getEmployee(unintCode_yg, _getEmployee_idCard); | |
| 93 | - String unintCode_nh = "KD01.0112"; | |
| 94 | - String _getEmployee_nh = port.getEmployee(unintCode_nh, _getEmployee_idCard);*/ | |
| 95 | - | |
| 96 | - logger.info("金蝶数据同步到:bsth_c_personnel 表,开始"); | |
| 97 | - | |
| 98 | - // 把所有人员设为停用状态,然后在用金蝶人员数据匹配,匹配上的设为在职 | |
| 99 | - conn = DBUtils_Personnel.getConnection(); | |
| 100 | - // 服务器 | |
| 101 | - ps = conn.prepareStatement("UPDATE pd_control.bsth_c_personnel SET destroy = 1"); | |
| 102 | - // 本地 | |
| 103 | -// ps = conn.prepareStatement("UPDATE pd_control.bsth_c_personnel SET destroy = 1"); | |
| 104 | - ps.executeUpdate(); | |
| 105 | - | |
| 106 | - // 取出所有调度人员,转存map | |
| 107 | - Map<String, Personnel> personnelMap= new HashMap<>(); | |
| 108 | - Iterator<Personnel> perIterator=repository.findAll().iterator(); | |
| 109 | -// List<Personnel> personnelList = new ArrayList<>(); | |
| 110 | - while(perIterator.hasNext()){ | |
| 111 | - Personnel per=perIterator.next(); | |
| 112 | - personnelMap.put(per.getJobCode(),per); | |
| 113 | - // 工号补全6位 | |
| 114 | -// String JobCodeori = per.getJobCodeori(); | |
| 115 | -// while (JobCodeori.length() < 6){ | |
| 116 | -// JobCodeori = "0"+JobCodeori; | |
| 117 | -// } | |
| 118 | -// per.setJobCodeori(JobCodeori); | |
| 119 | -// per.setJobCode(per.getCompanyCode()+"-"+JobCodeori); | |
| 120 | -// personnelList.add(per); | |
| 121 | - } | |
| 122 | - // 工号补全6位 | |
| 123 | -// repository.save(personnelList); | |
| 124 | - | |
| 125 | - for(int i=0;i<4;i++){ | |
| 126 | - String unintCode = "KD01.011"+i; | |
| 127 | - String _getEmployee__return = port.getEmployee(unintCode, _getEmployee_idCard); | |
| 128 | - String _getRetiredInfor_unintCode = null; | |
| 129 | - System.out.println("Invoking getRetiredInfor..."); | |
| 130 | - String _getRetiredInfor_beginDate = "1900-01-01"; | |
| 131 | - String _getRetiredInfor_endDate = rq; | |
| 132 | - JSONArray retiredInfor = new JSONArray(); | |
| 133 | - if(_getRetiredInfor_unintCode != null){ | |
| 134 | - String _getRetiredInfor__return = port.getRetiredInfor(unintCode, _getRetiredInfor_beginDate, _getRetiredInfor_endDate); | |
| 135 | - JSONObject jsonRetiredInfor= XmlToJson.xml2Json(_getRetiredInfor__return); | |
| 136 | - retiredInfor = JSONArray.parseArray(jsonRetiredInfor.get("Retire").toString()); | |
| 137 | - } | |
| 138 | - | |
| 139 | - personnelService.matchPersonnel(retiredInfor,_getEmployee__return,i,personnelMap); | |
| 140 | - } | |
| 141 | - | |
| 142 | - logger.info("金蝶数据同步到:bsth_c_personnel 表,完成"); | |
| 143 | - | |
| 144 | - logger.info("把bsth_c_personnel备份到:personnel库的bsth_c_personnel"+rq+" 表"); | |
| 145 | - String sql="CREATE TABLE `bsth_c_personnel"+rq+"` (" + | |
| 146 | - " `id` int(11) NOT NULL ," +//AUTO_INCREMENT | |
| 147 | - " `branche_company` varchar(255) DEFAULT NULL," + | |
| 148 | - " `branche_company_code` varchar(255) DEFAULT NULL," + | |
| 149 | - " `company` varchar(255) DEFAULT NULL," + | |
| 150 | - " `company_code` varchar(255) NOT NULL," + | |
| 151 | - " `create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP," + | |
| 152 | - " `descriptions` varchar(255) DEFAULT NULL," + | |
| 153 | - " `ic_card_code` varchar(255) DEFAULT NULL," + | |
| 154 | - " `job_code` varchar(255) NOT NULL," + | |
| 155 | - " `line_code` varchar(255) DEFAULT NULL," + | |
| 156 | - " `papers_code` varchar(255) DEFAULT NULL," + | |
| 157 | - " `personnel_code` varchar(255) DEFAULT NULL," + | |
| 158 | - " `personnel_img` varchar(255) DEFAULT NULL," + | |
| 159 | - " `personnel_name` varchar(255) NOT NULL," + | |
| 160 | - " `personnel_type` varchar(255) DEFAULT NULL," + | |
| 161 | - " `posts` varchar(255) DEFAULT NULL," + | |
| 162 | - " `telphone` varchar(255) DEFAULT NULL," + | |
| 163 | - " `update_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," + | |
| 164 | - " `create_by` int(11) DEFAULT NULL," + | |
| 165 | - " `update_by` int(11) DEFAULT NULL," + | |
| 166 | - " `card` varchar(255) DEFAULT NULL," + | |
| 167 | - " `ic_rfid` varchar(255) DEFAULT NULL," + | |
| 168 | - " `id_rfid` varchar(255) DEFAULT NULL," + | |
| 169 | - " `tag_rfid` varchar(255) DEFAULT NULL," + | |
| 170 | - " `remark` varchar(255) DEFAULT NULL," + | |
| 171 | - " `job_codeori` varchar(255) NOT NULL," + | |
| 172 | - " `destroy` int(11)," + | |
| 173 | - " `jd_codeori` varchar(255)," + | |
| 174 | - "PRIMARY KEY (`id`)"+ | |
| 175 | - ") ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8"; | |
| 176 | - | |
| 177 | - int returnRs; | |
| 178 | - try { | |
| 179 | - logger.info("创建personnel库的bsth_c_personnel"+rq+" 表"); | |
| 180 | -// conn = DBUtils_Personnel.getConnection(); | |
| 181 | - ps = conn.prepareStatement("DROP TABLE IF EXISTS `bsth_c_personnel"+rq+"`"); | |
| 182 | - ps.executeUpdate(); | |
| 183 | - ps = conn.prepareStatement(sql); | |
| 184 | - returnRs = ps.executeUpdate(); | |
| 185 | - if(returnRs == 0){ | |
| 186 | - logger.info("备份数据到personnel库的bsth_c_personnel"+rq+" 表,开始"); | |
| 187 | - // 服务器 | |
| 188 | - sql = "INSERT into `bsth_c_personnel"+rq+"` (SELECT * FROM control.bsth_c_personnel)"; | |
| 189 | - // 本地 | |
| 190 | -// sql = "INSERT into `bsth_c_personnel"+rq+"` (SELECT * FROM pd_control.bsth_c_personnel)"; | |
| 191 | - ps = conn.prepareStatement(sql); | |
| 192 | - returnRs = ps.executeUpdate(); | |
| 193 | - | |
| 194 | - logger.info("备份数据到personnel库的bsth_c_personnel"+rq+" 表,完成"); | |
| 195 | - } | |
| 196 | - } catch (SQLException e) { | |
| 197 | - // TODO Auto-generated catch block | |
| 198 | - e.printStackTrace(); | |
| 199 | - }finally { | |
| 200 | - DBUtils_Personnel.close(rs, ps, conn); | |
| 201 | - } | |
| 202 | - | |
| 203 | - | |
| 204 | -// logger.info("把bsth_c_personnel_copy数据同步到:bsth_c_personnel 表,开始"); | |
| 205 | -// List<Personnel> retuenList = jdbcTemplate.query("select * from bsth_c_personnel_copy",new BeanPropertyRowMapper(Personnel.class)); | |
| 206 | -// repository.save(retuenList); | |
| 207 | -// logger.info("bsth_c_personnel_copy数据同步到:bsth_c_personnel 表,完成"); | |
| 208 | -// jdbcTemplate.execute() | |
| 209 | -// // 备份数据 | |
| 210 | -// jdbcTemplate.update("INSERT into `bsth_c_personnel"+rq+"` (SELECT * FROM pd_control.bsth_c_personnel)"); | |
| 211 | -// jdbcTemplate.update("insert into bsth_c_personnel_copy select * from bsth_c_personnel"); | |
| 212 | -// String _getEmployee__return = _getEmployee_sn + _getEmployee_jg + _getEmployee_yg + _getEmployee_nh; | |
| 213 | -// JSONObject jsonInfor = XmlToJson.xml2Json(_getEmployee__return); | |
| 214 | -// System.out.println("getEmployee.result=" + _getEmployee__return); | |
| 215 | - | |
| 216 | - logger.info("同步完成: " + rq); | |
| 217 | - } catch (Exception e) { | |
| 218 | - logger.error("", e); | |
| 219 | - } | |
| 220 | - } | |
| 221 | - | |
| 222 | - public void sync(String s) { | |
| 223 | - final Map<String, String> unit2company = new HashMap<>(4); | |
| 224 | - unit2company.put("KD01.0110", "55-上南公司"); | |
| 225 | - unit2company.put("KD01.0111", "22-金高公司"); | |
| 226 | - unit2company.put("KD01.0112", "26-南汇公司"); | |
| 227 | - unit2company.put("KD01.0113", "05-杨高公司"); | |
| 228 | - final Map<String, String> unit2bcompany = new HashMap<>(); | |
| 229 | - unit2bcompany.put("杨高分公司", "5-杨高分公司"); | |
| 230 | - unit2bcompany.put("周浦分公司", "6-周浦分公司"); | |
| 231 | - unit2bcompany.put("川沙分公司", "1-川沙分公司"); | |
| 232 | - unit2bcompany.put("金桥分公司", "2-金桥分公司"); | |
| 233 | - unit2bcompany.put("上南六分", "3-上南六分公司"); | |
| 234 | - unit2bcompany.put("上南三分", "2-上南三分公司"); | |
| 235 | - unit2bcompany.put("上南一分", "4-上南一分公司"); | |
| 236 | - unit2bcompany.put("上南二分", "1-上南二分公司"); | |
| 237 | - unit2bcompany.put("金高二分", "2-二分公司"); | |
| 238 | - unit2bcompany.put("金高四分", "1-四分公司"); | |
| 239 | - unit2bcompany.put("金高三分", "3-三分公司"); | |
| 240 | - unit2bcompany.put("金高一分", "5-一分公司"); | |
| 241 | - unit2bcompany.put("南汇一分", "1-南汇一分"); | |
| 242 | - unit2bcompany.put("南汇二分", "2-南汇二分"); | |
| 243 | - unit2bcompany.put("南汇三分", "3-南汇三分"); | |
| 244 | - unit2bcompany.put("南汇六分", "6-南汇六分"); | |
| 245 | - unit2bcompany.put("南汇五分", "7-南汇五分"); | |
| 246 | - | |
| 247 | - PJDataSerivce ss = new PJDataSerivce(PJDataSerivce.WSDL_LOCATION, SERVICE_NAME); | |
| 248 | - PJDataSerivceSoap port = ss.getPJDataSerivceSoap12(); | |
| 249 | - //编程式事务 | |
| 250 | - DataSourceTransactionManager tran = new DataSourceTransactionManager(jdbcTemplate.getDataSource()); | |
| 251 | - DefaultTransactionDefinition def = new DefaultTransactionDefinition(); | |
| 252 | - def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); | |
| 253 | - TransactionStatus status = tran.getTransaction(def); | |
| 254 | - try { | |
| 255 | - logger.info("人事数据同步 开始"); | |
| 256 | - List<Personnel> personnels = jdbcTemplate.query("select * from bsth_c_personnel", BeanPropertyRowMapper.newInstance(Personnel.class)); | |
| 257 | - logger.info("personnel 原数据:" + new ObjectMapper().writeValueAsString(personnels)); | |
| 258 | - // KD01.0110上南 KD01.0111金高 KD01.0112南汇 KD01.0113杨高 | |
| 259 | - jdbcTemplate.execute("delete from bsth_c_employee"); | |
| 260 | - for (int i = 0;i < 4;i++) { | |
| 261 | - final String unintCode = "KD01.011" + i; | |
| 262 | - String employeeXml = port.getEmployee(unintCode, ""); | |
| 263 | - logger.info(unintCode + ":" + employeeXml); | |
| 264 | - JSONObject json = XmlToJson.xml2Json(employeeXml); | |
| 265 | - final JSONArray emps = (JSONArray)json.get("Emp"); | |
| 266 | - jdbcTemplate.batchUpdate("insert into bsth_c_employee values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new BatchPreparedStatementSetter() { | |
| 267 | - @Override | |
| 268 | - public void setValues(PreparedStatement ps, int i) throws SQLException { | |
| 269 | - JSONObject emp = emps.getJSONObject(i); | |
| 270 | - String jobCode = emp.getString("Code"), company = unit2company.get(unintCode), positName = emp.getString("PositName"), bcompany = unit2bcompany.get(emp.getString("HRMS_USERFIELD_45")); | |
| 271 | - String[] jobCodeSplit = jobCode.split("-"), companySplit = company.split("-"), bcompanySplit = bcompany == null ? null : bcompany.split("-"); | |
| 272 | - | |
| 273 | - ps.setString(1, jobCode); | |
| 274 | - ps.setString(2, companySplit[0] + "-" + jobCodeSplit[1]); | |
| 275 | - ps.setString(3, jobCodeSplit[1]); | |
| 276 | - ps.setString(4, emp.getString("EmployeeName")); | |
| 277 | - ps.setString(5, gzname2code.get(positName) == null ? "14" : gzname2code.get(positName)); | |
| 278 | - ps.setString(6, companySplit[0]); | |
| 279 | - ps.setString(7, companySplit[1]); | |
| 280 | - ps.setString(8, emp.getString("IDCardID") == null ? "" : emp.getString("IDCardID")); | |
| 281 | - ps.setString(9, emp.getString("EnterDeptDate")); | |
| 282 | - ps.setString(10, emp.getString("EnterGroupDate")); | |
| 283 | - ps.setString(11, emp.getString("InpositionDate")); | |
| 284 | - ps.setString(12, bcompanySplit == null ? null : bcompanySplit[0]); | |
| 285 | - ps.setString(13, bcompanySplit == null ? null : bcompanySplit[1]); | |
| 286 | - } | |
| 287 | - | |
| 288 | - @Override | |
| 289 | - public int getBatchSize() { | |
| 290 | - return emps.size(); | |
| 291 | - } | |
| 292 | - }); | |
| 293 | - } | |
| 294 | - // 根据工号关联更新姓名、公司、工种、身份证号 | |
| 295 | - int rows = jdbcTemplate.update("update bsth_c_personnel a inner join bsth_c_employee b on a.job_code = b.job_code1 set a.personnel_name = b.employee_name,a.personnel_type = b.job_type,a.company = b.company_name,a.company_code = b.company_code,a.card = b.id_card,a.branche_company_code = b.branche_company_code,a.branche_company = b.branche_company,a.jd_codeori = b.job_code,a.job_codeori = b.job_code2 where a.destroy = 0 and a.locked = 0"); | |
| 296 | - logger.info(String.format("根据工号关联更新姓名、公司、工种、身份证号,记录数%d",rows)); | |
| 297 | - // 未匹配上工号的数据 用身份证号关联更新工号、姓名、公司、工种 | |
| 298 | - rows = jdbcTemplate.update("update bsth_c_personnel a inner join bsth_c_employee b on a.card = b.id_card set a.job_code = b.job_code1,a.personnel_name = b.employee_name,a.personnel_type = b.job_type,a.company = b.company_name,a.company_code = b.company_code,a.branche_company_code = b.branche_company_code,a.branche_company = b.branche_company,a.jd_codeori = b.job_code,a.job_codeori = b.job_code2 where a.destroy = 0 and a.job_code <> b.job_code1 and a.locked = 0"); | |
| 299 | - logger.info(String.format("根据身份证号关联更新工号、姓名、公司、工种,记录数%d",rows)); | |
| 300 | - // 可能会出现数据异常情况 如一个是最新的工号无身份证号,一个是老的工号有身份证号,最后更新的结果会造成工号和身份证号重复 | |
| 301 | - // 将更早创建的人员信息更新为停用 | |
| 302 | - rows = jdbcTemplate.update("update bsth_c_personnel x INNER JOIN (select card,min(id) id from bsth_c_personnel a where destroy = 0 and LENGTH(a.card) > 1 and a.locked = 0 GROUP BY card having count(*) > 1) y on x.id = y.id set destroy = 1"); | |
| 303 | - logger.info(String.format("同一个人,将更早创建的人员停用,记录数%d",rows)); | |
| 304 | - // 仍未匹配上的调度数据作为停用人员 | |
| 305 | - rows = jdbcTemplate.update("update bsth_c_personnel a left join bsth_c_employee b on a.job_code = b.job_code1 set a.destroy = 1 where a.destroy = 0 and b.job_code1 is null and a.locked = 0"); | |
| 306 | - logger.info(String.format("调度系统未匹配数据标记为停用,记录数%d",rows)); | |
| 307 | - // 仍未匹配上的金蝶数据作为新人员 | |
| 308 | - rows = jdbcTemplate.update("insert into bsth_c_personnel (job_code,personnel_name,personnel_type,company,company_code,job_codeori,branche_company_code,branche_company,jd_codeori) select b.job_code1,b.employee_name,b.job_type,b.company_name,b.company_code,b.job_code2,b.branche_company_code,b.branche_company,b.job_code from bsth_c_employee b left join bsth_c_personnel a on b.job_code1 = a.job_code where a.job_code is null"); | |
| 309 | - logger.info(String.format("金蝶未匹配数据插入,记录数%d",rows)); | |
| 310 | - // 赋值分公司编码1 不然无法查询 | |
| 311 | - rows = jdbcTemplate.update("update bsth_c_personnel set branche_company_code = '1' where branche_company_code is null and locked = 0"); | |
| 312 | - logger.info(String.format("分公司代码更新为1,记录数%d",rows)); | |
| 313 | - logger.info(String.format("临港公交的人员全部启用,记录数%d",rows)); | |
| 314 | - logger.info("人事数据同步 结束"); | |
| 315 | - tran.commit(status); | |
| 316 | - } catch (Exception e) { | |
| 317 | - tran.rollback(status); | |
| 318 | - logger.error("", e); | |
| 319 | - } | |
| 320 | - } | |
| 321 | -} |
src/main/java/com/bsth/thread/PersonnelUpdateThrad.java renamed to src/main/java/com/bsth/thread/PersonnelUpdateThread.java
| ... | ... | @@ -14,7 +14,7 @@ import org.springframework.stereotype.Component; |
| 14 | 14 | * |
| 15 | 15 | */ |
| 16 | 16 | @Component |
| 17 | -public class PersonnelUpdateThrad extends Thread { | |
| 17 | +public class PersonnelUpdateThread extends Thread { | |
| 18 | 18 | @Autowired |
| 19 | 19 | personnelUpdateHandler personnelUpdateHandler; |
| 20 | 20 | ... | ... |
src/main/java/com/bsth/util/DBUtils_Personnel.java deleted
100644 → 0
| 1 | -package com.bsth.util; | |
| 2 | - | |
| 3 | -import com.mchange.v2.c3p0.DataSources; | |
| 4 | -import org.apache.log4j.Logger; | |
| 5 | - | |
| 6 | -import javax.sql.DataSource; | |
| 7 | -import java.io.FileNotFoundException; | |
| 8 | -import java.io.IOException; | |
| 9 | -import java.sql.Connection; | |
| 10 | -import java.sql.ResultSet; | |
| 11 | -import java.sql.SQLException; | |
| 12 | -import java.sql.Statement; | |
| 13 | -import java.util.HashMap; | |
| 14 | -import java.util.Map; | |
| 15 | -import java.util.Properties; | |
| 16 | - | |
| 17 | -/** | |
| 18 | - * 数据备份personnel库连接池 | |
| 19 | - * @author PanZhao | |
| 20 | - * | |
| 21 | - */ | |
| 22 | -//@Component | |
| 23 | -public class DBUtils_Personnel { | |
| 24 | - | |
| 25 | - private static String url = null; | |
| 26 | - | |
| 27 | - private static String username = null; | |
| 28 | - | |
| 29 | - private static String pwd = null; | |
| 30 | - | |
| 31 | - private static DataSource ds_pooled; | |
| 32 | - | |
| 33 | - static Logger logger = Logger.getLogger(DBUtils_Personnel.class); | |
| 34 | - | |
| 35 | - static { | |
| 36 | - Properties env = new Properties(); | |
| 37 | - | |
| 38 | - try { | |
| 39 | - env.load(DBUtils_Personnel.class.getClassLoader().getResourceAsStream("personnel-jdbc.properties")); | |
| 40 | - // 1. 加载驱动类 | |
| 41 | - Class.forName(env.getProperty("ms.mysql.driver")); | |
| 42 | - | |
| 43 | - url = env.getProperty("ms.mysql.url"); | |
| 44 | - username = env.getProperty("ms.mysql.username"); | |
| 45 | - pwd = env.getProperty("ms.mysql.password"); | |
| 46 | - | |
| 47 | - // 设置连接数据库的配置信息 | |
| 48 | - DataSource ds_unpooled = DataSources.unpooledDataSource(url, | |
| 49 | - username, pwd); | |
| 50 | - | |
| 51 | - Map<String, Object> pool_conf = new HashMap<String, Object>(); | |
| 52 | - // 设置最大连接数 | |
| 53 | - pool_conf.put("maxPoolSize", 10); | |
| 54 | - | |
| 55 | - pool_conf.put("testConnectionOnCheckout", false); | |
| 56 | - //异步检测连接的有效性 | |
| 57 | - pool_conf.put("testConnectionOnCheckin", true); | |
| 58 | - //30秒检测一次 | |
| 59 | - pool_conf.put("idleConnectionTestPeriod", 30); | |
| 60 | - ds_pooled = DataSources.pooledDataSource(ds_unpooled, pool_conf); | |
| 61 | - } catch (FileNotFoundException e) { | |
| 62 | - logger.error(e.toString()); | |
| 63 | - e.printStackTrace(); | |
| 64 | - } catch (IOException e) { | |
| 65 | - logger.error(e.toString()); | |
| 66 | - e.printStackTrace(); | |
| 67 | - } catch (ClassNotFoundException e) { | |
| 68 | - logger.error(e.toString()); | |
| 69 | - e.printStackTrace(); | |
| 70 | - } catch (SQLException e) { | |
| 71 | - logger.error(e.toString()); | |
| 72 | - e.printStackTrace(); | |
| 73 | - } | |
| 74 | - } | |
| 75 | - | |
| 76 | - /** | |
| 77 | - * 获取连接对象 | |
| 78 | - */ | |
| 79 | - public static Connection getConnection() throws SQLException { | |
| 80 | - return ds_pooled.getConnection(); | |
| 81 | - } | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * 释放连接池资源 | |
| 85 | - */ | |
| 86 | - public static void clearup() { | |
| 87 | - if (ds_pooled != null) { | |
| 88 | - try { | |
| 89 | - DataSources.destroy(ds_pooled); | |
| 90 | - } catch (SQLException e) { | |
| 91 | - logger.error(e.toString()); | |
| 92 | - e.printStackTrace(); | |
| 93 | - } | |
| 94 | - } | |
| 95 | - } | |
| 96 | - | |
| 97 | - /** | |
| 98 | - * 资源关闭 | |
| 99 | - * | |
| 100 | - * @param rs | |
| 101 | - * @param stmt | |
| 102 | - * @param conn | |
| 103 | - */ | |
| 104 | - public static void close(ResultSet rs, Statement stmt, Connection conn) { | |
| 105 | - if (rs != null) { | |
| 106 | - try { | |
| 107 | - rs.close(); | |
| 108 | - } catch (SQLException e) { | |
| 109 | - logger.error(e.toString()); | |
| 110 | - e.printStackTrace(); | |
| 111 | - } | |
| 112 | - } | |
| 113 | - | |
| 114 | - if (stmt != null) { | |
| 115 | - try { | |
| 116 | - stmt.close(); | |
| 117 | - } catch (SQLException e) { | |
| 118 | - logger.error(e.toString()); | |
| 119 | - e.printStackTrace(); | |
| 120 | - } | |
| 121 | - } | |
| 122 | - | |
| 123 | - if (conn != null) { | |
| 124 | - try { | |
| 125 | - conn.close(); | |
| 126 | - } catch (SQLException e) { | |
| 127 | - logger.error(e.toString()); | |
| 128 | - e.printStackTrace(); | |
| 129 | - } | |
| 130 | - } | |
| 131 | - } | |
| 132 | - | |
| 133 | - public static DataSource getDataSource(){ | |
| 134 | - return ds_pooled; | |
| 135 | - } | |
| 136 | -} |
src/main/resources/personnel-jdbc.properties deleted
100644 → 0
| 1 | -#ms.mysql.driver= com.mysql.jdbc.Driver | |
| 2 | -#ms.mysql.url= jdbc:mysql://127.0.0.1:3306/personnel?useUnicode=true&characterEncoding=utf-8&useSSL=false | |
| 3 | -#ms.mysql.username= root | |
| 4 | -#ms.mysql.password= | |
| 5 | - | |
| 6 | -ms.mysql.driver= com.mysql.jdbc.Driver | |
| 7 | -ms.mysql.url= jdbc:mysql://192.168.168.240:3306/personnel?useUnicode=true&characterEncoding=utf-8 | |
| 8 | -ms.mysql.username= root | |
| 9 | -ms.mysql.password= root2jsp |