package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TbSalesDiscount;
import com.ruoyi.project.business.domain.vo.salesDiscount.SalesDiscountReq;
import com.ruoyi.project.business.service.ITbSalesDiscountService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 销售折扣率Controller
 *
 * @author ruoyi
 * @date 2024-05-14
 */
@RestController
@RequestMapping("/user/discount")
@RequiredArgsConstructor
public class TbSalesDiscountController extends BaseController {
    private final ITbSalesDiscountService tbSalesDiscountService;

    /**
     * 查询销售折扣率列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SalesDiscountReq salesDiscountReq) {
        startPage();
        List<TbSalesDiscount> list = tbSalesDiscountService.selectTbSalesDiscountList(salesDiscountReq);
        return getDataTable(list);
    }

    /**
     * 导出销售折扣率列表
     */
    @Log(title = "销售折扣率", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalesDiscountReq salesDiscountReq) {
        List<TbSalesDiscount> list = tbSalesDiscountService.selectTbSalesDiscountList(salesDiscountReq);
        ExcelUtil<TbSalesDiscount> util = new ExcelUtil<>(TbSalesDiscount.class);
        util.exportExcel(response, list, "销售折扣率数据");
    }

    /**
     * 获取销售折扣率详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tbSalesDiscountService.getById(id));
    }

    /**
     * 新增销售折扣率
     */
    @Log(title = "销售折扣率", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbSalesDiscount tbSalesDiscount) {
        return toAjax(tbSalesDiscountService.add(tbSalesDiscount));
    }

    /**
     * 修改销售折扣率
     */
    @Log(title = "销售折扣率", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbSalesDiscount tbSalesDiscount) {
        return toAjax(tbSalesDiscountService.updateById(tbSalesDiscount));
    }

    /**
     * 删除销售折扣率
     */
    @Log(title = "销售折扣率", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbSalesDiscountService.removeByIds(Arrays.asList(ids)));
    }
}
