package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.WyBeerReviewLike;
import com.ruoyi.brewery.domain.WyBeerReviewReply;
import com.ruoyi.brewery.service.IWyBeerReviewReplyService;
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/reply")
public class BreweryWyBeerReviewReplyController extends BaseController
{
    @Autowired
    private IWyBeerReviewReplyService wyBeerReviewReplyService;

    /**
     * 查询酒评回复列表
     */
    @ApiOperation("查询酒评回复列表")
    @PreAuthorize("@bss.hasPermi('brewery:reply:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyBeerReviewReply wyBeerReviewReply)
    {
        wyBeerReviewReply.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        startPage();
        List<WyBeerReviewReply> list = wyBeerReviewReplyService.selectWyBeerReviewReplyList(wyBeerReviewReply);
        return getDataTable(list);
    }

    /**
     * 导出酒评回复列表
     */
    @ApiOperation("导出酒评回复列表")
    @PreAuthorize("@bss.hasPermi('brewery:reply:export')")
    @Log(title = "酒评回复", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyBeerReviewReply wyBeerReviewReply)
    {
        wyBeerReviewReply.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        List<WyBeerReviewReply> list = wyBeerReviewReplyService.selectWyBeerReviewReplyList(wyBeerReviewReply);
        ExcelUtil<WyBeerReviewReply> util = new ExcelUtil<WyBeerReviewReply>(WyBeerReviewReply.class);
        util.exportExcel(response, list, "酒评回复数据");
    }

    /**
     * 获取酒评回复详细信息
     */
    @ApiOperation("获取酒评回复详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:reply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        validate(id);
        return success(wyBeerReviewReplyService.selectWyBeerReviewReplyById(id));
    }

    /**
     * 新增酒评回复
     */
    @ApiOperation("新增酒评回复")
    @PreAuthorize("@bss.hasPermi('brewery:reply:add')")
    @Log(title = "酒评回复", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyBeerReviewReply wyBeerReviewReply)
    {
        return toAjax(wyBeerReviewReplyService.insertWyBeerReviewReply(wyBeerReviewReply));
    }

    /**
     * 修改酒评回复
     */
    @ApiOperation("修改酒评回复")
    @PreAuthorize("@bss.hasPermi('brewery:reply:edit')")
    @Log(title = "酒评回复", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyBeerReviewReply wyBeerReviewReply)
    {
        validate(wyBeerReviewReply.getId());
        return toAjax(wyBeerReviewReplyService.updateWyBeerReviewReply(wyBeerReviewReply));
    }

    /**
     * 删除酒评回复
     */
    @ApiOperation("删除酒评回复")
    @PreAuthorize("@bss.hasPermi('brewery:reply:remove')")
    @Log(title = "酒评回复", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        validate(id);
        return toAjax(wyBeerReviewReplyService.deleteWyBeerReviewReplyById(id));
    }

    private void validate(Long id) {
        final WyBeerReviewReply wyBeerReviewReply = wyBeerReviewReplyService.selectWyBeerReviewReplyById(id);
        if (wyBeerReviewReply == null) {
            throw new IllegalArgumentException("酒评回复不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyBeerReviewReply.getBreweryId())) {
            throw new IllegalArgumentException("只能操作自己的品牌方的酒评回复");
        }
    }
}
