package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.WyBeerQrcodeBatch;
import com.ruoyi.brewery.domain.WyRedeemOrder;
import com.ruoyi.brewery.service.IWyBeerQrcodeBatchService;
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/batch")
public class BreweryWyBeerQrcodeBatchController extends BaseController
{
    @Autowired
    private IWyBeerQrcodeBatchService wyBeerQrcodeBatchService;

    /**
     * 查询批次列表
     */
    @ApiOperation("查询批次列表")
    @PreAuthorize("@bss.hasPermi('brewery:batch:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyBeerQrcodeBatch wyBeerQrcodeBatch)
    {
        startPage();
        wyBeerQrcodeBatch.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyBeerQrcodeBatch> list = wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchList(wyBeerQrcodeBatch);
        return getDataTable(list);
    }

    /**
     * 导出批次列表
     */
//    @ApiOperation("导出批次列表")
//    @PreAuthorize("@bss.hasPermi('brewery:batch:export')")
//    @Log(title = "批次", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, WyBeerQrcodeBatch wyBeerQrcodeBatch)
//    {
//        List<WyBeerQrcodeBatch> list = wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchList(wyBeerQrcodeBatch);
//        ExcelUtil<WyBeerQrcodeBatch> util = new ExcelUtil<WyBeerQrcodeBatch>(WyBeerQrcodeBatch.class);
//        util.exportExcel(response, list, "批次数据");
//    }

    /**
     * 获取批次详细信息
     */
    @ApiOperation("获取批次详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:batch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        validate(id);
        return success(wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchById(id));
    }

    /**
     * 新增批次
     */
    @ApiOperation("新增批次")
    @PreAuthorize("@bss.hasPermi('brewery:batch:add')")
    @Log(title = "批次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyBeerQrcodeBatch wyBeerQrcodeBatch)
    {
        wyBeerQrcodeBatch.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        int rows = wyBeerQrcodeBatchService.insertWyBeerQrcodeBatch(wyBeerQrcodeBatch);
        if (rows > 0) {
            return AjaxResult.success(wyBeerQrcodeBatch.getId());
        }else{
            return AjaxResult.error("新增批次失败");
        }

    }

    /**
     * 修改批次
     */
    @ApiOperation("修改批次")
    @PreAuthorize("@bss.hasPermi('brewery:batch:edit')")
    @Log(title = "批次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyBeerQrcodeBatch wyBeerQrcodeBatch)
    {
        validate(wyBeerQrcodeBatch.getId());
        final WyBeerQrcodeBatch wyBeerQrcodeBatch1 = wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchById(wyBeerQrcodeBatch.getId());
        if (wyBeerQrcodeBatch1.getGenerateState() == 1) {
            throw new IllegalArgumentException("已生成的批次不能修改");
        }
        return toAjax(wyBeerQrcodeBatchService.updateWyBeerQrcodeBatch(wyBeerQrcodeBatch));
    }

    /**
     * 删除批次
     */
    @ApiOperation("删除批次")
    @PreAuthorize("@bss.hasPermi('brewery:batch:remove')")
    @Log(title = "批次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        validate(id);
        final WyBeerQrcodeBatch wyBeerQrcodeBatch1 = wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchById(id);
        if (wyBeerQrcodeBatch1.getGenerateState() == 1) {
            throw new IllegalArgumentException("已生成的批次不能删除");
        }
        return toAjax(wyBeerQrcodeBatchService.deleteWyBeerQrcodeBatchById(id));
    }

    private void validate(Long id) {
        final WyBeerQrcodeBatch wyBeerQrcodeBatch = wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchById(id);
        if (wyBeerQrcodeBatch == null) {
            throw new IllegalArgumentException("批次不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyBeerQrcodeBatch.getBreweryId())) {
            throw new IllegalArgumentException("只能操作自己的品牌方的批次");
        }
    }
}
