package com.engine.kq.cmd.hrmAttProcSet;

import com.api.hrm.util.PageUidFactory;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import weaver.common.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.common.SplitPageTagTableE9;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

/**
 * 获取考勤流程设置的查询列表
 * @author pzy
 *
 */
public class GetStateProcSetListSearchListCmd extends AbstractCommonCommand<Map<String, Object>> {

	protected HttpServletRequest request;
	
	public GetStateProcSetListSearchListCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}
	
	public GetStateProcSetListSearchListCmd(Map<String, Object> params,HttpServletRequest request, User user) {
		this.user = user;
		this.params = params;
		this.request = request;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {

		Map<String,Object> retmap = new HashMap<String,Object>();
		try{
			StringUtil strUtil = new StringUtil();
			RecordSet rs = new RecordSet();
			SubCompanyComInfo SubCompanyComInfo = new SubCompanyComInfo();
			ManageDetachComInfo ManageDetachComInfo = new ManageDetachComInfo();
			HttpSession session = request.getSession(true);
//			String name = Util.null2String(params.get("name"));
			String qCondition = strUtil.vString(params.get("qCondition"));
			String field001 = strUtil.vString(params.get("field001"));
			String field002 = strUtil.vString(params.get("field002"));
			String field002Name = "";
			if(field002.length() > 0){
				rs.executeSql("select b.labelname from WorkFlow_Bill a left join HtmlLabelInfo b on a.nameLabel = b.indexID and b.languageid = "+user.getLanguage()+" where a.id in ("+field002+")");
				while(rs.next()) field002Name+= (field002Name.length()==0?"":",") + rs.getString(1);
			}
			int field003 = strUtil.parseToInt(params.get("field003"));
			int field004 = strUtil.parseToInt(strUtil.vString(params.get("subcompanyid")), 0);
			field004 = field004 < 0 ? 0 : field004;
			
			String field004Name = field004 == 0 ? "" : SubCompanyComInfo.getSubCompanyname(String.valueOf(field004));
			int field005 = strUtil.parseToInt(params.get("field005"));
			int field006 = strUtil.parseToInt(params.get("field006"));
			
			String createdateselectselect = strUtil.vString(params.get("createdateselectselect"));
			String fromdate = strUtil.vString(params.get("createdateselectfrom"));
			String enddate = strUtil.vString(params.get("createdateselectto"));
			if(!createdateselectselect.equals("") && !createdateselectselect.equals("0")&& !createdateselectselect.equals("6")){
				fromdate = TimeUtil.getDateByOption(createdateselectselect,"0");
				enddate = TimeUtil.getDateByOption(createdateselectselect,"1");
			}
			
			String allIds = "";
      ManageDetachComInfo manageDetachComInfo = new ManageDetachComInfo();
      boolean hrmdetachable = manageDetachComInfo.isUseHrmManageDetach();//是否开启了人力资源模块的管理分权
      CheckSubCompanyRight checkSubCompanyRight = new CheckSubCompanyRight();
			if(strUtil.vString(ManageDetachComInfo.getDetachable()).equals("1")) {

				ArrayList sList = SubCompanyComInfo.getRightSubCompany(user.getUID(), "HrmAttendanceProcess:setting");
				for(int i=0;i<sList.size();i++) allIds += (allIds.length() == 0 ? "" : ",") + strUtil.vString(sList.get(i));
			}
      int operatelevel = -1;
			String detach_sqlWhere = "";
      if(hrmdetachable){
        int[] arrSubcompanyids = checkSubCompanyRight.getSubComByUserRightId(user.getUID(),"HrmAttendanceProcess:setting");
        String subcompanyids = "";
        if(user.getUID() != 1){
          for(int i=0;arrSubcompanyids!=null&&i<arrSubcompanyids.length;i++){
            if(subcompanyids.length()>0)subcompanyids+=",";
            subcompanyids += arrSubcompanyids[i];
          }
          if(subcompanyids.length() > 0 ){
            detach_sqlWhere += " and field004 in("+subcompanyids+")";
          }else{
            detach_sqlWhere += " and 1=2 ";
          }
        }
      }else{
        operatelevel = 2;
      }

      if(field004 > 0){
        detach_sqlWhere += " and field004 = " + field004;
        if(hrmdetachable){
          operatelevel=checkSubCompanyRight.ChkComRightByUserRightCompanyId(user.getUID(),"HrmAttendanceProcess:setting",field004);
        }
      }
      if(user.getUID() == 1){
        operatelevel = 2;
      }
			

			String sqlField = "id, field001, field002, field003, field004, field005, field006, field009, tablename, labelname,usedetail";
			String sqlFrom = " from ( select a.id, field001, field002, field003, field004, field005, field006, field009, b.tablename, c.labelname,a.usedetail from kq_ATT_PROC_SET a left join WorkFlow_Bill b on a.field002 = b.id left join HtmlLabelInfo c on b.nameLabel = c.indexID and c.languageid = "+user.getLanguage()+" ) a";
			String sqlWhere = " where 1=1 ";
			
			if(qCondition.length() > 0){
				sqlWhere += " and field001 in (select id from workflow_base where workflowname like '%"+qCondition+"%')";
			} else if(field001.length() > 0){
				sqlWhere += " and field001 in (select id from workflow_base where workflowname like '%"+field001+"%')";
			}
			if(field002.length() > 0) {
				sqlWhere += " and field002 in ("+field002+")";
			}
			if(field003 != -1) {
				sqlWhere += " and field003 = "+field003;
			}
			if(field004 > 0) {
				sqlWhere += " and field004 = "+field004;
			}
			if(detach_sqlWhere.length() > 0){
        sqlWhere += detach_sqlWhere;
      }
			if(field005 != -1) {
				sqlWhere += " and field005 = "+field005;
			}
			if(field006 != -1) {
				sqlWhere += " and field006 = "+field006;
			}
			if(fromdate.length() > 0 && enddate.length() > 0) {
				sqlWhere += " and field009 between '"+fromdate+"' and '"+enddate+"'";
			} else {
				if(fromdate.length() > 0) {
					sqlWhere += " and field009 >= '"+fromdate+"'";
				}
				if(enddate.length() > 0) {
					sqlWhere += " and field009 between '1949-10-01' and '"+enddate+"'";
				}
			}
			String pageid = "KQ_HrmAttProcSet";
			String pageUid = PageUidFactory.getHrmPageUid(pageid);	
			SplitPageTagTableE9 table = new SplitPageTagTableE9(pageid, user);
      if(operatelevel > 0){
        table.addOperate(SystemEnv.getHtmlLabelName(93,user.getLanguage()), "javascript:showContent();", "true", "column:field006");
        table.addOperate(SystemEnv.getHtmlLabelName(18095,user.getLanguage()), "javascript:changeStatus(1);", "+column:field005+==0and+column:field002+!=180and+column:field002+!=181and+column:field002+!=182");
        table.addOperate(SystemEnv.getHtmlLabelName(18096,user.getLanguage()), "javascript:changeStatus(0);", "+column:field005+==1and+column:field002+!=180and+column:field002+!=181and+column:field002+!=182");
        table.addOperate(SystemEnv.getHtmlLabelName(21954,user.getLanguage()), "javascript:procSet();", "true", "column:field001");
      }
      if(operatelevel > 1){
        table.addOperate(SystemEnv.getHtmlLabelName(20230,user.getLanguage()), "javascript:doDel();", "true");
      }
      table.addOperate(SystemEnv.getHtmlLabelName(83,user.getLanguage()), "javascript:showLog();", "true");
      table.setPopedompara("true");
			table.setSql(sqlField, sqlFrom, sqlWhere, "id");
			
			int width1 = 20, width2 = 20, width3 = 15, width4 = 0, width5 = 10, width6 = 15, width7 = 20;
			if(strUtil.vString(ManageDetachComInfo.getDetachable()).equals("1")) {
			  width3 = (width1 = width2 = width4 = 15) - 5;
      }
      table.addCol(width5, "ID", "id");
      table.addFormatCol(width1, SystemEnv.getHtmlLabelName(34067,user.getLanguage()), "field001", "field001", "{cmd:class[weaver.formmode.interfaces.WfToModeTransmethod.getWorkflowNameBlank(+column:field001+, "+user.getLanguage()+")]}");
			
			table.addCol(width2, SystemEnv.getHtmlLabelName(15600,user.getLanguage()), "labelname");
			table.addCol(width3, SystemEnv.getHtmlLabelName(18411,user.getLanguage()), "field003", "field003", "weaver.hrm.common.SplitPageTagFormat.colFormat", "{cmd:array["+user.getLanguage()+";default=19516and19532,1=468and19532]}");
			if(strUtil.vString(ManageDetachComInfo.getDetachable()).equals("1")) {
				table.addCol(width4, SystemEnv.getHtmlLabelName(19799,user.getLanguage()), "field004", "field004", "weaver.hrm.company.SubCompanyComInfo.getSubCompanyname");
			}
			table.addCol(width5, SystemEnv.getHtmlLabelName(602,user.getLanguage()), "field005", "field005", "weaver.hrm.common.SplitPageTagFormat.colFormat", "{cmd:style[0=color:red]}{cmd:array["+user.getLanguage()+";default=18096,1=18095]}");
			table.addCol(width6, SystemEnv.getHtmlLabelName(19521,user.getLanguage()), "field009");
			table.addCol(width7, SystemEnv.getHtmlLabelName(10000608,weaver.general.Util.getIntValue(user.getLanguage())), "field006", "field006", "weaver.hrm.common.SplitPageTagFormat.colFormat", "{cmd:array["+user.getLanguage()+";default=670and18015,1=20084and18015,2=24058and18015,3=6151and18015,4=127655,5=390737,6=389117,7=390274,8=513400]}");

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