package com.engine.kq.cmd.report4E;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQLeaveRulesBiz;
import com.engine.kq.biz.KQReportBiz;
import com.engine.kq.log.KQLog;
import com.engine.kq.util.KQDurationCalculatorUtil;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
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 weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 小E 获取我的考勤情况
 */
public class GetScheduleDataCmd extends AbstractCommonCommand<Map<String, Object>> {
  private KQLog kqLog = new KQLog();
  private HttpServletRequest request;

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

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		try{
      getScheduleData(retmap);
      retmap.put("status", "1");
    }catch (Exception e) {
      retmap.put("status", "-1");
      retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}

  public void getScheduleData(Map<String, Object> retmap) throws Exception{
    kqLog.info("GetScheduleDataCmd:params:"+params);
    RecordSet rs = new RecordSet();
    DateTimeFormatter datefullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    //当前用户
    String curResourceId = Util.null2String(params.get("curResourceId"));
    User curUser = new User(Util.getIntValue(curResourceId));
    //查询用户
    String resourceId = Util.null2String(params.get("resourceId"));
    String fromDate = Util.null2String(params.get("fromDate"));
    String toDate = Util.null2String(params.get("toDate"));
    LocalDateTime nowDateTIme = LocalDateTime.now();
    LocalDateTime localFromDateTime = LocalDateTime.parse(fromDate,datefullFormatter);
    LocalDateTime localToDateTime = LocalDateTime.parse(toDate,datefullFormatter);
    fromDate = localFromDateTime.format(dateFormatter);
    toDate = localToDateTime.format(dateFormatter);
//    if(localToDateTime.isAfter(nowDateTIme)){
//      toDate = nowDateTIme.format(dateFormatter);
//    }

    if(fromDate.equalsIgnoreCase(toDate)){
      retmap.put("title", fromDate);
    }else{
      retmap.put("title", fromDate+""+weaver.systeminfo.SystemEnv.getHtmlLabelName(15322,weaver.general.ThreadVarLanguage.getLang())+""+toDate);
    }

    KQReportBiz kqReportBiz = new KQReportBiz();
    String rightSql = kqReportBiz.getReportRight("1",""+curUser.getUID(),"a");

    String backFields = " a.id,a.lastname,a.workcode,a.dsporder,b.resourceid,b.subcompanyid,b.departmentid,b.jobtitle," +
        " sum(b.workdays) as workdays,sum(b.workMins) as workMins,sum(b.attendancedays) as attendancedays," +
        " sum(b.attendanceMins) as attendanceMins,sum(b.beLate) as beLate,sum(b.beLateMins) as beLateMins, " +
        " sum(b.graveBeLate) as graveBeLate, sum(b.graveBeLateMins) as graveBeLateMins,sum(b.leaveEearly) as leaveEearly," +
        " sum(b.leaveEarlyMins) as leaveEarlyMins, sum(b.graveLeaveEarly) as graveLeaveEarly, " +
        " sum(b.graveLeaveEarlyMins) as graveLeaveEarlyMins,sum(b.absenteeism) as absenteeism, " +
        " sum(b.absenteeismMins) as absenteeismMins, sum(b.forgotCheck) as forgotCheck ";
    String sqlFrom = " from hrmresource a, kq_format_total b where a.id= b.resourceid and b.kqdate >=? and b.kqdate <=? ";
    String sqlWhere = rightSql;
    String groupBy = " group by a.id,a.lastname,a.workcode,a.dsporder,b.resourceid,b.subcompanyid,b.departmentid,b.jobtitle ";

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

    String sql = "select "+ backFields + sqlFrom+sqlWhere +groupBy;
    rs.executeQuery(sql,fromDate,toDate);

    Map<String, Object> params = new HashMap<>();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("fromDate", fromDate);
    jsonObject.put("toDate", toDate);
    jsonObject.put("resourceId", resourceId);
    jsonObject.put("typeselect", "6");
    params.put("data", jsonObject);

    //请假
    List<Map<String, Object>> allLeaveRules = KQLeaveRulesBiz.getAllLeaveRules();
    Map<String,Object> flowData = kqReportBiz.getFlowData(params,curUser);

    List<Object> infoLists = new ArrayList<>();
    Map<String,Object> infoMaps = new LinkedHashMap<>();
    while (rs.next()){
      String id = rs.getString("id");
      String workdays = Util.null2o(rs.getString("workdays"));
      String workMins = Util.null2o(rs.getString("workMins"));
      String workHours = Util.round(Util.null2String(Util.getDoubleValue(workMins,0.0)/60.0),2);
      String beLate = rs.getString("beLate");
      String graveBeLate = rs.getString("graveBeLate");
      String leaveEearly = rs.getString("leaveEearly");
      String graveLeaveEarly = rs.getString("graveLeaveEarly");
      String absenteeism = rs.getString("absenteeism");
      String forgotCheck = rs.getString("forgotCheck");

      //加班
      String overtimeValue = "";
      //出差
      String businessLeaveValue = "";
      //外出
      String officialBusinessValue = "";
      List<Object> infoList = new ArrayList<>();
      Map<String,Object> infoMap = new LinkedHashMap<>();
      Map<String,Object> dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(132056,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(383378,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", workdays);
      infoList.add(dataMap);

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(390053,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(391,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", workHours);
      infoList.add(dataMap);

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20081,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", beLate);
      infoList.add(dataMap);

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(500546,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", graveBeLate);
      infoList.add(dataMap);

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20082,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", leaveEearly);
      infoList.add(dataMap);

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(500547,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", graveLeaveEarly);
      infoList.add(dataMap);

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20085,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", absenteeism);
      infoList.add(dataMap);

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20086,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", forgotCheck);
      infoList.add(dataMap);

      infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005335,weaver.general.ThreadVarLanguage.getLang())+"");
      infoMap.put("detail", infoList);
      infoLists.add(infoMap);

      infoList = new ArrayList<>();
      infoMap = new LinkedHashMap<>();
      Map<String, Object> leaveRule = null;
      for(int i=0;allLeaveRules!=null&&i<allLeaveRules.size();i++){
        leaveRule = (Map<String, Object>)allLeaveRules.get(i);
        String flowType = Util.null2String("leaveType_"+leaveRule.get("id"));
        String flowTypeName = Util.null2String(leaveRule.get("name"));
        dataMap = new LinkedHashMap<>();
        dataMap.put("name", flowTypeName);
        dataMap.put("value", Util.null2s(Util.null2String(flowData.get(id+"|"+flowType)),"0"));
        infoList.add(dataMap);
      }
      infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005336,weaver.general.ThreadVarLanguage.getLang())+"");
      infoMap.put("detail", infoList);
      infoLists.add(infoMap);

      infoList = new ArrayList<>();
      infoMap = new LinkedHashMap<>();
      double workingDayOvertime = Util.getDoubleValue(Util.null2String(flowData.get(id+"|workingDayOvertime")));
      workingDayOvertime = workingDayOvertime<0?0:workingDayOvertime;
      double restDayOvertime = Util.getDoubleValue(Util.null2String(flowData.get(id+"|restDayOvertime")));
      restDayOvertime = restDayOvertime<0?0:restDayOvertime;
      double holidayOvertime = Util.getDoubleValue(Util.null2String(flowData.get(id+"|holidayOvertime")));
      holidayOvertime = holidayOvertime<0?0:holidayOvertime;
      overtimeValue = KQDurationCalculatorUtil
          .getDurationRound(String.valueOf(workingDayOvertime+restDayOvertime+holidayOvertime));

      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(6151,weaver.general.ThreadVarLanguage.getLang())+"");
      dataMap.put("value", Util.null2s(overtimeValue,"0"));
      infoList.add(dataMap);

      infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005337,weaver.general.ThreadVarLanguage.getLang())+"");
      infoMap.put("detail", infoList);
      infoLists.add(infoMap);

      infoList = new ArrayList<>();
      infoMap = new LinkedHashMap<>();
      businessLeaveValue = KQDurationCalculatorUtil.getDurationRound(Util.null2String(flowData.get(id+"|businessLeave")));
      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20084,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(383378,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", Util.null2s(businessLeaveValue,"0"));
      infoList.add(dataMap);

      officialBusinessValue = KQDurationCalculatorUtil.getDurationRound(Util.null2String(flowData.get(id+"|officialBusiness")));
      dataMap = new LinkedHashMap<>();
      dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(24058,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(383378,weaver.general.ThreadVarLanguage.getLang())+")");
      dataMap.put("value", Util.null2s(officialBusinessValue,"0"));
      infoList.add(dataMap);

      infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(375,weaver.general.ThreadVarLanguage.getLang())+"");
      infoMap.put("detail", infoList);
      infoLists.add(infoMap);
    }
    if(infoLists.isEmpty()){
      fillBlank(allLeaveRules,infoLists);
    }
    retmap.put("data", infoLists);
    kqLog.info("GetScheduleDataCmd:infoLists:"+ JSON.toJSONString(infoLists));
  }

  /**
   * 需要给一个默认值
   * @param allLeaveRules
   * @param infoLists
   */
  public void fillBlank(
      List<Map<String, Object>> allLeaveRules,
      List<Object> infoLists) {
    List<Object> infoList = new ArrayList<>();
    Map<String,Object> infoMap = new LinkedHashMap<>();
    Map<String,Object> dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(132056,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(383378,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(390053,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(391,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20081,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20082,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20085,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20086,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(18083,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005335,weaver.general.ThreadVarLanguage.getLang())+"");
    infoMap.put("detail", infoList);
    infoLists.add(infoMap);

    infoList = new ArrayList<>();
    infoMap = new LinkedHashMap<>();
    Map<String, Object> leaveRule = null;
    for(int i=0;allLeaveRules!=null&&i<allLeaveRules.size();i++){
      leaveRule = (Map<String, Object>)allLeaveRules.get(i);
      String flowType = Util.null2String("leaveType_"+leaveRule.get("id"));
      String flowTypeName = Util.null2String(leaveRule.get("name"));
      dataMap = new LinkedHashMap<>();
      dataMap.put("name", flowTypeName);
      dataMap.put("value", "0");
      infoList.add(dataMap);
    }
    infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005336,weaver.general.ThreadVarLanguage.getLang())+"");
    infoMap.put("detail", infoList);
    infoLists.add(infoMap);

    infoList = new ArrayList<>();
    infoMap = new LinkedHashMap<>();
    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(6151,weaver.general.ThreadVarLanguage.getLang())+"");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005337,weaver.general.ThreadVarLanguage.getLang())+"");
    infoMap.put("detail", infoList);
    infoLists.add(infoMap);

    infoList = new ArrayList<>();
    infoMap = new LinkedHashMap<>();
    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(20084,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(383378,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    dataMap = new LinkedHashMap<>();
    dataMap.put("name", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(24058,weaver.general.ThreadVarLanguage.getLang())+"("+weaver.systeminfo.SystemEnv.getHtmlLabelName(383378,weaver.general.ThreadVarLanguage.getLang())+")");
    dataMap.put("value", "0");
    infoList.add(dataMap);

    infoMap.put("title", ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(375,weaver.general.ThreadVarLanguage.getLang())+"");
    infoMap.put("detail", infoList);
    infoLists.add(infoMap);
  }

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

}
