package com.ruoyi.brewery.controller;

import com.ruoyi.brewery.domain.WyBreweryAuth;
import com.ruoyi.brewery.service.IWyBreweryAuthService;
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.BreweryLoginUser;
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/auth")
public class BreweryAuthController extends BaseController
{
    @Autowired
    private IWyBreweryAuthService wyBreweryAuthService;

   /**
   * 查询主体认证信息列表
  */
  @ApiOperation("查询主体认证信息列表")
   @PreAuthorize("@bss.hasPermi('brewery:auth:list')")
  @GetMapping("/list")
  public TableDataInfo list(WyBreweryAuth wyBreweryAuth)
    {
        startPage();
        List<WyBreweryAuth> list = wyBreweryAuthService.selectWyBreweryAuthList(wyBreweryAuth);
        return getDataTable(list);
    }

//    /**
//     * 导出主体认证信息列表
//     */
//    @ApiOperation("导出主体认证信息列表")
//    @PreAuthorize("@bss.hasPermi('brewery:auth:export')")
//    @Log(title = "主体认证信息", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, WyBreweryAuth wyBreweryAuth)
//    {
//        List<WyBreweryAuth> list = wyBreweryAuthService.selectWyBreweryAuthList(wyBreweryAuth);
//        ExcelUtil<WyBreweryAuth> util = new ExcelUtil<WyBreweryAuth>(WyBreweryAuth.class);
//        util.exportExcel(response, list, "主体认证信息数据");
//    }

//    /**
//     * 获取主体认证信息详细信息
//     */
//    @ApiOperation("获取主体认证信息详细信息")
//    @PreAuthorize("@bss.hasPermi('brewery:auth:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id)
//    {
//        return success(wyBreweryAuthService.selectWyBreweryAuthById(id));
//    }

    /**
     * 获取主体认证信息详细信息
     */
    @ApiOperation("获取主体认证信息详细信息")
    @PreAuthorize("@bss.hasPermi('brewery:auth:query')")
    @GetMapping("/authInfo")
    public AjaxResult getInfo()
    {
        final BreweryLoginUser breweryLoginUser = SecurityUtils.getBreweryLoginUser();
        final Long breweryId = breweryLoginUser.getBreweryId();
        final WyBreweryAuth data = wyBreweryAuthService.selectWyBreweryAuthByBreweryId(breweryId);
        return success(data == null?"":data);
    }

    /**
     * 新增主体认证信息
     */
    @ApiOperation("新增主体认证信息")
    @PreAuthorize("@bss.hasPermi('brewery:auth:add')")
    @Log(title = "主体认证信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyBreweryAuth wyBreweryAuth)
    {
        wyBreweryAuth.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        wyBreweryAuth.setState(0);
        return toAjax(wyBreweryAuthService.insertWyBreweryAuth(wyBreweryAuth));
    }

    /**
     * 修改主体认证信息
     */
    @ApiOperation("修改主体认证信息")
    @PreAuthorize("@bss.hasPermi('brewery:auth:edit')")
    @Log(title = "主体认证信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyBreweryAuth wyBreweryAuth)
    {
        final WyBreweryAuth wyBreweryAuth1 = wyBreweryAuthService.selectWyBreweryAuthById(wyBreweryAuth.getId());
        if (wyBreweryAuth1 == null) {
            return error("数据不存在");
        }
        if (!SecurityUtils.getBreweryLoginUser().getBreweryId().equals(wyBreweryAuth1.getBreweryId())) {
            return error("只能修改自己的信息");
        }
        wyBreweryAuth.setState(null);
        wyBreweryAuth.setBreweryId(SecurityUtils.getBreweryLoginUser().getBreweryId());
        return toAjax(wyBreweryAuthService.updateWyBreweryAuth(wyBreweryAuth));
    }

    /**
     * 删除主体认证信息
     */
    @ApiOperation("删除主体认证信息")
    @PreAuthorize("@bss.hasPermi('brewery:auth:remove')")
    @Log(title = "主体认证信息", businessType = BusinessType.DELETE)
	@DeleteMapping
    public AjaxResult remove()
    {
        final Long breweryId = SecurityUtils.getBreweryLoginUser().getBreweryId();
        return toAjax(wyBreweryAuthService.deleteWyBreweryAuthByBreweryId(breweryId));
    }
}
