package com.engine.kq.cmd.shiftmanagement;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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;

/**
 *
 */
public class SaveMessageRemindCmd extends AbstractCommonCommand<Map<String, Object>> {

    public SaveMessageRemindCmd(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("KQClass:Management", user);
            if (!canEdit) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
                return resultMap;
            }

            String serialId = Util.null2String(params.get("serialId"));//班次ID
            String datas = Util.null2String(params.get("datas"));//数据
            if (serialId.equals("") || datas.equals("")) {
                resultMap.put("status", "-1");
                resultMap.put("message", SystemEnv.getHtmlLabelName(388858, user.getLanguage()));
                return resultMap;
            }

            String sql = "delete from kq_messageRemindTemplate where serialId=?";
            RecordSet recordSet = new RecordSet();
            recordSet.executeUpdate(sql, serialId);

            JSONArray jsonArray = JSONArray.parseArray(datas);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                String radio = jsonObject.getString("radio");
                String type = jsonObject.getString("signType");
                String content = jsonObject.getString("content");
                String signInOrSignOut = "";
                String availableParams = "";
                if ("1".equals(id)) {
                    signInOrSignOut = "2";
                    availableParams = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(10005342,weaver.general.ThreadVarLanguage.getLang())+"";
                } else {
                    signInOrSignOut = "1";
                    availableParams = ""+weaver.systeminfo.SystemEnv.getHtmlLabelName(18843,weaver.general.ThreadVarLanguage.getLang())+"";
                }

                sql = "insert into kq_messageRemindTemplate(serialId,signInOrSignOut,messageContent,availableParams) values(?,?,?,?)";
                boolean flag = recordSet.executeUpdate(sql, serialId, signInOrSignOut, content, availableParams);
                if (!flag) {
                    resultMap.put("sign", "-1");
                    resultMap.put("message", SystemEnv.getHtmlLabelName(84544, user.getLanguage()));//保存失败
                    return resultMap;
                }
            }
            resultMap.put("sign", "1");
            resultMap.put("message", SystemEnv.getHtmlLabelName(83551, user.getLanguage()));//保存成功
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultMap;
    }
}
