package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ss.refund.domain.Refund;
import com.ruoyi.ss.refund.domain.RefundBO;
import com.ruoyi.ss.refund.domain.RefundDTO;
import com.ruoyi.ss.refund.domain.RefundQuery;
import com.ruoyi.ss.refund.service.IRefundService;
import com.ruoyi.ss.refund.service.IRefundValidator;
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 邱贞招
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/system/refund")
public class RefundController extends BaseController
{
    @Autowired
    private IRefundService etRefundService;

    @Resource
    private IRefundValidator refundValidator;

    /**
     * 查询退款订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:refund:list')")
    @GetMapping("/list")
    public TableDataInfo list(RefundQuery etRefund)
    {
        startPage();
        List<Refund> list = etRefundService.selectEtRefundListWithIsolate(etRefund);
        return getDataTable(list);
    }

    /**
     * 导出退款订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:refund:export')")
    @Log(title = "退款订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RefundQuery etRefund)
    {
        List<Refund> list = etRefundService.selectEtRefundListWithIsolate(etRefund);
        ExcelUtil<Refund> util = new ExcelUtil<Refund>(Refund.class);
        util.exportExcel(response, list, "退款订单数据");
    }

    /**
     * 获取退款订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:refund:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(etRefundService.selectEtRefundById(id));
    }


    /**
     * 退款
     */
    @Log(title = "退款", businessType = BusinessType.REFUND)
    @PostMapping
    public AjaxResult refund(@RequestBody RefundDTO dto)
    {
        logger.info("退款:dto=【{}】", dto);
        RefundBO bo = refundValidator.preRefund(dto);
        return toAjax(etRefundService.refund(bo));
    }

    /**
     * 修改退款订单
     */
    @PreAuthorize("@ss.hasPermi('system:refund:edit')")
    @Log(title = "退款订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Refund etRefund)
    {
        return toAjax(etRefundService.updateEtRefund(etRefund));
    }

    /**
     * 删除退款订单
     */
    @PreAuthorize("@ss.hasPermi('system:refund:remove')")
    @Log(title = "退款订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(etRefundService.deleteEtRefundByIds(ids));
    }
}
