package com.engine.kq.cmd.schedulecode;

import java.util.HashMap;
import java.util.Map;
import com.cloudstore.dev.api.util.Util_TableMap;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.util.PageUidFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.common.database.dialect.DbDialectFactory;
import weaver.hrm.common.database.dialect.DialectUtil;
import weaver.hrm.common.database.dialect.IDbDialectSql;
import weaver.systeminfo.SystemEnv;
 

public class GetScheduleDeviceListCmd extends AbstractCommonCommand<Map<String, Object>>{
	
	public GetScheduleDeviceListCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@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 devicename = Util.null2String(params.get("devicename"));
			String devicecode = Util.null2String(params.get("devicecode"));
			String subcompanyid = Util.null2String(params.get("subcompanyids"));

			String backfields = " * ";
			String fromSql  = " kq_schedule_device ";
			String sqlWhere = " 1=1 ";
			String orderby = " id  " ;

			if (!subcompanyid.equals("")) {
				if (rs.getDBType().equals("oracle")) {
					sqlWhere += " and ','||cast(subcompanyids as VARCHAR(4000))||',' like '%," + subcompanyid + ",%'";
				} else if (DialectUtil.isMySql()) {
					IDbDialectSql dialect = DbDialectFactory.get(rs.getDBType());
					String castCondition = dialect.castToChar("subcompanyids", 4000);
					String dot = "','";
					String t0FieldConcat = dialect.concatStr(dot, castCondition, dot);
					sqlWhere += " and " + t0FieldConcat + " like '%," + subcompanyid + ",%'";
				} else {
					sqlWhere += " and ','+cast(subcompanyids as NVARCHAR(4000))+',' like '%," + subcompanyid + ",%'";
				}
			}

			if (!devicename.equals("")) {
				sqlWhere += " and devicename like '%";
				sqlWhere += devicename;
				sqlWhere += "%'";
			}
			if (!devicecode.equals("")) {
				sqlWhere += " and devicecode like '%";
				sqlWhere += devicecode;
				sqlWhere += "%'";
			}
			String pageUid = PageUidFactory.getHrmPageUid("ScheduleDeviceList");

			String operateString= "<operates width=\"20%\">";
			operateString+=" <popedom transmethod=\"com.engine.kq.util.TransMethod.getScheduleDeviceOperate\"></popedom> ";
			operateString+="     <operate href=\"javascript:doEdit();\" text=\""+SystemEnv.getHtmlLabelName(	93,user.getLanguage())+"\" index=\"0\"/>";
			operateString+="     <operate href=\"javascript:doDelete();\" text=\""+SystemEnv.getHtmlLabelName(91,user.getLanguage())+"\" index=\"1\"/>";
			operateString+="     <operate href=\"javascript:showLog();\" text=\""+SystemEnv.getHtmlLabelName(83,user.getLanguage())+"\" index=\"2\"/>";
			operateString+="</operates>";
			String tableString =" <table  pageUid=\""+pageUid+"\" pagesize=\"10\" tabletype=\"checkbox\">"+
							" <sql backfields=\""+backfields+"\" sqlform=\""+fromSql+"\" sqlwhere=\""+Util.toHtmlForSplitPage(sqlWhere)+"\"  sqlorderby=\""+orderby+"\"  sqlprimarykey=\"id\" sqlsortway=\"asc\" sqlisdistinct=\"\"/>"+
							" <checkboxpopedom showmethod=\"com.engine.kq.util.TransMethod.getScheduleDeviceCheckbox\"  id=\"checkbox\"  popedompara=\"column:id\" />"+
							operateString+
							"	<head>"+
							"		<col width=\"0%\"  hide=\"true\"  text=\"\" column=\"id\"/>"+
							"		<col width=\"25%\" text=\""+SystemEnv.getHtmlLabelName(512449,user.getLanguage())+"\" column=\"devicename\" orderkey=\"devicename\"/>"+
							"  <col width=\"25%\" text=\"" + SystemEnv.getHtmlLabelName(512448, user.getLanguage()) + "\" column=\"devicecode\" orderkey=\"devicecode\"/>"+
							"  <col width=\"25%\" text=\"" + SystemEnv.getHtmlLabelName(32531, user.getLanguage()) + "\" column=\"ip\" orderkey=\"ip\"/>"+
							"  <col width=\"25%\" text=\"" + SystemEnv.getHtmlLabelName(506413, user.getLanguage()) + "\" column=\"subcompanyids\" orderkey=\"subcompanyids\" transmethod=\"weaver.hrm.company.SubCompanyComInfo.getSubcompanynames\"/>";
							tableString+="	</head>"+
							" </table>";

			//主要用于 显示定制列以及 表格 每页展示记录数选择
			String sessionkey = pageUid + "_" + Util.getEncrypt(Util.getRandom());
			Util_TableMap.setVal(sessionkey, tableString);
			retmap.put("sessionkey", sessionkey);
			retmap.put("status", "1");
		} 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;
	}

}
