package com.ruoyi.web.controller.commonproblem;

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.base.commonproblem.domain.bo.CommonProblemBo;
import com.ruoyi.base.commonproblem.domain.vo.CommonProblemVo;
import com.ruoyi.base.commonproblem.service.ICommonProblemService;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
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 xiechao
 * @date 2023-12-21
 */
@Validated
@Api(value = "常见问题控制器", tags = {"常见问题管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/common/problem")
public class CommonProblemController extends BaseController {

    private final ICommonProblemService iCommonProblemService;

    /**
     * 分页查询常见问题列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("查询常见问题列表")
    @SaCheckPermission("common:problem:list")
    @GetMapping("/page")
    public TableDataInfo<CommonProblemVo> page(@Validated(QueryGroup.class) CommonProblemBo bo, PageQuery pageQuery) {
        return iCommonProblemService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取常见问题详细信息
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取常见问题详细信息")
    @SaCheckPermission("common:problem:query")
    @GetMapping("/info/{id}")
    public R<CommonProblemVo> getInfo(@ApiParam("主键") @NotNull(message = "主键不能为空")
                                      @PathVariable("id") Integer id) {
        return R.ok(iCommonProblemService.getById(id));
    }

    /**
     * 新增常见问题
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("新增常见问题")
    @SaCheckPermission("common:problem:add")
    @Log(title = "常见问题", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommonProblemBo bo) {
        return toAjax(iCommonProblemService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改常见问题
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("修改常见问题")
    @SaCheckPermission("common:problem:edit")
    @Log(title = "常见问题", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommonProblemBo bo) {
        return toAjax(iCommonProblemService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 上下架
     */
    @ApiOperation("上下架")
    @SaCheckPermission("common:problem:edit")
    @Log(title = "常见问题", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/status/{id}")
    public R<Void> status(@ApiParam("主键") @NotNull(message = "主键不能为空")
                              @PathVariable Integer id) {
        return toAjax(iCommonProblemService.setStatus(id) ? 1 : 0);
    }

    /**
     * 删除常见问题
     */
    @ApiOperation("删除常见问题(单个删除)")
    @SaCheckPermission("common:problem:remove")
    @Log(title = "常见问题", businessType = BusinessType.DELETE)
    @PostMapping("/del/single/{id}")
    public R<Void> removeSingle(@ApiParam("主键") @NotNull(message = "主键不能为空")
                          @PathVariable Integer id) {
        return toAjax(iCommonProblemService.deleteById(id) ? 1 : 0);
    }

}
