package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.*;
import com.ruoyi.brewery.service.IWyActivityRewardService;
import com.ruoyi.brewery.service.IWyActivityService;
import com.ruoyi.brewery.service.IWyGoodsService;
import com.ruoyi.brewery.service.IWyRedeemOrderService;
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.model.*;
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.aspectj.weaver.loadtime.Aj;
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/goods")
public class BreweryGoodsController extends BaseController
{
    @Autowired
    private IWyGoodsService wyGoodsService;
    @Resource
    private IWyRedeemOrderService wyRedeemOrderService;
    @Resource
    private IWyActivityService wyActivityService;
    @Resource
    private IWyActivityRewardService wyActivityRewardService;
    /**
     * 查询兑换商品列表
     */
    @ApiOperation(value = "查询兑换商品列表", notes = "查询兑换商品列表")
    @PreAuthorize("@bss.hasPermi('brewery:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyGoods wyGoods)
    {
        wyGoods.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        startPage();
        List<WyGoods> list = wyGoodsService.selectWyGoodsListWithData(wyGoods);
        return getDataTable(list);
    }

    /**
     * 查询所有兑换商品列表
     */
    @ApiOperation("查询所有商品列表")
    @PreAuthorize("@bss.hasPermi('brewery:goods:list')")
    @GetMapping("/all")
    public AjaxResult all(WyGoods wyGoods)
    {
        wyGoods.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyGoods> list = wyGoodsService.selectWyGoodsList(wyGoods);
        return success(list);
    }

    /**
     * 导出兑换商品列表
     */
//    @ApiOperation(value = "导出兑换商品列表", notes = "导出兑换商品列表")
//    @PreAuthorize("@bss.hasPermi('brewery:goods:export')")
//    @Log(title = "兑换商品", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, WyGoods wyGoods)
//    {
//        List<WyGoods> list = wyGoodsService.selectWyGoodsList(wyGoods);
//        ExcelUtil<WyGoods> util = new ExcelUtil<WyGoods>(WyGoods.class);
//        util.exportExcel(response, list, "兑换商品数据");
//    }

    /**
     * 获取兑换商品详细信息
     */
    @ApiOperation(value = "获取兑换商品详细信息", notes = "获取兑换商品详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        validate(id);
        return success(wyGoodsService.selectWyGoodsById(id));
    }

    /**
     * 新增兑换商品
     */
    @ApiOperation(value = "新增兑换商品", notes = "新增兑换商品")
    @PreAuthorize("@bss.hasPermi('brewery:goods:add')")
    @Log(title = "兑换商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyGoods wyGoods)
    {
        wyGoods.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyGoodsService.insertWyGoods(wyGoods));
    }

    /**
     * 修改兑换商品
     */
    @ApiOperation(value = "修改兑换商品", notes = "修改兑换商品")
    @PreAuthorize("@bss.hasPermi('brewery:goods:edit')")
    @Log(title = "兑换商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyGoods wyGoods)
    {
        wyGoods.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        validate(wyGoods.getId());
        return toAjax(wyGoodsService.updateWyGoods(wyGoods));
    }

    /**
     * 删除兑换商品
     */
    @ApiOperation(value = "删除兑换商品", notes = "删除兑换商品")
    @PreAuthorize("@bss.hasPermi('brewery:goods:remove')")
    @Log(title = "兑换商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        validate(id);
        return toAjax(wyGoodsService.deleteWyGoodsById(id));
    }

    /**
     * 上下架
     */
    @ApiOperation("上下架")
    @PreAuthorize("@bss.hasPermi('brewery:goods:edit')")
    @Log(title = "上下架", businessType = BusinessType.UPDATE)
    @PutMapping("/onsale")
    public AjaxResult onSale(@Validated @RequestBody WyGoodsSaleVO goodsSaleVO)
    {
        validate(goodsSaleVO.getGoodsId());
        if (goodsSaleVO.getState() != 0 && goodsSaleVO.getState() != 1) {
            return error("上下架状态异常");
        }
        final WyGoods wyGoods = new WyGoods();
        wyGoods.setId(goodsSaleVO.getGoodsId());
        wyGoods.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyGoods.setOnSale(goodsSaleVO.getState());
        return toAjax(wyGoodsService.updateWyGoods(wyGoods));
    }

    /**
     * 修改啤酒币数量
     */
    @ApiOperation("修改啤酒币数量")
    @PreAuthorize("@bss.hasPermi('brewery:goods:edit')")
    @Log(title = "修改啤酒币数量", businessType = BusinessType.UPDATE)
    @PutMapping("/icons")
    public AjaxResult editIcons(@RequestBody WyGoodsIconVO goodsIconVO)
    {
        validate(goodsIconVO.getGoodsId());
        if (goodsIconVO.getIcons() == null) {
            return error("啤酒币数量不能为空");
        }
        if (goodsIconVO.getIcons() < 1) {
            return error("啤酒币数量不能小于1");
        }
        if (goodsIconVO.getIcons() > 99999) {
            return error("啤酒币数量不能大于99999");
        }
        final WyGoods wyGoods = new WyGoods();
        wyGoods.setId(goodsIconVO.getGoodsId());
        wyGoods.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyGoods.setRedeemedNum(goodsIconVO.getIcons());
        return toAjax(wyGoodsService.updateWyGoods(wyGoods));
    }

    /**
     * 修改库存数量
     */
    @ApiOperation("修改库存数量")
    @PreAuthorize("@bss.hasPermi('brewery:goods:edit')")
    @Log(title = "修改库存数量", businessType = BusinessType.UPDATE)
    @PutMapping("/stock")
    public AjaxResult editStock(@RequestBody WyGoodsStockVO goodsStockVO)
    {
        validate(goodsStockVO.getGoodsId());

        final WyGoods wyGoods = new WyGoods();
        wyGoods.setId(goodsStockVO.getGoodsId());
        wyGoods.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyGoods.setStockNum(goodsStockVO.getStock());
        return toAjax(wyGoodsService.updateWyGoods(wyGoods));
    }

    /**
     * 查询某款兑换商品的兑换历史
     */
    @ApiOperation("查询某款兑换商品的兑换历史")
    @PreAuthorize("@bss.hasPermi('brewery:goods:exchangelist')")
    @GetMapping("/exchangeList/{goodsId}")
    public AjaxResult exchangeList(@PathVariable Long goodsId)
    {
        validate(goodsId);
        final WyRedeemOrder wyRedeemOrder = new WyRedeemOrder();
        wyRedeemOrder.setGoodsId(goodsId);
        final List<WyRedeemOrder> wyRedeemOrders = wyRedeemOrderService.selectWyRedeemOrderList(wyRedeemOrder);
        return AjaxResult.success(wyRedeemOrders);
    }

    /**
     * 查询某款兑付商品的返利活动
     */
    @ApiOperation("查询某款兑付商品的返利活动")
    @PreAuthorize("@bss.hasPermi('brewery:goods:activityList')")
    @GetMapping("/activityList/{goodsId}")
    public AjaxResult activityList(@PathVariable Long goodsId)
    {
        validate(goodsId);
        WyActivityReward wyActivityReward = new WyActivityReward();
        wyActivityReward.setRewardGoodsId(goodsId);
        wyActivityReward.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        final List<WyActivityRewardVO> wyActivityRewards = wyActivityRewardService.selectWyActivityRewardListWithData(wyActivityReward);
        return AjaxResult.success(wyActivityRewards);
    }

    private void validate(Long id) {
        final WyGoods wyGoods = wyGoodsService.selectWyGoodsById(id);
        if (wyGoods == null) {
            throw new IllegalArgumentException("商品不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyGoods.getBreweryId())) {
            throw new IllegalArgumentException("只能操作自己的品牌方的兑换商品");
        }
    }
}
