package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.WyBeerQrcode;
import com.ruoyi.brewery.domain.WyBeerQrcodeBatch;
import com.ruoyi.brewery.service.IWyBeerQrcodeBatchService;
import com.ruoyi.brewery.service.IWyBeerQrcodeService;
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.domain.entity.WyBreweryUser;
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 com.ruoyi.framework.config.ServerConfig;
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/qrcode")
public class BreweryWyBeerQrcodeController extends BaseController
{
    @Autowired
    private IWyBeerQrcodeService wyBeerQrcodeService;
    @Autowired
    private IWyBeerQrcodeBatchService wyBeerQrcodeBatchService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询酒品二维码列表
     */
    @ApiOperation("查询酒品二维码列表")
    @PreAuthorize("@bss.hasPermi('brewery:qrcode:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyBeerQrcode wyBeerQrcode)
    {
        wyBeerQrcode.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        startPage();
        List<WyBeerQrcode> list = wyBeerQrcodeService.selectWyBeerQrcodeListWithData(wyBeerQrcode);
        return getDataTable(list);
    }

    /**
     * 导出酒品二维码列表
     */
    @ApiOperation("导出酒品二维码列表")
    @PreAuthorize("@bss.hasPermi('brewery:qrcode:export')")
    @Log(title = "酒品二维码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyBeerQrcode wyBeerQrcode)
    {
        wyBeerQrcode.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyBeerQrcode> list = wyBeerQrcodeService.selectWyBeerQrcodeList(wyBeerQrcode);
        ExcelUtil<WyBeerQrcode> util = new ExcelUtil<WyBeerQrcode>(WyBeerQrcode.class);
        util.exportExcel(response, list, "酒品二维码数据");
    }

    /**
     * 获取酒品二维码详细信息
     */
    @ApiOperation("获取酒品二维码详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:qrcode:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        validate(id);
        return success(wyBeerQrcodeService.selectWyBeerQrcodeAllDataById(id));
    }

    /**
     * 批量新增酒品二维码
     */
    @ApiOperation("批量新增酒品二维码")
    @PreAuthorize("@bss.hasPermi('brewery:qrcode:add')")
    @Log(title = "批量新增酒品二维码", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult batchAdd(@RequestParam("batchId") Long batchId)
    {
        final WyBeerQrcodeBatch wyBeerQrcodeBatch = wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchById(batchId);
        if (wyBeerQrcodeBatch == null) {
            return AjaxResult.error("酒品二维码批次不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyBeerQrcodeBatch.getBreweryId())) {
            return AjaxResult.error("只能操作自己的品牌方的酒品二维码批次");
        }
        return toAjax(wyBeerQrcodeService.batchInsertWyBeerQrcode(wyBeerQrcodeBatch,serverConfig.getUrl()));
    }

    /**
     * 新增酒品二维码
     */
    @ApiOperation("新增酒品二维码")
    @PreAuthorize("@bss.hasPermi('brewery:qrcode:add')")
    @Log(title = "酒品二维码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyBeerQrcode wyBeerQrcode)
    {
        wyBeerQrcode.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyBeerQrcodeService.insertWyBeerQrcode(wyBeerQrcode));
    }

    /**
     * 修改酒品二维码
     */
    @ApiOperation("修改酒品二维码")
    @PreAuthorize("@bss.hasPermi('brewery:qrcode:edit')")
    @Log(title = "酒品二维码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyBeerQrcode wyBeerQrcode)
    {
        validate(wyBeerQrcode.getId());
        return toAjax(wyBeerQrcodeService.updateWyBeerQrcode(wyBeerQrcode));
    }

    /**
     * 删除酒品二维码
     */
    @ApiOperation("删除酒品二维码")
    @PreAuthorize("@bss.hasPermi('brewery:qrcode:remove')")
    @Log(title = "酒品二维码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        validate(id);
        return toAjax(wyBeerQrcodeService.deleteWyBeerQrcodeById(id));
    }

    private void validate(Long id) {
        final WyBeerQrcode wyBeerQrcode = wyBeerQrcodeService.selectWyBeerQrcodeById(id);
        if (wyBeerQrcode == null) {
            throw new IllegalArgumentException("酒品二维码不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyBeerQrcode.getBreweryId())) {
            throw new IllegalArgumentException("只能操作自己的品牌方的酒品二维码");
        }
    }
}
