package com.engine.kq.cmd.settings;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQSettingsComInfo;
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 SaveSettingsCmd extends AbstractCommonCommand<Map<String, Object>> {

    public SaveSettingsCmd(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 datas = Util.null2String(params.get("datas"));
        JSONObject jsonObject = JSONObject.parseObject(datas);

        List valueList = new ArrayList();
        List paramList = new ArrayList();

        if (jsonObject != null) {
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                valueList = new ArrayList();
                valueList.add(entry.getValue());
                valueList.add(entry.getKey());
                paramList.add(valueList);
            }
        }

        boolean flag = true;
        if (paramList.size() > 0) {
            String sql = "UPDATE KQ_Settings SET main_val=? where main_key=?";
            RecordSet recordSet = new RecordSet();
            flag = recordSet.executeBatchSql(sql, paramList);
        }
        KQSettingsComInfo kqSettingsComInfo = new KQSettingsComInfo();
        kqSettingsComInfo.removeCache();
        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;
    }
}
