package com.ruoyi.web.controller.system;

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

import com.ruoyi.ss.card.service.ISsCardAssembler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.card.domain.SsCard;
import com.ruoyi.ss.card.domain.SsCardVO;
import com.ruoyi.ss.card.domain.SsCardQuery;
import com.ruoyi.ss.card.service.ISsCardService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 卡券Controller
 *
 * @author qzz
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/system/card")
public class CardController extends BaseController
{
    @Autowired
    private ISsCardService ssCardService;

    @Autowired
    private ISsCardAssembler ssCardAssembler;


    /**
     * 查询卡券列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(SsCardQuery query)
    {
        startPage();
        startOrderBy();
        List<SsCardVO> list = ssCardService.selectSsCardList(query);
        ssCardAssembler.assembleInfo(list);
        return getDataTable(list);
    }

    /**
     * 导出卡券列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:export')")
    @Log(title = "卡券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SsCardQuery query)
    {
        List<SsCardVO> list = ssCardService.selectSsCardList(query);
        ExcelUtil<SsCardVO> util = new ExcelUtil<SsCardVO>(SsCardVO.class);
        util.exportExcel(response, list, "卡券数据");
    }

    /**
     * 获取卡券详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:card:query')")
    @GetMapping(value = "/{couponId}")
    public AjaxResult getInfo(@PathVariable("couponId") Long couponId)
    {
        return success(ssCardService.selectSsCardByCouponId(couponId));
    }

    /**
     * 新增卡券
     */
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @Log(title = "卡券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SsCard ssCard)
    {
        return toAjax(ssCardService.insertSsCard(ssCard));
    }

    /**
     * 修改卡券
     */
    @PreAuthorize("@ss.hasPermi('system:card:edit')")
    @Log(title = "卡券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SsCard ssCard)
    {
        return toAjax(ssCardService.updateSsCard(ssCard));
    }

    /**
     * 删除卡券
     */
    @PreAuthorize("@ss.hasPermi('system:card:remove')")
    @Log(title = "卡券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(ssCardService.deleteSsCardByCouponIds(couponIds));
    }
}
