package com.engine.kq.cmd.groupmemeber;

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.biz.KQGroupComInfo;
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.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

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

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

	public GetGroupMemberSearchListCmd(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();
		try {
			if(!HrmUserVarify.checkUserRight("HrmKQGroup:Add",user)) {
				retmap.put("status", "-1");
				retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
				return retmap;
			}
			String groupId = Util.null2String(params.get("groupId"));
			String subcompanyId = new KQGroupComInfo().getSubcompanyid(groupId);
			CheckSubCompanyRight checkSubCompanyRight = new CheckSubCompanyRight();
			ManageDetachComInfo manageDetachComInfo = new ManageDetachComInfo();
			boolean hrmdetachable = manageDetachComInfo.isUseHrmManageDetach();//是否开启了人力资源模块的管理分权
			int operatelevel = -1;
			if(hrmdetachable) {
				operatelevel = checkSubCompanyRight.ChkComRightByUserRightCompanyId(user.getUID(), "HrmKQGroup:Add", Util.getIntValue(subcompanyId, -1));
			}else{
				operatelevel = 2;
			}

			if(user.getUID() == 1){
				operatelevel = 2;
			}


			String hrmName = Util.null2String(params.get("hrmName"));
			String hrmJobtitle = Util.null2String(params.get("hrmJobtitle"));
			String hrmSubcompany = Util.null2String(params.get("hrmSubcompany"));
			String department = Util.null2String(params.get("department"));

			String backfields = " * ";
			String fromSql  = " kq_groupmember a ";
			String sqlWhere = " (isdelete is null "+(rs.getDBType().equals("oracle") ? "" : " or isdelete=0")+") and groupId =  "+groupId;
			String orderby = " a.id asc " ;

			if(hrmName.length() > 0 ){
				sqlWhere += " and ( exists ( select 1 from hrmresource where a.typevalue=id and a.type=1 and lastname like '%"+hrmName+"%') " +
								"	or exists ( select 1 from hrmjobtitles where a.typevalue=id and a.type=5 and jobtitlename like '%"+hrmName+"%')" +
								" or exists ( select 1 from hrmsubcompany where a.typevalue=id and a.type=2 and subcompanyname like '%"+hrmName+"%')" +
								" or exists ( select 1 from hrmdepartment where a.typevalue=id and a.type=3 and departmentname like '%"+hrmName+"%')) ";

			}
			if(hrmJobtitle.length() > 0){
				sqlWhere += " and a.type=5 and a.typevalue = "+hrmJobtitle;
			}
			if(hrmSubcompany.length() > 0){
				sqlWhere += " and a.type=2 and a.typevalue = "+hrmSubcompany;
			}
			if(department.length() >0){
				sqlWhere += " and a.type=3 and a.typevalue = "+department ;
			}

			String pageUid = PageUidFactory.getHrmPageUid("KQGroupMembersSearchList");

			String operateString= "<operates width=\"20%\">";
			operateString+=" <popedom transmethod=\"com.engine.kq.util.TransMethod.getGroupMembersOperate\" otherpara=\""+groupId+"\"></popedom> ";
			//operateString+="     <operate href=\"javascript:doEdit();\" text=\""+SystemEnv.getHtmlLabelName(93,user.getLanguage())+"\" index=\"0\"/>";
			if(operatelevel > 1) {
				operateString += "     <operate href=\"javascript:doDel();\" text=\"" + SystemEnv.getHtmlLabelName(91, user.getLanguage()) + "\" index=\"0\"/>";
			}
			operateString+="</operates>";
			String tableString =" <table  pageUid=\""+pageUid+"\" pagesize=\"10\" tabletype=\"checkbox\">"+
							" <sql backfields=\""+backfields+"\" sqlform=\""+fromSql+"\" sqlwhere=\""+Util.toHtmlForSplitPage(sqlWhere)+"\"  sqlorderby=\""+orderby+"\"  sqlprimarykey=\"a.id\" sqlsortway=\"desc\" sqlisdistinct=\"\"/>"+
							" <checkboxpopedom showmethod=\"com.engine.kq.util.TransMethod.getKQGroupMembersCheckbox\"  id=\"checkbox\"  popedompara=\"column:id+"+groupId+"\" />"+
							operateString+
							"	<head>"+
							"	  <col width=\"0%\"  hide=\"true\"  text=\"\" column=\"id\"/>"+
							"		<col width=\"25%\" text=\""+SystemEnv.getHtmlLabelName(21956,user.getLanguage())+"\" column=\"type\" orderkey=\"type\" transmethod=\"com.engine.hrm.util.HrmTransMethod.getResourceType\" otherpara=\""+user.getLanguage()+"\"/>"+
							"		<col width=\"30%\" text=\""+SystemEnv.getHtmlLabelName(106,user.getLanguage())+"\" column=\"typevalue\" orderkey=\"typevalue\" transmethod=\"com.engine.hrm.util.HrmTransMethod.getResourceName\" otherpara=\"column:type+column:alllevel+"+user.getLanguage()+"+column:jobtitlelevel+column:jobtitlelevelvalue\" />"+
							"		<col width=\"25%\" text=\""+SystemEnv.getHtmlLabelName(683,user.getLanguage())+"\" column=\"seclevel\" orderkey=\"seclevel\" transmethod=\"com.engine.hrm.util.HrmTransMethod.getSecLevel\" otherpara=\"column:seclevelto\"/>"+
							"	</head>"+
							" </table>";

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

}
