package com.engine.kq.cmd.overtimerules;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 加班规则--判断某考勤组是否重复设置了加班规则
 */
public class IsRepeatSettingCmd extends AbstractCommonCommand<Map<String, Object>> {

    public IsRepeatSettingCmd(Map<String, Object> params, User user) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        String ruleId = Util.null2String(params.get("ruleId"));//加班规则的ID
        String groupIds = Util.null2String(params.get("groupIds"));//考勤组的ID

        List<String> errMsgList = new ArrayList<String>();//考勤组重复设置了加班规则的错误信息提示集合

        RecordSet recordSet = new RecordSet();
        String[] groupIdArr = groupIds.split(",");
        for (int i = 0; i < groupIdArr.length; i++) {
            String groupId = groupIdArr[i];
            String groupName = "";//考勤组的名称
            String sql = "select * from kq_overtimeRules where (isDelete is null or isDelete!=1) ";
            if (recordSet.getDBType().equalsIgnoreCase("sqlserver")) {
                sql += " and ','+shiftIds+',' like '%," + groupId + ",%'";
            } else if (recordSet.getDBType().equalsIgnoreCase("mysql")) {
                sql += " and contract(',',shiftIds,',') like '%," + groupId + ",%'";
            } else {
                sql += " and ','||shiftIds||',' like '%," + groupId + ",%'";
            }
            if (!ruleId.equals("")) {
                sql += " and ruleId not in (" + ruleId + ")";
            }
            recordSet.executeQuery(sql);
            if (recordSet.next()) {
                errMsgList.add(groupName + SystemEnv.getHtmlLabelName(81869, user.getLanguage()));
            }
        }

        if(errMsgList.size()==0){
            resultMap.put("isRepeatSetting",false);
        }else{
            resultMap.put("isRepeatSetting",true);
            resultMap.put("message",errMsgList);
        }
        return resultMap;
    }
}
