package com.engine.htxc.tycgsq.web;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-24 15:11
 * @Description:采购相关接口
 * @ModifyLog:
 **/
public class GetWorkflowLdspAjax {
    @GET
    @Path("/getWorkflowLdspAjax")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkflowLdspAjax(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> map = new HashMap<String, Object>();
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        int requestid = Util.getIntValue( request.getParameter( "requestid" ) );
        String nodeid = Util.null2String( request.getParameter( "nodeid" ));
        String login = Util.null2String( request.getParameter( "loginid" ));
        bb.writeLog("login:"+login);
        JSONObject json = new JSONObject();
        String loginids = "";

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

        bb.writeLog("loginids="+loginids);
        List<String> list = new ArrayList<>();
        String sql = " select distinct userid from workflow_currentoperator where requestid="+requestid+" and nodeid in("+nodeid+") ";
        bb.writeLog(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;
        }

//        json.put("count",retval);
        map.put("data",retval);
        map.put("code",200);
        map.put("msg","SUCCESS");
        bb.writeLog( "getworkflow_ldsp_Ajax=================json:" +JSONObject.toJSONString(map) );
//        out.print( json.toJSONString() );
        return JSONObject.toJSONString(map);
    }
}
