package com.engine.kq.util;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author ml
 * @Date 2023/4/24
 * @Description 新创元考勤模块
 * @Since version-1.0
 */
public class UtilKQXcy extends BaseBean {
    // 补卡流程
    private static final String bkWorkFlowMainTable = new BaseBean().getPropValue("xcy_kq", "xcy_kq_bk_mainTab");
    private static final String bkWorkFlowDetTable = new BaseBean().getPropValue("xcy_kq", "xcy_kq_bk_detailTab");

    /**
     * 获取人员离职日期
     * @return String
     */
    public String getCompanyEndDate (RecordSet rs,String resourceId) {
        String sql = "select changedate from hrmstatushistory a where TYPE_N=5 and RESOURCEID=?";
        rs.executeQuery(sql,resourceId);
        if (rs.next()) {
            return Util.null2String(rs.getString("changedate"));
        }
        return "";
    }

    /**
     * 判断日期是否在选择日期范围内
     * @return boolean
     */
    public boolean isRangeOfDate (String begDate, String endDate, String targetDate) {

        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date1 = simpleDateFormat.parse(begDate);
            Date date2 = simpleDateFormat.parse(endDate);
            Date date3 = simpleDateFormat.parse(targetDate);

            if(date1.getTime()<=date3.getTime() && date2.getTime()>=date3.getTime()){
                System.out.println("date3在date1和date2日期范围内！");
                return true;
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return false;
    }

    // 获取人员补卡次数
    public String getBkCounts (RecordSet rs,String resourceId,String fromDate,String toDate) {
        String sql = "SELECT userid,signfrom FROM HRMRESOURCE a,\n" +
                "HrmScheduleSign b WHERE a.id=b.USERID AND a.id ='"+resourceId+"' AND signdate >= '"+fromDate+"' AND signdate <= '"+toDate+"'";
        rs.execute(sql);
        RecordSet recordSet = new RecordSet();
        int count = 0;
        while (rs.next()) {
            String signfrom = Util.null2String(rs.getString("signfrom"));
            if (signfrom.contains("card")) {
                String signfrom1 = signfrom.substring(signfrom.indexOf("|",signfrom.indexOf("|") + 1) + 1);
                String signfrom2 = signfrom1.substring(signfrom1.indexOf("|",signfrom1.indexOf("|") + 1) + 1);
                String requestId = signfrom2.substring(0,signfrom2.indexOf("|"));
                recordSet.execute("SELECT count(1) counts FROM "+bkWorkFlowMainTable+" a,"+bkWorkFlowDetTable+" b WHERE a.ID =b.MAINID AND a.REQUESTID = '"+requestId+"' AND bkyy='0'");
                if (recordSet.next()) {
                    count = count + Integer.parseInt(recordSet.getString("counts"));
                }
            }
        }
        return String.valueOf(count);
    }
}
