package com.engine.htxc.tycgsq.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.workflow.request.wfAgentCondition;

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.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810027
 * @version: 1.0
 * @CreateDate: 2022-08-24 13:38
 * @Description://采购相关接口
 * @ModifyLog:
 **/
public class GetBeagentAjax {
    @GET
    @Path("/getBeagentAjax")
    @Produces({MediaType.TEXT_PLAIN})
    public String getBeagentAjax(@Context HttpServletRequest request, @Context HttpServletResponse response){
        HashMap<String, Object> result = new HashMap<>();
        try {
            ResourceComInfo resourceInfo = new ResourceComInfo();
            DepartmentComInfo departInfo = new DepartmentComInfo();
            BaseBean bb = new BaseBean();
            RecordSet rs = new RecordSet();
            PropBean pb = new PropBean();
            String requestid = Util.null2String(request.getParameter("requestid"));
            String wfunnumber = Util.null2String(request.getParameter("wfunnumber"));
            String workflowid = pb.getActiveWorkflowIdByUnNumber(wfunnumber);
            String operatorid = Util.null2String(request.getParameter("operatorid"));
            bb.writeLog("requestid:"+requestid);
            bb.writeLog("workflowid:"+workflowid);
            bb.writeLog("operatorid:"+operatorid);
            JSONObject json = new JSONObject();
            wfAgentCondition wfAgentCondition = new wfAgentCondition();
            JSONArray jsonArray = new JSONArray();
            Map<String,String> map =  wfAgentCondition.getAgentInfoByResouce(workflowid,operatorid,requestid);
            for(Map.Entry<String, String> entry : map.entrySet())
            {
                String key = entry.getKey();
                String value = entry.getValue();
                String name = "" ;
                if(value !="")
                {
                    name = resourceInfo.getLastname(value);
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("operatorid",key);
                jsonObject.put("beagentid",value);
                jsonObject.put("beagentname",name);
                jsonArray.add(jsonObject);
            }
//            json.put("data",jsonArray);
            bb.writeLog( "getFreeNode_Ajax=================json:" + json.toJSONString() );
//            out.print(json.toJSONString());
            result.put("code",200);
            result.put("msg","SUCCESS");
            result.put("data",jsonArray);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code",500);
            result.put("msg",e.getMessage());
        }
        return JSONObject.toJSONString(result);
    }
}
