package com.engine.htsc.payment.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.payment.pojo.SettlePayAutoRequestParam;
import com.engine.htsc.payment.pojo.SettlePaymentSplLinkReq;
import com.engine.htsc.payment.service.SettlePaymentFlowService;
import com.engine.htsc.payment.service.impl.SettlePaymentFlowServiceImpl;
import com.engine.htsc.subcompany.dao.UfZgssplDao;
import com.engine.htsc.subcompany.model.SubCompanySplLinkReq;
import com.engine.htsc.trip.util.AjaxResult;
import com.informix.lang.JavaToIfxType;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.payment.SettlePaymentInvoke;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import javax.servlet.http.HttpServletRequest;
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 SettlePaymentFlowAction {


    private SettlePaymentFlowService getServices() {
        return ServiceUtil.getService(SettlePaymentFlowServiceImpl.class);
    }

    protected UfZgssplDao ufZgssplDao = new UfZgssplDao();


    /**
     * 采购商城结算付款自动构建结算付款流程
     * @param requestid
     * @return
     */
    @POST
    @Path("/createFlow")
    @Produces({MediaType.APPLICATION_JSON})
    public Map<String,Object> createBusinessTravelWorkflow(@QueryParam("requestid") String requestid){
        Map<String, Object> apidatas = new HashMap<>();
        try {
//            apidatas = this.getService().createWorkflow(ids);
            JAVATools.SetCmdwritelog("requestid: ", requestid+" 采购商城结算付款自动构建结算付款流程 ");
            List<SettlePayAutoRequestParam> paramList = this.getServices().getParam(requestid);
            String requestidNew = this.getServices().createWorkflowMain(paramList);
            if (!requestidNew.contains("-")&&!"".equals(requestidNew)){
                apidatas.put("code", 200);
                apidatas.put("message","接口调用成功 新流程requestid："+requestidNew);
            }else {
                apidatas.put("code", -1);
                apidatas.put("message","创建流程失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("code",-1);
            apidatas.put("message","接口调用失败");
            apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
        }
        return apidatas;
    }

    /**
     * 采购商城结算付款调用审批链接口,保存到主表
     * @param requestid
     * @return
     */
    @POST
    @Path("/getSettlePaymentSpl")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSettlePaymentSpl(@QueryParam("requestid") String requestid) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JAVATools.SetCmdwritelog("requestid: ", requestid+" 采购商城系统发起节点提交，调用审批链接口,保存到主表 ");
            if(!"".equals(Util.null2String(requestid))){
                HashMap<String,String> reqMap = this.getServices().getSplParam(requestid);
                reqMap.put("requestid",requestid);
                this.getServices().saveSubCompanySpl(reqMap);
                apidatas.put("success",true);
                apidatas.put("msg","========更新成功========");
                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);
    }


    /**
     * 采购商城预算接口
     * @param requestid
     * @return
     */
    @POST
    @Path("/paymentOccupation")
    @Produces(MediaType.TEXT_PLAIN)
    public String paymentOccupation(@QueryParam("requestid") String requestid, @QueryParam("billStatus") String billStatus, @QueryParam("src") String src) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            JAVATools.SetCmdwritelog("requestid: ", requestid+" 采购商城预占，流程状态： "+src);
            if(!"".equals(Util.null2String(requestid))){
                RequestInfo requestInfo = new RequestInfo();
                RequestManager requestManager = new RequestManager();
                requestManager.setSrc(src);
                requestInfo.setRequestManager(requestManager);
                requestInfo.setRequestid(requestid);
                SettlePaymentInvoke settlePaymentInvoke = new SettlePaymentInvoke();
                settlePaymentInvoke.setBillStatus(billStatus);
                String result = settlePaymentInvoke.execute(requestInfo);
                SettlePaymentFlowServiceImpl settlePaymentFlowService = new SettlePaymentFlowServiceImpl();
                settlePaymentFlowService.updatepayStatus("",requestid,"5");
                apidatas.put("msg",result);
            }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);
    }


    /**
     * 预算实占、废弃 总部
     *
     * @param request 请求体
     * @return 消息体
     */
    @POST
    @Path("/doBudgetZb")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<Map<String, String>> doBudgetZb(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String rid = Util.null2String(requestParam.getOrDefault("rid", ""));
            String discard = Util.null2String(requestParam.getOrDefault("discard", ""));
            return AjaxResult.ok(this.getServices().doBudgetByBatchIdZb(rid, discard));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }

    /**
     * 核算 总部
     *
     * @param request 请求体
     * @return 消息体
     */
    @POST
    @Path("/doAccountZb")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<Map<String, String>> doAccountZb(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String id = Util.null2String(requestParam.getOrDefault("id", ""));
            return AjaxResult.ok(this.getServices().doAccountByBatchIdZb(id));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }



    /**
     * 预算实占、废弃 分支
     *
     * @param request 请求体
     * @return 消息体
     */
    @POST
    @Path("/doBudgetFzjg")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<Map<String, String>> doBudgetFzjg(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String rid = Util.null2String(requestParam.getOrDefault("rid", ""));
            String discard = Util.null2String(requestParam.getOrDefault("discard", ""));
            return AjaxResult.ok(this.getServices().doBudgetByBatchIdFzjg(rid, discard));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }

    /**
     * 核算 分支
     *
     * @param request 请求体
     * @return 消息体
     */
    @POST
    @Path("/doAccountFzjg")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<Map<String, String>> doAccountFzjg(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String id = Util.null2String(requestParam.getOrDefault("id", ""));
            return AjaxResult.ok(this.getServices().doAccountByBatchIdFzjg(id));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }


    /**
     * 页面获取审批链数据
     * @param settlePaymentSplLinkReq
     * @return
     */
    @POST
    @Path("/getSettlePaymentSplWeb")
    @Produces({MediaType.TEXT_PLAIN})
    @Consumes({MediaType.APPLICATION_JSON})
    public String getSettlePaymentSplWeb(SettlePaymentSplLinkReq settlePaymentSplLinkReq){
        String unnumber = com.weaver.general.Util.null2String(settlePaymentSplLinkReq.getUnnumber());
        String unnumbercode = com.weaver.general.Util.null2String(settlePaymentSplLinkReq.getUnnumbercode());
        JSONObject jsonObject = settlePaymentSplLinkReq.getValue();
        if(jsonObject==null||"".equals(jsonObject)){
            return  JAVATools.Apiretrun("400","参数为空");
        }
        JAVATools.writelog("获取审批链入参: unnumber:"+unnumber+" unnumbercode:"+unnumbercode+" value:"+jsonObject.toJSONString());
        HashMap<String,String> req = JSON.parseObject(jsonObject.toString(),HashMap.class);
        req.put("unnumber", unnumber);
        req.put("unnumbercode",unnumbercode);
        JSONObject res = this.getServices().getSettlePaymentSplWeb(req);
        return JAVATools.Apiretrun("200", res);
    }

    /**
     * 付款 总部
     *
     * @param request 请求体
     * @return 消息体
     */
    @POST
    @Path("/doPaymentZb")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<Map<String, String>> doPaymentZb(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String id = Util.null2String(requestParam.getOrDefault("id", ""));
            return AjaxResult.ok(this.getServices().doPaymentByBatchIdZb(id));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }

    /**
     * 手动付款 总部
     */
    @POST
    @Path("/doManualPaymentZb")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> doManualPaymentZb(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String id = Util.null2String(requestParam.getOrDefault("id", ""));
            return AjaxResult.ok(this.getServices().updateManualPaymentStatusZb(id));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }

    /**
     * 付款 分支
     *
     * @param request 请求体
     * @return 消息体
     */
    @POST
    @Path("/doPaymentFzjg")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<Map<String, String>> doPaymentFzjg(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String id = Util.null2String(requestParam.getOrDefault("id", ""));
            return AjaxResult.ok(this.getServices().doPaymentByBatchIdFzjg(id));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }

    /**
     * 手动付款 分支
     */
    @POST
    @Path("/doManualPaymentFzjg")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public AjaxResult<String> doManualPaymentFzjg(@Context HttpServletRequest request) {
        try {
            Map<String, Object> requestParam = ParamUtil.request2Map(request);
            String id = Util.null2String(requestParam.getOrDefault("id", ""));
            return AjaxResult.ok(this.getServices().updateManualPaymentStatusFzjg(id));
        } catch (Exception e) {
            JAVATools.writelog(e.getMessage());
            return AjaxResult.error(e.getMessage());

        }
    }


}
