package com.engine.kq.bean;

import com.engine.kq.biz.KQLeaveRulesComInfo;
import com.engine.kq.biz.KQUsageHistoryBiz;
import com.engine.kq.entity.KQUsageHistoryEntity;
import weaver.conn.RecordSet;
import weaver.file.Prop;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.common.database.dialect.DialectUtil;
import weaver.systeminfo.SystemEnv;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用于调休明细的导入
 */
public class KQBalanceDetail extends BaseBean {

    /**
     * 人员ID
     */
    private String resourceId;
    /**
     * 人员姓名
     */
    private String lastName;
    /**
     * 工号
     */
    private String workCode;
    /**
     * 年份
     */
    private String belongYear;
    /**
     * 月份
     */
    private String belongMonth;
    /**
     * 假期类型ID
     */
    private String leaveRulesId;
    /**
     * 操作类型：
     * 3--加班流程生成调休
     * 4--打卡数据生成调休
     * 7--管理员Excel导入
     */
    private String overtimeType = "7";
    /**
     * 总数
     */
    private String totalAmount;
    /**
     * 已休
     */
    private String usedAmount;
    /**
     * 生效日期
     */
    private String effectiveDate;
    /**
     * 失效日期
     */
    private String expirationDate;
    /**
     * 用户语言
     */
    private int userLanguage = 7;
    /**
     * 关键字
     */
    private String keyField;
    /**
     * 操作者
     */
    private String operator;

    /**
     * 验证模板数据有效性
     *
     * @return
     */
    public String valExcelData() {
        String errorInfo = "";
        if (!isExist()) {
            errorInfo = SystemEnv.getHtmlLabelName(83535, userLanguage);//人员未找到
            return errorInfo;
        }
        if (this.belongYear == null || this.belongYear.equals("")) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005283,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        Pattern p_belongYear = Pattern.compile("^\\d{4}$");
        Matcher m_belongYear = p_belongYear.matcher(this.belongYear);
        if (!m_belongYear.matches()) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005284,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        if (this.belongMonth == null || this.belongMonth.equals("")) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005285,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        Pattern p_belongMonth = Pattern.compile("^01|02|03|04|05|06|07|08|09|10|11|12$");
        Matcher m_belongMonth = p_belongMonth.matcher(this.belongMonth);
        if (!m_belongMonth.matches()) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005286,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        double _totalAmount = Util.getDoubleValue(this.totalAmount, 0.00);
        if (_totalAmount < 0) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005287,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        double _usedAmount = Util.getDoubleValue(this.usedAmount, 0.00);
        if (_usedAmount < 0) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005288,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
//        if (_totalAmount < _usedAmount) {
//            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(508124,weaver.general.ThreadVarLanguage.getLang())+"";
//            return errorInfo;
//        }
        if (this.leaveRulesId.equals("")) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005289,weaver.general.ThreadVarLanguage.getLang())+"ID";
            return errorInfo;
        }
        if (this.effectiveDate == null || this.effectiveDate.equals("")) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005290,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        Pattern p = Pattern.compile("^\\d{4}\\-\\d{2}\\-\\d{2}$");
        Matcher m = p.matcher(this.effectiveDate);
        if (!m.matches()) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005291,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        if (this.expirationDate == null || this.effectiveDate.equals("")) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005292,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        m = p.matcher(this.expirationDate);
        if (!m.matches()) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005293,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        if (this.effectiveDate.compareTo(this.expirationDate) > 0) {
            errorInfo = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005294,weaver.general.ThreadVarLanguage.getLang())+"";
            return errorInfo;
        }
        return errorInfo;
    }

    /**
     * 判断ID和姓名是否匹配，系统中是否存在此人员
     *
     * @return
     */
    private boolean isExist() {
        String sql = "select 1 from HrmResource where id=? ";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql, resourceId);
        if (recordSet.next()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 保存数据
     *
     * @return
     */
    public String save() {
        String resultStr = "false";
        try {
            /*获取当前日期，当前时间*/
            Calendar today = Calendar.getInstance();
            String currentDate = Util.add0(today.get(Calendar.YEAR), 4) + "-" +
                    Util.add0(today.get(Calendar.MONTH) + 1, 2) + "-" +
                    Util.add0(today.get(Calendar.DAY_OF_MONTH), 2);
            String currentTime = Util.add0(today.get(Calendar.HOUR_OF_DAY), 2) + ":" +
                    Util.add0(today.get(Calendar.MINUTE), 2) + ":" +
                    Util.add0(today.get(Calendar.SECOND), 2);

            KQLeaveRulesComInfo rulesComInfo = new KQLeaveRulesComInfo();
            String minimumUnit = rulesComInfo.getMinimumUnit(this.leaveRulesId);

            String sql = "INSERT INTO KQ_BalanceOfLeave(resourceId,leaveRulesId,belongYear,belongMonth,overtimeType,baseAmount,extraAmount,usedAmount,effectiveDate,expirationDate) VALUES(?,?,?,?,?,?,?,?,?,?)";
            RecordSet recordSet = new RecordSet();
            boolean flag = recordSet.executeUpdate(sql, this.resourceId, this.leaveRulesId, this.belongYear, this.belongMonth, this.overtimeType, 0, this.totalAmount, this.usedAmount, this.effectiveDate, this.expirationDate);
            resultStr = String.valueOf(flag);
            if (flag) {
                BigDecimal _totalAmount = new BigDecimal(this.totalAmount);
                BigDecimal _usedAmount = new BigDecimal(this.usedAmount);

                /*员工假期余额变更记录*/
                KQUsageHistoryEntity usageHistoryEntity = new KQUsageHistoryEntity();
                List<KQUsageHistoryEntity> usageHistoryEntityList = new ArrayList<KQUsageHistoryEntity>();

                /*员工假期余额变更记录*/
                usageHistoryEntity = new KQUsageHistoryEntity();
                usageHistoryEntity.setLeaveRulesId(this.leaveRulesId);
                usageHistoryEntity.setRelatedId(this.resourceId);
                usageHistoryEntity.setWfRequestId("");
                usageHistoryEntity.setOperator(this.operator);
                usageHistoryEntity.setOperateDate(currentDate);
                usageHistoryEntity.setOperateTime(currentTime);
                usageHistoryEntity.setOperateType("7");
                usageHistoryEntity.setInsertOrUpdate("INSERT");
                usageHistoryEntity.setBelongYear(this.belongYear);
                usageHistoryEntity.setOldExtraAmount("0");
                usageHistoryEntity.setNewExtraAmount(_totalAmount.subtract(_usedAmount).setScale(2, RoundingMode.HALF_UP).toPlainString());
                usageHistoryEntity.setOldMinimumUnit(minimumUnit);
                usageHistoryEntity.setNewMinimumUnit(minimumUnit);
                usageHistoryEntityList.add(usageHistoryEntity);
                KQUsageHistoryBiz usageHistoryBiz = new KQUsageHistoryBiz();
                flag = usageHistoryBiz.save(usageHistoryEntityList);

                resultStr = String.valueOf(flag);
                /*员工假期余额变更记录*/
            }
        } catch (Exception e) {
            writeLog(e);
        }
        return resultStr;
    }

    public String getResourceId() {
        return resourceId;
    }

    public void setResourceId(String resourceId) {
        this.resourceId = resourceId;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getWorkCode() {
        return workCode;
    }

    public void setWorkCode(String workCode) {
        this.workCode = workCode;
    }

    public String getBelongYear() {
        return belongYear;
    }

    public void setBelongYear(String belongYear) {
        this.belongYear = belongYear;
    }

    public String getBelongMonth() {
        return belongMonth;
    }

    public void setBelongMonth(String belongMonth) {
        this.belongMonth = belongMonth;
    }

    public String getLeaveRulesId() {
        return leaveRulesId;
    }

    public void setLeaveRulesId(String leaveRulesId) {
        this.leaveRulesId = leaveRulesId;
    }

    public String getOvertimeType() {
        return overtimeType;
    }

    public void setOvertimeType(String overtimeType) {
        this.overtimeType = overtimeType;
    }

    public String getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(String totalAmount) {
        BigDecimal _totalAmount = new BigDecimal(Util.getDoubleValue(totalAmount, 0));
        this.totalAmount = _totalAmount.setScale(2, RoundingMode.HALF_UP).toPlainString();
    }

    public String getUsedAmount() {
        return usedAmount;
    }

    public void setUsedAmount(String usedAmount) {
        BigDecimal _usedAmount = new BigDecimal(Util.getDoubleValue(usedAmount, 0));
        this.usedAmount = _usedAmount.setScale(2, RoundingMode.HALF_UP).toPlainString();
    }

    public String getEffectiveDate() {
        return effectiveDate;
    }

    public void setEffectiveDate(String effectiveDate) {
        this.effectiveDate = effectiveDate;
    }

    public String getExpirationDate() {
        return expirationDate;
    }

    public void setExpirationDate(String expirationDate) {
        this.expirationDate = expirationDate;
    }

    public int getUserLanguage() {
        return userLanguage;
    }

    public void setUserLanguage(int userLanguage) {
        this.userLanguage = userLanguage;
    }

    public void setUserLanguage(String userLanguage) {
        this.userLanguage = Util.getIntValue(userLanguage, 7);
    }

    public String getKeyField() {
        return keyField;
    }

    public void setKeyField(String keyField) {
        this.keyField = keyField;
    }

    public String getOperator() {
        return operator;
    }

    public void setOperator(String operator) {
        this.operator = operator;
    }
}
