package com.wzj.soopin.transaction.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import com.wzj.soopin.transaction.domain.bo.MerchantAddBO;
import com.wzj.soopin.transaction.domain.bo.PaymentBO;
import com.wzj.soopin.order.domain.bo.RefundBO;
import com.wzj.soopin.transaction.domain.bo.easypay.EasyPayRequest;
import com.wzj.soopin.transaction.domain.bo.easypay.separate.apply.resp.SeparateRespOrderInfo;
import com.wzj.soopin.transaction.domain.vo.EasypayTransResultVO;
import com.wzj.soopin.transaction.domain.vo.EasypayPrePayVO;
import com.wzj.soopin.transaction.service.IEasypayService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.rmi.ServerException;
import java.util.HashMap;
import java.util.Map;

/**
 * 易生支付
 */
@RestController
@Slf4j
@RequestMapping("/trans/easypay")
@RequiredArgsConstructor
@Validated
public class TransEasypayController {

    private final IEasypayService easypayService;

    /**
     * 处理易生支付结果通知回调  处理
     *
     * @param easyPayRequest
     */
    @SaIgnore
    @PostMapping("/trade/callback")
    public Map tradeCallback(@RequestBody EasyPayRequest easyPayRequest) {
        easypayService.handleTradeCallback(easyPayRequest);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", "000000");
        map.put("msg", "Success");
        return map;
    }

    /**
     * 发起支付
     *
     * @param paymentBO
     */
    @Log(title = "易生支付-发起支付", businessType = BusinessType.OTHER)
    @PostMapping("/trade")
    @SaIgnore
    public R<EasypayPrePayVO> trade(@RequestBody PaymentBO paymentBO) throws ServerException {
        EasypayPrePayVO easypayPrePayVO = easypayService.payment(paymentBO);
        return R.ok(easypayPrePayVO);
    }

    /**
     * 查询支付结果
     *
     * @param orderId 订单id
     */
    @GetMapping("/paymentQuery/{orderId}")
    public R<EasypayTransResultVO> paymentQuery(@PathVariable("orderId") Long orderId) throws ServerException {
        EasypayTransResultVO easypayTransResultVO = easypayService.paymentQuery(orderId);
        return R.ok(easypayTransResultVO);
    }

    /**
     * 实时退款
     *
     * @param refundBO 退款信息
     */
    @Log(title = "易生支付-实时退款", businessType = BusinessType.OTHER)
    @PostMapping("/refund")
    @SaCheckPermission(value = "trans:easypay:refund",mode = SaMode.OR, orRole = TenantConstants.TENANT_ADMIN_ROLE_KEY)
    public R refund(@RequestBody RefundBO refundBO) throws ServerException {
        easypayService.refund(refundBO);
        return R.ok();
    }

    /**
     * 查询退款结果
     *
     * @param orderId 订单id
     */
    @GetMapping("/refundQuery/{orderId}")
    public R<EasypayTransResultVO> refundQuery(@PathVariable("orderId") Long orderId) throws ServerException {
        EasypayTransResultVO easypayTransResultVO = easypayService.refundQuery(orderId);
        return R.ok(easypayTransResultVO);
    }


    /**
     * 查询分账结果
     *
     * @param orderItemId 订单详情id
     */
    @GetMapping("/separateqQuery/{orderItemId}")
    public R<SeparateRespOrderInfo> separateqQuery(@PathVariable("orderItemId") Long orderItemId) throws ServerException {
        return R.ok(easypayService.separateqQuery(orderItemId));
    }

    /**
     * 上传图片至易生
     *
     * @param pic
     */
    @PostMapping("/merchantPicUpload")
    @SaIgnore
    public R<String> merchantPicUpload(@RequestParam("pic") MultipartFile pic) throws IOException {
        return R.ok(easypayService.merchantPicUpload(pic));
    }

    /**
     * 添加分账商户
     *
     * @param merchantAddBO
     */
    @PostMapping("/merchantAdd")
    @SaIgnore
    public R merchantAdd(@RequestBody MerchantAddBO merchantAddBO) throws ServerException {
        easypayService.merchantAdd(merchantAddBO);
        return R.ok();
    }

}
