package com.engine.kq.cmd.shiftmanagement.toolkit;

import com.engine.kq.biz.*;
import com.engine.kq.entity.WorkTimeEntity;
import com.engine.kq.log.KQLog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.systeminfo.SystemEnv;



public class ShiftManagementToolKit {
  private static KQLog kqLog = new KQLog();

  /**
   * 判断班次列表里是否显示checkbox
   * @param id
   * @return
   */
  public String getCheckbox(String id){
    KQGroupBiz groupBiz = new KQGroupBiz();
    boolean serialIdUesed = groupBiz.serialIdIsUesed(id);

    if(serialIdUesed){
      return "false";
    }else{
      return "true";
    }
  }

  /**
   * 判断班次列表右侧操作按钮是否显示删除
   * @param id
   * @return
   */
  public ArrayList<String> getShiftListOperate(String id){
    ArrayList<String> resultList = new ArrayList<String>();
    KQGroupBiz groupBiz = new KQGroupBiz();
    boolean serialIdUesed = groupBiz.serialIdIsUesed(id);

    resultList.add("true");
    resultList.add("true");

    if(serialIdUesed){
      resultList.add("false");
    }else{
      resultList.add("true");
    }

    resultList.add("true");
    return resultList;
  }

  /**
   * 获取工作时段
   * @param id
   * @return
   */
  public String getShiftOnOffWorkSection(String id){
    if(id.length() == 0) {
      return "";
    }
    String workSections = "";
    KQShiftOnOffWorkSectionComInfo workSectionComInfo = new KQShiftOnOffWorkSectionComInfo();
    List<Object> workSectionList = workSectionComInfo.getWorkSectionTimes(id);
    if(workSectionList != null){
      for(int i = 0; i < workSectionList.size() ;i++){
        List<Object> workTimes = (List<Object>) workSectionList.get(i);
        if(workTimes != null && !workTimes.isEmpty() && (workTimes.size() % 2 ==0)){
          Map<String,String> onWorkMap = (Map<String, String>) workTimes.get(0);
          Map<String,String> offWorkMap = (Map<String, String>) workTimes.get(1);
          String ontime = onWorkMap.get("times");
          String offtime = offWorkMap.get("times");
          if(workSections.length() > 0){
            workSections += " ";
          }
          workSections += ontime+"-"+offtime;
        }
      }
    }

    return Util.null2String(workSections);
  }

  public String getShiftOnOffWorkSections(String id,String langid){
    return getShiftOnOffWorkSections(id,Util.getIntValue(langid,7));
  }
  /**
   * 获取工作时段+班次名称
   * @param id
   * @return
   */
  public String getShiftOnOffWorkSections(String id,int langid){
    String serials = "";
    KQShiftManagementComInfo kqShiftManagementComInfo = new KQShiftManagementComInfo();
    String[] ids = id.split(",");
    for(int i = 0 ; i < ids.length ; i++){
      String serialInfo = "";
      String serial = kqShiftManagementComInfo.getSerial(ids[i]);
      if(serial.length() > 0){
        String workSection = getShiftOnOffWorkSection(ids[i]);
        if(workSection.length()>0){
          serialInfo = serial+"("+workSection+")";
        }
      }else{
        serialInfo = SystemEnv.getHtmlLabelName(26593, langid);
      }
      if(serials.length()>0){
        serials+=" ";
      }
      serials+=serialInfo;
    }
    return serials;
  }


  /**
   * 获取工作时段+班次名称--yz
   * --加入自由班制的班次判断
   * @param id
   * @return
   */
  public String getShiftOnOffWorkSections(String id,int langid,String date,String resourceid){
    String serials = "";
    KQShiftManagementComInfo kqShiftManagementComInfo = new KQShiftManagementComInfo();
    String[] ids = id.split(",");
    for(int i = 0 ; i < ids.length ; i++){
      String serialInfo = "";
      String serial = kqShiftManagementComInfo.getSerial(ids[i]);
      if(serial.length() > 0){
        String workSection = getShiftOnOffWorkSection(ids[i]);
        if(workSection.length()>0){
          serialInfo = serial+"("+workSection+")";
        }
      }else{
        try{
          KQWorkTime kqWorkTime = new KQWorkTime();
          WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceid, date);
          if("3".equals(workTime.getKQType())){
            if("0".equals(Util.null2String(workTime.getWorkMins()))){
              serialInfo = SystemEnv.getHtmlLabelName(26593, langid);
            }else{
              serialInfo = SystemEnv.getHtmlLabelName(389129, langid);
            }
          }else{
            serialInfo = SystemEnv.getHtmlLabelName(26593, langid);
          }
        }catch (Exception e){
          e.printStackTrace();
        }
      }
      if(serials.length()>0){
        serials+=" ";
      }
      serials+=serialInfo;
    }
    return serials;
  }

  /**
   * 获取班次里个性化设置配置信息
   * @param serialid
   * @param isFormat 为true表示自己查询数据库
   * @return
   * key:ruleDetail
   * val:返回的是个性化设置里的其他规则,是个map结果集
   *      map里存着三种类型：允许早到早走 earlyinearlyout 允许晚到晚走 lateinlateout 允许晚走晚到 lateoutlatein
   *      map.get("earlyinearlyout") 返回的是个list，list里放的是
   *      sectionMap.put("enable", enable);
   *      sectionMap.put("advancetime", advancetime);
   *      sectionMap.put("postponetime", postponetime);
   *      sectionMap.put("record", record);
   *      sectionMap.put("isoffdutyfreecheck", 0);
   */
  public static Map<String,Object> getShiftRuleInfo(String serialid,boolean isFormat){
    Map<String,Object> shiftRuleMap = new HashMap<>();
    if("".equalsIgnoreCase(serialid)){
      //kqLog.info("getShiftRuleInfo:serialid is null ");
      return shiftRuleMap;
    }
    if(isFormat){
      String shiftonoffworkcount = "";
      RecordSet rs = new RecordSet();
      String sql  = "select * from kq_ShiftManagement where 1=1 and id="+serialid+" order by id ";
      rs.executeQuery(sql);
      if(rs.next()){
        //是否开启下班不用打卡 1表示下班不用打卡
        String isoffdutyfreecheck = rs.getString("isoffdutyfreecheck");
        shiftonoffworkcount = Util.null2String(rs.getString("shiftonoffworkcount"));
        if("1".equalsIgnoreCase(isoffdutyfreecheck)){
          shiftRuleMap.put("isoffdutyfreecheck", "1");
        }else{
          shiftRuleMap.put("isoffdutyfreecheck", "0");
        }
      }

//    允许迟到是否开启
      sql  = "select * from kq_ShiftPersonalizedRule where 1=1 and serialid="+serialid+" order by id ";
      rs.executeQuery(sql);
      if(rs.next()){
        String permitlatestatus = Util.null2String(rs.getString("permitlatestatus"));
        String permitlateminutes = Util.null2String(rs.getString("permitlateminutes"));
        String permitleaveearlystatus = Util.null2String(rs.getString("permitleaveearlystatus"));
        String permitleaveearlyminutes = Util.null2String(rs.getString("permitleaveearlyminutes"));
        String seriouslatestatus = Util.null2String(rs.getString("seriouslatestatus"));
        String seriouslateminutes = Util.null2String(rs.getString("seriouslateminutes"));
        String seriousleaveearlystatus = Util.null2String(rs.getString("seriousleaveearlystatus"));
        String seriousleaveearlyminutes = Util.null2String(rs.getString("seriousleaveearlyminutes"));
        String lateabsentstatus = Util.null2String(rs.getString("lateabsentstatus"));
        String lateabsentminutes = Util.null2String(rs.getString("lateabsentminutes"));
        String leaveearlyabsentstatus = Util.null2String(rs.getString("leaveearlyabsentstatus"));
        String leaveearlyabsentminutes = Util.null2String(rs.getString("leaveearlyabsentminutes"));
        String personalizedruleid = Util.null2String(rs.getString("id"));
        String enableexcludelate = Util.null2String(rs.getString("enableexcludelate"));
        String enableexcludeleaveearly = Util.null2String(rs.getString("enableexcludeleaveearly"));

        if("1".equalsIgnoreCase(permitlatestatus)){
          shiftRuleMap.put("permitlateminutes", permitlateminutes);
          shiftRuleMap.put("permitlatestatus", "1");
        }else{
          shiftRuleMap.put("permitlatestatus", "0");
        }

//    允许早退是否开启
        if("1".equalsIgnoreCase(permitleaveearlystatus)){
          shiftRuleMap.put("permitleaveearlyminutes", permitleaveearlyminutes);
          shiftRuleMap.put("permitleaveearlystatus", "1");
        }else{
          shiftRuleMap.put("permitleaveearlystatus", "0");
        }
//    严重迟到是否开启
        if("1".equalsIgnoreCase(seriouslatestatus)){
          shiftRuleMap.put("seriouslateminutes", seriouslateminutes);
          shiftRuleMap.put("seriouslatestatus", "1");
        }else{
          shiftRuleMap.put("seriouslatestatus", "0");
        }

//    严重早退是否开启
        if("1".equalsIgnoreCase(seriousleaveearlystatus)){
          shiftRuleMap.put("seriousleaveearlyminutes", seriousleaveearlyminutes);
          shiftRuleMap.put("seriousleaveearlystatus", "1");
        }else{
          shiftRuleMap.put("seriousleaveearlystatus", "0");
        }

//    迟到视作旷工是否开启
        if("1".equalsIgnoreCase(lateabsentstatus)){
          shiftRuleMap.put("lateabsentminutes", lateabsentminutes);
          shiftRuleMap.put("lateabsentstatus", "1");
        }else{
          shiftRuleMap.put("lateabsentstatus", "0");
        }
//    早退视作旷工是否开启
        if("1".equalsIgnoreCase(leaveearlyabsentstatus)){
          shiftRuleMap.put("leaveearlyabsentminutes", leaveearlyabsentminutes);
          shiftRuleMap.put("leaveearlyabsentstatus", "1");
        }else{
          shiftRuleMap.put("leaveearlyabsentstatus", "0");
        }
        if (personalizedruleid.length() >0){
          if("1".equalsIgnoreCase(shiftonoffworkcount)){
            KQShiftPersonalizedRuleDetailComInfo ruleDetailComInfo = new KQShiftPersonalizedRuleDetailComInfo();
            //只有一次签到签退才有个性化的这些设置
            Map<String,Object> ruleDetailMap = ruleDetailComInfo.getPersonalizedRuleDetail_sql(personalizedruleid);
            if(ruleDetailMap != null && !ruleDetailMap.isEmpty()){
              shiftRuleMap.put("ruleDetail", ruleDetailMap);
            }
          }
        }


//    是否排除迟到分钟数
        if("1".equalsIgnoreCase(enableexcludelate)){
          shiftRuleMap.put("enableexcludelate", "1");
        }else{
          shiftRuleMap.put("enableexcludelate", "0");
        }

//    是否排除早退分钟数
        if("1".equalsIgnoreCase(enableexcludeleaveearly)){
          shiftRuleMap.put("enableexcludeleaveearly", "1");
        }else{
          shiftRuleMap.put("enableexcludeleaveearly", "0");
        }

      }
    }else{
      KQShiftManagementComInfo kqShiftManagementComInfo = new KQShiftManagementComInfo();
      KQShiftPersonalizedRuleCominfo ruleCominfo = new KQShiftPersonalizedRuleCominfo();
      KQShiftPersonalizedRuleDetailComInfo ruleDetailComInfo = new KQShiftPersonalizedRuleDetailComInfo();
      //是否开启下班不用打卡 1表示下班不用打卡
      String isoffdutyfreecheck = Util.null2String(kqShiftManagementComInfo.getIsoffdutyfreecheck(serialid));
      if("1".equalsIgnoreCase(isoffdutyfreecheck)){
        shiftRuleMap.put("isoffdutyfreecheck", "1");
      }else{
        shiftRuleMap.put("isoffdutyfreecheck", "0");
      }

//    允许迟到是否开启
      String permitlatestatus = Util.null2String(ruleCominfo.getPermitlatestatus(serialid));
      if("1".equalsIgnoreCase(permitlatestatus)){
        String permitlateminutes = ruleCominfo.getPermitlateminutes(serialid);
        shiftRuleMap.put("permitlateminutes", permitlateminutes);
        shiftRuleMap.put("permitlatestatus", "1");
      }else{
        shiftRuleMap.put("permitlatestatus", "0");
      }
//    允许早退是否开启
      String permitleaveearlystatus = Util.null2String(ruleCominfo.getPermitleaveearlystatus(serialid));
      if("1".equalsIgnoreCase(permitleaveearlystatus)){
        String permitleaveearlyminutes = ruleCominfo.getPermitleaveearlyminutes(serialid);
        shiftRuleMap.put("permitleaveearlyminutes", permitleaveearlyminutes);
        shiftRuleMap.put("permitleaveearlystatus", "1");
      }else{
        shiftRuleMap.put("permitleaveearlystatus", "0");
      }
//    严重迟到是否开启
      String seriouslatestatus = Util.null2String(ruleCominfo.getSeriouslatestatus(serialid));
      if("1".equalsIgnoreCase(seriouslatestatus)){
        String seriouslateminutes = ruleCominfo.getSeriouslateminutes(serialid);
        shiftRuleMap.put("seriouslateminutes", seriouslateminutes);
        shiftRuleMap.put("seriouslatestatus", "1");
      }else{
        shiftRuleMap.put("seriouslatestatus", "0");
      }
//    严重早退是否开启
      String seriousleaveearlystatus = Util.null2String(ruleCominfo.getSeriousleaveearlystatus(serialid));
      if("1".equalsIgnoreCase(seriousleaveearlystatus)){
        String seriousleaveearlyminutes = ruleCominfo.getSeriousleaveearlyminutes(serialid);
        shiftRuleMap.put("seriousleaveearlyminutes", seriousleaveearlyminutes);
        shiftRuleMap.put("seriousleaveearlystatus", "1");
      }else{
        shiftRuleMap.put("seriousleaveearlystatus", "0");
      }
//    迟到视作旷工是否开启
      String lateabsentstatus = Util.null2String(ruleCominfo.getLateabsentstatus(serialid));
      if("1".equalsIgnoreCase(lateabsentstatus)){
        String lateabsentminutes = ruleCominfo.getLateabsentminutes(serialid);
        shiftRuleMap.put("lateabsentminutes", lateabsentminutes);
        shiftRuleMap.put("lateabsentstatus", "1");
      }else{
        shiftRuleMap.put("lateabsentstatus", "0");
      }
//    早退视作旷工是否开启
      String leaveearlyabsentstatus = Util.null2String(ruleCominfo.getLeaveearlyabsentstatus(serialid));
      if("1".equalsIgnoreCase(leaveearlyabsentstatus)){
        String leaveearlyabsentminutes = ruleCominfo.getLeaveearlyabsentminutes(serialid);
        shiftRuleMap.put("leaveearlyabsentminutes", leaveearlyabsentminutes);
        shiftRuleMap.put("leaveearlyabsentstatus", "1");
      }else{
        shiftRuleMap.put("leaveearlyabsentstatus", "0");
      }
      String personalizedruleid = ruleCominfo.getID(serialid);
      if (personalizedruleid.length() >0){
        String shiftonoffworkcount = Util.null2String(kqShiftManagementComInfo.getShiftonoffworkcounts(serialid));
        if("1".equalsIgnoreCase(shiftonoffworkcount)){
          //只有一次签到签退才有个性化的这些设置
          Map<String,Object> ruleDetailMap = ruleDetailComInfo.getPersonalizedRuleDetail(personalizedruleid);
          if(ruleDetailMap != null && !ruleDetailMap.isEmpty()){
            shiftRuleMap.put("ruleDetail", ruleDetailMap);
          }
        }
      }

//    是否排除迟到分钟数
      String enableexcludelate = Util.null2String(ruleCominfo.getEnableexcludelate(serialid));
      if("1".equalsIgnoreCase(enableexcludelate)){
        shiftRuleMap.put("enableexcludelate", "1");
      }else{
        shiftRuleMap.put("enableexcludelate", "0");
      }

//    是否排除早退分钟数
      String enableexcludeleaveearly = Util.null2String(ruleCominfo.getEnableexcludeleaveearly(serialid));
      if("1".equalsIgnoreCase(enableexcludeleaveearly)){
        shiftRuleMap.put("enableexcludeleaveearly", "1");
      }else{
        shiftRuleMap.put("enableexcludeleaveearly", "0");
      }
    }

    return shiftRuleMap;
  }

}
