package com.yiqi.admin.controller.recharge;

import com.yiqi.common.annotation.Log;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AjaxResult;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.core.page.TableDataInfo;
import com.yiqi.common.enums.BusinessType;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.utils.poi.ExcelUtil;
import com.yiqi.lock.RedisLockUtil;
import com.yiqi.order.domain.OrderRechargeRefund;
import com.yiqi.order.domain.dto.OrderRechargeRefundDTO;
import com.yiqi.order.domain.dto.OrderRechargeRefundQueryDTO;
import com.yiqi.order.domain.vo.OrderRechargeListVO;
import com.yiqi.order.domain.vo.OrderRechargeRefundListVO;
import com.yiqi.order.service.IOrderRechargeRefundService;
import com.yiqi.order.service.IOrderRechargeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 退卡管理Controller
 *
 * @author ruoyi
 * @date 2023-09-27
 */
@Api(tags = "管理后台 - 退卡管理")
@RestController
@RequestMapping(UrlConstants.managerApi + "/recharge/refund")
public class RechargeRefundController extends BaseController {
    @Autowired
    private IOrderRechargeService orderRechargeService;

    @Autowired
    private IOrderRechargeRefundService orderRechargeRefundService;

    /**
     * 充值订单-退款列表
     */
    @GetMapping("/refundRechargeOrderList")
    @ApiOperation(value = "查询撤单退款充值订单列表")
    public TableDataInfo refundRechargeOrderList(OrderRechargeRefundQueryDTO orderRechargeRefundQueryDTO) {
        startPage();
        List<OrderRechargeRefundListVO> list = orderRechargeService.refundRechargeOrderList(orderRechargeRefundQueryDTO);
        return getDataTable(list);
    }

    /**
     * 导出退款充值订单列表
     */
    @PreAuthorize("@ss.hasPermi('recharge:order:export')")
    @Log(title = "退款充值订单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportByRefund")
    @ApiOperation(value = "导出退款充值订单列表")
    public void exportByRefund(HttpServletResponse response, OrderRechargeRefundQueryDTO orderRechargeRefundQueryDTO) {
        orderRechargeRefundQueryDTO.setPageSizeExport(50000);
        List<OrderRechargeRefundListVO> list = orderRechargeService.refundRechargeOrderList(orderRechargeRefundQueryDTO);
        ExcelUtil<OrderRechargeRefundListVO> util = new ExcelUtil<OrderRechargeRefundListVO>(OrderRechargeRefundListVO.class);
        util.exportExcel(response, list, "充值退款订单数据");
    }

    @PreAuthorize("@ss.hasPermi('recharge:refund:list')")
    @GetMapping("/list1")
    @ApiOperation(value = "充值管理-退卡管理")
    public TableDataInfo refundList(OrderRechargeListVO orderRechargeListVO) {
        startPage();
        List<OrderRechargeListVO> list = orderRechargeService.selectRefundList(orderRechargeListVO);
        return getDataTable(list);
    }


    /**
     * 充值订单-撤单退款
     */
    @PostMapping("/refundRecharge")
    @ApiOperation(value = "充值订单-撤单退款")
    @Log(title = "充值撤单退款", businessType = BusinessType.INSERT)
    public R refundRecharge(@RequestBody OrderRechargeRefundDTO orderRechargeRefundDTO) throws InterruptedException {
        return R.ok(orderRechargeService.refundRechargeApply(orderRechargeRefundDTO));
    }

    /**
     * 退卡审核
     */
    @PreAuthorize("@ss.hasPermi('recharge:refund:edit')")
    @Log(title = "退卡审核拒绝", businessType = BusinessType.UPDATE)
    @GetMapping("/refundApprovalNo")
    @ApiOperation(value = "退卡审核拒绝")
    public AjaxResult refundApprovalNo(@RequestParam Long id) {
        OrderRechargeRefund orderRechargeRefund = orderRechargeRefundService.getById(id);
        orderRechargeRefund.setUpdateById(getUserId());
        orderRechargeRefund.setUpdateBy(getUsername());
        return toAjax(orderRechargeService.refundRechargeNo(orderRechargeRefund));
    }

    /**
     * 退卡审核
     */
    @PreAuthorize("@ss.hasPermi('recharge:refund:edit')")
    @Log(title = "退卡审核同意", businessType = BusinessType.UPDATE)
    @GetMapping("/refundApprovalYes")
    @ApiOperation(value = "退卡审核同意")
    public AjaxResult refundApprovalYes(@RequestParam Long id) {
        OrderRechargeRefund orderRechargeRefund = orderRechargeRefundService.getById(id);
        orderRechargeRefund.setUpdateById(getUserId());
        orderRechargeRefund.setUpdateBy(getUsername());
        return toAjax(orderRechargeService.refundRechargeYes(orderRechargeRefund));
    }

}
