package com.engine.htsc.gsfw.web;

import com.weaver.general.Util;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.interfaces.htsc.comInfo.PropBean;
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.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompanyWorkflowAction {

    private Log log = LogFactory.getLog(this.getClass());

    /**
     * 公司发文公文发送节点送办结前校验领导会签
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/checkTransferred")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> checkTransferred(@Context HttpServletRequest request,
                                               @Context HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        PropBean pb = new PropBean();
        String leaderIds = Util.null2String(request.getParameter("leaderIds"));
        List<String> leaderIdList = new ArrayList<>();
        String[] split = leaderIds.split(",");
        for (String leaderId : split){
            leaderIdList.add(leaderId);
        }
        String requestid = Util.null2String(request.getParameter("requestid"));
        String nodeunnumber = Util.null2String(request.getParameter("nodeunnumber"));
        String nodeid_ldhq = pb.getActionNodeIdByUnNumber(nodeunnumber);
        log.info("nodeid_ldhq"+nodeid_ldhq);
        try {
            RecordSet rs = new RecordSet();
            String selectLeader = "select DISTINCT userid from workflow_currentoperator " +
                    " where requestid = ? and  nodeid = ? and isremark <> 0 ";
            rs.executeQuery(selectLeader,requestid,nodeid_ldhq);
            log.info("sql--"+selectLeader);
            List<String> useridList = new ArrayList<>();
            while (rs.next()){
                String userid = Util.null2String(rs.getString("userid"));
                useridList.add(userid);
            }
            Boolean flag = useridList.containsAll(leaderIdList);
            log.info("flag:"+flag);
            map.put("status",1);
            map.put("msg","success");
            map.put("result",flag);

        }catch (Exception e){
            map.put("msg","校验失败");
            e.printStackTrace();
        }
        return map;
    }
}
