personnelUpdateHandler.java 7.86 KB
package com.bsth.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.entity.Personnel;
import com.bsth.repository.PersonnelRepository;
import com.bsth.service.PersonnelService;
import com.bsth.util.DBUtils_Personnel;
import com.bsth.util.XmlToJson;
import com.bsth.wsdl.PJDataSerivce;
import com.bsth.wsdl.PJDataSerivceSoap;
import org.hibernate.collection.internal.PersistentBag;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.xml.namespace.QName;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


@Component
public class personnelUpdateHandler {

	@Autowired
	PersonnelService personnelService;

	@Autowired
	PersonnelRepository repository;

	@Autowired
    JdbcTemplate jdbcTemplate;
	
	Logger logger = LoggerFactory.getLogger(this.getClass());

	private static final QName SERVICE_NAME = new QName("http://www.pd-transport.com/", "PJDataSerivce");
	
	public void sync() {

		URL wsdlURL = PJDataSerivce.WSDL_LOCATION;

		PJDataSerivce ss = new PJDataSerivce(wsdlURL, SERVICE_NAME);
		PJDataSerivceSoap port = ss.getPJDataSerivceSoap12();




    	try {
			Connection conn ;
			PreparedStatement ps;
			ResultSet rs = null ;
	        DateTime dt = DateTime.now();
	        String rq = dt.toString("yyyy-MM-dd");
	        logger.info("同步人员数据: " + rq);
			System.out.println("Invoking getEmployee...");
			String _getEmployee_idCard = "";
			/*String unintCode_sn = "KD01.0110";
			String _getEmployee_sn = port.getEmployee(unintCode_sn, _getEmployee_idCard);
			String unintCode_jg = "KD01.0111";
			String _getEmployee_jg = port.getEmployee(unintCode_jg, _getEmployee_idCard);
			String unintCode_yg = "KD01.0113";
			String _getEmployee_yg = port.getEmployee(unintCode_yg, _getEmployee_idCard);
			String unintCode_nh = "KD01.0112";
			String _getEmployee_nh = port.getEmployee(unintCode_nh, _getEmployee_idCard);*/

			logger.info("金蝶数据同步到:bsth_c_personnel 表,开始");

			// 把所有人员设为停用状态,然后在用金蝶人员数据匹配,匹配上的设为在职
			conn = DBUtils_Personnel.getConnection();
			// 服务器
			ps = conn.prepareStatement("UPDATE control.bsth_c_personnel SET destroy = 1");
			// 本地
//			ps = conn.prepareStatement("UPDATE pd_control.bsth_c_personnel SET destroy = 1");
			ps.executeUpdate();

			// 取出所有调度人员,转存map
			Map<String, Personnel> personnelMap= new HashMap<>();
			Iterator<Personnel> perIterator=repository.findAll().iterator();
//			List<Personnel> personnelList = new ArrayList<>();
			while(perIterator.hasNext()){
				Personnel per=perIterator.next();
				personnelMap.put(per.getJobCode(),per);
				// 工号补全6位
//				String JobCodeori = per.getJobCodeori();
//				while (JobCodeori.length() < 6){
//					JobCodeori = "0"+JobCodeori;
//				}
//				per.setJobCodeori(JobCodeori);
//				per.setJobCode(per.getCompanyCode()+"-"+JobCodeori);
//				personnelList.add(per);
			}
			// 工号补全6位
//			repository.save(personnelList);

			for(int i=0;i<4;i++){
				String unintCode = "KD01.011"+i;
				String _getEmployee__return = port.getEmployee(unintCode, _getEmployee_idCard);
				String _getRetiredInfor_unintCode = null;
				System.out.println("Invoking getRetiredInfor...");
				String _getRetiredInfor_beginDate = "1900-01-01";
				String _getRetiredInfor_endDate = rq;
				JSONArray retiredInfor = new JSONArray();
				if(_getRetiredInfor_unintCode != null){
					String _getRetiredInfor__return = port.getRetiredInfor(unintCode, _getRetiredInfor_beginDate, _getRetiredInfor_endDate);
					JSONObject jsonRetiredInfor= XmlToJson.xml2Json(_getRetiredInfor__return);
					retiredInfor = JSONArray.parseArray(jsonRetiredInfor.get("Retire").toString());
				}

				personnelService.matchPersonnel(retiredInfor,_getEmployee__return,i,personnelMap);
			}

			logger.info("金蝶数据同步到:bsth_c_personnel 表,完成");

			logger.info("把bsth_c_personnel备份到:personnel库的bsth_c_personnel"+rq+" 表");
			String sql="CREATE TABLE `bsth_c_personnel"+rq+"` (" +
					"  `id` int(11) NOT NULL ," +//AUTO_INCREMENT
					"  `branche_company` varchar(255) DEFAULT NULL," +
					"  `branche_company_code` varchar(255) DEFAULT NULL," +
					"  `company` varchar(255) DEFAULT NULL," +
					"  `company_code` varchar(255) NOT NULL," +
					"  `create_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP," +
					"  `descriptions` varchar(255) DEFAULT NULL," +
					"  `ic_card_code` varchar(255) DEFAULT NULL," +
					"  `job_code` varchar(255) NOT NULL," +
					"  `line_code` varchar(255) DEFAULT NULL," +
					"  `papers_code` varchar(255) DEFAULT NULL," +
					"  `personnel_code` varchar(255) DEFAULT NULL," +
					"  `personnel_img` varchar(255) DEFAULT NULL," +
					"  `personnel_name` varchar(255) NOT NULL," +
					"  `personnel_type` varchar(255) DEFAULT NULL," +
					"  `posts` varchar(255) DEFAULT NULL," +
					"  `telphone` varchar(255) DEFAULT NULL," +
					"  `update_date` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
					"  `create_by` int(11) DEFAULT NULL," +
					"  `update_by` int(11) DEFAULT NULL," +
					"  `card` varchar(255) DEFAULT NULL," +
					"  `ic_rfid` varchar(255) DEFAULT NULL," +
					"  `id_rfid` varchar(255) DEFAULT NULL," +
					"  `tag_rfid` varchar(255) DEFAULT NULL," +
					"  `remark` varchar(255) DEFAULT NULL," +
					"  `job_codeori` varchar(255) NOT NULL," +
					"  `destroy` int(11)," +
					"  `jd_codeori` varchar(255)," +
					"PRIMARY KEY (`id`)"+
					") ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8";

			int returnRs;
			try {
				logger.info("创建personnel库的bsth_c_personnel"+rq+" 表");
//				conn = DBUtils_Personnel.getConnection();
				ps = conn.prepareStatement("DROP TABLE IF EXISTS `bsth_c_personnel"+rq+"`");
				ps.executeUpdate();
				ps = conn.prepareStatement(sql);
				returnRs = ps.executeUpdate();
				if(returnRs == 0){
					logger.info("备份数据到personnel库的bsth_c_personnel"+rq+" 表,开始");
					// 服务器
					sql = "INSERT into `bsth_c_personnel"+rq+"` (SELECT * FROM control.bsth_c_personnel)";
					// 本地
//					sql = "INSERT into `bsth_c_personnel"+rq+"` (SELECT * FROM pd_control.bsth_c_personnel)";
					ps = conn.prepareStatement(sql);
					returnRs = ps.executeUpdate();

					logger.info("备份数据到personnel库的bsth_c_personnel"+rq+" 表,完成");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				DBUtils_Personnel.close(rs, ps, conn);
			}


//			logger.info("把bsth_c_personnel_copy数据同步到:bsth_c_personnel 表,开始");
//			List<Personnel> retuenList = jdbcTemplate.query("select * from bsth_c_personnel_copy",new BeanPropertyRowMapper(Personnel.class));
//			repository.save(retuenList);
//			logger.info("bsth_c_personnel_copy数据同步到:bsth_c_personnel 表,完成");
//			jdbcTemplate.execute()
//			// 备份数据
//			jdbcTemplate.update("INSERT into `bsth_c_personnel"+rq+"` (SELECT * FROM pd_control.bsth_c_personnel)");
//			jdbcTemplate.update("insert into bsth_c_personnel_copy select * from bsth_c_personnel");
//			String _getEmployee__return = _getEmployee_sn + _getEmployee_jg + _getEmployee_yg + _getEmployee_nh;
//			JSONObject jsonInfor = XmlToJson.xml2Json(_getEmployee__return);
//			System.out.println("getEmployee.result=" + _getEmployee__return);

	        logger.info("同步完成: " + rq);
    	} catch (Exception e) {
            logger.error("", e);
        } 
	}
}