package com.engine.kq.cmd.leavetypes;

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.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: xiaj
 * @Date: 2019-03-07 16:43
 * @Description:
 */
public class IsNameRepeatCmd extends AbstractCommonCommand<Map<String, Object>> {

    public IsNameRepeatCmd(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>();
        try{
            boolean canEdit = HrmUserVarify.checkUserRight("KQLeaveRulesEdit:Edit", user);//是否具有编辑权限
            if (!canEdit) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return resultMap;
            }
            /*假期类型的ID*/
            String ruleId = Util.null2String(params.get("typeId"));

            /*假期类型的名称*/
            String leaveName = Util.null2String(params.get("leaveName"));

            String sql = "select * from kq_LeaveRules where (isDelete is null or isDelete<>1) and leaveName=? and id<>?";
            RecordSet recordSet = new RecordSet();
            recordSet.executeQuery(sql,leaveName,ruleId);
            if(recordSet.next()){
                resultMap.put("isRepeat",true);
            }else{
                resultMap.put("isRepeat",false);
            }
        }catch (Exception e){
            writeLog(e);
        }
        return resultMap;
    }
}
