package com.engine.kq.cmd.grouplocation;

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.KQGroupComInfo;
import com.engine.kq.biz.KQLoactionComInfo;
import weaver.conn.DBUtil;
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 DelGroupLocationCmd extends AbstractCommonCommand<Map<String, Object>> {

	private SimpleBizLogger logger;
	public DelGroupLocationCmd(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 ids = Util.null2String(params.get("ids"));

      String groupId = Util.null2String(params.get("groupId"));
      KQGroupComInfo kqGroupComInfo = new KQGroupComInfo();
      String auto_checkin = kqGroupComInfo.getAuto_checkin(groupId);
      String auto_checkout = kqGroupComInfo.getAuto_checkout(groupId);
      if("1".equalsIgnoreCase(auto_checkin) || "1".equalsIgnoreCase(auto_checkout)){
        String check_empty_sql = "select 1 from kq_wifi where groupId=? ";
        rs.executeQuery(check_empty_sql, groupId);
        if(!rs.next()){
          //如果wifi已经空了，删除办公地点的时候需要判断了
          List sqlParams = new ArrayList();
          Object[] objects= DBUtil.transListIn(ids,sqlParams);
          check_empty_sql = "select 1 from kq_location where groupId=? and id not in ("+objects[0]+") ";
          rs.executeQuery(check_empty_sql, groupId, sqlParams);
          if(!rs.next()){
            //如果除去要删除的信息之后，当前考勤组的办公地点没了，说明要被清空了。这是要给出提示的
            retmap.put("status", "-1");
            retmap.put("message",  SystemEnv.getHtmlLabelName(519989,user.getLanguage()));
            return retmap;
          }
        }

      }

			List sqlParams = new ArrayList();
			Object[] objects= DBUtil.transListIn(ids,sqlParams);
			sql = "delete from kq_location where id in ("+objects[0]+") ";
			rs.executeUpdate(sql, sqlParams);

			new KQLoactionComInfo().removeCache();
			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();
	}
}
