package com.yongqi.xinrenli.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
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.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.utils.poi.ExcelUtil;
import com.yongqi.xinrenli.domain.vo.DbArticleVo;
import com.yongqi.xinrenli.domain.bo.DbArticleBo;
import com.yongqi.xinrenli.service.IDbArticleService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章
 *
 * @author zyf
 * @date 2023-03-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xinrenli/article")
public class DbArticleController extends BaseController {

    private final IDbArticleService iDbArticleService;

    /**
     * 查询文章列表
     */
    @SaCheckPermission("xinrenli:article:list")
    @GetMapping("/list")
    public TableDataInfo<DbArticleVo> list(DbArticleBo bo, PageQuery pageQuery) {
        return iDbArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章列表
     */
    @SaCheckPermission("xinrenli:article:export")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DbArticleBo bo, HttpServletResponse response) {
        List<DbArticleVo> list = iDbArticleService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章", DbArticleVo.class, response);
    }

    /**
     * 获取文章详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xinrenli:article:query")
    @GetMapping("/{id}")
    public R<DbArticleVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(iDbArticleService.queryById(id));
    }

    /**
     * 新增文章
     */
    @SaCheckPermission("xinrenli:article:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DbArticleBo bo) {
        return toAjax(iDbArticleService.insertByBo(bo));
    }

    /**
     * 修改文章
     */
    @SaCheckPermission("xinrenli:article:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DbArticleBo bo) {
        return toAjax(iDbArticleService.updateByBo(bo));
    }

    /**
     * 删除文章
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xinrenli:article:remove")
    @Log(title = "文章", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iDbArticleService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
