package cn.pluss.platform.controller.home;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.dto.jft.JftPayDataDTO;
import cn.pluss.platform.entity.JftReceiptInfo;
import cn.pluss.platform.entity.JftReceiptOrder;
import cn.pluss.platform.jft.JftReceiptInfoService;
import cn.pluss.platform.jft.JftReceiptOrderService;
import cn.pluss.platform.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import mob.push.api.http.Http;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO
 * 缴费通入口
 * @author crystal
 * @date 2022/6/22 17:28
 */
@Controller
@RequestMapping(value = "jft")
public class JftController {

    @Resource
    private JftReceiptInfoService jftReceiptInfoService;

    @Resource
    private JftReceiptOrderService jftReceiptOrderService;

    @RequestMapping(value = "/auth")
    public ModelAndView auth(HttpServletRequest request, HttpServletResponse response, String tk){
        try {
            JftReceiptInfo receiptInfo = jftReceiptInfoService.wapCheckTk(tk,request);
            response.sendRedirect(receiptInfo.getJumpUrl());
            return null;
        }catch (Exception e){
            ModelAndView view = new ModelAndView("merchant/errorInfo");
            request.setAttribute("error",e.getMessage());
            return view;
        }
    }

    @RequestMapping(value = "/pay")
    public String pay(HttpServletRequest request,HttpServletResponse response,String tk,String auth_code,String userId){
        try {
            jftReceiptInfoService.getPayInfo(request,response,tk,auth_code,userId);
            return "jft/th_jft_pay";
        }catch (Exception e){
            request.setAttribute("error",e.getMessage());
            return "merchant/errorInfo2";
        }
    }

    @RequestMapping(value = "/jump")
    public String pay(HttpServletRequest request,String orderNumber){
        JftReceiptOrder orderInfo = jftReceiptOrderService.getOrderByNo(orderNumber);
        JftReceiptInfo receiptInfo = jftReceiptInfoService.getById(orderInfo.getRid());
        request.setAttribute("merchantName",orderInfo.getMerchantName());
        request.setAttribute("payTypeDesc",orderInfo.getPayTypeDesc());
        request.setAttribute("amount",orderInfo.getAmount());
        request.setAttribute("pName",receiptInfo.getProjectName());
        request.setAttribute("orderNumber",orderInfo.getOrderNumber());
        request.setAttribute("date", DateUtils.toString(orderInfo.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        return "jft/th_jft_success";
    }

    /**
     * 发起支付
     * @param dto
     * @return
     */
    @RequestMapping(value = "/tradePay")
    @ResponseBody
    public Result<Object> tradePay(JftPayDataDTO dto,HttpServletRequest request){
        return  jftReceiptOrderService.tradePay(dto,request);
    }
}
