package com.engine.htsc.subcompany.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.subcompany.model.SubCompanySplLinkReq;
import com.engine.htsc.subcompany.service.SubCompanyLinkService;
import com.engine.htsc.subcompany.service.impl.*;
import com.weaver.general.Util;
import weaver.conn.RecordSet;
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 SubCompanyLinkController {

    SubCompanyLinkService getService(String unumber) {
        if("lhtybx".equals(unumber)) {
            //联合通用报销
            return new LhSubCompanyLinkServiceImpl();
        }else if("lhghfk".equals(unumber)) {
            //联合工会付款
            return new LhghgkSubCompanyLinkServiceImpl();
        }else if("zjtybx".equals(unumber)) {
            //紫金通用报销
            return new ZjSubCompanyLinkServiceImpl();
        }else if("zjclbx".equals(unumber)) {
            //紫金差旅报销
            return new ZjclSubCompanyLinkServiceImpl();
        }else if("xgtybx".equals(unumber)) {
            //香港通用报销
            return new XgtySubCompanyLinkServiceImpl();
        }else if("xgtyfk".equals(unumber)) {
            //香港通用付款
            return new XgtyfkSubCompanyLinkServiceImpl();
        }else if("zgtyfk".equals(unumber)) {
            //资管通用付款
            return new ZgfkSubCompanyLinkServiceImpl();
        }else if("zgsclbx".equals(unumber)) {
            //子公司差旅报销
            return new SubClCompanyLinkServiceImpl();
        }else{
            //子公司通用报销，资管通用报销，资管差旅报销
            return new SubCompanyLinkServiceImpl();
        }
    }

    @POST
    @Path("/getSubCompanySpl")
    @Produces({MediaType.TEXT_PLAIN})
    @Consumes({MediaType.APPLICATION_JSON})
    public String getSubCompanySpl(SubCompanySplLinkReq subCompanySplLinkReq){
        String unnumber = Util.null2String(subCompanySplLinkReq.getUnnumber());
        String unnumbercode = Util.null2String(subCompanySplLinkReq.getUnnumbercode());
        JSONArray jsonArray = subCompanySplLinkReq.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(unnumber).getSubCompanySpl(reqMap);
        return JAVATools.Apiretrun("200", res);
    }

    /**
     * 香港通用付款系统发起节点提交，调用审批链接口,保存到主表
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/xgtyfkSaveSubCompanySpl")
    @Produces(MediaType.TEXT_PLAIN)
    public String xgtyfkSaveSubCompanySpl(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String requestid = weaver.general.Util.null2String(request.getParameter("requestid"));
            JAVATools.SetCmdwritelog("香港通用付款系统发起节点提交，调用审批链接口,保存到主表 requestid====: ", requestid);
            if(!"".equals(requestid)){
                XgtyfkSubCompanyLinkServiceImpl xgtyfkSubCompanyLinkService = new XgtyfkSubCompanyLinkServiceImpl();
                xgtyfkSubCompanyLinkService.saveSubCompanySpl(requestid);
                apidatas.put("success",true);
                apidatas.put("msg","updatezw====更新成功");
                apidatas.put("requestid",requestid);
            }else{
                apidatas.put("success",false);
                apidatas.put("msg","requestid存在空");
            }
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
            apidatas.put("msg", "系统内部错误");
        }
        return JSONObject.toJSONString(apidatas);
    }


}
