package com.engine.kq.biz;

import com.engine.kq.bean.KQHrmScheduleSign;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.util.KQSignUtil;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * 签到签退数据处理类
 */
public class KQScheduleSignBiz {

  private KQLog kqLog = new KQLog();
  private String resourceid;
  private String userType;
  private String signDate;
  private String signTime;
  private String worksection;
  /**
   * 考勤数据所属日期 因为班次可能跨天
   */
  private String belongdate;
  private String signtype;
  private String signDateTimeSql;
  /**
   * 上班开始打卡时间
   */
  private String signSectionTime;

  /**
   * 上班结束打卡时间
   */
  private String signSectionEndTime;
  /**
   * 下班开始打卡时间
   */
  private String signSectionBeginTime;

  /**
   * 下班结束打卡时间
   */
  private String offSignSectionTime;

  private KQScheduleSignBiz(KQScheduleSignParamBuilder build){
    this.resourceid = build.resourceid;
    this.userType = build.userType;
    this.signDate = build.signDate;
    this.signTime = build.signTime;
    this.belongdate = build.belongdate;
    this.signtype = build.signtype;
    this.worksection = build.worksection;
    this.signDateTimeSql = build.signDateTimeSql;
    this.signSectionTime = build.signSectionTime;
    this.signSectionEndTime = build.signSectionEndTime;
    this.signSectionBeginTime = build.signSectionBeginTime;
    this.offSignSectionTime = build.offSignSectionTime;
  }

  /**
   * 自由班制的签到签退数据
   * @return
   */
  public List<KQHrmScheduleSign> getFreeScheduleSignInfo() {

    RecordSet rs = new RecordSet();
    String signSql = "select * from hrmschedulesign where 1=1 and isInCom='1' ";
    String sqlWhere = sqlParamWhere();
    if(sqlWhere.length() > 0){
      signSql += sqlWhere;
    }
    signSql += " order by signdate,signtime";
    List<KQHrmScheduleSign> kqHrmScheduleSigns = new ArrayList<>();

    try{
      rs.execute(signSql);
      while (rs.next()){
        KQHrmScheduleSign kqHrmScheduleSign = getSignBean(rs);

        kqHrmScheduleSigns.add(kqHrmScheduleSign);
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    return kqHrmScheduleSigns;
  }
  /**
   * 非自由班制的考勤数据
   * 针对设置了打卡开始范围，结束范围的数据，即可以出现，只签退没有签到的数据的处理
   * @return
   */
  public Map<String,KQHrmScheduleSign> getScheduleSignInfoWithCardRange(){

    String signSql = "";
    Map<String,KQHrmScheduleSign> signMap = new HashMap<>();
    RecordSet rs = new RecordSet();
    String dbtype = rs.getDBType();
    String baseSql = "select * from hrmschedulesign where 1=1 and isInCom='1' ";
    String sqlWhere = sqlParamWhere();
    if(sqlWhere.length() > 0){
      baseSql += sqlWhere;
    }
    if(signSectionEndTime.length() == 0 && signSectionBeginTime.length() == 0){
      //如果没设置上班后，下班前打卡
      String signTimeSql = KQSignUtil.buildSignSql(signSectionTime,offSignSectionTime);
      if(signTimeSql.length() > 0){
        baseSql += " and "+signTimeSql;
      }
      if(baseSql.length() > 0){
        signSql = baseSql;
        signSql += " order by signdate,signtime";
        kqLog.info("signSql:"+signSql);
        try{
          rs.execute(signSql);
          while (rs.next()){
            getScheduleSignBean(rs,signMap);
          }
        }catch (Exception e){
          e.printStackTrace();
        }
      }
    }else {
      String onSignTimeSql = KQSignUtil.buildSignSql(signSectionTime,signSectionEndTime);
      String offSignTimeSql = KQSignUtil.buildSignSql(signSectionBeginTime,offSignSectionTime);
      String onSql = "";
      String orderSql = "";
      if(onSignTimeSql.length() > 0){
        orderSql = " order by signdate asc, signtime asc ";
        onSql += "select 'on' as belongtype,t.* from ("+baseSql+" and "+onSignTimeSql+") t";
        if("oracle".equalsIgnoreCase(dbtype)){
          onSql = "select * from ("+onSql+" "+orderSql+" ) a where rownum=1";
        }else if("mysql".equalsIgnoreCase(dbtype)){
          onSql = "select * from (select * from ("+onSql+" "+orderSql+" ) a limit 0,1) a ";
        }
        else if("postgresql".equalsIgnoreCase(dbtype)){
          onSql = "select * from (select * from ("+onSql+" "+orderSql+" ) a limit 1 offset 0) a ";
        }
        else if("sqlserver".equalsIgnoreCase(dbtype)){
          onSql = "select top 1 * from ("+onSql+" ) a "+" "+orderSql ;
        }
      }
      String offSql = "";
      if(offSignTimeSql.length() > 0){
        orderSql = " order by signdate desc, signtime desc ";
        offSql += "select 'off' as belongtype,t.* from ("+baseSql+" and "+offSignTimeSql+") t";
        if("oracle".equalsIgnoreCase(dbtype)){
          offSql = "select * from ("+offSql+" "+orderSql+" ) a where rownum=1";
        }else if("mysql".equalsIgnoreCase(dbtype)){
          offSql = "select * from (select * from ("+offSql+" "+orderSql+" ) a limit 0,1) a ";
        }
        else if("postgresql".equalsIgnoreCase(dbtype)){
          offSql = "select * from (select * from ("+offSql+" "+orderSql+" ) a limit 1 offset 0) a ";
        }
        else if("sqlserver".equalsIgnoreCase(dbtype)){
          offSql = "select top 1 * from ("+offSql+" ) a "+" "+orderSql ;
        }
      }
      if(onSql.length() > 0 && offSql.length() > 0){
        signSql = "select * from ("+onSql+" UNION ALL "+offSql+") s ";
        kqLog.info("signSql:"+signSql);
        try{
          Map<String,KQHrmScheduleSign> checkMap = new HashMap<>();
          rs.execute(signSql);
          while (rs.next()){
            getScheduleSignCardRangeBean(rs,signMap,checkMap);
          }
        }catch (Exception e){
          e.printStackTrace();
        }
      }
    }

    return signMap;
  }

  /**
   * 如果签到和签退可以独立出来，用这个方法
   * @param rs
   * @param signMap
   * @param checkMap
   */
  public void getScheduleSignCardRangeBean(RecordSet rs, Map<String, KQHrmScheduleSign> signMap,
      Map<String, KQHrmScheduleSign> checkMap){
    String belongtype		=Util.null2String(rs.getString("belongtype"));
    KQHrmScheduleSign kqHrmScheduleSign = getSignBean(rs);
    if("on".equalsIgnoreCase(belongtype)){
      if(!checkMap.containsKey("on")){
        signMap.put("signin",kqHrmScheduleSign);
      }
    }else{
      signMap.put("signout",kqHrmScheduleSign);
    }
  }

  /**
   * 原始的获取最早时间作为签到，最晚时间作为签退的方法
   * @param rs
   * @param signMap
   */
  public void getScheduleSignBean(RecordSet rs,Map<String,KQHrmScheduleSign> signMap){
    KQHrmScheduleSign kqHrmScheduleSign = getSignBean(rs);
    if(signMap.isEmpty()){
      signMap.put("signin",kqHrmScheduleSign);
    }else {
      signMap.put("signout",kqHrmScheduleSign);
    }
  }

  public KQHrmScheduleSign getSignBean(RecordSet rs){

    KQHrmScheduleSign kqHrmScheduleSign = new KQHrmScheduleSign();
    String id=Util.null2String(rs.getString("id"));
    String userid=Util.null2String(rs.getString("userid"));
    String usertype		=Util.null2String(rs.getString("usertype"));
    String signtype=Util.null2String(rs.getString("signtype"));
    String signdate=Util.null2String(rs.getString("signdate"));
    String signtime=Util.null2String(rs.getString("signtime"));
    String clientaddress=Util.null2String(rs.getString("clientaddress"));
    String signfrom=Util.null2String(rs.getString("signfrom"));
    String longitude=Util.null2String(rs.getString("longitude"));
    String latitude=Util.null2String(rs.getString("latitude"));
    String addr=Util.null2String(rs.getString("addr"));
    String isincom=Util.null2String(rs.getString("isincom"));
    String isimport=Util.null2String(rs.getString("isimport"));
    String importsql=Util.null2String(rs.getString("importsql"));
    String belongdate=Util.null2String(rs.getString("belongdate"));
    String serialid=Util.null2String(rs.getString("serialid"));
    String worksections=Util.null2String(rs.getString("worksection"));
    String signstatus=Util.null2String(rs.getString("signstatus"));

    kqHrmScheduleSign.setId(id);
    kqHrmScheduleSign.setUserid(userid);
    kqHrmScheduleSign.setUsertype(usertype);
    kqHrmScheduleSign.setSigntype(signtype);
    kqHrmScheduleSign.setSigndate(signdate);
    kqHrmScheduleSign.setSigntime(signtime);
    kqHrmScheduleSign.setClientaddress(clientaddress);
    kqHrmScheduleSign.setSignfrom(signfrom);
    kqHrmScheduleSign.setLongitude(longitude);
    kqHrmScheduleSign.setLatitude(latitude);
    kqHrmScheduleSign.setAddr(addr);
    kqHrmScheduleSign.setIsincom(isincom);
    kqHrmScheduleSign.setIsimport(isimport);
    kqHrmScheduleSign.setImportsql(importsql);
    kqHrmScheduleSign.setBelongdate(belongdate);
    kqHrmScheduleSign.setSerialid(serialid);
    kqHrmScheduleSign.setWorksections(worksections);
    kqHrmScheduleSign.setSignstatus(signstatus);

    return kqHrmScheduleSign;
  }

  /**
   * 非自由班制的考勤数据
   * @return
   */
  public Map<String,KQHrmScheduleSign> getScheduleSignInfo(){

    RecordSet rs = new RecordSet();
    String signSql = "select * from hrmschedulesign where 1=1 and isInCom='1' ";
    String sqlWhere = sqlParamWhere();
    if(sqlWhere.length() > 0){
      signSql += sqlWhere;
    }
    signSql += " order by signdate,signtime";
    Map<String,KQHrmScheduleSign> signMap = new HashMap<>();

    try{
      kqLog.info("signSql:"+signSql);
      rs.execute(signSql);
      while (rs.next()){
        getScheduleSignBean(rs,signMap);
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    return signMap;
  }

  /**
   * 生成相应的查询条件
   * @return
   */
  private String sqlParamWhere() {
    String sqlWhere = "";
    if(resourceid.length() > 0){
      sqlWhere += " and userid in ( "+resourceid+" )";
    }
    if(signDate.length() > 0){
      sqlWhere += " and signDate = '"+signDate+"' ";
    }
    if(signTime.length() > 0){
      sqlWhere += " and signTime = '"+signTime+"' ";
    }
    if(signDateTimeSql.length() > 0){
      sqlWhere += " and "+signDateTimeSql;
    }
    if(belongdate.length() > 0){
      sqlWhere += " and belongdate = '"+belongdate+"' ";
    }
    if(signtype.length() > 0){
      sqlWhere += " and signtype = '"+signtype+"' ";
    }
    //不需要userType，没啥用
//    if(userType.length() > 0){
//      sqlWhere += " and userType = '"+userType+"' ";
//    }
    if(worksection.length() > 0){
      sqlWhere += " and worksection = '"+worksection+"' ";
    }
    return sqlWhere;
  }

  public static class KQScheduleSignParamBuilder{
    private String resourceid;
    private String userType;
    private String signDate;
    private String signTime;
    private String belongdate;
    private String signtype;
    private String worksection;
    /**
     * 自定义sql
     */
    private String signDateTimeSql;
    /**
     * 上班开始打卡时间
     */
    private String signSectionTime;

    /**
     * 上班结束打卡时间
     */
    private String signSectionEndTime;
    /**
     * 下班开始打卡时间
     */
    private String signSectionBeginTime;

    /**
     * 下班结束打卡时间
     */
    private String offSignSectionTime;

    public KQScheduleSignParamBuilder() {
      this.resourceid = resourceid;
      //初始化的时候需要把其他参数先清空下
      this.userType = "";
      this.signDate = "";
      this.signTime = "";
      this.belongdate = "";
      this.signtype = "";
      this.worksection = "";
      this.signDateTimeSql = "";
      this.signSectionTime = "";
      this.signSectionEndTime = "";
      this.signSectionBeginTime = "";
      this.offSignSectionTime = "";
    }

    //成员方法返回其自身，所以可以链式调用
    public KQScheduleSignBiz.KQScheduleSignParamBuilder resourceidParam(final String resourceid) {
      this.resourceid = resourceid;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder userTypeParam(final String userType) {
      this.userType = userType;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder signDateParam(final String signDate) {
      this.signDate = signDate;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder signTimeParam(final String signTime) {
      this.signTime = signTime;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder belongDateParam(final String belongdate) {
      this.belongdate = belongdate;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder signtypeParam(final String signtype) {
      this.signtype = signtype;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder worksectionParam(final String worksection) {
      this.worksection = worksection;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder signDateTimeSqlParam(final String signDateTimeSql) {
      this.signDateTimeSql = signDateTimeSql;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder signSectionTimeParam(final String signSectionTime) {
      this.signSectionTime = signSectionTime;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder signSectionEndTimeParam(final String signSectionEndTime) {
      this.signSectionEndTime = signSectionEndTime;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder signSectionBeginTimeParam(final String signSectionBeginTime) {
      this.signSectionBeginTime = signSectionBeginTime;
      return this;
    }

    public KQScheduleSignBiz.KQScheduleSignParamBuilder offSignSectionTimeParam(final String offSignSectionTime) {
      this.offSignSectionTime = offSignSectionTime;
      return this;
    }

    //Builder的build方法，返回外部类的实例
    public KQScheduleSignBiz build() {
      return new KQScheduleSignBiz(this);
    }

  }


}
