package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.WyBreweryBanner;
import com.ruoyi.brewery.service.IWyBreweryBannerService;
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.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 品牌方广告图Controller
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@Api(tags = "品牌方广告图")
@RestController
@RequestMapping("/brewery/banner")
public class BreweryBannerController extends BaseController
{
    @Autowired
    private IWyBreweryBannerService wyBreweryBannerService;

    /**
     * 查询品牌方广告图列表
     */
    @ApiOperation("查询品牌方广告图列表")
    @PreAuthorize("@bss.hasPermi('brewery:banner:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyBreweryBanner wyBreweryBanner)
    {
        startPage();
        wyBreweryBanner.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyBreweryBanner> list = wyBreweryBannerService.selectWyBreweryBannerList(wyBreweryBanner);
        return getDataTable(list);
    }

    /**
     * 导出品牌方广告图列表
     */
//    @ApiOperation("导出品牌方广告图列表")
//    @PreAuthorize("@bss.hasPermi('brewery:banner:export')")
//    @Log(title = "品牌方广告图", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, WyBreweryBanner wyBreweryBanner)
//    {
//        List<WyBreweryBanner> list = wyBreweryBannerService.selectWyBreweryBannerList(wyBreweryBanner);
//        ExcelUtil<WyBreweryBanner> util = new ExcelUtil<WyBreweryBanner>(WyBreweryBanner.class);
//        util.exportExcel(response, list, "品牌方广告图数据");
//    }

    /**
     * 获取品牌方广告图详细信息
     */
    @ApiOperation("获取品牌方广告图详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:banner:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        final WyBreweryBanner wyBreweryBanner1 = wyBreweryBannerService.selectWyBreweryBannerById(id);
        if (wyBreweryBanner1 == null) {
            return AjaxResult.error("记录不存在");
        }
        if(!wyBreweryBanner1.getBreweryId().equals(SecurityUtils.getBreweryLoginUser().getBreweryId())){
            return AjaxResult.error("只能查询自己的广告图");
        }
        return success(wyBreweryBannerService.selectWyBreweryBannerById(id));
    }

    /**
     * 新增品牌方广告图
     */
    @ApiOperation("新增品牌方广告图")
    @PreAuthorize("@bss.hasPermi('brewery:banner:add')")
    @Log(title = "品牌方广告图", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyBreweryBanner wyBreweryBanner)
    {
        wyBreweryBanner.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyBreweryBannerService.insertWyBreweryBanner(wyBreweryBanner));
    }

    /**
     * 修改品牌方广告图
     */
    @ApiOperation("修改品牌方广告图")
    @PreAuthorize("@bss.hasPermi('brewery:banner:edit')")
    @Log(title = "品牌方广告图", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyBreweryBanner wyBreweryBanner)
    {
        final WyBreweryBanner wyBreweryBanner1 = wyBreweryBannerService.selectWyBreweryBannerById(wyBreweryBanner.getId());
        if (wyBreweryBanner1 == null) {
            return AjaxResult.error("修改失败，记录不存在");
        }
        if(!wyBreweryBanner1.getBreweryId().equals(SecurityUtils.getBreweryLoginUser().getBreweryId())){
            return AjaxResult.error("修改失败，只能修改自己的广告图");
        }
        wyBreweryBanner.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyBreweryBannerService.updateWyBreweryBanner(wyBreweryBanner));
    }

    /**
     * 删除品牌方广告图
     */
    @ApiOperation("删除品牌方广告图")
    @PreAuthorize("@bss.hasPermi('brewery:banner:remove')")
    @Log(title = "品牌方广告图", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        final WyBreweryBanner wyBreweryBanner1 = wyBreweryBannerService.selectWyBreweryBannerById(id);
        if (wyBreweryBanner1 == null) {
            return AjaxResult.error("删除失败，记录不存在");
        }
        if(!wyBreweryBanner1.getBreweryId().equals(SecurityUtils.getBreweryLoginUser().getBreweryId())){
            return AjaxResult.error("删除失败，只能删除自己的广告图");
        }
        return toAjax(wyBreweryBannerService.deleteWyBreweryBannerById(id));
    }
}
