package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.WyActivity;
import com.ruoyi.brewery.domain.WyBeer;
import com.ruoyi.brewery.domain.WyBeerQrcodeBatch;
import com.ruoyi.brewery.service.IWyActivityBeerService;
import com.ruoyi.brewery.service.IWyBeerQrcodeBatchService;
import com.ruoyi.brewery.service.IWyBeerService;
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.domain.model.WyBeerIconVO;
import com.ruoyi.common.core.domain.model.WyBeerSaleVO;
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.system.domain.WyBeerStyle;
import com.ruoyi.system.service.IWyBeerStyleService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 啤酒Controller
 * 
 * @author ruoyi
 * @date 2024-12-21
 */
@Api(tags = "啤酒")
@RestController
@RequestMapping("/brewery/beer")
public class BreweryWyBeerController extends BreweryBaseController
{
    @Autowired
    private IWyBeerService wyBeerService;
    @Resource
    private IWyActivityBeerService wyActivityBeerService;
    @Autowired
    private IWyBeerQrcodeBatchService wyBeerQrcodeBatchService;
    @Resource
    private IWyBeerStyleService beerStyleService;

    /**
     * 查询啤酒风味列表
     */
    @ApiOperation("查询啤酒风味列表")
    @PreAuthorize("@bss.hasPermi('brewery:beer:styleList')")
    @GetMapping("/styleList")
    public AjaxResult styleList()
    {
        final List<WyBeerStyle> wyBeerStyles = beerStyleService.selectWyBeerStyleTree();
        return AjaxResult.success(wyBeerStyles);
    }

    /**
     * 查询啤酒列表
     */
    @ApiOperation("查询啤酒列表")
    @PreAuthorize("@bss.hasPermi('brewery:beer:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyBeer wyBeer)
    {
        startPage();
        wyBeer.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyBeer> list = wyBeerService.selectWyBeerListWithData(wyBeer);
        return getDataTable(list);
    }

    /**
     * 查询所有啤酒列表
     */
    @ApiOperation("查询所有啤酒列表")
    @PreAuthorize("@bss.hasPermi('brewery:beer:list')")
    @GetMapping("/all")
    public AjaxResult all(WyBeer wyBeer)
    {
        wyBeer.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyBeer> list = wyBeerService.selectWyBeerList(wyBeer);
        return success(list);
    }

    /**
     * 导出啤酒列表
     */
    @ApiOperation("导出啤酒列表")
    @PreAuthorize("@bss.hasPermi('brewery:beer:export')")
    @Log(title = "啤酒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyBeer wyBeer)
    {
        wyBeer.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyBeer> list = wyBeerService.selectWyBeerList(wyBeer);
        ExcelUtil<WyBeer> util = new ExcelUtil<WyBeer>(WyBeer.class);
        util.exportExcel(response, list, "啤酒数据");
    }

    /**
     * 获取啤酒详细信息
     */
    @ApiOperation("获取啤酒详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:beer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        validate(id);
        return success(wyBeerService.selectWyBeerById(id));
    }

    /**
     * 新增啤酒
     */
    @ApiOperation("新增啤酒")
    @PreAuthorize("@bss.hasPermi('brewery:beer:add')")
    @Log(title = "啤酒", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WyBeer wyBeer)
    {
        wyBeer.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyBeerService.insertWyBeer(wyBeer));
    }

    /**
     * 修改啤酒
     */
    @ApiOperation("修改啤酒")
    @PreAuthorize("@bss.hasPermi('brewery:beer:edit')")
    @Log(title = "啤酒", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyBeer wyBeer)
    {
        validate(wyBeer.getId());
        return toAjax(wyBeerService.updateWyBeer(wyBeer));
    }

    /**
     * 修改扫码赠送啤酒币数量
     */
    @ApiOperation("修改扫码赠送啤酒币数量")
    @PreAuthorize("@bss.hasPermi('brewery:beer:edit')")
    @Log(title = "修改扫码赠送啤酒币数量", businessType = BusinessType.UPDATE)
    @PostMapping("/icons")
    public AjaxResult editIcons(@Validated @RequestBody WyBeerIconVO wyBeer)
    {
        validate(wyBeer.getBeerId());
//        if (wyBeer.getIcons() == null) {
//            return error("啤酒币数量不能为空");
//        }
//        if (wyBeer.getIcons() < 1) {
//            return error("啤酒币数量不能小于1");
//        }
//        if (wyBeer.getIcons() > 9999) {
//            return error("啤酒币数量不能大于9999");
//        }
        final WyBeer wyBeer1 = new WyBeer();
        wyBeer1.setId(wyBeer.getBeerId());
        wyBeer1.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyBeer1.setScanCoinCount(wyBeer.getIcons());
        return toAjax(wyBeerService.updateWyBeer(wyBeer1));
    }

    /**
     * 修改首评赠送啤酒币数量
     */
    @ApiOperation("修改首评赠送啤酒币数量")
    @PreAuthorize("@bss.hasPermi('brewery:beer:edit')")
    @Log(title = "修改首评赠送啤酒币数量", businessType = BusinessType.UPDATE)
    @PostMapping("/reviewIcons")
    public AjaxResult editReviewIcons(@Validated @RequestBody WyBeerIconVO wyBeer)
    {
        validate(wyBeer.getBeerId());
//        if (wyBeer.getIcons() == null) {
//            return error("啤酒币数量不能为空");
//        }
//        if (wyBeer.getIcons() < 1) {
//            return error("啤酒币数量不能小于1");
//        }
//        if (wyBeer.getIcons() > 9999) {
//            return error("啤酒币数量不能大于9999");
//        }
        final WyBeer wyBeer1 = new WyBeer();
        wyBeer1.setId(wyBeer.getBeerId());
        wyBeer1.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyBeer1.setFirstReviewCoin(wyBeer.getIcons());
        return toAjax(wyBeerService.updateWyBeer(wyBeer1));
    }

    /**
     * 上下架
     */
    @ApiOperation("上下架")
    @PreAuthorize("@bss.hasPermi('brewery:beer:edit')")
    @Log(title = "上下架", businessType = BusinessType.UPDATE)
    @PutMapping("/onsale")
    public AjaxResult onSale(@Validated @RequestBody WyBeerSaleVO wyBeer)
    {
        validate(wyBeer.getBeerId());
        if (wyBeer.getState() != 0 && wyBeer.getState() != 1) {
            return error("上下架状态异常");
        }
        final WyBeer wyBeer1 = new WyBeer();
        wyBeer1.setId(wyBeer.getBeerId());
        wyBeer1.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyBeer1.setOnSale(wyBeer.getState());
        return toAjax(wyBeerService.updateWyBeer(wyBeer1));
    }

    /**
     * 删除啤酒
     */
    @ApiOperation("删除啤酒")
    @PreAuthorize("@bss.hasPermi('brewery:beer:remove')")
    @Log(title = "啤酒", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        validate(id);
        return toAjax(wyBeerService.deleteWyBeerById(id));
    }

    /**
     * 查询某酒款有关的营销活动列表
     */
    @ApiOperation("查询某酒款有关的营销活动列表")
    @PreAuthorize("@bss.hasPermi('brewery:beer:activity')")
    @GetMapping("/activityListByBeerId/{beerId}")
    public TableDataInfo list(@PathVariable("beerId") Long beerId,
                              @RequestParam(value = "status", required = false) Integer status) {
        final Long breweryId = SecurityUtils.getBreweryLoginUser().getBreweryId();
        startPage();
        final List<WyActivity> list = wyActivityBeerService.selectWyActivityListByBeerId(beerId, breweryId,status);
        return getDataTable(list);
    }

    /**
     * 查询某酒款有关批次列表
     */
    @ApiOperation("查询某酒款有关批次列表")
    @PreAuthorize("@bss.hasPermi('brewery:beer:batch')")
    @GetMapping("/batchListByBeerId/{beerId}")
    public TableDataInfo list(@PathVariable("beerId") Long beerId)
    {
        WyBeerQrcodeBatch wyBeerQrcodeBatch = new WyBeerQrcodeBatch();
        wyBeerQrcodeBatch.setBeerId(beerId);
        wyBeerQrcodeBatch.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        startPage();
        List<WyBeerQrcodeBatch> list = wyBeerQrcodeBatchService.selectWyBeerQrcodeBatchListWithData(wyBeerQrcodeBatch);
        return getDataTable(list);
    }

    private void validate(Long id) {
        final WyBeer wyBeer = wyBeerService.selectWyBeerById(id);
        if (wyBeer == null) {
            throw new IllegalArgumentException("酒款不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyBeer.getBreweryId())) {
            throw new IllegalArgumentException("只能操作自己的品牌方的酒款");
        }
    }
}
