package com.engine.htsc.cgscfyqr.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.cgscfyqr.service.CgscfyqrService;
import com.engine.htsc.cgscfyqr.service.impl.CgscfyqrServiceImpl;
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.HashMap;
import java.util.List;
import java.util.Map;

public class CgscfyqrAction {

    private CgscfyqrService getService() {
        return ServiceUtil.getService(CgscfyqrServiceImpl.class);
    }

    /*
     * 获取指定流程原本的操作者是否被授权
     * @return 返回部门ID或人员ID
     * */
    @GET
    @Path("/getempower")
    @Produces({MediaType.APPLICATION_JSON})
    public String getEmpower(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        BaseBean bb = new BaseBean();
        boolean flag = true;
        bb.writeLog("=============================采购商城费用确认流程API-Check-In==========================");
        Map<String, Object> params = new HashMap<>();
        String type = Util.null2String(request.getParameter("type"));
        String wfid = Util.null2String(request.getParameter("workflowid"));
        String id = Util.null2String(request.getParameter("id"));
        params.put("id", id);
        params.put("wfid", wfid);
        bb.writeLog("=========接受的参数为：type:" + type + ",wfid:" + wfid + ",id:" + id);
        if ("".equals(type) || "".equals(wfid) || "".equals(id)) {
            result.put("code", "500");
            result.put("message", "必要参数为空，无法进行");
            flag = false;
        } else if ("id".equals(type)) {
            params.put("type", "id");
        } else if ("dept".equals(type)) {
            params.put("type", "dept");
        }
        if (flag) {
            if (params.size() != 0) {
                Map<String, Object> serviceBack = getService().getEmpowerInfo(params);
                String serviceType = Util.null2String(serviceBack.get("type"));
                result.put("code", "200");
                result.put("id".equals(serviceType) ? "empowerId" : "empowerDeptid", "id".equals(serviceType) ? Util.null2String(serviceBack.get("empowerId")) : Util.null2String(serviceBack.get("empowerDeptid")));
            } else {
                result.put("code", "405");
                result.put("message", "准备传递给service的参数size为0，请检查");
                bb.writeLog("===================准备传递给service的参数size为0，请检查=================");
            }
        }

        return JSONObject.toJSONString(result);
    }

    /*
     * 获取明细表4数据
     * 入参:String-deptCode;String-type;String-requestid
     * 出参:List<Map<String,Object>>-needshow
     * */
    @GET
    @Path("/getdtfourdata")
    @Produces({MediaType.APPLICATION_JSON})
    public String getDt4Data(@Context HttpServletRequest request){
        List<Map<String,Object>> needshow;
        Map<String,Object> paramsMap = new HashMap<>();
        paramsMap.put("deptCode",request.getParameter("deptCode"));
        paramsMap.put("type",request.getParameter("type"));
        paramsMap.put("requestid", request.getParameter("requestid"));
        needshow = getService().getDt4Data(paramsMap);
        return JSONObject.toJSONString(needshow);
    }
}
