package com.engine.htsc.branch.approvalChain.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.branch.approvalChain.model.BranchLinkReq;
import com.engine.htsc.branch.approvalChain.service.FzjgTravelLinkService;
import com.engine.htsc.branch.approvalChain.service.impl.FzjgTravelLinkServiceImpl;
import com.weaver.general.Util;
import weaver.general.GCONST;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
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 FzjgTravelLinkController {

    FzjgTravelLinkService getService() {
        return ServiceUtil.getService(FzjgTravelLinkServiceImpl.class);
    }

    @POST
    @Path("/getBranchSpl")
    @Produces({MediaType.TEXT_PLAIN})
    @Consumes({MediaType.APPLICATION_JSON})
    public String getFzjgDetail(BranchLinkReq branchLinkReq){
        String unnumber = Util.null2String(branchLinkReq.getUnnumber());
        String unnumbercode = Util.null2String(branchLinkReq.getUnnumbercode());
        JSONArray jsonArray = branchLinkReq.getValue();
        if(jsonArray==null||jsonArray.size()<=0||"".equals(jsonArray)){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        JAVATools.writelog("获取审批链入参: unnumber:"+unnumber+" unnumbercode:"+unnumbercode+" value:"+jsonArray.toJSONString());
        List<Map<String,String>> req = (List<Map<String,String>>) JSONArray.parse(String.valueOf(jsonArray));
        HashMap<String,Object> reqMap = new HashMap();
        reqMap.put("unnumber", unnumber);
        reqMap.put("unnumbercode",unnumbercode);
        reqMap.put("reqMapList",req);
        JSONObject res = this.getService().getFzjgDetail(reqMap);
        return JAVATools.Apiretrun("200", res);
    }

    /**
     * 分支机构通用付款获取审批链,保存到主表
     * @param requestids
     * @return
     */
    @POST
    @Path("/getSaveBranchSpl")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSaveBranchSpl(@QueryParam("requestids") List<String> requestids) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JAVATools.SetCmdwritelog("requestids: ", requestids+" 分支机构通用付款获取审批链,保存到主表 ");
            if(null!=requestids&&requestids.size()>0){
                this.getService().getSaveBranchSpl(requestids);
                apidatas.put("success",true);
                apidatas.put("msg","========更新成功========");
                apidatas.put("requestids",requestids);
            }else{
                apidatas.put("success",false);
                apidatas.put("msg","requestids为空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
            apidatas.put("msg", "系统内部错误");
        }
        return JSONObject.toJSONString(apidatas);
    }


    @POST
    @Path("/getCwzxhj")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCwzxhj(@Context HttpServletRequest request,
                            @Context HttpServletResponse response){
        String bm = Util.null2String(request.getParameter("value"));
        if("".equals(bm)){
            return JAVATools.Apiretrun("400","参数不允许为空");
        }
        String res = this.getService().getCwzxhj(bm);
        return JAVATools.Apiretrun("200",res);
    }
    
    public static void main(String[] args) {
        GCONST.setRootPath("");
        GCONST.setServerName("ecology");
        BranchLinkReq branchLinkReq = new BranchLinkReq();
        branchLinkReq.setUnnumber("fzjgclbx");
        branchLinkReq.setUnnumbercode("fzjgclbx");
        String jsonValue = "[{ngr: \"23626\", bxry: \"28159\", sfzf: \"1\", zfmxhj: \"0.00\", bxrbmfzr: \"23626\", fzzdsfgfyhj: \"0\"},{ngr: \"23626\", bxry: \"23626\", sfzf: \"1\", zfmxhj: \"0.00\", bxrbmfzr: \"23626\", fzzdsfgfyhj: \"0\"}]";
        branchLinkReq.setValue(JSON.parseArray(jsonValue));
        FzjgTravelLinkController fzjgTravelLinkController = new FzjgTravelLinkController();
        fzjgTravelLinkController.getFzjgDetail(branchLinkReq);
    }
}
