package com.kingdee.shr.customer.gtiit.rpt;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.web.handler.ListHandler;
import com.kingdee.shr.base.syssetting.web.json.JSONUtils;
import com.kingdee.shr.customer.gtiit.util.ContractRptUtil;
import com.kingdee.shr.customer.gtiit.util.DateTimeUtils;

public class EmployeeResignationContractListHandler extends ListHandler {

	Context ctx = SHRContext.getInstance().getContext();
	private int totalCount;

	public EmployeeResignationContractListHandler() {
	}

	public String getGridColModelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws SHRWebException, BOSException, SQLException {
		List<String> colNames = this.getcolNamesAction();
		List<Map<String, Object>> colModel = this.getcolModelAction();
		LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
		map.put("colNames", colNames);
		map.put("colModel", colModel);
		JSONUtils.writeJson(response, map);
		return null;
	}

	public void getGridDataAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws BOSException, SQLException, SHRWebException {
		int rows = Integer.parseInt(request.getParameter("rows"));
		int page = Integer.parseInt(request.getParameter("page"));
		List<Map<String, Object>> maplist = new ArrayList<Map<String, Object>>();
		int num = 0;
		// 获取报表查询过滤条件
		String parameter = request.getParameter("fastFilterItems");
		JSONObject paramJson = JSON.parseObject(parameter);
		String personIds = null;
		String contractdate = null;
		JSONObject personJson = paramJson.getJSONObject("person");

		String ids = personJson.getString("values");
		if (StringUtils.isNotBlank(ids)) {
			personIds = "'" + ids.replace(",", "','") + "'";
		}
		JSONObject contractdateJson = paramJson.getJSONObject("contractdate");

		JSONObject values = contractdateJson.getJSONObject("values");
		if (values != null) {
			contractdate = values.getString("date");
		}

		Map<String, Object> params = new HashMap<>();
		params.put("personid", personIds);
		params.put("contractdate", contractdate);

		// 获取报表数据源
		String sqlInfo = ContractRptUtil.getContractSql(params);
		IRowSet rs = DbUtil.executeQuery(this.ctx, sqlInfo);
		while (rs.next()) {
			++num;
			if (num >= rows * (page - 1) + 1 && num <= rows * page) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("PersonNumber", rs.getString("PersonNumber"));
				map.put("LastName", rs.getString("LastName"));
				map.put("FirstName", rs.getString("FirstName"));
				map.put("EnglishName", rs.getString("EnglishName"));
				map.put("JobName", rs.getString("JobName"));
				map.put("WorkerCategory", rs.getString("WorkerCategory"));
				map.put("PersonType", rs.getString("PersonType"));
				map.put("FullorPartTime", rs.getString("FullorPartTime"));
				map.put("HireDate", DateTimeUtils.dateFormat(rs.getDate("HireDate"), "yyyy-MM-dd"));
				map.put("ProbationEndDate", DateTimeUtils.dateFormat(rs.getDate("ProbationEndDate"), "yyyy-MM-dd"));
				map.put("ContractNo", rs.getString("ContractNo"));
				map.put("Type", rs.getString("Type"));
				map.put("CommencingDate", DateTimeUtils.dateFormat(rs.getDate("CommencingDate"), "yyyy-MM-dd"));
				map.put("ExpiredDate", DateTimeUtils.dateFormat(rs.getDate("ExpiredDate"), "yyyy-MM-dd"));
				String YearsOfService = rs.getString("YearsOfService");
				YearsOfService = YearsOfService.substring(0, (YearsOfService.indexOf(".") + 2));
				map.put("YearsOfService", YearsOfService);
				maplist.add(map);
			}
		}
		this.totalCount = num;
		Map<String, Object> gridDataMap = new LinkedHashMap<String, Object>();
		if (this.totalCount % rows == 0) {
			gridDataMap.put("total", this.totalCount / rows);
		} else {
			gridDataMap.put("total", this.totalCount / rows + 1);
		}
		gridDataMap.put("page", page);
		gridDataMap.put("records", this.totalCount);
		gridDataMap.put("rows", maplist);
		JSONUtils.writeJson(response, gridDataMap);
	}

	public ArrayList<String> getcolNamesAction() throws SHRWebException {
		ArrayList<String> list = new ArrayList<String>();
		list.add("工号");
		list.add("姓");
		list.add("名");
		list.add("英文名");
		list.add("职务");
		list.add("员工类别");
		list.add("人员类型");
		list.add("全职或兼职");
		list.add("雇佣日期");
		list.add("试用期结束日期");
		list.add("合同/协议编码");
		list.add("类型");
		list.add("开始时间");
		list.add("结束时间");
		list.add("服务年限");
		ArrayList<String> englishList = new ArrayList<String>();
		englishList.add("PersonNumber");
		englishList.add("LastName");
		englishList.add("FirstName");
		englishList.add("EnglishName");
		englishList.add("JobName");
		englishList.add("WorkerCategory");
		englishList.add("PersonType");
		englishList.add("FullorPartTime");
		englishList.add("HireDate");
		englishList.add("ProbationEndDate");
		englishList.add("Contract/AgreementNo");
		englishList.add("Type");
		englishList.add("CommencingDate");
		englishList.add("ExpiredDate");
		englishList.add("Yearsofservice");

		return englishList;
	}

	public List<Map<String, Object>> getcolModelAction() {
		List<Map<String, Object>> modelColList = new ArrayList<Map<String, Object>>();
		modelColList.add(this.buildColModelMap("PersonNumber", "工号", 120));
		modelColList.add(this.buildColModelMap("LastName", "姓", 120));
		modelColList.add(this.buildColModelMap("FirstName", "名", 120));
		modelColList.add(this.buildColModelMap("EnglishName", "英文名", 120));
		modelColList.add(this.buildColModelMap("JobName", "职务", 120));
		modelColList.add(this.buildColModelMap("WorkerCategory", "员工类别", 120));
		modelColList.add(this.buildColModelMap("PersonType", "人员类型", 120));
		modelColList.add(this.buildColModelMap("FullorPartTime", "全职或兼职", 120));
		modelColList.add(this.buildColModelMap("HireDate", "雇佣日期", 120));
		modelColList.add(this.buildColModelMap("ProbationEndDate", "试用期结束日期", 120));
		modelColList.add(this.buildColModelMap("ContractNo", "合同/协议编码", 120));
		modelColList.add(this.buildColModelMap("Type", "类型", 120));
		modelColList.add(this.buildColModelMap("CommencingDate", "开始时间", 120));
		modelColList.add(this.buildColModelMap("ExpiredDate", "结束时间", 120));
		modelColList.add(this.buildColModelMap("YearsOfService", "服务年限", 120));

		return modelColList;
	}

	private Map<String, Object> buildColModelMap(String index, String label, int width) {
		return this.buildColModelMap(index, label, width, false, false, false);
	}

	private Map<String, Object> buildColModelMap(String index, String label, int width, boolean rowspan, boolean isKey,
			boolean isHedden) {
		Map<String, Object> gridIdMap = new LinkedHashMap<String, Object>();
		gridIdMap.put("index", index);
		gridIdMap.put("name", index);
		gridIdMap.put("label", label);
		gridIdMap.put("width", width);
		if (rowspan) {
			gridIdMap.put("rowspan", rowspan);
		}
		if (isKey) {
			gridIdMap.put("key", isKey);
		}
		if (isHedden) {
			gridIdMap.put("hidden", isHedden);
		}
		return gridIdMap;
	}

	public String getSql(String dateTime, String dept, String line) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT  max(emp.FLEFFDT ), emp.fpersonid FROM  T_HR_EmpLaborRelationHis  emp ");
		sql.append("left join   T_HR_BDEmployeeType dbe  ");
		sql.append("on emp.FLABORRELATIONSTATEID  = dbe.fid ");
		sql.append("where   dbe.FNAME_L2 IN ('辞退','辞职','离职') ");
		sql.append("group by emp.fpersonid ");
		sql.append("");
		return sql.toString();
	}

}
