package com.ruoyi.mall.controller;

import java.util.List;

import org.apache.ibatis.annotations.Param;
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.mall.domain.MallSeckillGoods;
import com.ruoyi.mall.service.IMallSeckillGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 秒杀商品Controller
 *
 * @author ruoyi
 * @date 2022-07-01
 */
@RestController
@RequestMapping("/mall/mallSeckillGoods")
public class MallSeckillGoodsController extends BaseController
{
    @Autowired
    private IMallSeckillGoodsService mallSeckillGoodsService;

    /**
     * 秒杀可选商品列表
     * @return
     */
    @GetMapping("/enableSeckillGoodsList")
    public AjaxResult enableSeckillGoodsList(){
        try {
            return AjaxResult.success(mallSeckillGoodsService.enableSeckillGoodsList());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }


    /**
     * 查询秒杀商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSeckillGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallSeckillGoods mallSeckillGoods)
    {
        startPage();
        List<MallSeckillGoods> list = mallSeckillGoodsService.selectMallSeckillGoodsList(mallSeckillGoods);
        return getDataTable(list);
    }

    /**
     * 导出秒杀商品列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSeckillGoods:export')")
    @Log(title = "秒杀商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallSeckillGoods mallSeckillGoods)
    {
        List<MallSeckillGoods> list = mallSeckillGoodsService.selectMallSeckillGoodsList(mallSeckillGoods);
        ExcelUtil<MallSeckillGoods> util = new ExcelUtil<MallSeckillGoods>(MallSeckillGoods.class);
        return util.exportExcel(list, "秒杀商品数据");
    }

    /**
     * 获取秒杀商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSeckillGoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(mallSeckillGoodsService.selectMallSeckillGoodsById(id));
    }

    /**
     * 新增秒杀商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSeckillGoods:add')")
    @Log(title = "秒杀商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallSeckillGoods mallSeckillGoods)
    {
        return mallSeckillGoodsService.insertMallSeckillGoods(mallSeckillGoods);
    }

    /**
     * 修改秒杀商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSeckillGoods:edit')")
    @Log(title = "秒杀商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallSeckillGoods mallSeckillGoods)
    {
        return toAjax(mallSeckillGoodsService.updateMallSeckillGoods(mallSeckillGoods));
    }

    /**
     * 删除秒杀商品
     */
    @PreAuthorize("@ss.hasPermi('mall:mallSeckillGoods:remove')")
    @Log(title = "秒杀商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mallSeckillGoodsService.deleteMallSeckillGoodsByIds(ids));
    }
}
