package com.ruoyi.web.controller.info;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.common.wrapper.DictTextWrapper;
import com.ruoyi.common.wrapper.Wrapper;
import com.ruoyi.info.ad.domain.bo.AdBo;
import com.ruoyi.info.ad.domain.vo.AdVo;
import com.ruoyi.info.ad.service.IAdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 广告Controller
 *
 * @author lubo
 * @date 2023-12-21
 */
@Validated
@Api(value = "广告控制器", tags = {"广告管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/info/ad")
public class AdController extends BaseController {

    private final IAdService iAdService;

    /**
     * 分页查询广告列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询广告列表")
    @Wrapper(value= DictTextWrapper.class)
    @SaCheckPermission("ad:ad:list")
    @GetMapping("/page")
    public TableDataInfo<AdVo> page(@Validated(QueryGroup.class) AdBo bo, PageQuery pageQuery) {
        return iAdService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取广告详细信息
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取广告详细信息")
    @Wrapper(value= DictTextWrapper.class)
    @SaCheckPermission("ad:ad:query")
    @GetMapping("/info/{adId}")
    public R<AdVo> getInfo(@ApiParam("主键") @NotNull(message = "主键不能为空")
                               @PathVariable("adId") Long adId) {
        return R.ok(iAdService.queryById(adId));
    }

    /**
     * 新增广告
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("新增广告")
    @SaCheckPermission("ad:ad:add")
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AdBo bo) {
        return toAjax(iAdService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改广告
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("修改广告")
    @SaCheckPermission("ad:ad:edit")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdBo bo) {
        return toAjax(iAdService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 修改广告状态
     */
    @ApiOperation("修改广告状态")
    @SaCheckPermission("ad:ad:status")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PostMapping("/setStatus/{id}")
    public R<Void> setStatus(@ApiParam("主键串") @NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return toAjax(iAdService.setStatus(id) ? 1 : 0);
    }

    /**
     * 删除广告
     */
    @ApiOperation("删除广告")
    @SaCheckPermission("ad:ad:remove")
    @Log(title = "广告", businessType = BusinessType.DELETE)
    @PostMapping("/delSingle/{id}")
    public R<Void> remove(@ApiParam("主键串") @NotNull(message = "主键不能为空")
                          @PathVariable Long id) {
        return toAjax(iAdService.delSingle(id) ? 1 : 0);
    }

}
