package org.dromara.pay.controller.app;

import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.pay.domain.bo.PayRefundBo;
import org.dromara.pay.domain.vo.PayRefundVo;
import org.dromara.pay.service.PayRefundService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 移动端退款Controller
 *
 * @author pengles
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/pay/refund")
public class AppPayRefundController extends BaseController {

    private final PayRefundService refundService;

    /**
     * 申请退款
     */
    @PostMapping("/apply")
    @Log(title = "申请退款", businessType = BusinessType.INSERT)
    public R<Void> applyRefund(@Validated @RequestBody PayRefundBo bo) {
        return toAjax(refundService.createRefund(bo));
    }

    /**
     * 查询退款订单列表
     */
    @GetMapping("/list")
    public TableDataInfo<PayRefundVo> list(PayRefundBo bo, PageQuery pageQuery) {
        return refundService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询退款订单详情
     */
    @GetMapping("/{id}")
    public R<PayRefundVo> getInfo(@PathVariable Long id) {
        return R.ok(refundService.queryById(id));
    }

    /**
     * 根据商户退款编号查询
     */
    @GetMapping("/query")
    public R<PayRefundVo> queryByMerchantRefundId(@RequestParam String merchantRefundId) {
        PayRefundBo bo = new PayRefundBo();
        bo.setMerchantRefundId(merchantRefundId);
        List<PayRefundVo> list = refundService.queryList(bo);
        if (list.isEmpty()) {
            return R.fail("退款订单不存在");
        }
        return R.ok(list.get(0));
    }

}
