package com.ruoyi.frequency.mallgoods.controller;

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.frequency.mallgoods.entity.MallGoods;
import com.ruoyi.frequency.mallgoods.service.MallGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品Controller
 *
 * @author liwenlong
 * @date 2024-01-25
 */
@RestController
@RequestMapping("/frequency/mallgoods")
public class MallGoodsController extends BaseController
{
    @Autowired
    private MallGoodsService mallGoodsService;

    /**
     * 查询商品列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:mallgoods:list')")
    @PostMapping("/list")
    public TableDataInfo<MallGoods> list(@RequestBody MallGoods mallGoods)
    {
        startPage();
        List<MallGoods> list = mallGoodsService.selectMallGoodsList(mallGoods);
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('frequency:mallgoods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallGoods mallGoods)
    {
        List<MallGoods> list = mallGoodsService.selectMallGoodsList(mallGoods);
        ExcelUtil<MallGoods> util = new ExcelUtil<MallGoods>(MallGoods.class);
        return util.exportExcel(list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('frequency:mallgoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<MallGoods> getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallGoodsService.selectMallGoodsById(id));
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('frequency:mallgoods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallGoods mallGoods)
    {
        return toAjax(mallGoodsService.insertMallGoods(mallGoods));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('frequency:mallgoods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallGoods mallGoods)
    {
        return toAjax(mallGoodsService.updateMallGoods(mallGoods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('frequency:mallgoods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}/{delFlag}")
    public AjaxResult remove(@PathVariable("ids") Long[] ids,@PathVariable("delFlag") Integer delFlag)
    {
        return toAjax(mallGoodsService.deleteMallGoodsByIds(ids,delFlag));
    }
}
