package com.engine.kq.biz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.service.task.impl.KQAutoCardTaskServiceImpl;
import com.cloudstore.dev.api.util.Util_Message;
import com.engine.kq.bean.KQHrmScheduleSign;
import com.engine.kq.entity.TimeScopeEntity;
import com.engine.kq.entity.TimeSignScopeEntity;
import com.engine.kq.entity.WorkTimeEntity;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.util.KQSignUtil;
import com.engine.kq.wfset.util.SplitActionUtil;
import com.engine.msgcenter.biz.ConfigManager;
import com.engine.msgcenter.biz.WeaMessageTypeConfig;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.dateformat.DateTransformer;
import weaver.dateformat.TimeZoneVar;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

/**
 * 自动打卡 给云桥提供接口
 */
public class KQAutoCheckBiz extends BaseBean {

  //是否开启日志
  public boolean isLog = false;
  public KQLog kqLog = new KQLog();
  private DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  private DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
  private DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  private DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm");
  private DateTimeFormatter fullTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
  public static String task_id = "hrm_auto_card_";

  public KQAutoCheckBiz() {
    KQConfigComInfo kqConfigComInfo = new KQConfigComInfo();
    isLog = "1".equalsIgnoreCase(Util.null2String(kqConfigComInfo.getValue("auto_card_log")));
  }

  public void autoLog(String info){
    if(isLog){
      kqLog.info(info);
    }
  }

  public Map<String,String> auto_punch_card(HttpServletRequest request,
      HttpServletResponse response, String json,String uuid) throws Exception{
    JSONObject jsonObject = JSON.parseObject(json);
    if(jsonObject != null && !jsonObject.isEmpty()){
      RecordSet rs = new RecordSet();
      String userid = Util.null2String(jsonObject.get("userid"));
      String json_task_id = Util.null2String(jsonObject.get("task_id"));
      String report_type = Util.null2String(jsonObject.get("report_type"));
      if(userid.length() > 0 && report_type.length() > 0){
        if(!(json_task_id.indexOf(task_id) > -1)){
          autoLog(userid+"::auto_punch_card　task_id　is not right:"+json_task_id+":uuid:"+uuid);
          return null;
        }
        if(!"1".equalsIgnoreCase(report_type) && !"2".equalsIgnoreCase(report_type) && !"3".equalsIgnoreCase(report_type)){
          autoLog(userid+"::auto_punch_card　report_type　is not right:"+report_type+":uuid:"+uuid);
          return null;
        }
        KQAutoCardTaskServiceImpl kqAutoCardTaskService = new KQAutoCardTaskServiceImpl();

        if(!kqAutoCardTaskService.show_auto_card_schedule()){
          //没有启用计划任务 report_type的不做打卡
          if("1".equalsIgnoreCase(report_type)){
            autoLog(userid+"::auto_punch_card　没有启用计划任务上报　report_type:"+report_type+" 不做打卡"+":uuid:"+uuid);
            return null;
          }

        }
        if(!jsonObject.containsKey("location_info")){
          autoLog(userid+"::auto_punch_card　location_info　is null"+":uuid:"+uuid);
          return null;
        }
        if(!jsonObject.containsKey("client_info")){
          autoLog(userid+"::auto_punch_card　client_info　is null"+":uuid:"+uuid);
          return null;
        }
        JSONObject location_info = (JSONObject) jsonObject.get("location_info");
        if(location_info == null || location_info.isEmpty()){
          autoLog(userid+"::auto_punch_card　client_info　isEmpty"+":uuid:"+uuid);
          return null;
        }
        String longitude = Util.null2String(location_info.get("longitude"));
        String latitude = Util.null2String(location_info.get("latitude"));
        String addr = Util.null2String(location_info.get("address"));
        String locating = Util.null2String(location_info.get("locating"));
        if(addr.length() == 0){
          //ios传的是locating作为地理位置的说明，安卓又是传的addr，没办法，只能这样兼容下了
          addr = locating;
        }

        JSONObject client_info = (JSONObject) jsonObject.get("client_info");
        if(client_info == null || client_info.isEmpty()){
          autoLog(userid+"::auto_punch_card　client_info　isEmpty"+":uuid:"+uuid);
          return null;
        }
        String SSID = Util.null2String(client_info.get("SSID"));
        String BSSID = Util.null2String(client_info.get("BSSID"));

        DateTimeFormatter fullFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String curdatetime = localDateTime.format(fullFormatter);
        String signTime =localDateTime.format(dateTimeFormatter);
        String signDate = localDateTime.format(dateFormatter);

        String belongdate = "";
        boolean isInRange = false;
        boolean can_punch_card = true;
        String today = DateUtil.getCurrentDate();//今天
        Map<String,Object> rangeInfoMap = Maps.newHashMap();
        KQConfigComInfo kqConfigComInfo = new KQConfigComInfo();
        String auto_card_cominfo = Util.null2String(kqConfigComInfo.getValue("auto_card_cominfo"),"0");
        autoLog(userid+"::auto_card_cominfo:::"+auto_card_cominfo+":uuid:"+uuid);
        if("1".equalsIgnoreCase(auto_card_cominfo)){
          //如果性能差再走缓存,1表示开启缓存
          getRangeInfoFromCominfo(userid,today,rangeInfoMap);
        }else{
          getRangeInfo(userid,today,rangeInfoMap);
        }
        autoLog("userid::"+userid+"::auto_punch_card:rangeInfoMap:"+rangeInfoMap+":uuid:"+uuid);
        String card_type = "";
        String beginSignScope = "";
        if(!rangeInfoMap.isEmpty()){
          isInRange = (boolean) rangeInfoMap.get("isInRange");
          can_punch_card = (boolean) rangeInfoMap.get("can_punch_card");
          belongdate = Util.null2String(rangeInfoMap.get("belongdate"));
          card_type = Util.null2String(rangeInfoMap.get("card_type"));
          beginSignScope = Util.null2String(rangeInfoMap.get("beginSignScope"));
        }
        if(!isInRange){
          autoLog("userid::"+userid+"::auto_punch_card　isInRange not in "+":uuid:"+uuid);
          return null;
        }
        if(!can_punch_card){
          autoLog("userid::"+userid+"::auto_punch_card　can_punch_card not in "+":uuid:"+uuid);
          return null;
        }

        String deviceInfo = "";
        if(!client_info.isEmpty()){
          deviceInfo = client_info.toJSONString();
        }

        //自动打开的时间是否在范围内
        boolean isInCom = true;
        boolean needLocationRange = false;
        boolean needWifiRange = false;
        boolean isLocationRange = false;
        boolean isWifiRange = false;
        KQGroupBiz kqGroupBiz = new KQGroupBiz();
        Map<String,Object> locationMap = kqGroupBiz.checkLocationScope(userid,longitude,latitude);
        String locationNeedCheck = Util.null2String(locationMap.get("needCheck"));
        String locationfacecheck = Util.null2String(locationMap.get("locationfacecheck"));
        boolean locationInScope = Boolean.parseBoolean(Util.null2String(locationMap.get("inScope")));
        if("1".equalsIgnoreCase(locationNeedCheck)){
          needLocationRange = true;
          if(locationInScope){
            String locationshowaddress = Util.null2String(locationMap.get("locationshowaddress"));
            //记录统一地址
            if(locationshowaddress.equals("1")) {
              Map<String,Object> loactionInfo = (Map<String, Object>) locationMap.get("loactionInfo");
              String rangename = Util.null2String(loactionInfo.get("locationname"));
              if(rangename.length() > 0){
                addr = rangename;
              }
            }
            isLocationRange = true;
          }
          if("1".equalsIgnoreCase(locationfacecheck)){
//            只要开启了任何一个人脸打卡，就不允许自动打卡了
            isInCom = false;
            autoLog("userid::"+userid+"::initAutoInfo:地理位置的人脸识别考勤开启了:所以把isInCom置位false"+":uuid:"+uuid);
          }
        }
        String wifiNeedCheck = "";
        Map<String,Object> wifiMap = kqGroupBiz.checkWifiScope(userid, SSID, BSSID);
        wifiNeedCheck = Util.null2String(wifiMap.get("needCheck"));
        String wififacecheck = Util.null2String(locationMap.get("wififacecheck"));
        boolean wifiInScope = Boolean.parseBoolean(Util.null2String(wifiMap.get("inScope")));
        if("1".equalsIgnoreCase(wifiNeedCheck)){
          needWifiRange = true;
          if(wifiInScope){
            isWifiRange = true;
          }
          if("1".equalsIgnoreCase(wififacecheck)){
//            只要开启了任何一个人脸打卡，就不允许自动打卡了
            isInCom = false;
            autoLog("userid::"+userid+"::initAutoInfo:wifi的人脸识别考勤开启了:所以把isInCom置位false"+":uuid:"+uuid);
          }
        }
        autoLog("userid::"+userid+"::auto_punch_card:needLocationRange:"+needLocationRange+":isLocationRange:"+isLocationRange+
            ":needWifiRange:"+needWifiRange+":isWifiRange:"+isWifiRange+":uuid:"+uuid);
        if(needLocationRange){
          if(isLocationRange){
          }else{
            if(needWifiRange){
              if(isWifiRange){
              }else{
                //地理位置开启，而且不在范围内，且开启wifi验证,不在范围内
                isInCom = false;
              }
            }else {
              //地理位置开启，而且不在范围内，且未开启wifi验证
              isInCom = false;
            }
          }
        }else{
          if(needWifiRange) {
            if (isWifiRange) {
            } else {
              //地理位置未开启，且开启wifi验证,不在范围内
              isInCom = false;
            }
          }
        }
        if(!isInCom){
          return null;
        }

        User tmp_user = new User(Util.getIntValue(userid));
        String userType = tmp_user.getLogintype();
        String signType = "";
        String clientAddress = Util.getIpAddr(request);
        String isInComs = "1";
        String signfrom = "auto_card";
        if(belongdate.length() == 0){
          belongdate = signDate;
        }

        String timeZone = Util.null2String(TimeZoneVar.getTimeZone(),"");
        //处理多时区
        String timeZoneConversion = Util.null2String(new weaver.general.BaseBean().getPropValue("weaver_timezone_conversion","timeZoneConversion")).trim();
        if("1".equals(timeZoneConversion)) {
          DateTransformer dateTransformer=new DateTransformer();
          String[] zone_localTime = dateTransformer.getLocaleDateAndTime(signDate,signTime);
          if(zone_localTime != null && zone_localTime.length == 2){
            signDate = zone_localTime[0];
            signTime = zone_localTime[1];
          }
        }        

        //需要增加下判断，同样时间是否已经有数据了
        boolean has_same_card = check_same_card(userid,signDate,card_type,beginSignScope);
        autoLog("userid::"+userid+"::auto_punch_card:has_same_card:"+has_same_card+":userid:"+userid+
            ":signDate:"+signDate+":signTime:"+signTime+":uuid:"+uuid);
        if(has_same_card){
          return null;
        }

        signType = "on".equals(card_type)?"1":"2";
        String punchSql = "insert into HrmScheduleSign(userId,userType,signType,signDate,signTime,clientAddress,isInCom,timeZone,belongdate,signfrom,longitude,latitude,addr,deviceInfo) "+
            " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        boolean isOk = rs.executeUpdate(punchSql,userid,userType,signType,signDate,signTime,clientAddress,isInComs,
            timeZone,belongdate,signfrom,longitude,latitude,addr,deviceInfo);
        //同步更新考勤数据到考勤报表
        new KQFormatBiz().formatDate(userid,belongdate);
        // 处理加班规则
        SplitActionUtil.pushOverTimeTasksAll(belongdate,belongdate,""+userid);
        autoLog("userid::"+userid+"::auto_punch_card:punchSql KQFormatBiz: isOk:"+isOk+":userid:"+userid+":belongdate:"+belongdate+":signDate:"+signDate+":signTime:"+signTime+":uuid:"+uuid);
        appNotification(userid,curdatetime,tmp_user.getLanguage());
        Map<String,String> result = Maps.newHashMap();
        if(isOk){
          String title = SystemEnv.getHtmlLabelName(519318, tmp_user.getLanguage())+"（"+curdatetime+"）";
          result.put("title",title);
          return result;
        }else{
          return null;
        }
      }
    }

    return null;
  }

  /**
   * 判断下同样时间是否已经有数据了
   * @param userid
   * @param signDate
   * @param card_type
   * @param beginSignScope
   * @return
   */
  public boolean check_same_card(String userid, String signDate, String card_type,
      String beginSignScope) {
    RecordSet rs = new RecordSet();
    String signtype = "on".equals(card_type)?"1":"2";
    StringBuffer sb = new StringBuffer();
    if(beginSignScope.length() > 0){
      String beginSignDateTime = beginSignScope+":00";
      if(rs.getDBType().equals("oracle")){
        sb.append(" signDate||' '||signTime>='").append(beginSignDateTime).append("' ");
      }else if(rs.getDBType().equals("mysql")){
        sb.append(" concat(signDate,' ',signTime)>='").append(beginSignDateTime).append("' ");
      }else{
        sb.append(" signDate+' '+signTime>='").append(beginSignDateTime).append("' ");
      }
    }
    String sql = "select 1 from HrmScheduleSign where userid = ? and signtype=? ";
    if(sb.toString().length() > 0){
      sql += " and "+sb.toString();
    }
    rs.executeQuery(sql, userid,signtype);
    if(rs.next()){
      return true;
    }
    return false;
  }

  /**
   * 实时获取当前的班次获取到需要的区间
   * @param userid
   * @param today
   * @param rangeInfoMap
   */
  public void getRangeInfo(String userid, String today, Map<String, Object> rangeInfoMap) throws Exception{
    boolean isInRange = false;
    boolean can_punch_card = true;
    //允许打卡的时间范围 用于判断是否已经打过卡，如果打过卡就不需要打了
    String beginSignScope = "";
    String endSignScope = "";
    //打卡类型，签到还是签退卡  on是签到卡，off是签退卡
    String card_type = "";
    //是否班次设置了打卡归属，设置了的话，签到和签退是独立的，只要查询到有一条数据就表示打卡过了，如果没设置的话，签退是需要有签到才可以签退的
    boolean hasSetSignScope = false;
    String locationcheck = "";
    String wificheck = "";
    String locationfacecheck = "";
    String wififacecheck = "";
    String belongdate = "";

    String preDay = DateUtil.addDate(today, -1);//昨天
    String nextDay = DateUtil.addDate(today, 1);//明天

    List<Map<String,String>> auto_list_all = Lists.newArrayList();
    List<Map<String,String>> auto_list = Lists.newArrayList();
    initAutoInfo(userid,today,auto_list);
    if(!auto_list.isEmpty()){
      auto_list_all.addAll(auto_list);
    }
    auto_list = Lists.newArrayList();
    initAutoInfo(userid,preDay,auto_list);
    if(!auto_list.isEmpty()){
      auto_list_all.addAll(auto_list);
    }
    auto_list = Lists.newArrayList();
    initAutoInfo(userid,nextDay,auto_list);
    if(!auto_list.isEmpty()){
      auto_list_all.addAll(auto_list);
    }
    LocalTime now_time = LocalTime.now();
    if(!auto_list_all.isEmpty()){
      JSONArray jsonArray = new JSONArray();
      for(Map<String,String> auto_map : auto_list_all){
        String starttime_key = today+"_task_starttime";
        String task_endtime_key = today+"_task_endtime";
        if(auto_map.containsKey(starttime_key) && auto_map.containsKey(task_endtime_key)){

          String task_starttime = auto_map.get(starttime_key);
          String task_endtime = auto_map.get(task_endtime_key);
          String task_beginSignScope = auto_map.get("beginSignScope");
          String task_endSignScope = auto_map.get("endSignScope");
          String task_hasSetSignScope = auto_map.get("hasSetSignScope");
          String task_card_type = auto_map.get("card_type");
          String task_locationcheck = auto_map.get("locationcheck");
          String task_wificheck = auto_map.get("wificheck");
          String task_locationfacecheck = auto_map.get("locationfacecheck");
          String task_wififacecheck = auto_map.get("wififacecheck");
          String task_belongdate = auto_map.get("belongdate");

          task_starttime = task_starttime +":00";
          task_endtime = task_endtime +":00";
          LocalTime local_task_starttime = LocalTime.parse(task_starttime,fullTimeFormatter);
          LocalTime local_task_endtime = LocalTime.parse(task_endtime,fullTimeFormatter);
          if(!local_task_starttime.isAfter(now_time) && !local_task_endtime.isBefore(now_time)){
            isInRange = true;
            beginSignScope = Util.null2String(task_beginSignScope);
            endSignScope = Util.null2String(task_endSignScope);
            hasSetSignScope = "1".equalsIgnoreCase(Util.null2String(task_hasSetSignScope));
            card_type = Util.null2String(task_card_type);
            locationcheck = Util.null2String(task_locationcheck);
            wificheck = Util.null2String(task_wificheck);
            locationfacecheck = Util.null2String(task_locationfacecheck);
            wififacecheck = Util.null2String(task_wififacecheck);
            belongdate = task_belongdate;

            if(beginSignScope.length() > 0 && endSignScope.length() > 0){
              can_punch_card = can_punch_card(userid,beginSignScope,endSignScope,card_type,hasSetSignScope);
            }
            break;
          }
        }
      }
    }
    rangeInfoMap.put("isInRange", isInRange);
    rangeInfoMap.put("can_punch_card", can_punch_card);
    rangeInfoMap.put("beginSignScope", beginSignScope);
    rangeInfoMap.put("endSignScope", endSignScope);
    rangeInfoMap.put("card_type", card_type);
    rangeInfoMap.put("hasSetSignScope", hasSetSignScope);
    rangeInfoMap.put("locationcheck",locationcheck);
    rangeInfoMap.put("wificheck",wificheck);
    rangeInfoMap.put("locationfacecheck",locationfacecheck);
    rangeInfoMap.put("wififacecheck",wififacecheck);
    rangeInfoMap.put("belongdate",belongdate);
  }

  /**
   * 从缓存里获取数据
   * @param userid
   * @param today
   * @param rangeInfoMap
   */
  public void getRangeInfoFromCominfo(String userid, String today,
      Map<String, Object> rangeInfoMap) throws Exception{
    boolean isInRange = false;
    boolean can_punch_card = true;
    //允许打卡的时间范围 用于判断是否已经打过卡，如果打过卡就不需要打了
    String beginSignScope = "";
    String endSignScope = "";
    //打卡类型，签到还是签退卡  on是签到卡，off是签退卡
    String card_type = "";
    //是否班次设置了打卡归属，设置了的话，签到和签退是独立的，只要查询到有一条数据就表示打卡过了，如果没设置的话，签退是需要有签到才可以签退的
    boolean hasSetSignScope = false;

    KQAutoTimeRangeComInfo kqAutoTimeRangeComInfo = new KQAutoTimeRangeComInfo();
    String user_kq_date = userid+"_"+today;
    String kq_param = Util.null2s(kqAutoTimeRangeComInfo.getKq_param(user_kq_date),"");
    autoLog("auto_punch_card　kq_param　:"+kq_param);
    LocalTime now_time = LocalTime.now();
    if(kq_param.length() > 0){
      JSONArray jsonArray = JSON.parseArray(kq_param);
      if(jsonArray !=  null && !jsonArray.isEmpty()){
        for(int i = 0 ; i < jsonArray.size() ; i++){
          JSONObject otherparam = (JSONObject) jsonArray.get(i);
          String task_starttime = Util.null2String(otherparam.get("task_starttime"));
          String task_endtime = Util.null2String(otherparam.get("task_endtime"));
          task_starttime = task_starttime +":00";
          task_endtime = task_endtime +":00";
          LocalTime local_task_starttime = LocalTime.parse(task_starttime,fullTimeFormatter);
          LocalTime local_task_endtime = LocalTime.parse(task_endtime,fullTimeFormatter);
          if(!local_task_starttime.isAfter(now_time) && !local_task_endtime.isBefore(now_time)){
            isInRange = true;
            beginSignScope = Util.null2String(otherparam.get("beginSignScope"));
            endSignScope = Util.null2String(otherparam.get("endSignScope"));
            hasSetSignScope = "1".equalsIgnoreCase(Util.null2String(otherparam.get("hasSetSignScope")));
            card_type = Util.null2String(otherparam.get("card_type"));

            if(beginSignScope.length() > 0 && endSignScope.length() > 0){
              can_punch_card = can_punch_card(userid,beginSignScope,endSignScope,card_type,hasSetSignScope);
            }
            break;
          }

        }
      }
    }
    rangeInfoMap.put("isInRange", isInRange);
    rangeInfoMap.put("can_punch_card", can_punch_card);
    rangeInfoMap.put("beginSignScope", beginSignScope);
    rangeInfoMap.put("endSignScope", endSignScope);
    rangeInfoMap.put("card_type", card_type);
    rangeInfoMap.put("hasSetSignScope", hasSetSignScope);
  }

  public void appNotification(String resourceId, String curdatetime, int language) {
    try {
      String detailTitle = "528466";
      String title = SystemEnv.getHtmlLabelName(519318, language)+"（"+curdatetime+"）";
      String content = "";
      int createrId = 1;
      String pcUrl = "";
      String mobileUrl = "/spa/hrm/static4mobile/index.html#/sign";
      ConfigManager configManager = new ConfigManager();
      // 2、默认规则检查用户配置,返回后台自定义消息类型和对应通过配置的用户
      //    @param singPath 与需求变更之前的path参数含义一致,代表该消息类型详细配置中唯一标志值,各模块参考自己的值(有疑问请联系云商店-田泽法)
      //    @param userid 按默认规则检查的用户配置信息(检查多人参考重载方法)
      Map<WeaMessageTypeConfig, List<String>> accessConfig = configManager.defaultRuleCheckConfig(
          MessageType.HRM_AUTOCARD_REMIND, resourceId, null);

      // 3、遍历自定义消息类型集合
      for (Map.Entry<WeaMessageTypeConfig, List<String>> entry : accessConfig.entrySet()) {
        // 4、构造消息实体
        MessageBean bean = Util_Message
            .createMessage(MessageType.HRM_AUTOCARD_REMIND, 0, title, detailTitle, content, pcUrl, mobileUrl, createrId);

        // 5、获取新的自定义消息类型
        WeaMessageTypeConfig config = entry.getKey();

        // 6、新的自定义消息类型相关信息通知到消息实体bean上
        bean.setMessageConfig(config);

        // 7、设置检查配置通过需要发送消息的用户
        bean.setUserList(Sets.newHashSet(entry.getValue()));

        // 8、发送消息
        autoLog("appNotification:bean:"+ JSON.toJSONString(bean));
        Util_Message.sendAndpublishMessage(bean);
      }
    } catch (Exception e) {
      StringWriter errorsWriter = new StringWriter();
      e.printStackTrace(new PrintWriter(errorsWriter));
    }
  }

  /**
   * 判断指定人员是否已经打卡了
   * @param userid
   * @param beginSignScope
   * @param endSignScope
   * @param card_type
   * @param hasSetSignScope
   * @return
   *  false 表示已经打卡了。不能再继续搞了
   */
  public boolean can_punch_card(String userid, String beginSignScope, String endSignScope,
      String card_type,boolean hasSetSignScope) throws Exception{
    boolean can_punch_card = true;

    String buildSql = KQSignUtil.buildSignSql(beginSignScope, endSignScope);
    autoLog("can_punch_card:buildSql:"+buildSql);
    KQScheduleSignBiz kqScheduleSignBiz = new KQScheduleSignBiz.KQScheduleSignParamBuilder().resourceidParam(userid).
        signDateTimeSqlParam(buildSql).build();
    Map<String, KQHrmScheduleSign> signMap = kqScheduleSignBiz.getScheduleSignInfo();
    autoLog("can_punch_card:signMap:"+signMap);
    if(signMap != null && !signMap.isEmpty()){
      if("on".equalsIgnoreCase(card_type)){
        if(signMap.size() >= 1){
          //说明有签到了。不需要再签到了
          can_punch_card = false;
        }
      }
      if("off".equalsIgnoreCase(card_type)){
        if(hasSetSignScope){
          //如果设置了打卡归属 有一条打卡都算签退卡
          if(signMap.size() >= 1){
            //说明有签退了。不需要再签退了
            can_punch_card = false;
          }
        }else{
          if(signMap.size() > 1){
            //超过一条打卡 说明有签退了。不需要再签退了
            can_punch_card = false;
          }
        }
      }
    }
    autoLog("can_punch_card:can_punch_card:"+can_punch_card);
    return can_punch_card;
  }

  /**
   * 根据后台的考勤组获取到需要处理的自动打卡的考勤组信息
   * @param resourceid
   * @param date
   * @param auto_list
   */
  public void initAutoInfo(String resourceid, String date,List<Map<String,String>> auto_list) throws Exception{
    autoLog("initAutoInfo:resourceid:"+resourceid+":date:"+date);
    KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
    KQWorkTime kqWorkTime = new KQWorkTime();
    WorkTimeEntity workTime = kqWorkTime.getWorkTime(resourceid, date);
    List<TimeScopeEntity> lsSignTime = new ArrayList<>();
    List<TimeScopeEntity> lsWorkTime = new ArrayList<>();
    lsSignTime = workTime.getSignTime();//允许打卡时间
    lsWorkTime = workTime.getWorkTime();//工作时间
    String groupId = workTime.getGroupId();
    String auto_checkin = kqGroupComInfo.getAuto_checkin(groupId);
    String auto_checkout = kqGroupComInfo.getAuto_checkout(groupId);
    autoLog("initAutoInfo:groupId:"+groupId+":auto_checkin:"+auto_checkin+":auto_checkout:"+auto_checkout);
    if(!"1".equalsIgnoreCase(auto_checkin) && !"1".equalsIgnoreCase(auto_checkout)){
      //如果上班自动打卡没开启，下班自动打卡也没开启，直接不要搞了
      autoLog("initAutoInfo:如果上班自动打卡没开启，下班自动打卡也没开启，直接不要搞了:");
      return ;
    }
    //判断下个人自动打卡是否开启了
    String self_auto_checkin = "";
    String self_auto_checkout = "";
    Map<String,String> selfMap = isSelfAutoCard(resourceid);
    if(!selfMap.isEmpty()){
      self_auto_checkin = Util.null2String(selfMap.get("auto_checkin"));
      self_auto_checkout = Util.null2String(selfMap.get("auto_checkout"));
      if("0".equalsIgnoreCase(self_auto_checkin) && "0".equalsIgnoreCase(self_auto_checkout)){
        autoLog("initAutoInfo:isSelfAutoCard:用户自定义自动打卡给关闭了");
        return ;
      }
      autoLog("initAutoInfo:isSelfAutoCard:self_auto_checkin:"+self_auto_checkin+":self_auto_checkout:"+self_auto_checkout);
    }

    String auto_checkin_before = Util.null2String(kqGroupComInfo.getAuto_checkin_before(groupId));
    String auto_checkin_after = Util.null2String(kqGroupComInfo.getAuto_checkin_after(groupId));
    String locationcheck = Util.null2String(kqGroupComInfo.getLocationcheck(groupId));
    String wificheck = Util.null2String(kqGroupComInfo.getWificheck(groupId));
    String locationfacecheck = Util.null2String(kqGroupComInfo.getLocationfacecheck(groupId));
    String wififacecheck = Util.null2String(kqGroupComInfo.getWififacecheck(groupId));
    if(!"1".equalsIgnoreCase(locationcheck) && !"1".equalsIgnoreCase(wificheck)){
      //如果wifi和地理位置检测都没有开启
      autoLog("initAutoInfo:locationcheck:wificheck都没开启");
      return ;
    }
    if("1".equalsIgnoreCase(locationcheck)){
      if("1".equalsIgnoreCase(locationfacecheck)){
        //wifi和地理位置的人脸识别任何一个开启了。那自动打卡就没效果了
        autoLog("initAutoInfo:wifi和地理位置的人脸识别考勤也都开启了。那就没效果了自动打卡");
        return;
      }else{
        if("1".equalsIgnoreCase(wificheck)){
          if("1".equalsIgnoreCase(wififacecheck)){
            //wifi和地理位置的人脸识别任何一个开启了。那自动打卡就没效果了
            autoLog("initAutoInfo:wifi的人脸识别考勤开启了。那就没效果了自动打卡");
            return ;
          }
        }
      }
    }else{
      if("1".equalsIgnoreCase(wificheck)){
        //如果只开启了wifi考勤
        if("1".equalsIgnoreCase(wififacecheck)){
          autoLog("initAutoInfo:wifi的人脸识别考勤开启了。那就没效果了自动打卡");
          return ;
        }
      }
    }

    int auto_checkin_before_min = Util.getIntValue(auto_checkin_before);
    int auto_checkin_after_min = Util.getIntValue(auto_checkin_after);

    String auto_checkout_before = Util.null2String(kqGroupComInfo.getAuto_checkout_before(groupId));
    String auto_checkout_after = Util.null2String(kqGroupComInfo.getAuto_checkout_after(groupId));
    int auto_checkout_before_min = Util.getIntValue(auto_checkout_before);
    int auto_checkout_after_min = Util.getIntValue(auto_checkout_after);
    autoLog("initAutoInfo:auto_checkin_before:"+auto_checkin_before+":auto_checkin_after:"+auto_checkin_after+
        ":auto_checkout_before:"+auto_checkout_before+":auto_checkout_after:"+auto_checkout_after);

    if(lsWorkTime != null && !lsWorkTime.isEmpty()){
      for (int i = 0; lsWorkTime != null && i < lsWorkTime.size(); i++) {
        TimeScopeEntity signTimeScope = lsSignTime.get(i);
        TimeScopeEntity workTimeScope = lsWorkTime.get(i);

        if("1".equalsIgnoreCase(auto_checkin)){
          if("0".equalsIgnoreCase(self_auto_checkin)){
            //个人没开启的话，就不处理
            autoLog("initAutoInfo:个人没开启的话:上班打卡:"+self_auto_checkin);
          }else{
            if(auto_checkin_before_min > 0 && auto_checkin_after_min >= 0){
              do_auto_checkin(auto_checkin_before_min,auto_checkin_after_min,auto_list,workTimeScope,signTimeScope,date,resourceid);
            }
          }
        }

        if("1".equalsIgnoreCase(auto_checkout)){
          if("0".equalsIgnoreCase(self_auto_checkout)){
            //个人没开启的话，就不处理
            autoLog("initAutoInfo:个人没开启的话:下班打卡:"+self_auto_checkout);
          }else{
            if(auto_checkout_before_min >= 0 && auto_checkout_after_min >= 0){
              do_auto_checkout(auto_checkout_before_min,auto_checkout_after_min,auto_list,workTimeScope,signTimeScope,date,resourceid);
            }
          }
        }
      }
    }

    if(!auto_list.isEmpty()){
      for(Map<String,String> auto_map : auto_list){
        auto_map.put("locationcheck",locationcheck);
        auto_map.put("wificheck",wificheck);
        auto_map.put("locationfacecheck",locationfacecheck);
        auto_map.put("wififacecheck",wififacecheck);
      }
    }
    autoLog("initAutoInfo:auto_list:"+auto_list);
  }

  /**
   * 判断个人自动打卡是否开启了
   * @param resourceid
   */
  public Map<String,String> isSelfAutoCard(String resourceid) {
    Map<String,String> selfMap = Maps.newHashMap();
    RecordSet rs = new RecordSet();
    String auto_checkin = "";
    String auto_checkout = "";
    String sql = " select * from kq_autocheck_set where resourceid=?  ";
    rs.executeQuery(sql, resourceid);
    if (rs.next()){
      auto_checkin = rs.getString("auto_checkin");
      auto_checkout = rs.getString("auto_checkout");
      selfMap.put("auto_checkin", auto_checkin);
      selfMap.put("auto_checkout", auto_checkout);
    }
    return selfMap;
  }

  public void do_auto_checkout(int auto_checkout_before_min, int auto_checkout_after_min,
      List<Map<String, String>> auto_list, TimeScopeEntity workTimeScope,
      TimeScopeEntity signTimeScope, String date, String resourceid) throws Exception{
    Map<String,String> auto_map = Maps.newHashMap();
    //昨日的跨天的上报数据
    Map<String,String> pre_auto_map = Maps.newHashMap();
    //明日的跨天的上报数据
    Map<String,String> next_auto_map = Maps.newHashMap();
    LocalDate localDate = LocalDate.parse(date,dateFormatter);
    LocalDate next_localDate = localDate.plusDays(1);
    LocalDate pre_localDate = localDate.minusDays(1);

    KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();

    String signBeginTime = signTimeScope.getBeginTime();
    LocalTime localTime = LocalTime.parse(signBeginTime, timeFormatter);
    String signEndTime = signTimeScope.getEndTime();
    String beginSignScope = "";
    String endSignScope = localDate.format(dateFormatter)+" "+signEndTime;
    int signEndIdx = kqTimesArrayComInfo.getArrayindexByTimes(signEndTime);
    boolean signEndTimeAcross = signTimeScope.getEndTimeAcross();
    if(signEndTimeAcross){
      endSignScope = next_localDate.format(dateFormatter)+" "+signEndTime;
      signEndIdx = kqTimesArrayComInfo.turn24to48TimeIndex(signEndIdx);
    }
    int signBeginIdx = kqTimesArrayComInfo.getArrayindexByTimes(signBeginTime);
    //上班允许打卡是否跨天，跨到第二天
    boolean signBenginTimeAcross = signTimeScope.getBeginTimeAcross();
    //上班允许打卡是否跨到前一天
    boolean beginTimePreAcross = signTimeScope.isBeginTimePreAcross();

    TimeSignScopeEntity timeSignScopeEntity = signTimeScope.getTimeSignScopeEntity();
    boolean hasSetSignScope = false;
    String endTimeStart = "";
    boolean endTimeStartAcross = false;

    if(timeSignScopeEntity == null){
      //没有设置 签到最晚时间和签退最早时间
      beginSignScope = localDate.format(dateFormatter)+" "+localTime.plusMinutes(1).format(timeFormatter);
      if(signBenginTimeAcross){
        beginSignScope = next_localDate.format(dateFormatter)+" "+signBeginTime;
        signBeginIdx = kqTimesArrayComInfo.turn24to48TimeIndex(signBeginIdx);
      }
      if(beginTimePreAcross){
        beginSignScope = pre_localDate.format(dateFormatter)+" "+signBeginTime;
      }
    }else{
      hasSetSignScope = true;
      endTimeStart = timeSignScopeEntity.getEndTimeStart();
      endTimeStartAcross = timeSignScopeEntity.isBeginTimeEndAcross();
      beginSignScope = localDate.format(dateFormatter)+" "+endTimeStart;
      if(endTimeStartAcross){
        beginSignScope = next_localDate.format(dateFormatter)+" "+endTimeStart;
        endTimeStart = kqTimesArrayComInfo.turn24to48Time(endTimeStart);
      }
    }

    String workEndTime = Util.null2String(workTimeScope.getEndTime());
    int workEndIdx = kqTimesArrayComInfo.getArrayindexByTimes(workEndTime);
    boolean workEndTimeAcross = workTimeScope.getEndTimeAcross();

    autoLog("do_auto_checkout:workEndTimeAcross:"+workEndTimeAcross+":workEndTime:"+workEndTime+
        ":signEndTime:"+signEndTime+":signEndTimeAcross:"+signEndTimeAcross);
    //处理下班自动打卡
    if(workEndTimeAcross){
      //下班时间跨天的话，意味着今天没有需要自动打卡的
      //如果下班时间跨天，因为下班打卡都是往后推的，所以只有一种可能，跨天
      int before_workEndIdx = workEndIdx + auto_checkout_before_min;
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signEndIdx < before_workEndIdx){
        before_workEndIdx = signEndIdx;
      }
      next_auto_map.put(next_localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(kqTimesArrayComInfo.turn48to24TimeIndex(before_workEndIdx)));

      //下面开始处理下班后自动打卡
      int after_workEndIdx = workEndIdx + auto_checkout_after_min;
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signEndIdx < after_workEndIdx){
        after_workEndIdx = signEndIdx;
      }
      next_auto_map.put(next_localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(kqTimesArrayComInfo.turn48to24TimeIndex(after_workEndIdx)));

      next_auto_map.put("endSignScope", endSignScope);
      next_auto_map.put("beginSignScope", beginSignScope);
      next_auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
      next_auto_map.put("card_type", "off");
      next_auto_map.put("belongdate", date);
      auto_list.add(next_auto_map);
    }else{
      //如果下班时间不跨天，因为下班打卡都是往后推的，所以只有两种可能，或者跨天或者不跨天
      int before_workEndIdx = workEndIdx + auto_checkout_before_min;
      if(before_workEndIdx >= 1440){
        before_workEndIdx = before_workEndIdx - 1440;
      }
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signEndIdx < before_workEndIdx){
        before_workEndIdx = signEndIdx;
      }
      //如果自动打卡时间比下班时间还小，说明自动打卡跨天了
      if(before_workEndIdx < workEndIdx){
        next_auto_map.put(next_localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(before_workEndIdx));
      }else{
        auto_map.put(localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(before_workEndIdx));
      }

      //下面开始处理下班后自动打卡
      int after_workEndIdx = workEndIdx + auto_checkout_after_min;
      if(after_workEndIdx >= 1440){
        after_workEndIdx = after_workEndIdx - 1440;
      }
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signEndIdx < after_workEndIdx){
        after_workEndIdx = signEndIdx;
      }
      if(after_workEndIdx < workEndIdx){
        next_auto_map.put(next_localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(after_workEndIdx));
        if(!next_auto_map.containsKey(next_localDate.format(dateFormatter)+"_task_starttime")){
          next_auto_map.put(next_localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(0));
        }
        if(auto_map.containsKey(localDate.format(dateFormatter)+"_task_starttime")){
          auto_map.put(localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(1439));
        }
      }else{
        auto_map.put(localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(after_workEndIdx));
      }
      boolean can_punch_card = can_punch_card(resourceid,beginSignScope,endSignScope,"off",hasSetSignScope);
      if(!can_punch_card){
        autoLog("do_auto_checkout:beginSignScope:"+beginSignScope+":endSignScope:"+endSignScope+
            ":hasSetSignScope:"+hasSetSignScope+":can_punch_card:"+can_punch_card);
        return ;
      }
      auto_map.put("endSignScope", endSignScope);
      auto_map.put("beginSignScope", beginSignScope);
      auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
      auto_map.put("card_type", "off");
      auto_map.put("belongdate", date);
      auto_list.add(auto_map);
      if(!next_auto_map.isEmpty()){
        next_auto_map.put("endSignScope", endSignScope);
        next_auto_map.put("beginSignScope", beginSignScope);
        next_auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
        next_auto_map.put("card_type", "off");
        next_auto_map.put("belongdate", date);
        auto_list.add(next_auto_map);
      }
    }
  }

  /**
   * 获取需要打上班卡的时间
   * @param auto_checkin_before_min
   * @param auto_checkin_after_min
   * @param auto_list
   * @param workTimeScope
   * @param signTimeScope
   * @param date
   * @param resourceid
   */
  public void do_auto_checkin(int auto_checkin_before_min, int auto_checkin_after_min,
      List<Map<String, String>> auto_list, TimeScopeEntity workTimeScope,
      TimeScopeEntity signTimeScope, String date, String resourceid) throws Exception{

    Map<String,String> auto_map = Maps.newHashMap();
    //昨日的跨天的上报数据
    Map<String,String> pre_auto_map = Maps.newHashMap();
    //明日的跨天的上报数据
    Map<String,String> next_auto_map = Maps.newHashMap();

    LocalDate localDate = LocalDate.parse(date,dateFormatter);
    LocalDate next_localDate = localDate.plusDays(1);
    LocalDate pre_localDate = localDate.minusDays(1);
    KQTimesArrayComInfo kqTimesArrayComInfo = new KQTimesArrayComInfo();
    String signBeginTime = signTimeScope.getBeginTime();
    String signEndTime = signTimeScope.getEndTime();

    String beginSignScope = localDate.format(dateFormatter)+" "+signBeginTime;
    String endSignScope = localDate.format(dateFormatter)+" "+signEndTime;
    int signBeginIdx = kqTimesArrayComInfo.getArrayindexByTimes(signBeginTime);
    //上班允许打卡是否跨天，跨到第二天
    boolean signBenginTimeAcross = signTimeScope.getBeginTimeAcross();
    //上班允许打卡是否跨到前一天
    boolean beginTimePreAcross = signTimeScope.isBeginTimePreAcross();
    if(signBenginTimeAcross){
      beginSignScope = next_localDate.format(dateFormatter)+" "+signBeginTime;
      signBeginIdx = kqTimesArrayComInfo.turn24to48TimeIndex(signBeginIdx);
    }
    if(beginTimePreAcross){
      beginSignScope = pre_localDate.format(dateFormatter)+" "+signBeginTime;
    }
    int signEndIdx = kqTimesArrayComInfo.getArrayindexByTimes(signEndTime);
    boolean signEndTimeAcross = signTimeScope.getEndTimeAcross();
    if(signEndTimeAcross){
      endSignScope = next_localDate.format(dateFormatter)+" "+signEndTime;
      signEndIdx = kqTimesArrayComInfo.turn24to48TimeIndex(signEndIdx);
    }
    TimeSignScopeEntity timeSignScopeEntity = signTimeScope.getTimeSignScopeEntity();
    boolean hasSetSignScope = false;
    String beginTimeEnd = "";
    boolean beginTimeEndAcross = false;

    if(timeSignScopeEntity == null){
      //没有设置 签到最晚时间和签退最早时间
    }else{
      hasSetSignScope = true;
      beginTimeEnd = timeSignScopeEntity.getBeginTimeEnd();
      beginTimeEndAcross = timeSignScopeEntity.isBeginTimeEndAcross();
      endSignScope = localDate.format(dateFormatter)+" "+beginTimeEnd;
      if(beginTimeEndAcross){
        endSignScope = next_localDate.format(dateFormatter)+" "+beginTimeEnd;
        beginTimeEnd = kqTimesArrayComInfo.turn24to48Time(beginTimeEnd);
      }
    }

    String workBeginTime = Util.null2String(workTimeScope.getBeginTime());
    int workBeginIdx = kqTimesArrayComInfo.getArrayindexByTimes(workBeginTime);
    boolean workBenginTimeAcross = workTimeScope.getBeginTimeAcross();

    autoLog("do_auto_checkin:workBenginTimeAcross:"+workBenginTimeAcross+":workBeginTime:"+workBeginTime+
        ":signBeginTime:"+signBeginTime+":signBenginTimeAcross:"+signBenginTimeAcross+":beginTimePreAcross:"+beginTimePreAcross);
    //先处理上班的
    if(workBenginTimeAcross){

      //如果上班时间跨天，那只有一种可能，就是一天多次打卡，然后上班时间跨到第二天去了
      int before_workBeginIdx = workBeginIdx - auto_checkin_before_min;
      if(before_workBeginIdx < 0){
        before_workBeginIdx = 1440-(auto_checkin_before_min-(workBeginIdx-0));
      }
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signBeginIdx > before_workBeginIdx){
        before_workBeginIdx = signBeginIdx;
      }
      //上班时间和自动上班前打卡时间混合完之后，如果自动打卡的时间还是比上班时间小，表示这俩时间还是在同一天，都是跨天的，否则就是不跨天了的
      if(before_workBeginIdx <= workBeginIdx){
        next_auto_map.put(next_localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(before_workBeginIdx));
      }else{
        auto_map.put(localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(before_workBeginIdx));
        auto_map.put(localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(1439));
      }
      //下面开始处理上班后自动打卡
      int after_workBeginIdx = workBeginIdx + auto_checkin_after_min;
      if(hasSetSignScope){
        if(beginTimeEnd.length() > 0){
          int beginTimeEndIdx = kqTimesArrayComInfo.getArrayindexByTimes(beginTimeEnd);
          if(after_workBeginIdx > beginTimeEndIdx){
            after_workBeginIdx = beginTimeEndIdx;
          }
        }
      }
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signEndIdx < after_workBeginIdx){
        after_workBeginIdx = signEndIdx;
      }
      next_auto_map.put(next_localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(after_workBeginIdx));
      if(!next_auto_map.containsKey(next_localDate.format(dateFormatter)+"_task_starttime")){
        next_auto_map.put(next_localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(0));
      }

      auto_map.put("endSignScope", endSignScope);
      auto_map.put("beginSignScope", beginSignScope);
      auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
      auto_map.put("card_type", "on");
      auto_map.put("belongdate", date);
      auto_list.add(auto_map);

      if(!next_auto_map.isEmpty()){
        next_auto_map.put("endSignScope", endSignScope);
        next_auto_map.put("beginSignScope", beginSignScope);
        next_auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
        next_auto_map.put("card_type", "on");
        next_auto_map.put("belongdate", date);
        auto_list.add(next_auto_map);
      }
    }else{
      //如果上班时间不跨天，需要考虑上班时间向前或者向后推算的自动打卡时间可能是跨天的
      int before_workBeginIdx = workBeginIdx - auto_checkin_before_min;
      if(before_workBeginIdx < 0){
        before_workBeginIdx = 1440-(auto_checkin_before_min-(workBeginIdx-0));
      }
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signBeginIdx > before_workBeginIdx){
        before_workBeginIdx = signBeginIdx;
      }
      //上班时间和自动上班前打卡时间混合完之后，如果自动打卡的时间还是比上班时间小，表示这俩时间还是在同一天，否则就是自动打卡的再前一天了
      if(before_workBeginIdx <= workBeginIdx){
        auto_map.put(localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(before_workBeginIdx));
      }else{
        pre_auto_map.put(pre_localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(before_workBeginIdx));
        pre_auto_map.put(pre_localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(1439));
        auto_map.put(localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(0));
      }
      int after_workBeginIdx = workBeginIdx + auto_checkin_after_min;
      if(hasSetSignScope){
        if(beginTimeEnd.length() > 0){
          int beginTimeEndIdx = kqTimesArrayComInfo.getArrayindexByTimes(beginTimeEnd);
          if(after_workBeginIdx > beginTimeEndIdx){
            after_workBeginIdx = beginTimeEndIdx;
          }
        }
      }
      //自动打卡时间还是要和允许打卡时间进行比较下
      if(signEndIdx < after_workBeginIdx){
        after_workBeginIdx = signEndIdx;
      }

      if(after_workBeginIdx < workBeginIdx){
        //这表示自动打卡的时间跨天了
        next_auto_map.put(next_localDate.format(dateFormatter)+"_task_starttime", kqTimesArrayComInfo.getTimesByArrayindex(0));
        next_auto_map.put(next_localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(after_workBeginIdx));

        auto_map.put(localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(1439));
      }else{
        auto_map.put(localDate.format(dateFormatter)+"_task_endtime", kqTimesArrayComInfo.getTimesByArrayindex(after_workBeginIdx));
      }
      boolean can_punch_card = can_punch_card(resourceid,beginSignScope,endSignScope,"on",hasSetSignScope);
      if(!can_punch_card){
        return ;
      }
      auto_map.put("endSignScope", endSignScope);
      auto_map.put("beginSignScope", beginSignScope);
      auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
      auto_map.put("card_type", "on");
      auto_map.put("belongdate", date);
      auto_list.add(auto_map);

      if(!next_auto_map.isEmpty()){
        next_auto_map.put("endSignScope", endSignScope);
        next_auto_map.put("beginSignScope", beginSignScope);
        next_auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
        next_auto_map.put("card_type", "on");
        next_auto_map.put("belongdate", date);
        auto_list.add(next_auto_map);
      }

      if(!pre_auto_map.isEmpty()){
        pre_auto_map.put("endSignScope", endSignScope);
        pre_auto_map.put("beginSignScope", beginSignScope);
        pre_auto_map.put("hasSetSignScope", hasSetSignScope?"1":"0");
        pre_auto_map.put("card_type", "on");
        pre_auto_map.put("belongdate", date);
        auto_list.add(pre_auto_map);
      }

    }
  }

}