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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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;

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

    private final IEasypayService easypayService;


    @SaIgnore
    @Log(title = "易生支付-处理结果回调", businessType = BusinessType.OTHER)
    @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;
    }


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


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


    @Operation(summary = "实时退款")
    @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();
    }


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



    @Operation(summary = "查询分账结果")
    @GetMapping("/separateqQuery/{orderItemId}")
    public R<SeparateRespOrderInfo> separateqQuery(@PathVariable("orderItemId") Long orderItemId) throws ServerException {
        return R.ok(easypayService.separateqQuery(orderItemId));
    }


    @Operation(summary = "上传图片至易生")
    @PostMapping("/merchantPicUpload")
    @SaCheckPermission(value = "trans:easypay:merchantPicUpload",mode = SaMode.OR, orRole = TenantConstants.TENANT_ADMIN_ROLE_KEY)
    public R<String> merchantPicUpload(@RequestParam("pic") MultipartFile pic) throws IOException {
        return R.ok(easypayService.merchantPicUpload(pic));
    }

    @Operation(summary = "商户新增")
    @PostMapping("/merchantAdd")
    @SaCheckPermission(value = "trans:easypay:merchantAdd",mode = SaMode.OR, orRole = TenantConstants.TENANT_ADMIN_ROLE_KEY)
    public R merchantAdd(@RequestBody MerchantAddBO merchantAddBO) throws ServerException {
        easypayService.merchantAdd(merchantAddBO);
        return R.ok();
    }



    @PostMapping("/addSubAppidConfig")
    @Operation(summary = "微信商户新增APPID")
    public R addSubAppidConfig(@RequestParam("subMchtCode") String subMchtCode) throws IOException {
        easypayService.addSubAppidConfig(subMchtCode);
        return R.ok();
    }

}
