package com.engine.kq.cmd.group;

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.User;
import weaver.systeminfo.SystemEnv;

import java.util.*;

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

	private SimpleBizLogger logger;
	public DelGroupCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;

		String ids = Util.null2String(params.get("ids"));//考勤组id
		this.logger = new SimpleBizLogger();
		BizLogContext bizLogContext = new BizLogContext();
		bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
		bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_KQGROUP);
		bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_KQGROUP);//当前小类型
		bizLogContext.setParams(params);//当前request请求参数
		logger.setUser(user);//当前操作人
		String mainSql = "select * from kq_group where id in("+ids+") and (isdelete is null or isdelete <> '1') ";
		logger.setMainSql(mainSql,"id");//主表sql
		logger.setMainPrimarykey("id");//主日志表唯一key
		logger.setMainTargetNameColumn("groupname");//当前targetName对应的列（对应日志中的对象名）
		logger.before(bizLogContext);
	}

	@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"));//考勤组id
			List sqlParams = new ArrayList();
			Object[] objects=DBUtil.transListIn(ids,sqlParams);
		  sql = "update kq_group set isdelete = 1 where id in ("+objects[0]+") ";
		  rs.executeUpdate(sql, sqlParams);

			sql = "update kq_groupmember set isdelete = 1 where groupid in ("+objects[0]+") ";
			rs.executeUpdate(sql, sqlParams);
			
			sql = "update kq_shiftschedule set isdelete=1 where groupid in ("+objects[0]+")" ;
			rs.executeUpdate(sql, sqlParams);

			sql = "update kq_fixedschedulce set isdelete=1 where groupid in ("+objects[0]+")" ;
			rs.executeUpdate(sql, sqlParams);

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

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

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