package com.engine.htsc.itcglc.cmd;

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.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.util.*;
import java.util.stream.Collectors;

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

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

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

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

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        System.out.println( "in=================ItlxlcLdspInfoCmd" );
        Map<String, Object> apimap = new HashMap<String, Object>();

        int requestid = Util.getIntValue(Util.null2String(params.get("requestid")));

        PropBean pb = new PropBean();
        String ldspjd = pb.getNodeIdsByUnNumber("itlxsq_gsldsp"); //领导审批节点

        String ldsp_value = Util.null2String(params.get("ldsp_value"));

        System.out.println("ldsp_value:"+ldsp_value);

        List<String> qcList = new ArrayList<String>();
        if(!"".equals(ldsp_value) && ldsp_value !=null)
        {
            List<String> lis = Arrays.asList(ldsp_value.split(","));
            qcList = lis.stream().distinct().collect(Collectors.toList());
        }

        List<String> list = new ArrayList<>();
        String sql = " select distinct userid from workflow_currentoperator where requestid="+requestid+" and nodeid in("+ldspjd+") and userid in ("+ldsp_value+")";
        System.out.println(sql);
        rs.executeQuery(sql);
        while(rs.next()){
            String userid = Util.null2String(rs.getString("userid"));
            list.add(userid);
        }

        int retval = 0;
        if(qcList !=null && qcList.size() >0 )
        {
            if(list !=null && list.size() > 0)
            {
                int wscount = 0;
                for(String ldspid : qcList)
                {
                    if(!list.contains(ldspid))
                    {
                        wscount++;
                    }
                }
                if(wscount>0){
                    retval = 0;
                }else{
                    retval = 1;
                }
            }else{
                retval = 1;
            }
        }else{
            retval = 1;
        }
        apimap.put("count",retval);
        return apimap;
    }

    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("requestid","6814809");
        params.put("ldsp_value","22797,17556");
        User user = new User();
        user.setUid(11260);
        ItcglcLdspInfoCmd ItlxlcLdspInfoCmd = new ItcglcLdspInfoCmd(user,params);
        CommandContext ceshi= new CommandContext(null,null);
        ItlxlcLdspInfoCmd.execute(ceshi);
    }

}
