package beilv.web.controller.cardpurchaserecord;

import beilv.cardpurchaserecord.domain.CardPurchaseRecord;
import beilv.cardpurchaserecord.service.ICardPurchaseRecordService;
import beilv.common.annotation.Log;
import beilv.common.core.controller.BaseController;
import beilv.common.core.domain.AjaxResult;
import beilv.common.core.page.TableDataInfo;
import beilv.common.enums.BusinessType;
import beilv.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购卡记录Controller
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Controller
@RequestMapping("/record")
public class CardPurchaseRecordController extends BaseController {
    private String prefix = "record";

    @Autowired
    private ICardPurchaseRecordService cardPurchaseRecordService;

    @RequiresPermissions("system:record:view")
    @GetMapping()
    public String record() {
        return prefix + "/record";
    }

    /**
     * 查询购卡记录列表
     */
    @RequiresPermissions("system:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CardPurchaseRecord cardPurchaseRecord) {
        startPage();
        List<CardPurchaseRecord> list = cardPurchaseRecordService.selectCardPurchaseRecordList(cardPurchaseRecord);
        return getDataTable(list);
    }

    /**
     * 导出购卡记录列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "购卡记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CardPurchaseRecord cardPurchaseRecord) {
        List<CardPurchaseRecord> list = cardPurchaseRecordService.selectCardPurchaseRecordList(cardPurchaseRecord);
        ExcelUtil<CardPurchaseRecord> util = new ExcelUtil<CardPurchaseRecord>(CardPurchaseRecord.class);
        return util.exportExcel(list, "购卡记录数据");
    }

    /**
     * 新增购卡记录
     */
    @RequiresPermissions("system:record:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存购卡记录
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "购卡记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CardPurchaseRecord cardPurchaseRecord) {
        return AjaxResult.success(cardPurchaseRecordService.insertCardPurchaseRecord(cardPurchaseRecord));
    }

    /**
     * 修改购卡记录
     */
    @RequiresPermissions("system:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CardPurchaseRecord cardPurchaseRecord = cardPurchaseRecordService.selectCardPurchaseRecordById(id);
        mmap.put("cardPurchaseRecord", cardPurchaseRecord);
        return prefix + "/edit";
    }

    /**
     * 修改保存购卡记录
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "购卡记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CardPurchaseRecord cardPurchaseRecord) {
        return toAjax(cardPurchaseRecordService.updateCardPurchaseRecord(cardPurchaseRecord));
    }

    /**
     * 删除购卡记录
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "购卡记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(cardPurchaseRecordService.deleteCardPurchaseRecordByIds(ids));
    }
}
