package com.ruoyi.web.controller.brewery;

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

import com.ruoyi.web.domain.BeerApproveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.brewery.domain.WyBeer;
import com.ruoyi.brewery.service.IWyBeerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 啤酒Controller
 *
 * @author ruoyi
 * @date 2024-12-21
 */
@Api(tags = "啤酒")
@RestController
@RequestMapping("/system/brewery/beer")
public class WyBeerController extends BaseController {
    @Autowired
    private IWyBeerService wyBeerService;

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

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

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

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

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

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

    /**
     * 审核啤酒信息
     */
    @ApiOperation("审核啤酒信息")
    @PreAuthorize("@ss.hasPermi('brewery:beer:approval')")
    @Log(title = "啤酒", businessType = BusinessType.UPDATE)
    @PutMapping("/approval")
    public AjaxResult approval(@RequestBody BeerApproveVO beerApprove) {
        final WyBeer beer = new WyBeer();
        if (beerApprove.getApproval() == null || beerApprove.getBeerId() == null) {
            return error("审核失败，请提交正确的审核结果和啤酒ID");
        }
        beer.setId(beerApprove.getBeerId());
        if (beerApprove.getApproval() != 1 && beerApprove.getApproval() != 2) {
            return error("审核失败，请提交正确的审核结果：通过1，不通过2");
        }
        beer.setApproval(beerApprove.getApproval());
        if (beerApprove.getApproval() == 2 && beerApprove.getApprovalReason() == null) {
            return error("审核失败，请填写审核原因");
        }
        beer.setApprovalReason(beerApprove.getApprovalReason());
        return toAjax(wyBeerService.updateWyBeer(beer));
    }


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