package com.microservices.pms.document.controller;

import com.microservices.common.core.utils.poi.ExcelUtil;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.pms.document.domain.PmsDocument;
import com.microservices.pms.document.domain.vo.*;
import com.microservices.pms.document.service.IPmsDocumentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目管理-文档库Controller
 * 
 * @author wanjia
 * @date 2024-01-08
 */
@RestController
@RequestMapping("/{enterpriseIdentifier}/document")
@Api(tags = "项目管理-知识库相关接口")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsDocumentController extends BaseController
{
    @Autowired
    private IPmsDocumentService pmsDocumentService;

    /**
     * 查询项目管理-文档库列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询项目管理-文档库列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列（更新时间：updateTime，创建时间：createTime）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<PmsDocumentVo> list( @Validated PmsDocumentSearchVo pmsDocumentSearchVo)
    {
        return pmsDocumentService.selectPmsDocumentVoList(pmsDocumentSearchVo);
    }

    /**
     * 导出项目管理-文档库列表
     */
    @RequiresPermissions("pms:document:export")
    @Log(title = "项目管理-文档库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出项目管理-文档库列表", hidden = true)
    public void export(HttpServletResponse response, PmsDocument pmsDocument)
    {
        List<PmsDocument> list = pmsDocumentService.selectPmsDocumentList(pmsDocument);
        ExcelUtil<PmsDocument> util = new ExcelUtil<PmsDocument>(PmsDocument.class);
        util.exportExcel(response, list, "项目管理-文档库数据");
    }

    /**
     * 获取项目管理-文档库详细信息
     */
//    @RequiresPermissions("pms:document:query")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取项目管理-文档库详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id, @ApiParam("企业id") @RequestParam(name = "enterpriseId") Long enterpriseId)
    {
        return success(pmsDocumentService.selectPmsDocumentDetailVoByIdAndEnterpriseId(id, enterpriseId));
    }

    /**
     * 新增项目管理-文档库
     */
//    @RequiresPermissions("pms:document:add")
    @Log(title = "项目管理-文档库", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增项目管理-文档库")
    public AjaxResult add(@RequestBody @Validated PmsDocumentInputVo pmsDocumentInputVo)
    {
        return toAjax(pmsDocumentService.insertPmsDocument(pmsDocumentInputVo.toPmsDocument()));
    }

    /**
     * 新增附件类型文档
     */
//    @RequiresPermissions("pms:document:add")
    @Log(title = "新增附件类型文档", businessType = BusinessType.INSERT)
    @PostMapping("/fileType")
    @ApiOperation(value = "新增附件类型文档")
    public AjaxResult addFileTypeDoc(@RequestBody @Validated PmsFileTypeDocumentInputVo pmsFileTypeDocumentInputVo)
    {
        return toAjax(pmsDocumentService.insertPmsFileTypeDocument(pmsFileTypeDocumentInputVo));
    }

    /**
     * 修改项目管理-文档库
     */
//    @RequiresPermissions("pms:document:edit")
    @Log(title = "项目管理-文档库", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改项目管理-文档库")
    public AjaxResult edit(@RequestBody PmsDocumentUpdateVo pmsDocumentUpdateVo)
    {
        return toAjax(pmsDocumentService.updatePmsDocument(pmsDocumentUpdateVo.toPmsDocument()));
    }

    /**
     * 删除项目管理-文档库
     */
//    @RequiresPermissions("pms:document:remove")
    @Log(title = "项目管理-文档库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value = "删除项目管理-文档库")
    public AjaxResult remove(@PathVariable Long[] ids,
                             @ApiParam("企业id") @RequestParam(name = "enterpriseId") Long enterpriseId,
                             @ApiParam("项目id") @RequestParam(name = "pmsProjectId", defaultValue = "0") Long pmsProjectId)
    {
        return toAjax(pmsDocumentService.deletePmsDocumentByIds(ids, enterpriseId, pmsProjectId));
    }

    /**
     * 导入wiki时可选的仓库列表
     */
    @GetMapping(value = "/optionalWikiRepoList")
    @ApiOperation(value = "导入wiki时可选的仓库列表")
    public AjaxResult getOptionalWikiRepoList(@Validated OptionalWikiReposSearchVo optionalWikiReposSearchVo)
    {
        return success(pmsDocumentService.getOptionalWikiRepoList(optionalWikiReposSearchVo));
    }
}
