package com.yingpaipay.business.controller;

import java.io.IOException;
import java.util.List;

import com.yingpaipay.business.domain.bo.*;
import com.yingpaipay.business.domain.vo.DocumentAuditLogVo;
import com.yingpaipay.business.domain.vo.DocumentListOnSpecifyVo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import com.yingpaipay.business.domain.vo.DocumentVo;
import com.yingpaipay.business.service.IDocumentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 文档
 *
 * @author Huanyi
 * @date 2025-12-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/document/document")
public class DocumentController extends BaseController {

    private final IDocumentService documentService;

    /**
     * 查询文档列表
     */
    @SaCheckPermission("document:document:list")
    @GetMapping("/list")
    public TableDataInfo<DocumentVo> list(DocumentBo bo, PageQuery pageQuery) {
        return documentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文档列表
     */
    @SaCheckPermission("document:document:export")
    @Log(title = "文档", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DocumentBo bo, HttpServletResponse response) {
        List<DocumentVo> list = documentService.queryList(bo);
        ExcelUtil.exportExcel(list, "文档", DocumentVo.class, response);
    }

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

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

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

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

    @SaCheckPermission("document:document:mark")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping("/mark")
    public R<Void> mark(@RequestBody DocumentMarkBo bo) {
        return toAjax(documentService.mark(bo));
    }

    @SaCheckPermission("document:document:submit")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping("/submit")
    public R<Void> submit(@RequestBody DocumentSubmitBo bo) {
        return toAjax(documentService.submit(bo));
    }

    @SaCheckPermission("document:document:audit")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping("/audit")
    public R<Void> audit(@RequestBody DocumentAuditBo bo) {
        return toAjax(documentService.audit(bo));
    }

    @SaCheckPermission("document:document:confirmSubmit")
    @Log(title = "文档", businessType = BusinessType.DELETE)
    @DeleteMapping("/confirmSubmit")
    public R<Void> confirmSubmit(@RequestParam("id") Long id) {
        return toAjax(documentService.confirmSubmit(id));
    }

    @SaCheckPermission("document:document:logAudit")
    @GetMapping("/logAudit")
    public TableDataInfo<DocumentAuditLogVo> logAudit(DocumentAuditLogBo bo, PageQuery pageQuery) {
        return documentService.logAudit(bo, pageQuery);
    }

    @SaCheckPermission("document:document:filing")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping("/filing")
    public R<Void> filing(@RequestBody DocumentFilingBo bo) {
        return toAjax(documentService.filing(bo));
    }

    @PostMapping(value = "/download/{ossId}")
    public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
        documentService.download(ossId, response);
    }

    @GetMapping("/countTemp")
    public R<Long> countTemp(@RequestParam Long projectId) {
        return R.ok(documentService.countTemp(projectId));
    }

    @SaCheckPermission("document:document:removeTemp")
    @Log(title = "文档", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeTemp/{documentId}")
    public R<Void> removeTemp(@PathVariable Long documentId) {
        return toAjax(documentService.removeTemp(documentId));
    }

    @SaCheckPermission("document:document:specify")
    @Log(title = "文档", businessType = BusinessType.UPDATE)
    @PutMapping("/specify")
    public R<Void> specify(@RequestBody DocumentSpecifyBo bo) {
        return toAjax(documentService.specify(bo));
    }

    @GetMapping("/listOnSpecify")
    public TableDataInfo<DocumentListOnSpecifyVo> listOnSpecify(DocumentListOnSpecifyBo bo, PageQuery pageQuery) {
        return documentService.listOnSpecify(bo, pageQuery);
    }

}
