PersonnelServiceImpl.java 11.9 KB
package com.bsth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.entity.Business;
import com.bsth.entity.Personnel;
import com.bsth.entity.PersonnelCopy;
import com.bsth.repository.PersonnelRepository;
import com.bsth.service.PersonnelService;
import com.bsth.util.XmlToJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Transient;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by xu on 16/6/15.
 */
@Service
public class PersonnelServiceImpl implements PersonnelService {
	// TODO 以后过滤退休人员
	@Autowired
	PersonnelRepository repository;

	@Autowired
	JdbcTemplate jdbcTemplate;

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Transient
	@Override
	public List<PersonnelCopy> matchPersonnel(JSONArray retiredInfor,String _getEmployee__return,int index,Map<String, Personnel> personnelMap) {

		Map<String,String> gzDictionaryMap = new HashMap<>();
		gzDictionaryMap.put("路线驾驶员","1");
		gzDictionaryMap.put("团客车驾驶员","1");
		gzDictionaryMap.put("后方驾驶员","1");
		gzDictionaryMap.put("实习驾驶员","1");
		gzDictionaryMap.put("调度员","2");
		gzDictionaryMap.put("售票员","3");
		gzDictionaryMap.put("公共汽电车售票员","3");
		gzDictionaryMap.put("管理员","3");
		gzDictionaryMap.put("站员","4");
		gzDictionaryMap.put("安检员","6");
		gzDictionaryMap.put("机务","7");
		gzDictionaryMap.put("机务员/质检员","7");
		gzDictionaryMap.put("引导员","8");
		gzDictionaryMap.put("乘务员","9");
		gzDictionaryMap.put("线长(正职)","10");
		gzDictionaryMap.put("线长(副职)","10");
		gzDictionaryMap.put("见习管理人员","11");
		gzDictionaryMap.put("公司部门管理人员","11");
		gzDictionaryMap.put("机务员/警消人员","12");
		gzDictionaryMap.put("线长(副职)","13");
		gzDictionaryMap.put("见习管理人员","11");
		gzDictionaryMap.put("公司部门管理人员","11");
		gzDictionaryMap.put("机务员/警消人员","12");
		gzDictionaryMap.put("票务人员","13");
		gzDictionaryMap.put("其他服务人员","14");

		String sql="select * from bsth_c_business ";
		List<Business> businessList=jdbcTemplate.query(sql, new BeanPropertyRowMapper(Business.class));
		Map<String,Map<String,Business>> businessMap = new HashMap<>();
		for (Business bu:businessList) {

			Map<String,Business> buMap = new HashMap<>();
			if (businessMap.get(bu.getUpCode()) != null)
				buMap = businessMap.get(bu.getUpCode());

			/*if(bu.getUpCode().equals("55"))
				buMap.put(bu.getBusinessName().substring(2),bu);
			else*/
				buMap.put(bu.getBusinessName(),bu);

			businessMap.put(bu.getUpCode(),buMap);
		}

		List<Personnel> newPersonnel = new ArrayList<>();

//		Map<String, Object> retiredInforMap = new HashMap<>();
		try{
//			if(retiredInfor.size() > 0){
//				for(int i=0;i<retiredInfor.size()-1; i++){
//					JSONObject pjson =  retiredInfor.getJSONObject(i);
//					String codeArr[] = pjson.getString("Code").split("-");
//					String code = "",
//							companyCode="";
//
//					if(codeArr.length > 1){
//						if (index == 0) {
//							// 上南
//							code = 55+"-"+codeArr[1];
//							companyCode = "55";
//						} else if (index == 1) {
//							// 金高
//							code = 22+"-"+codeArr[1];
//							companyCode = "22";
//						} else if (index == 2) {
//							// 南汇
//							code = 26+"-"+codeArr[1];
//							companyCode = "26";
//						} else if(index == 3){
//							// 杨高
//							code = "05"+"-"+codeArr[1];
//							companyCode = "05";
//						}
////						retiredInforMap.put(code,pjson);
//					}
//				}
//			}

			// 金蝶人员数据转为json,然后遍历匹配
			JSONObject jsonInfor = XmlToJson.xml2Json(_getEmployee__return);
			System.out.println("getEmployee.result=" + _getEmployee__return);


			JSONArray jsonArr = JSONArray.parseArray(jsonInfor.get("Emp").toString());
			for(int i=0;i<jsonArr.size()-1; i++){
				JSONObject pjson =  jsonArr.getJSONObject(i);
				String codeArr[] = pjson.getString("Code").split("-");
				String JobCodeori = codeArr[1];
				while (JobCodeori.length() < 6){
					JobCodeori = "0"+JobCodeori;
				}


//				Personnel p = new Personnel();
//				p.setJdCodeori(pjson.getString("Code"));
//				p.setJobCodeori(JobCodeori);
//				p.setPersonnelName(pjson.getString("EmployeeName"));
//				p.setCompany(pjson.getString("CompanyName"));
//
//				newPersonnel.add(p);

				String code = "",
						companyCode="",
						company="";
				Business business = new Business();
				String companyName = pjson.getString("CompanyName");

				// 人员数据匹配公司
				if (index == 0) {
					// 上南
					code = 55+"-"+JobCodeori;
					companyCode = "55";
					company = "上南公司";
					if (companyName.indexOf("一分") > -1) {
						business = businessMap.get(companyCode).get("上南一分公司");
					} else if (companyName.indexOf("二分") > -1) {
						business = businessMap.get(companyCode).get("上南二分公司");
					} else if (companyName.indexOf("三分") > -1) {
						business = businessMap.get(companyCode).get("上南三分公司");
					} else if (companyName.indexOf("六分") > -1) {
						business = businessMap.get(companyCode).get("上南六分公司");
					} else if (companyName.indexOf("山南") > -1) {
						business = businessMap.get(companyCode).get("山南培训部");
					} else {
						business = businessMap.get(companyCode).get("其它分公司");
					}
				} else if (index == 1) {
					// 金高
					code = 22+"-"+JobCodeori;
					companyCode = "22";
					company = "金高公司";
					if (companyName.indexOf("一分") > -1) {
						business = businessMap.get(companyCode).get("一分公司");
					} else if (companyName.indexOf("二分") > -1) {
						business = businessMap.get(companyCode).get("二分公司");
					} else if (companyName.indexOf("三分") > -1) {
						business = businessMap.get(companyCode).get("三分公司");
					} else if (companyName.indexOf("四分") > -1) {
						business = businessMap.get(companyCode).get("四分公司");
					} else {
						business = businessMap.get(companyCode).get("其它分公司");
					}
				} else if (index == 2) {
					// 南汇
					code = 26+"-"+JobCodeori;
					companyCode = "26";
					company = "南汇公司";
					if (companyName.indexOf("一分") > -1) {
						business = businessMap.get(companyCode).get("南汇一分");
					} else if (companyName.indexOf("二分") > -1) {
						business = businessMap.get(companyCode).get("南汇二分");
					} else if (companyName.indexOf("三分") > -1) {
						business = businessMap.get(companyCode).get("南汇三分");
					} else if (companyName.indexOf("维修") > -1) {
						business = businessMap.get(companyCode).get("南汇维修公司");
					} else if (companyName.indexOf("南汇公司") > -1) {
						business = businessMap.get(companyCode).get("南汇公司");
					} else if (companyName.indexOf("航头") > -1) {
						business = businessMap.get(companyCode).get("航头枢纽站");
					} else {
						business = businessMap.get(companyCode).get("其它分公司");
					}
				} else if(index == 3){
					code = "05"+"-"+JobCodeori;
					companyCode = "05";
					company = "杨高公司";
					if (companyName.indexOf("川沙") > -1) {
						business = businessMap.get(companyCode).get("川沙分公司");
					} else if (companyName.indexOf("金桥") > -1) {
						business = businessMap.get(companyCode).get("金桥分公司");
					} else if (companyName.indexOf("杨高") > -1) {
						business = businessMap.get(companyCode).get("杨高分公司");
					} else if (companyName.indexOf("周浦") > -1) {
						business = businessMap.get(companyCode).get("周浦分公司");
					} else if (companyName.indexOf("芦潮港") > -1) {
						business = businessMap.get(companyCode).get("芦潮港分公司");
					} else {
						business = businessMap.get(companyCode).get("其它分公司");
					}
				}

//
//				if(pjson.getString("Code").equals("03-S07265") ||pjson.getString("Code").equals("03-S07273") ||pjson.getString("Code").equals("03-S07264")
//						||pjson.getString("Code").equals("03-S07269") ||pjson.getString("Code").equals("03-S07274") ||pjson.getString("Code").equals("03-S07275")
//						||pjson.getString("Code").equals("03-S07267") ||pjson.getString("Code").equals("03-S07268") ||pjson.getString("Code").equals("05-006008")
//						||pjson.getString("Code").equals("02-017122")){
//					System.out.print(pjson);
//				}
//				if(pjson.getString("EmployeeName").equals("吴燕辉") || pjson.getString("EmployeeName").equals("顾振军")|| pjson.getString("EmployeeName").equals("宋荣甲")){
//					System.out.print(pjson);
//				}

				/*if(codeArr[0].equals("02")){
					code = "05"+"-"+JobCodeori;
					companyCode = "05";
					company = "杨高公司";
				} else if(codeArr[0].equals("03")){
					code = 55+"-"+JobCodeori;
					companyCode = "55";
					company = "上南公司";
				} else if(codeArr[0].equals("04")){
					code = 22+"-"+JobCodeori;
					companyCode = "22";
					company = "金高公司";
				} else if(codeArr[0].equals("05")){
					code = 26+"-"+JobCodeori;
					companyCode = "26";
					company = "南汇公司";
				}*/

				Personnel p = personnelMap.get(code);
//				Object isRetiredInfor = retiredInforMap.get(code);
				// p不为空这表示调度数据和金蝶数据能匹配、则更新数据,不能匹配这新增数据
				if(p != null ){
//					p.setCard(pjson.getString("IDCardID"));// 第一次需要添加身份证,后面则不需要了
					if(pjson.getString("IDCardID").equals(p.getCard())){
						p.setBrancheCompanyCode(business.getBusinessCode());
						p.setBrancheCompany(business.getBusinessName());
						p.setPersonnelName(pjson.getString("EmployeeName"));
						p.setDescriptions(pjson.getString("UnitFullName"));
						p.setJdCodeori(pjson.getString("Code")); // 金蝶工号
						p.setUpdateDate(new Date());
						// 设置人员为在职状态
						p.setDestroy(0);
						newPersonnel.add(p);
					}
				} else {
					p = new Personnel();
					p.setJobCode(code);
					p.setJobCodeori(JobCodeori);
					p.setCard(pjson.getString("IDCardID"));
//					String companyName = pjson.getString("CompanyName");
//					// 搜索分公司
//					Business business = businessMap.get(companyCode).get(companyName);
//					if(business != null){
//					} else {
//						p.setBrancheCompanyCode("");
//						p.setBrancheCompany("");
//					}

					p.setBrancheCompanyCode(business.getBusinessCode());
					p.setBrancheCompany(business.getBusinessName());
					p.setCompanyCode(companyCode);
					p.setCompany(company);
					p.setJdCodeori(pjson.getString("Code")); // 金蝶工号
					p.setPersonnelName(pjson.getString("EmployeeName"));
					p.setPersonnelType(pjson.getString("SexType").indexOf("男") > -1 ?"1":"2");
					p.setPosts(gzDictionaryMap.get(pjson.getString("PositName")) == null?"14":gzDictionaryMap.get(pjson.getString("PositName")));
					p.setDescriptions(pjson.getString("UnitFullName"));
					p.setTelphone(pjson.getString("Telphone"));
					p.setUpdateDate(new Date());
					p.setRemark("新增");
					// 设置人员为在职状态
					p.setDestroy(0);
//					if(isRetiredInfor != null && isRetiredInfor != ""){
//						p.setRemark("退休");
//					}
					newPersonnel.add(p);
				}
			}
			// 批量保存
			repository.save(newPersonnel);

		} catch (Exception e) {
		logger.error("", e);
		}
		return  null;
	}
	
	/*public Map<String, Personnel> loadPersonnel(){
		Map<String, Personnel> personnelMap= new HashMap<>();
		Iterator<Personnel> perIterator=repository.findAll().iterator();
		Personnel per;
		while(perIterator.hasNext()){
			per=perIterator.next();
			personnelMap.put(per.getJobCode(),per);
		}
		return personnelMap;
		
	}*/
}