package com.engine.kq.cmd.schedulecode;

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 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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


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

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

		this.logger = new SimpleBizLogger();
		BizLogContext bizLogContext = new BizLogContext();
		bizLogContext.setLogType(BizLogType.HRM_ENGINE);//模块类型
		bizLogContext.setBelongType(BizLogSmallType4Hrm.HRM_ENGINE_SCHEDULEDEVICE);
		bizLogContext.setLogSmallType(BizLogSmallType4Hrm.HRM_ENGINE_SCHEDULEDEVICE);//当前小类型
		bizLogContext.setParams(params);//当前request请求参数
		logger.setUser(user);//当前操作人

		String ids = Util.null2String(params.get("ids"));
		String mainSql = "select * from kq_schedule_device where id in("+ids+") ";
		logger.setMainSql(mainSql, "id");//主表sql
		logger.setMainPrimarykey("id");//主日志表唯一key
		logger.setMainTargetNameColumn("devicename");
		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("ScheduleCode:Edit",user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}

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

			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() {
		// TODO Auto-generated method stub
		return null;
	}

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