package com.engine.htsc.receipt.service;

import com.engine.core.impl.Service;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;

import java.util.*;

public class ReceiptAgentService extends Service {

    public Map<String,Object> getAgent(String requestid, String fieldvalue){
        BaseBean bs = new BaseBean();
        RecordSet rs = new RecordSet();
        ResourceComInfo resourceComInfo = null;
        Map<String,Object> resultMap = new HashMap<>();

        try{
            resourceComInfo = new ResourceComInfo();
            List<String> valueList = new ArrayList<>();
            List<Map<String,String>> agentList = new ArrayList<>();
            List<String> useridList = Arrays.asList(fieldvalue.split(","));
            for(String userid:useridList){
                Map<String,String> agentMap = new HashMap<>();
                String userName = resourceComInfo.getLastname(userid);
                String sql = "select * from (select agentorbyagentid,agenttype from workflow_currentoperator t " +
                        "where agenttype in (1,2) and requestid = '"+requestid+"' and userid = '"+userid+"' " +
                        "order by t.receivedate desc,t.receivetime desc) where ROWNUM <= 1 ";
                bs.writeLog(sql);
                rs.executeQuery(sql);
                if(rs.next()){
                    String agentorbyagentid = Util.null2String(rs.getString("agentorbyagentid"));
                    String agenttype = Util.null2String(rs.getString("agenttype"));
                    String agentName = resourceComInfo.getLastname(agentorbyagentid);
                    bs.writeLog("agentorbyagentid:"+agentorbyagentid);
                    bs.writeLog("agentName:"+agentName);
                    bs.writeLog("agenttype:"+agenttype);
                    if("1".equals(agenttype)){
                        valueList.add(agentorbyagentid);
                        agentMap.put("id",agentorbyagentid);
                        agentMap.put("name",agentName+"("+userName+"授权)");
                    }else{
                        valueList.add(userid);
                        agentMap.put("id",userid);
                        agentMap.put("name",userName+"("+agentName+"授权)");
                    }
                }else {
                    valueList.add(userid);
                    agentMap.put("id",userid);
                    agentMap.put("name",userName);
                }
                agentList.add(agentMap);
            }
            resultMap.put("value", StringUtils.join(valueList, ","));
            resultMap.put("specialobj",agentList);
            resultMap.put("api_status",true);
        }catch(Exception e){
            e.printStackTrace();
            resultMap.put("api_status",false);
            resultMap.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return resultMap;
    }
}
