package com.engine.htsc.trip.service.impl;

import com.engine.htsc.trip.service.BtService;
import com.engine.core.impl.Service;
import java.util.HashSet;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by qlk
 */
public class BtServiceImpl extends Service implements BtService {

	private final Logger log = LoggerFactory.getLogger(BtServiceImpl.class);
	private final RecordSet rs = new RecordSet();

	@Override
	public List<String> getTravelStandard(String travelers, String destinations) {
		//回传信息
		String msg = null;
		String[] checkTravelers = travelers.split(",");
		String[] checkDestinations = destinations.split(",");
		List<String> standardMsg = new ArrayList<>(checkTravelers.length * checkDestinations.length);
		//城市信息
		//甲类城市
		String[] cityA = {"34", "112", "246", "248", "128", "338", "284", "99"};
		List<String> cityAList = Arrays.asList(cityA);
		//乙类城市
		String[] cityB = {"75", "118", "283", "287", "357", "289", "434273726", "180", "935078239", "179", "157", "197", "30002", "216", "231", "321", "81095659", "126111795", "387", "35", "158", "381", "30003", "311", "49", "346", "74", "89", "114", "129"};
		List<String> cityBList = Arrays.asList(cityB);
		//丙类城市 不包含a,b的
		//人员信息
		//公司领导(董事会487,执行委员会489,党委713)+2
		List<String> deptLeader = new ArrayList<>();
		String sql_dept = "SELECT id FROM hrmresource WHERE departmentid IN ('487','489','713') AND id IS NOT NULL";
		rs.execute(sql_dept);
		while (rs.next()) {
			deptLeader.add(rs.getString("id"));
		}
		deptLeader.add("6633"); //合规总监,李洪涛
		deptLeader.add("5891"); //宁敖
		//判断标识,匹配金额
		//获取后台部门
		PropBean pp = new PropBean();
		String backdept = pp.getPropName("backdept");
		log.info("====获取配置表中数据:{}", backdept);
		int code = 2;
		for (String traveler : checkTravelers) {
			if (deptLeader.contains(traveler)) {
				//traveler是实报实销的
				for (String checkDestination : checkDestinations) {
					standardMsg.add(null + "|" + null);
				}
				continue;
			} else {
				//需要判断逻辑,给traveler赋一个code
				//中后台部门 id >> '495','671','717','493','494'
				String sql_traveler_isNot_middle_backstage = "SELECT COUNT(*) AS cnt FROM hrmresource WHERE departmentid IN (" + backdept + ") AND id = ?";
				rs.executeQuery(sql_traveler_isNot_middle_backstage, new Object[]{traveler});
				rs.next();
				if (rs.getInt("cnt") == 1) { //属于中后台
					RecordSet recordSet = new RecordSet();
					String sql_SD_D = "SELECT t1.ID,t1.field9 as field,t2.LOGINID FROM cus_fielddata t1\n" +
							" left join HRMRESOURCE t2\n" +
							" on t1.ID=t2.ID\n" +
							" WHERE t1.scopeid=1 and t1.ID = ?";
					recordSet.executeQuery(sql_SD_D, new Object[]{traveler});
					log.info("==当前人员为中后台");
					if (recordSet.next()) {
						String field = recordSet.getString("field");
						String loginid = recordSet.getString("LOGINID");
						log.info("==当前人员:{},当前职级{}", traveler, field);
						if ("MD".equals(field) || "ED".equals(field) || "SD".equals(field) || "D".equals(field)) {
							code = 1; //traveler属于SD,D
							log.info("==匹配人员code:{}", code);
						} else {
							code = 2; // SS及以下
							//判断是否为实习生
							if (loginid.startsWith("sx")) {
								code = 3;
							}
							log.info("==匹配人员code:{}", code);
						}
					}
				} else {
					//属于前台
					RecordSet recordSet = new RecordSet();
					String sql_MD_ED = "SELECT t1.ID,t1.field9 as field,t2.LOGINID FROM cus_fielddata t1\n" +
							" left join HRMRESOURCE t2\n" +
							" on t1.ID=t2.ID\n" +
							" WHERE t1.scopeid=1 and t1.ID = ?";
					recordSet.executeQuery(sql_MD_ED, new Object[]{traveler});
					log.info("==当前人员为前台");
					if (recordSet.next()) {
						String field = recordSet.getString("field");
						String loginid = recordSet.getString("LOGINID");
						log.info("==当前人员:{},当前职级{}", traveler, field);
						if ("MD".equals(field) || "ED".equals(field)) {
							code = 1; //属于SD,D
							log.info("==匹配人员code:{}", code);
						} else {
							code = 2; //D及以下
							//判断是否为实习生
							if (loginid.startsWith("sx")) {
								code = 3;
							}
							log.info("==匹配人员code:{}", code);
						}
					}
				}
			}
			//根据业务，新增L3P差标
			//获取匹配L3P差标人员配置
			Set<String> l3pTravelStandardSet = new HashSet<>(Arrays.asList(pp.getPropName("travel_standard_l3p").split(",")));
			//获取出差人员的华泰工号
			String htWorkCode = "";
			String getHTWorkCodeSql = "select workcode from hrmresource where id = ?";
			rs.executeQuery(getHTWorkCodeSql,traveler);
			while (rs.next()){
				htWorkCode = rs.getString("workcode");
			}
			if(l3pTravelStandardSet.contains(htWorkCode)){
				for (String destination : checkDestinations) {
					if (cityAList.contains(destination)) {
						msg = "1680";
					} else if (cityBList.contains(destination)) {
						msg = "1200";
					} else {
						msg = "960";
					}
						//提高机票差标
						msg = String.join("|", msg, "软卧、一等座", "商务舱", "二等舱");
					standardMsg.add(msg);
				}
			}else{
				//code和地区信息组合
				if (code == 1) {
					for (String destination : checkDestinations) {
						if (cityAList.contains(destination)) {
							msg = "1680";
						} else if (cityBList.contains(destination)) {
							msg = "1200";
						} else {
							msg = "960";
						}
						//交通
						if (!"6483".equals(traveler) || !"6579".equals(traveler) || !"2906".equals(traveler)) { //协管领导
							msg = String.join("|", msg, "软卧、一等座", "经济舱", "二等舱");
						} else {
							msg += "|" + null;
						}
						standardMsg.add(msg);
					}
				} else if (code == 2) {
					for (String destination : checkDestinations) {
						if (cityAList.contains(destination)) {
							msg = "1200";
						} else if (cityBList.contains(destination)) {
							msg = "840";
						} else {
							msg = "672";
						}
						//交通
						if (!"6483".equals(traveler) || !"6579".equals(traveler) || !"2906".equals(traveler)) { //协管领导
							msg = String.join("|", msg, "软卧、一等座", "经济舱", "二等舱");
						} else {
							msg += "|" + null;
						}
						standardMsg.add(msg);
						msg = null;
					}
				} else if (code == 3) {
					//实习生
					for (String destination : checkDestinations) {
						if (cityAList.contains(destination)) {
							msg = "1000";
						} else if (cityBList.contains(destination)) {
							msg = "700";
						} else {
							msg = "560";
						}
						//交通
						if (!"6483".equals(traveler) || !"6579".equals(traveler) || !"2906".equals(traveler)) { //协管领导
							msg = String.join("|", msg, "硬座、二等座", "无","三等舱");
						} else {
							msg += "|" + null;
						}
						standardMsg.add(msg);
						msg = null;
					}
				}
			}

		}
		return standardMsg;
	}

	@Override
	public Boolean checkNames(String names) {
		String[] checkName = names.split(",");
		List<String> nameSpace = new ArrayList<>(); //姓名空间
		//特定人员
		nameSpace.add("18879"); //党委书记,江禹
		nameSpace.add("21749"); //党委副书记
//		nameSpace.add("6633"); //合规总监
		nameSpace.add("5891"); //宁敖
		for (String name : checkName) {
			if (nameSpace.contains(name)) {
				return true;
			}
		}
		nameSpace.clear();
		//特定部门(董事会487,执行委员会489,党委713,投资银行业务线/专职合规管理666,债券业务线/专职合规管理667)
//		String sql_SpecifyDept = "SELECT DISTINCT id FROM hrmresource WHERE departmentid IN ('666','667','487','489','713') AND id IS NOT NULL";
		//移除 专职合规管理666,专职合规管理667所在部门人员
		String sql_SpecifyDept = "SELECT DISTINCT id FROM hrmresource WHERE departmentid IN ('487','489','713') AND id IS NOT NULL";
		rs.execute(sql_SpecifyDept);
		while (rs.next()) {
			nameSpace.add(rs.getString("id"));
		}
		for (String name : checkName) {
			if (nameSpace.contains(name)) {
				return true;
			}
		}
		//部门负责人
		nameSpace.clear();
		for (String s : checkName) {
//			String sql_deptDirectors = "SELECT DISTINCT deptDirectors AS id FROM matrixtable_2 WHERE deptDirectors IS NOT NULL and deptDirectors like '%" + s + "%'";
//			rs.executeQuery(sql_deptDirectors);
//			if (rs.next() && rs.getCounts() > 0) {
//				String id = Util.null2String(rs.getString("id"));
//				return Arrays.asList(id.split(",")).contains(s);
//			}
			if (isDirector(s)) {
				return true;
			}




		}
		return false;
	}

	@Override
	public Boolean checkDeptAndName(String dept, String name) {
		String[] checkDept = dept.split(",");
		String[] checkName = name.split(",");
		List<String> deptSpace = new ArrayList<>(); //部门空间
		List<String> nameSpace = new ArrayList<>(); //人员空间
		deptSpace.add("487"); //董事会
		deptSpace.add("489"); //执行委员会
		deptSpace.add("713"); //执行委员会
		for (String d : checkDept) {
			if (deptSpace.contains(d)) {
				return true;
			}
		}
		nameSpace.add("18879"); //党委书记,江禹
		nameSpace.add("21749"); //党委副书记(人),曹海涛
		nameSpace.add("6633"); //合规总监,李洪涛
		for (String n : checkName) {
			if (nameSpace.contains(n)) {
				return true;
			}
		}
		return false;
	}

	@Override
	public String getDirectors(String dept, String expDept) {
		String sql_deptDirectors = "SELECT DISTINCT deptDirectors AS leader FROM matrixtable_2 WHERE id IN (?)";
		StringBuilder deptLeader = new StringBuilder();
		rs.executeQuery(sql_deptDirectors, new Object[]{dept});
		while (rs.next()) {
			deptLeader.append(rs.getString("leader"));
		}
		StringBuilder expDeptLeader = new StringBuilder();
		rs.executeQuery(sql_deptDirectors, new Object[]{expDept});
		while (rs.next()) {
			expDeptLeader.append(rs.getString("leader"));
		}
		return String.join("|", deptLeader.toString(), expDeptLeader.toString());
	}


	public boolean isDirector(String uid) {
		String sql = "SELECT h2.DEPARTMENTID,h.DEPTDIRECTORS,h2.ID FROM HRMDEPARTMENTDEFINED h \n" +
				"LEFT JOIN HRMRESOURCE h2 \n" +
				"ON h2.DEPARTMENTID=h.DEPTID\n" +
				"WHERE h2.ID = ?";
		RecordSet rs = new RecordSet();
		rs.executeQuery(sql, uid);
		String directors = "";
		if (rs.next()) {
			directors = Util.null2String(rs.getString("DEPTDIRECTORS"));
		}
		return directors.contains(uid);
	}
}
