package com.engine.kq.cmd.shiftmanagement;

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.HashMap;
import java.util.Map;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.moduledetach.ManageDetachComInfo;
import weaver.systeminfo.SystemEnv;
import weaver.systeminfo.systemright.CheckSubCompanyRight;

/**
 * 获取班次管理查询结果列表
 * @author pzy
 *
 */
public class GetShiftManagementSearchListCmd extends AbstractCommonCommand<Map<String, Object>>{

	public GetShiftManagementSearchListCmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
    Map<String, Object> retmap = new HashMap<String, Object>();
    if(!HrmUserVarify.checkUserRight("KQClass:Management",user)) {
      retmap.put("status", "-1");
      retmap.put("message", SystemEnv.getHtmlLabelName(2012, user.getLanguage()));
      return retmap;
    }
    try {
      String type = Util.null2String(params.get("type"));
      String id = Util.null2String(params.get("id"));

      String serial = Util.null2String(params.get("serial"));
      String subcompanyid=Util.null2String(params.get("subcompanyid"));
      CheckSubCompanyRight checkSubCompanyRight = new CheckSubCompanyRight();
      ManageDetachComInfo manageDetachComInfo = new ManageDetachComInfo();
      boolean hrmdetachable = manageDetachComInfo.isUseHrmManageDetach();//是否开启了人力资源模块的管理分权
      int operatelevel = -1;

      String backfields = " * ";
      String fromSql  = " kq_ShiftManagement ";
      String sqlWhere = " where 1=1 and (isdelete is null or  isdelete <> '1') ";
      if(hrmdetachable){
        int[] arrSubcompanyids = checkSubCompanyRight.getSubComByUserRightId(user.getUID(),"KQClass:Management");
        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 ){
            sqlWhere += " and subcompanyid in("+subcompanyids+")";
          }else{
            sqlWhere += " and 1=2 ";
          }
        }
      }else{
        operatelevel = 2;
      }
      String orderby = " id " ;
      String tableString = "";

      if(subcompanyid.length()>0 && !subcompanyid.equalsIgnoreCase("0")){
        sqlWhere += " and subcompanyid = " + subcompanyid;
        if(hrmdetachable){
          operatelevel=checkSubCompanyRight.ChkComRightByUserRightCompanyId(user.getUID(),"KQClass:Management",Util.getIntValue(subcompanyid,-1));
        }
      }
      if(user.getUID() == 1){
        operatelevel = 2;
      }

      if(serial.length() > 0){
        sqlWhere += " and serial like '%"+serial+"%'";
      }

      //操作字符串
      String  operateString= "";
      operateString = "<operates width=\"20%\">";
      operateString+=" <popedom transmethod=\"com.engine.kq.cmd.shiftmanagement.toolkit.ShiftManagementToolKit.getShiftListOperate\" ></popedom> ";
      if(operatelevel > 0){
        operateString+="     <operate href=\"javascript:doEdit();\" text=\""+SystemEnv.getHtmlLabelName(93,user.getLanguage())+"\" index=\"0\"/>";
        operateString+="     <operate href=\"javascript:personalizedEdit()\" text=\""+SystemEnv.getHtmlLabelName(388528,user.getLanguage())+"\" index=\"1\"  />";
      }
      if(operatelevel > 1){
        operateString+="     <operate href=\"javascript:doDelete()\" text=\""+SystemEnv.getHtmlLabelName(91,user.getLanguage())+"\" index=\"2\"/>";
      }
      operateString+="     <operate href=\"javascript:showLog()\" text=\""+SystemEnv.getHtmlLabelName(83,user.getLanguage())+"\" index=\"3\"/>";
      operateString+="</operates>";
      String tabletype="checkbox";
      if(HrmUserVarify.checkUserRight("KQClass:Management", user)){
        tabletype = "checkbox";
      }
      String pageUid = PageUidFactory.getHrmPageUid("ShiftManagementSearchList");

      tableString =" <table pageUid=\""+pageUid+"\" tabletype=\""+tabletype+"\" pagesize=\"10\" >"+
          " <checkboxpopedom showmethod=\"com.engine.kq.cmd.shiftmanagement.toolkit.ShiftManagementToolKit.getCheckbox\"  id=\"checkbox\"  popedompara=\"column:id\" />"+
          "	   <sql backfields=\""+backfields+"\" sqlform=\""+fromSql+"\" sqlwhere=\""+Util.toHtmlForSplitPage(sqlWhere)+"\"  sqlorderby=\""+orderby+"\"  sqlprimarykey=\"a.id\" sqlsortway=\"desc\" sqlisdistinct=\"false\"/>"+
          operateString+
          "			<head>"+
          "				<col width=\"0%\"   hide=\"true\" column=\"id\" />"+
          "				<col width=\"50%\"  text=\""+SystemEnv.getHtmlLabelName(125818,user.getLanguage()) +"\" column=\"serial\" orderkey=\"serial\" />"+
          "				<col width=\"50%\"  text=\""+SystemEnv.getHtmlLabelName(27961,user.getLanguage()) +"\" column=\"id\" orderkey=\"id\" transmethod=\"com.engine.kq.cmd.shiftmanagement.toolkit.ShiftManagementToolKit.getShiftOnOffWorkSection\" />"+
          "			</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;
	}

}
