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.HashMap;
import java.util.Map;

/**
 * 保存是否默认
 */
public class SaveDefaultCmd extends AbstractCommonCommand<Map<String, Object>> {

    public SaveDefaultCmd(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
        if (ruleId.equals("")) {
            resultMap.put("sign", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(388858, user.getLanguage()));
            return resultMap;
        }
        boolean flag = false;
        String sql = "update kq_OvertimeRules set isDefault=1 where id=?";
        RecordSet recordSet = new RecordSet();
        flag = recordSet.executeUpdate(sql, ruleId);
        if (!flag) {
            resultMap.put("sign", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
            return resultMap;
        }

        if (!ruleId.equals("")) {
            sql = "update kq_OvertimeRules set isDefault=0 where id not in (" + ruleId + ")";
            flag = recordSet.executeUpdate(sql);
            if (!flag) {
                resultMap.put("sign", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
                return resultMap;
            }
        }

        if (flag) {
            resultMap.put("sign", "1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(83551, user.getLanguage()));//保存成功
        } else {
            resultMap.put("sign", "-1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
        }
        return resultMap;
    }
}
