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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.SQLException;
import java.util.ArrayList;
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.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudera.impala.jdbc4.internal.apache.log4j.Logger;
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;
import com.kingdee.shr.customer.gtiit.util.ExportUtils;
import com.kingdee.shr.customer.gtiit.util.NewExportUtils;
import com.kingdee.shr.customer.gtiit.util.RptUtils;
/**
 * 全部合同报表
 * @author ISSUSER
 *
 */
public class ContractListReportALLListHandler extends ListHandler {
	private static Logger logger = Logger.getLogger("com.kingdee.shr.customer.gtiit.rpt.ContractListReportALLListHandler");
	Context ctx = SHRContext.getInstance().getContext();
	private int totalCount;

	public ContractListReportALLListHandler() {
	}

	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;
		// 获取报表查询过滤条件

		List<String> qfNames = new ArrayList<>();
		qfNames.add("personStatus");
		qfNames.add("queryTime");
		qfNames.add("workerCategory");
		qfNames.add("job");
		qfNames.add("contractDate");
		String parameter = request.getParameter("fastFilterItems");
		JSONObject paramJson = JSON.parseObject(parameter);
		Map<String, Object> rptSQLFilterate = RptUtils.getRptSQLFilterate(paramJson, qfNames);
		// 获取报表数据源
		String sqlInfo = ContractRptUtil.getContractSql(rptSQLFilterate);
		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("Department", rs.getString("Department"));
				map.put("Position", rs.getString("Positions"));
				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", rs.getDate("HireDate") == null ? null
						: DateTimeUtils.dateFormat(rs.getDate("HireDate"), "yyyy-MM-dd"));
				map.put("ProbationEndDate", rs.getDate("ProbationEndDate") == null ? null
						: DateTimeUtils.dateFormat(rs.getDate("ProbationEndDate"), "yyyy-MM-dd"));
				map.put("ContractNo", rs.getString("ContractNo"));
				map.put("Type", rs.getString("Type"));
				map.put("CommencingDate", rs.getDate("CommencingDate") == null ? null
						: DateTimeUtils.dateFormat(rs.getDate("CommencingDate"), "yyyy-MM-dd"));
				map.put("ExpiredDate", rs.getDate("ExpiredDate") == null ? null
						: DateTimeUtils.dateFormat(rs.getDate("ExpiredDate"), "yyyy-MM-dd"));
				String YearsOfService = rs.getString("YearsOfService");
				BigDecimal bgyears=new BigDecimal(YearsOfService);
				//YearsOfService = YearsOfService.substring(0, (YearsOfService.indexOf(".") + 2));
				// 四舍五入保留两位
		        BigDecimal toYearsOfService = bgyears.setScale(2, RoundingMode.HALF_UP);  
		        logger.error("服务年限的保留两位"+toYearsOfService);
		        System.out.println("服务年限的保留两位"+toYearsOfService);
		        //YearsOfService = YearsOfService.substring(0, (YearsOfService.indexOf(".") + 2));
				map.put("YearsOfService", toYearsOfService.toString());
				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("Person Number 工号");
		list.add("Last Name 姓");
		list.add("First Name 名");
		list.add("English Name 英文名");
		list.add("Department 部门名称");
		list.add("Position 职位名称");
		list.add("Job Name 职务");
		list.add("Worker Category 员工类别");
		list.add("Person Type 人员类型");
		list.add("Full or Part Time 全职或兼职");
		list.add("Hire Date 雇佣日期");
		list.add("Probation End Date 试用期结束日期");
		list.add("Contract/Agreement No. 合同/协议编码");
		list.add("Type 类型");
		list.add("Commencing Date 开始时间");
		list.add("Expired Date 结束时间");
		list.add("Service life 服务年限");
		return list;
	}

	public List<Map<String, Object>> getcolModelAction() {
		List<Map<String, Object>> modelColList = new ArrayList<Map<String, Object>>();
		modelColList.add(this.buildColModelMap("PersonNumber", "Person Number 工号", 120));
		modelColList.add(this.buildColModelMap("LastName", "Last Name 姓", 120));
		modelColList.add(this.buildColModelMap("FirstName", "First Name 名", 120));
		modelColList.add(this.buildColModelMap("EnglishName", "English Name 英文名", 120));
		modelColList.add(this.buildColModelMap("Department", "Department 部门名称", 120));
		modelColList.add(this.buildColModelMap("Position", "Position 职位名称", 120));
		modelColList.add(this.buildColModelMap("JobName", "Job Name 职务", 120));
		modelColList.add(this.buildColModelMap("WorkerCategory", "Worker Category 员工类别", 120));
		modelColList.add(this.buildColModelMap("PersonType", "Person Type 人员类型", 120));
		modelColList.add(this.buildColModelMap("FullorPartTime", "Full or Part Time 全职或兼职", 120));
		modelColList.add(this.buildColModelMap("HireDate", "Hire Date 雇佣日期", 120));
		modelColList.add(this.buildColModelMap("ProbationEndDate", "Probation End Date 试用期结束日期", 120));
		modelColList.add(this.buildColModelMap("ContractNo", "Contract/Agreement No. 合同/协议编码", 120));
		modelColList.add(this.buildColModelMap("Type", "Type 类型", 120));
		modelColList.add(this.buildColModelMap("CommencingDate", "Commencing Date 开始时间", 120));
		modelColList.add(this.buildColModelMap("ExpiredDate", "Expired Date 结束时间", 120));
		modelColList.add(this.buildColModelMap("YearsOfService", "Service life 服务年限", 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 toExcelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
			throws BOSException, SQLException, SHRWebException {

		List<String> qfNames = new ArrayList<>();
		qfNames.add("personStatus");
		qfNames.add("queryTime");
		qfNames.add("workerCategory");
		qfNames.add("job");
		qfNames.add("contractDate");
		String parameter = request.getParameter("fastFilterItems");
		JSONObject paramJson = JSON.parseObject(parameter);
		Map<String, Object> rptSQLFilterate = RptUtils.getRptSQLFilterate(paramJson, qfNames);
		// 获取报表数据源
		String sqlInfo = ContractRptUtil.getContractSql(rptSQLFilterate);
		IRowSet rs = DbUtil.executeQuery(this.ctx, sqlInfo);
		
		
		LinkedHashMap<String,String> selectFieldsTypeMap =new LinkedHashMap<String, String>();
		String stringType  ="String_@";
		String dateType  ="Date_yyyy/MM/dd";
		selectFieldsTypeMap.put("PersonNumber","Number_0_%s");
		selectFieldsTypeMap.put("LastName",stringType);
		selectFieldsTypeMap.put("FirstName",stringType);
		selectFieldsTypeMap.put("EnglishName",stringType);
		selectFieldsTypeMap.put("Department",stringType);
		selectFieldsTypeMap.put("Positions",stringType);
		selectFieldsTypeMap.put("JobName",stringType);
		selectFieldsTypeMap.put("WorkerCategory",stringType);
		selectFieldsTypeMap.put("PersonType",stringType);
		selectFieldsTypeMap.put("FullorPartTime",stringType);
		selectFieldsTypeMap.put("HireDate",dateType);
		selectFieldsTypeMap.put("ProbationEndDate",dateType);
		selectFieldsTypeMap.put("ContractNo",stringType);
		selectFieldsTypeMap.put("Type",stringType);
		selectFieldsTypeMap.put("CommencingDate",dateType);
		selectFieldsTypeMap.put("ExpiredDate",dateType);
		selectFieldsTypeMap.put("Yearsofservice","Number_0.0_%s");
		// 列名的标题
		ArrayList<String> list = new ArrayList<String>();
		list.add("Person Number 工号");
		list.add("Last Name 姓");
		list.add("First Name 名");
		list.add("English Name 英文名");
		list.add("Department 部门名称");
		list.add("Position 职位名称");
		list.add("Job Name 职务");
		list.add("Worker Category 员工类别");
		list.add("Person Type 人员类型");
		list.add("Full or Part Time 全职或兼职");
		list.add("Hire Date 雇佣日期");
		list.add("Probation End Date 试用期结束日期");
		list.add("Contract/Agreement No. 合同/协议编码");
		list.add("Type 类型");
		list.add("Commencing Date 开始时间");
		list.add("Expired Date 结束时间");
		list.add("Service life 服务年限");
		NewExportUtils.exportData(response, rs, "ContractListReportALL.xlsx",selectFieldsTypeMap,list);
		return null;
	}

}
