package com.engine.kq.bean;

import com.engine.kq.biz.KQGroupComInfo;
import com.engine.kq.biz.KQHolidaySetComInfo;
import com.engine.kq.biz.KQHolidaySetThread;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.ThreadVarLanguage;
import weaver.general.Util;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 节假日设置--用于Excel导入
 */
public class KQHolidaySet extends BaseBean {

    /**
     * 考勤组名称
     */
    private String groupName;
    /**
     * 考勤组ID
     */
    private String groupId;
    /**
     * 日期
     */
    private String holidayDate;
    /**
     * 类型：公众假日、调配工作日、调配休息日
     */
    private String changeTypeName;
    /**
     * 类型对应的数值：1-公众假日、2-调配工作日、3-调配休息日
     */
    private int changeType = 1;
    /**
     * 说明
     */
    private String holidayDesc;
    /**
     * 对应工作日：星期日、星期一、星期二、星期三、星期四、星期五、星期六
     * 当考勤组的考勤类型为固定班制和自由班制的时启用
     */
    private String relatedDayName;
    /**
     * 6-星期日、0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六
     * 当考勤组的考勤类型为固定班制和自由班制的时启用
     */
    private int relatedDay = -1;
    /**
     * 用户语言
     */
    private int userlanguage = 7;

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

    /**
     * 校验数据有效性--用于Excel导入
     *
     * @return
     */
    public String valExcelData() {
        //简单校验日期格式
        String str = "[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}";

        String errorInfo = "";
        KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();

        if (Util.null2String(this.groupName).length() == 0) {
            errorInfo = SystemEnv.getHtmlLabelName(390305, userlanguage);//考勤组不能为空
            return errorInfo;
        } else if (getGroupId(this.groupName).equals("")) {
            errorInfo = SystemEnv.getHtmlLabelName(390306, userlanguage);//没有找到对应的考勤组
            return errorInfo;
        } else if (Util.null2String(this.holidayDate).length() == 0) {
            errorInfo = SystemEnv.getHtmlLabelName(388723, userlanguage);//日期不能为空
            return errorInfo;
        } else if(Util.null2String(this.holidayDate).length()!=0 && !Pattern.matches(str,this.holidayDate)){
            errorInfo = SystemEnv.getHtmlLabelName(130208, userlanguage);//无效日期格式
            return errorInfo;
        }else if (getChengeType(this.changeTypeName) != 1 && getChengeType(this.changeTypeName) != 2 && getChengeType(this.changeTypeName) != 3) {
            errorInfo = SystemEnv.getHtmlLabelName(83411, userlanguage);//类型不匹配
            return errorInfo;
        } else if (getChengeType(this.changeTypeName) == 2) {
            String kqType = kqGroupComInfo.getKqtype(this.groupId);
            if (kqType.equals("2")) {
                errorInfo = SystemEnv.getHtmlLabelName(526272, userlanguage);//暂时不支持排班制的考勤组设置调配工作日
                return errorInfo;
            }else if (getRelatedDay(this.relatedDayName) < 0 || getRelatedDay(this.relatedDayName) > 6) {
                errorInfo = SystemEnv.getHtmlLabelName(388724, userlanguage);//未找到对应的工作日
                return errorInfo;
            }
        }
        return errorInfo;
    }

    /**
     * 保存数据
     *
     * @return
     */
    public String save() {
        boolean flag = false;
        try {
            RecordSet recordSet = new RecordSet();

            String sql = "select 1 from kq_HolidaySet where groupId=" + this.groupId + " and holidayDate='" + this.holidayDate + "'";
            recordSet.executeQuery(sql);
            if (recordSet.next()) {
                sql = "delete from kq_HolidaySet where groupId=" + this.groupId + " and holidayDate='" + this.holidayDate + "'";
                recordSet.executeUpdate(sql);
            }

            sql = " insert into kq_HolidaySet(groupId,holidayDate,changeType,holidayDesc,relatedDay) values(?,?,?,?,?)";
            flag = recordSet.executeUpdate(sql, this.groupId, this.holidayDate, this.changeType, this.holidayDesc, this.relatedDay);
        } catch (Exception e) {
            writeLog(e);
        } finally {
            KQHolidaySetComInfo kqHolidaySetComInfo = new KQHolidaySetComInfo();
            kqHolidaySetComInfo.removeCache();

            /*刷新考勤报表*/
            List<String> groupIdList = new ArrayList<String>();
            groupIdList.add(this.groupId);
            List<String> holidayDateList = new ArrayList<String>();
            holidayDateList.add(this.holidayDate);
            new KQHolidaySetThread(groupIdList,holidayDateList).start();
        }
        return String.valueOf(flag);
    }

    /**
     * 根据考勤组名称获取考勤组ID
     *
     * @param groupName
     * @return
     */
    public String getGroupId(String groupName) {
        String groupId = "";
        String sql = "select id from kq_Group where (isDelete is null or isDelete <>1) and groupName='" + groupName + "' ";
        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sql);
        if (recordSet.next()) {
            groupId = recordSet.getString("id");
        }
        this.groupId = groupId;
        return groupId;
    }

    /**
     * 获取类型
     *
     * @param changeTypeName
     * @return
     */
    public int getChengeType(String changeTypeName) {
        int changeType = -1;
        if (Util.null2String(changeTypeName).trim().equals(SystemEnv.getHtmlLabelName(16478, ThreadVarLanguage.getLang()).trim())) {
            changeType = 1;
        } else if (Util.null2String(changeTypeName).trim().equals(SystemEnv.getHtmlLabelName(16751, ThreadVarLanguage.getLang()).trim())) {
            changeType = 2;
        } else if (Util.null2String(changeTypeName).trim().equals(SystemEnv.getHtmlLabelName(16752, ThreadVarLanguage.getLang()).trim())) {
            changeType = 3;
        } else {
            changeType = -1;
        }
        this.changeType = changeType;
        return changeType;
    }

    /**
     * 获取对应工作日
     *
     * @param relatedDayName 对应工作日：0-星期一、1-星期二、2-星期三、3-星期四、4-星期五、5-星期六、6-星期日
     * @return
     */
    public int getRelatedDay(String relatedDayName) {
        int relatedDay = -1;
        if (Util.null2String(relatedDayName).trim().equals(SystemEnv.getHtmlLabelName(528000, ThreadVarLanguage.getLang()).trim())) {
            relatedDay = 0;
        } else if (Util.null2String(relatedDayName).trim().equals(SystemEnv.getHtmlLabelName(528001, ThreadVarLanguage.getLang()).trim())) {
            relatedDay = 1;
        } else if (Util.null2String(relatedDayName).trim().equals(SystemEnv.getHtmlLabelName(528002, ThreadVarLanguage.getLang()).trim())) {
            relatedDay = 2;
        } else if (Util.null2String(relatedDayName).trim().equals( SystemEnv.getHtmlLabelName(528003, ThreadVarLanguage.getLang()).trim())) {
            relatedDay = 3;
        } else if (Util.null2String(relatedDayName).trim().equals(SystemEnv.getHtmlLabelName(528004, ThreadVarLanguage.getLang()).trim())) {
            relatedDay = 4;
        } else if (Util.null2String(relatedDayName).trim().equals(SystemEnv.getHtmlLabelName(528005, ThreadVarLanguage.getLang()).trim())) {
            relatedDay = 5;
        } else if (Util.null2String(relatedDayName).trim().equals( SystemEnv.getHtmlLabelName(527999, ThreadVarLanguage.getLang()).trim())) {
            relatedDay = 6;
        } else {
            relatedDay = -1;
        }
        this.relatedDay = relatedDay;
        return relatedDay;
    }

    /**
     * 加载缓存
     */
    public void removeCache() {
        try {
            KQHolidaySetComInfo kqHolidaySetComInfo = new KQHolidaySetComInfo();
            kqHolidaySetComInfo.removeCache();
        } catch (Exception e) {
            writeLog(e);
        }
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public String getHolidayDate() {
        return holidayDate;
    }

    public void setHolidayDate(String holidayDate) {
        this.holidayDate = holidayDate;
    }

    public String getChangeTypeName() {
        return changeTypeName;
    }

    public void setChangeTypeName(String changeTypeName) {
        this.changeTypeName = changeTypeName;
    }

    public String getHolidayDesc() {
        return holidayDesc;
    }

    public void setHolidayDesc(String holidayDesc) {
        this.holidayDesc = holidayDesc;
    }

    public String getRelatedDayName() {
        return relatedDayName;
    }

    public void setRelatedDayName(String relatedDayName) {
        this.relatedDayName = relatedDayName;
    }
}
