package com.engine.kq.cmd.groupmemeber;

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.KQGroupMemberComInfo;
import weaver.conn.DBUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.Reminder.KQAutoCardTask;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

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

	private SimpleBizLogger logger;
	public DelGroupMemberCmd(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"));
			List sqlParams = new ArrayList();
			Object[] objects= DBUtil.transListIn(ids,sqlParams);
			sql = "update kq_groupmember set isDelete=1 where id in ("+objects[0]+") ";
			rs.executeUpdate(sql, sqlParams);

			new KQGroupMemberComInfo().removeCache();

      KQGroupComInfo kQGroupComInfo = new KQGroupComInfo();
      String auto_checkout = kQGroupComInfo.getAuto_checkout(groupId);
      String auto_checkin = kQGroupComInfo.getAuto_checkin(groupId);
      if("1".equalsIgnoreCase(auto_checkin) || "1".equalsIgnoreCase(auto_checkout)){
        //如果开启了自动打卡，保存考勤组成员之后需要格式化下缓存
        KQAutoCardTask kqAutoCardTask = new KQAutoCardTask();
        kqAutoCardTask.initAutoCardTask(groupId);
      }
			retmap.put("status", "1");
			retmap.put("message", SystemEnv.getHtmlLabelName(20461, 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_groupmember where (isdelete is null or isdelete <> '1') and 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();
	}

}
