package com.engine.htsc.workflow.cmd;

import com.cloudstore.eccom.constant.WeaBoolAttr;
import com.cloudstore.eccom.pc.table.WeaTable;
import com.cloudstore.eccom.pc.table.WeaTableColumn;
import com.cloudstore.eccom.result.WeaResultMsg;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import org.apache.commons.lang3.StringUtils;
import weaver.general.PageIdConst;
import weaver.general.Util;
import weaver.hrm.User;

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

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

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

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> apidatas = new HashMap<>();
        //角色判断参考代码
//        if(!HrmUserVarify.checkUserRight("LogView:View", user)){
//            apidatas.put("hasRight", false);
//            return apidatas;
//        }

        if (null == user) {
            apidatas.put("hasRight", false);
            return apidatas;
        }

        try {
            //返回消息结构体
            WeaResultMsg result = new WeaResultMsg(false);
            String pageID = "ad4ca476-a2ce-4b5b-9d80-bbeace096e79";
            String pageUid = pageID + "_" + user.getUID();
            String pageSize = PageIdConst.getPageSize(pageID, user.getUID());
            String sqlwhere = " 1=1 ";

            //新建一个weatable
            WeaTable table = new WeaTable();
            table.setPageUID(pageUid);
            table.setPageID(pageID);
            table.setPagesize(pageSize);

            String fileds = "MAINID as id ,wb.WORKFLOWNAME as lc,h.LASTNAME as dqpzld,hr.LASTNAME as zdclr,CLR as zdclr_id,t.LC as lcid,t.DQPZLD as dqpzld_id";
            table.setBackfields(fileds);
            //流程
            String state = Util.null2String(params.get("lc"));
            if (StringUtils.isNotBlank(state) && !StringUtils.equals("0", state)) {
                sqlwhere += " and LC=" + state;
            }
            //流程处理人
            String type = Util.null2String(params.get("dqpzld"));
            if (StringUtils.isNotBlank(type) && !StringUtils.equals("0", type)) {
                sqlwhere += " AND DQPZLD=" + type;
            }

            table.setSqlform(" (select distinct LC,DQPZLD,TO_CHAR(ZDCLR) as CLR,uz.MAINID from UF_ZDCLRPZ u join UF_ZDCLRPZ_DT1  uz on u.ID = uz.MAINID )t  join HRMRESOURCE h on t.DQPZLD = h.ID join HRMRESOURCE hr on t.CLR=hr.ID JOIN WORKFLOW_BASE wb on LC= wb.UNNUMBER ");
            table.setSqlwhere(sqlwhere);
            table.setSqlorderby("lcid");
            table.setSqlisdistinct("false");
            //设置隐层的字段
            table.getColumns().add(new WeaTableColumn("id").setDisplay(WeaBoolAttr.FALSE));
            table.getColumns().add(new WeaTableColumn("lcid").setDisplay(WeaBoolAttr.FALSE));
            table.getColumns().add(new WeaTableColumn("dqpzld_id").setDisplay(WeaBoolAttr.FALSE));
            table.getColumns().add(new WeaTableColumn("zdclr_id").setDisplay(WeaBoolAttr.FALSE));

            table.getColumns().add(new WeaTableColumn("25%", "流程", "lc", "lc"));
            table.getColumns().add(new WeaTableColumn("24%", "流程处理人", "dqpzld", "dqpzld"));
            table.getColumns().add(new WeaTableColumn("24%", "指定处理人", "zdclr", "zdclr"));

            //设置左侧check默认不存在
            table.setCheckboxList(null);
            table.setCheckboxpopedom(null);

            result.putAll(table.makeDataResult());
            result.put("hasRight", true);
            result.success();
            apidatas = result.getResultMap();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return apidatas;
    }
}
