package com.engine.kq.cmd.grouplocation;

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.KQLoactionComInfo;
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 SaveGroupLocationCmd extends AbstractCommonCommand<Map<String, Object>> {

	private SimpleBizLogger logger;
	public SaveGroupLocationCmd(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 locationname = Util.null2String(jsonObj.get("locationname"));//名称
			String checkscope = Util.null2String(jsonObj.get("checkscope"));//有效识别半径
			String longitude = Util.null2String(jsonObj.get("longitude"));//经度
			String latitude = Util.null2String(jsonObj.get("latitude"));//纬度
			String address = Util.null2String(jsonObj.get("address"));//地址
			String groupId = Util.null2String(jsonObj.get("groupId"));//分组id

			List<Object> lsParams = new ArrayList<>();
			lsParams.add(locationname.length()==0?null:locationname);
			lsParams.add(checkscope.length()==0?null:checkscope);
			lsParams.add(longitude.length()==0?null:longitude);
			lsParams.add(latitude.length()==0?null:latitude);
			lsParams.add(address.length()==0?null:address);
			lsParams.add(groupId.length()==0?null:groupId);

			if(id.length()>0){
				sql = " update kq_location set locationname=?,checkscope=?,longitude=?,latitude=?,address=?,groupid=? where id=? ";
				lsParams.add(id);
				rs.executeUpdate(sql,lsParams);
			}else{
				sql = " insert into kq_location (locationname,checkscope,longitude,latitude,address,groupid) values (?,?,?,?,?,?)";
				rs.executeUpdate(sql,lsParams);

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

			new KQLoactionComInfo().removeCache();
			retmap.put("id", id);
			retmap.put("status", "1");
			retmap.put("message", SystemEnv.getHtmlLabelName(18758, user.getLanguage()));
		}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_location where groupid="+id;
		subLogInfo1.setSubPrimarykey("id");
		subLogInfo1.setSubTargetNameColumn("id");
		subLogInfo1.setGroupId("0");  //所属分组， 按照groupid排序显示在详情中， 不设置默认按照add的顺序。
		subLogInfo1.setSubGroupNameLabel(389125); //在详情中显示的分组名称，不设置默认显示明细x
		subLogInfo1.setSubSql(subSql1);
		logger.addSubLogInfo(subLogInfo1);

		logger.before(logContext);
	}

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

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