package com.engine.kq.cmd.schedulecode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

	private SimpleBizLogger logger;
	public SaveScheduleDeviceCmd(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 id = Util.null2String(params.get("id"));
		String devicecode = Util.null2String(params.get("devicecode"));
		String mainSql = "";
		if(id.length()>0){
			mainSql = "select * from kq_schedule_device where id = "+id;
		}else{
			mainSql = "select * from kq_schedule_device where devicecode = '"+devicecode+"'";
		}
		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 checkSql = "";
		String sql = "";
		try {
			if(!HrmUserVarify.checkUserRight("ScheduleCode:Edit",user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}

			String id = Util.null2String(params.get("id"));
			String devicename = Util.null2String(params.get("devicename"));
			String devicecode = Util.null2String(params.get("devicecode"));
			String ip = Util.null2String(params.get("ip"));
			String subcompanyids = Util.null2String(params.get("subcompanyids"));


			if(id.length()>0){
				checkSql = "select count(1) from kq_schedule_device where devicecode='"+devicecode+"' and id != "+id;
				sql = "update kq_schedule_device set devicename = '"+devicename+"',devicecode='"+devicecode+"',ip='"+ip+"',subcompanyids='"+subcompanyids+"' where id = "+id;
			}else{
				checkSql = "select count(1) from kq_schedule_device where devicecode='"+devicecode+"'";
				sql = "INSERT INTO kq_schedule_device( devicename ,devicecode ,ip,subcompanyids ) VALUES  ( '"+devicename+"' ,'"+devicecode+"' ,'"+ip+"','"+subcompanyids+"')";
			}
			if(rs.executeQuery(checkSql)){
				if(rs.next() ) {
					if (rs.getInt(1) > 0) {
						retmap.put("status", "-1");
						retmap.put("message", SystemEnv.getHtmlLabelName(513267, user.getLanguage()));
						return retmap;
					}
				}
			}
			rs.executeUpdate(sql);
			retmap.put("status", "1");
		} catch (Exception e) {
			writeLog(e);
			retmap.put("status", "-1");
			retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
		}
		return retmap;
	}
	
	
	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}

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