package com.engine.kq.cmd.groupwifi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.biz.SimpleBizLogger;
import com.engine.common.constant.BizLogSmallType4Hrm;
import com.engine.common.constant.BizLogType;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQWifiComInfo;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

public class SaveGroupWifiCmd extends AbstractCommonCommand<Map<String, Object>> {

	private SimpleBizLogger logger;
	public SaveGroupWifiCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
		String groupId = Util.null2String(params.get("groupId"));
		this.boforeLog(groupId);
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String, Object> retmap = new HashMap<String, Object>();
		RecordSet rs = new RecordSet();
		String sql = "";
		try{
			//必要的权限判断
			if(!HrmUserVarify.checkUserRight("HrmKQGroup:Add",user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}
			String datas = Util.null2String(params.get("data"));
			JSONObject jsonObj = JSON.parseObject(datas);
			String id = Util.null2String(jsonObj.get("id"));
			String wifiname = Util.null2String(jsonObj.get("wifiname"));//名称
			String mac = Util.null2String(jsonObj.get("mac"));//MAC地址
			String groupId = Util.null2String(jsonObj.get("groupId"));

			List<Object> lsParams = new ArrayList<>();
			lsParams.add(wifiname.length()==0?null:wifiname);
			lsParams.add(mac.length()==0?null:mac);
			lsParams.add(groupId.length()==0?null:groupId);

			if(id.length()>0){
				sql = " update kq_wifi set wifiname=?,mac=?,groupid=? where id=? ";
				lsParams.add(id);
				rs.executeUpdate(sql,lsParams);
			}else{
				sql = " insert into kq_wifi (wifiname,mac,groupid) values (?,?,?)";
				rs.executeUpdate(sql,lsParams);

				rs.executeQuery("select max(id) from kq_wifi") ;
				if(rs.next()){
					id = rs.getString(1);
				}
			}

			new KQWifiComInfo().removeCache();
			retmap.put("id", id);
			retmap.put("status", "1");
		} catch (Exception e) {
			retmap.put("status", "-1");
			retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
			writeLog(e);
		}
		return retmap;
	}

	private void boforeLog(String id){
		this.logger = new SimpleBizLogger();
		BizLogContext logContext = new BizLogContext();
		logContext.setDateObject(new Date());
		logContext.setLogType(BizLogType.HRM_ENGINE);
		logContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQGROUP);
		logContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQGROUP);
		logContext.setParams(params);
		logger.setUser(user);//当前操作人

		String mainSql = " select * from kq_group where id= " + id;
		logger.setMainSql(mainSql);//主表sql
		logger.setMainPrimarykey("id");//主日志表唯一key
		logger.setMainTargetNameColumn("groupname");

		SimpleBizLogger.SubLogInfo subLogInfo1 = logger.getNewSubLogInfo();
		String subSql1 = "select * from kq_wifi where groupid="+id;
		subLogInfo1.setSubPrimarykey("id");
		subLogInfo1.setSubTargetNameColumn("id");
		subLogInfo1.setGroupId("0");  //所属分组， 按照groupid排序显示在详情中， 不设置默认按照add的顺序。
		subLogInfo1.setSubGroupNameLabel(388736); //在详情中显示的分组名称，不设置默认显示明细x
		subLogInfo1.setSubSql(subSql1);
		logger.addSubLogInfo(subLogInfo1);

		logger.before(logContext);
	}

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

	@Override
	public List<BizLogContext> getLogContexts() {
		return logger.getBizLogContexts();
	}
}
