package com.engine.kq.cmd.reportdetial;

import com.cloudstore.dev.api.util.Util_TableMap;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.enums.FlowReportTypeEnum;
import com.engine.kq.enums.ReportColumnEnum;
import com.engine.kq.util.PageUidFactory;
import weaver.conn.RecordSet;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/***
 * 应出勤
 */
public class GetNeedSignInfoCmd extends AbstractCommonCommand<Map<String, Object>> {

	public GetNeedSignInfoCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();
		String sql = "";
		try{
			//必要的权限判断
			if(!HrmUserVarify.checkUserRight("BohaiInsuranceScheduleReport:View",user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}

			String dataIndex = Util.null2String(params.get("dataIndex"));

			Map<String, Object> tableInfo = new HashMap<String, Object>();
			String dialogTitle = "";
			List<Object> tabs =new ArrayList<Object>();
			List<Object> sessionKeys = new ArrayList<Object>();
			dialogTitle = SystemEnv.getHtmlLabelName(20088, user.getLanguage());
			if(dataIndex.equals(ReportColumnEnum.BELATE.getName())){
				tabs.add(SystemEnv.getHtmlLabelName(20088, user.getLanguage()));
				tabs.add(SystemEnv.getHtmlLabelName(390520, user.getLanguage()));
				sessionKeys.add(getBeLateInfo());
				sessionKeys.add(getGraveBeLateMinsInfo());
			}

			tableInfo.put("dialogTitle",dialogTitle);
			tableInfo.put("tabs",tabs);
			tableInfo.put("sessionkeys",sessionKeys);
			retmap.put("tableInfo", tableInfo);
			retmap.put("status", "1");
		}catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}


	private String getBeLateInfo(){
		String sessionkey = "";
		try{
			String dataIndex = Util.null2String(params.get("dataIndex"));
			String resourceId = Util.null2String(params.get("resourceId"));
			String keyWord = Util.null2String(params.get("keyWord"));
			String fromDate = Util.null2String(params.get("fromDate"));
			String toDate = Util.null2String(params.get("toDate"));
			String typeselect =Util.null2String(params.get("typeselect"));
			if(typeselect.length()==0)typeselect = "3";
			if(!typeselect.equals("") && !typeselect.equals("0")&& !typeselect.equals("6")){
				if(typeselect.equals("1")){
					fromDate = TimeUtil.getCurrentDateString();
					toDate = TimeUtil.getCurrentDateString();
				}else{
					fromDate = TimeUtil.getDateByOption(typeselect,"0");
					toDate = TimeUtil.getDateByOption(typeselect,"1");
				}
			}

			String backFields = " id, resourceid,departmentid, lastname, workcode, status, dsporder, kqdate, serialid, workbegindate1,workbegintime1,workbegindate2,workbegintime2,workbegindate3,workbegintime3, signdate,signtime,beLateMins ";
			String sqlFrom  = " (select b.id, b.resourceid,a.departmentid, a.lastname, a.workcode, a.status, a.dsporder, b.kqdate, b.serialid, b.workbegindate1 as workbegindate,b.workbegintime1 as workbegintime,b.signindate1 as signdate,b.signintime1 as signtime,beLateMins1 as beLateMins " +
												" from hrmresource a, kq_format b where a.id = b.resourceid and kqdate >= '"+fromDate+"' and kqdate <= '"+toDate+"' and beLateMins1>0 " +
												" UNION ALL " +
												" select b.id, b.resourceid,a.departmentid, a.lastname, a.workcode, a.status, a.dsporder, b.kqdate, b.serialid, b.workbegindate2 as workbegindate,b.workbegintime2,b.signindate2 as signdate,b.signintime2 as signtime,beLateMins2 as beLateMins " +
												" from hrmresource a, kq_format b where a.id = b.resourceid and kqdate >= '"+fromDate+"' and kqdate <= '"+toDate+"' and beLateMins2>0 " +
												" UNION ALL " +
												" select b.id, b.resourceid,a.departmentid, a.lastname, a.workcode, a.status, a.dsporder, b.kqdate, b.serialid, b.workbegindate3 as workbegindate,b.workbegintime3,b.signindate3 as signdate,b.signintime3 as signtime,beLateMins3 as beLateMins " +
												" from hrmresource a, kq_format b where a.id = b.resourceid and kqdate >= '"+fromDate+"' and kqdate <= '"+toDate+"' and beLateMins3>0 ) t";
			String sqlWhere = " where 1=1 ";
			String orderby = " dsporder " ;
			String tableString = "";

			if (keyWord.length() > 0){
				sqlWhere += " and lastname = "+keyWord;
			}

			if (fromDate.length() > 0){
				sqlWhere += " and kqdate >= '"+fromDate+"'";
			}

			if (toDate.length() > 0){
				sqlWhere += " and kqdate <= '"+toDate+"'";
			}

			if (resourceId.length() > 0){
				sqlWhere += " and resourceid in ( "+resourceId+")";
			}

			String pageUid = PageUidFactory.getHrmPageUid("KQReportDetialList");

			String operateString= "<operates width=\"20%\">";
			operateString+=" <popedom transmethod=\"com.engine.kq.util.TransMethod.getOperate\" otherpara=\"column:kqtype\" ></popedom> ";
			operateString+="     <operate href=\"javascript:doEditBase();\" text=\""+SystemEnv.getHtmlLabelName(388734,user.getLanguage())+"\" index=\"0\"/>";
			operateString+="     <operate href=\"javascript:doEditKQType();\" text=\""+SystemEnv.getHtmlLabelName(388735,user.getLanguage())+"\" index=\"1\"/>";
			operateString+="</operates>";
			tableString=""+
							"<table pageUid=\""+pageUid+"\" pagesize=\"10\" tabletype=\"none\">"+
							"<sql backfields=\""+backFields+"\" sqlform=\""+Util.toHtmlForSplitPage(sqlFrom)+"\" sqlprimarykey=\"id\" sqlorderby=\""+orderby+"\" sqlsortway=\"asc\" sqldistinct=\"true\" sqlwhere=\""+Util.toHtmlForSplitPage(sqlWhere)+"\"/>"+
							//operateString+
							"<head>"+
							"				<col width=\"10%\" text=\""+SystemEnv.getHtmlLabelName(413,user.getLanguage())+"\" column=\"lastname\" orderkey=\"lastname\"/>"+
							"				<col width=\"10%\" text=\""+SystemEnv.getHtmlLabelName(714,user.getLanguage())+"\" column=\"workcode\" orderkey=\"workcode\"/>"+
							"				<col width=\"20%\" text=\""+SystemEnv.getHtmlLabelName(124,user.getLanguage())+"\" column=\"departmentid\" orderkey=\"departmentid\" transmethod=\"weaver.hrm.company.DepartmentComInfo.getDepartmentname\"/>"+
							"				<col width=\"10%\" text=\""+SystemEnv.getHtmlLabelName(602,user.getLanguage())+"\" column=\"status\" transmethod=\"weaver.hrm.resource.ResourceComInfo.getStatusName\" otherpara=\"+user.getLanguage()+\"/>"+
							"				<col width=\"30%\" text=\""+SystemEnv.getHtmlLabelName(390054,user.getLanguage())+"\" column=\"serialid\" transmethod=\"com.engine.kq.cmd.shiftmanagement.toolkit.ShiftManagementToolKit.getShiftOnOffWorkSections\"/>"+
							"				<col width=\"20%\" text=\""+SystemEnv.getHtmlLabelName(390495,user.getLanguage())+"\" column=\"signdate\" transmethod=\"com.engine.kq.util.TransMethod.getSignTime\" otherpara=\"column:signtime\" />"+
							"</head>"+
							"</table>";

			//主要用于 显示定制列以及 表格 每页展示记录数选择
			sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
			Util_TableMap.setVal(sessionkey, tableString);
		}catch (Exception e){
			writeLog(e);
		}
		return sessionkey;
	}

	private String getGraveBeLateMinsInfo(){
		String sessionkey = "";
		try{
			String dataIndex = Util.null2String(params.get("dataIndex"));
			String resourceId = Util.null2String(params.get("resourceId"));
			String keyWord = Util.null2String(params.get("keyWord"));
			String fromDate = Util.null2String(params.get("fromDate"));
			String toDate = Util.null2String(params.get("toDate"));
			String typeselect =Util.null2String(params.get("typeselect"));
			if(typeselect.length()==0)typeselect = "3";
			if(!typeselect.equals("") && !typeselect.equals("0")&& !typeselect.equals("6")){
				if(typeselect.equals("1")){
					fromDate = TimeUtil.getCurrentDateString();
					toDate = TimeUtil.getCurrentDateString();
				}else{
					fromDate = TimeUtil.getDateByOption(typeselect,"0");
					toDate = TimeUtil.getDateByOption(typeselect,"1");
				}
			}

			String backFields = " id, resourceid,departmentid, lastname, workcode, status, dsporder, kqdate, serialid, workbegindate1,workbegintime1,workbegindate2,workbegintime2,workbegindate3,workbegintime3, signdate,signtime,beLateMins ";
			String sqlFrom  = " (select b.id, b.resourceid,a.departmentid, a.lastname, a.workcode, a.status, a.dsporder, b.kqdate, b.serialid, b.workbegindate1,b.workbegintime1,b.workbegindate2,b.workbegintime2,b.workbegindate3,b.workbegintime3,b.signindate1 as signdate,b.signintime1 as signtime,beLateMins1 as beLateMins " +
							" from hrmresource a, kq_format b where a.id = b.resourceid and kqdate >= '"+fromDate+"' and kqdate <= '"+toDate+"' and graveBeLateMins>0 " +
							" UNION ALL " +
							" select b.id, b.resourceid,a.departmentid, a.lastname, a.workcode, a.status, a.dsporder, b.kqdate, b.serialid, b.workbegindate1,b.workbegintime1,b.workbegindate2,b.workbegintime2,b.workbegindate3,b.workbegintime3,b.signindate2 as signdate,b.signintime2 as signtime,beLateMins2 as beLateMins " +
							" from hrmresource a, kq_format b where a.id = b.resourceid and kqdate >= '"+fromDate+"' and kqdate <= '"+toDate+"' and graveBeLateMins>0 " +
							" UNION ALL " +
							" select b.id, b.resourceid,a.departmentid, a.lastname, a.workcode, a.status, a.dsporder, b.kqdate, b.serialid, b.workbegindate1,b.workbegintime1,b.workbegindate2,b.workbegintime2,b.workbegindate3,b.workbegintime3,b.signindate3 as signdate,b.signintime3 as signtime,beLateMins3 as beLateMins " +
							" from hrmresource a, kq_format b where a.id = b.resourceid and kqdate >= '"+fromDate+"' and kqdate <= '"+toDate+"' and graveBeLateMins>0 ) t";
			String sqlWhere = " where 1=1 ";
			String orderby = " dsporder " ;
			String tableString = "";

			if (keyWord.length() > 0){
				sqlWhere += " and lastname = "+keyWord;
			}

			if (fromDate.length() > 0){
				sqlWhere += " and kqdate >= '"+fromDate+"'";
			}

			if (toDate.length() > 0){
				sqlWhere += " and kqdate <= '"+toDate+"'";
			}

			if (resourceId.length() > 0){
				sqlWhere += " and resourceid = "+resourceId;
			}

			String pageUid = PageUidFactory.getHrmPageUid("KQReportDetialList");

			String operateString= "<operates width=\"20%\">";
			operateString+=" <popedom transmethod=\"com.engine.kq.util.TransMethod.getOperate\" otherpara=\"column:kqtype\" ></popedom> ";
			operateString+="     <operate href=\"javascript:doEditBase();\" text=\""+SystemEnv.getHtmlLabelName(388734,user.getLanguage())+"\" index=\"0\"/>";
			operateString+="     <operate href=\"javascript:doEditKQType();\" text=\""+SystemEnv.getHtmlLabelName(388735,user.getLanguage())+"\" index=\"1\"/>";
			operateString+="</operates>";
			tableString=""+
							"<table pageUid=\""+pageUid+"\" pagesize=\"10\" tabletype=\"none\">"+
							"<sql backfields=\""+backFields+"\" sqlform=\""+Util.toHtmlForSplitPage(sqlFrom)+"\" sqlprimarykey=\"a.id\" sqlorderby=\""+orderby+"\" sqlsortway=\"asc\" sqldistinct=\"true\" sqlwhere=\""+Util.toHtmlForSplitPage(sqlWhere)+"\"/>"+
							//operateString+
							"<head>"+
							"				<col width=\"10%\" text=\""+SystemEnv.getHtmlLabelName(413,user.getLanguage())+"\" column=\"lastname\" orderkey=\"lastname\"/>"+
							"				<col width=\"10%\" text=\""+SystemEnv.getHtmlLabelName(714,user.getLanguage())+"\" column=\"workcode\" orderkey=\"workcode\"/>"+
							"				<col width=\"20%\" text=\""+SystemEnv.getHtmlLabelName(124,user.getLanguage())+"\" column=\"departmentid\" orderkey=\"departmentid\" transmethod=\"weaver.hrm.company.DepartmentComInfo.getDepartmentname\"/>"+
							"				<col width=\"10%\" text=\""+SystemEnv.getHtmlLabelName(602,user.getLanguage())+"\" column=\"status\" transmethod=\"weaver.hrm.resource.ResourceComInfo.getStatusName\" otherpara=\"+user.getLanguage()+\"/>"+
							"				<col width=\"30%\" text=\""+SystemEnv.getHtmlLabelName(390054,user.getLanguage())+"\" column=\"serialid\" transmethod=\"com.engine.kq.cmd.shiftmanagement.toolkit.ShiftManagementToolKit.getShiftOnOffWorkSections\"/>"+
							"				<col width=\"20%\" text=\""+SystemEnv.getHtmlLabelName(390495,user.getLanguage())+"\" column=\"signdate\" transmethod=\"com.engine.kq.util.TransMethod.getSignTime\" otherpara=\"column:signtime\" />"+
							"</head>"+
							"</table>";

			//主要用于 显示定制列以及 表格 每页展示记录数选择
			sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
			Util_TableMap.setVal(sessionkey, tableString);
		}catch (Exception e){
			writeLog(e);
		}
		return sessionkey;
	}

	@Override
	public BizLogContext getLogContext() {
		return null;
	}

}
