package com.engine.kq.cmd.schedulecode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.api.hrm.bean.HrmFieldBean;
import com.api.hrm.util.HrmFieldUtil;
import com.engine.common.biz.AbstractCommonCommand;
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.hrm.moduledetach.ManageDetachComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;


public class GetScheduleCodeFormCmd extends AbstractCommonCommand<Map<String, Object>>{
	
	public GetScheduleCodeFormCmd(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;
			}

			ResourceComInfo resourceComInfo = new ResourceComInfo();
			String id = Util.null2String(params.get("id"));
			Map<String, Object> table = new HashMap<String, Object>();
			List<Map<String, Object>> columns = new ArrayList<>();
			List<Map<String, Object>> datas = new ArrayList<>();
			Map<String, Object> data = null;
			List<HrmFieldBean> titles = new ArrayList<>();

			HrmFieldBean hrmFieldBean = null;
			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("deviceid");
			hrmFieldBean.setFieldlabel("511397");
			hrmFieldBean.setFieldhtmltype("3");
			hrmFieldBean.setType("scheduledevice");
			hrmFieldBean.setViewAttr(3);
			hrmFieldBean.setWidth("200");
			Map<String,Object> dataParams = new HashMap<String, Object>();
			dataParams.put("subcompanyid",resourceComInfo.getSubCompanyID(id));
			hrmFieldBean.setDataParams(dataParams);
			titles.add(hrmFieldBean);

			hrmFieldBean = new HrmFieldBean();
			hrmFieldBean.setFieldname("schedulecode");
			hrmFieldBean.setFieldlabel("27960");
			hrmFieldBean.setFieldhtmltype("1");
			hrmFieldBean.setType("1");
			hrmFieldBean.setViewAttr(3);
			hrmFieldBean.setWidth("200");
			hrmFieldBean.setMultilang(false);
			titles.add(hrmFieldBean);

			Map<String,String> scheduledeviceMap = new HashMap<>();
			sql = "  select * from kq_schedule_device ";
			rs.executeQuery(sql);
			while(rs.next()){
				scheduledeviceMap.put(rs.getString("id"),rs.getString("devicename"));
			}

			columns = HrmFieldUtil.getHrmDetailTable(titles, null, user);
			table.put("columns", columns);
			sql = "  select * from kq_schedule_code where resourceid = ? ";
			rs.executeQuery(sql,id);
			while(rs.next()){
				data = new HashMap<>();
				data.put("id", rs.getString("id"));
				data.put("deviceid", Util.getIntValue(rs.getString("deviceid"))>0?rs.getString("deviceid"):"");
				data.put("deviceidspan", scheduledeviceMap.get(rs.getString("deviceid")));
				data.put("schedulecode", rs.getString("schedulecode"));
				datas.add(data);
			}
			table.put("datas",datas);

			retmap.put("status", "1");
			retmap.put("table", table);

			int operatelevel = -1;
			String subcompanyid = resourceComInfo.getSubCompanyID(id);
			ManageDetachComInfo manageDetachComInfo = new ManageDetachComInfo();
			boolean hrmdetachable = manageDetachComInfo.isUseHrmManageDetach();//是否开启了人力资源模块的管理分权
			if(hrmdetachable){
				if(subcompanyid.length()>0 && !subcompanyid.equalsIgnoreCase("0")){
					CheckSubCompanyRight checkSubCompanyRight = new CheckSubCompanyRight();
					operatelevel=checkSubCompanyRight.ChkComRightByUserRightCompanyId(user.getUID(),"ScheduleCode:Edit",Util.getIntValue(subcompanyid,-1));
				}
			}else{
				operatelevel = 2;
			}

			if(user.getUID() == 1){
				operatelevel = 2;
			}
			if(operatelevel > 0){
				retmap.put("canAdd", true);
			}else{
				retmap.put("canAdd", false);
			}
		} 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;
	}
	
	
}
