package com.ruoyi.oa.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 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.ruoyi.oa.domain.vo.OaArticleVo;
import com.ruoyi.oa.domain.bo.OaArticleBo;
import com.ruoyi.oa.service.IOaArticleService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章
 *
 * @author Joshi
 * @date 2025-09-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/articles")
public class OaArticleController extends BaseController {

    private final IOaArticleService iOaArticleService;

    /**
     * 查询文章列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaArticleVo> list(OaArticleBo bo, PageQuery pageQuery) {
        return iOaArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章列表
     */
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaArticleBo bo, HttpServletResponse response) {
        List<OaArticleVo> list = iOaArticleService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章", OaArticleVo.class, response);
    }

    /**
     * 获取文章详细信息
     *
     * @param articleId 主键
     */
    @GetMapping("/{articleId}")
    public R<OaArticleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long articleId) {
        return R.ok(iOaArticleService.queryById(articleId));
    }

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

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

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