package com.microservices.pms.project.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
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.security.annotation.Logical;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.pms.project.domain.vo.*;
import com.microservices.pms.project.service.IPmsProjectTestsheetIssuesService;
import com.microservices.pms.project.service.PmsProjectIssuesService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 项目工作项Controller
 *
 * @author wanjia
 * @date 2023-10-13
 */
@RestController
@RequestMapping("/{enterpriseIdentifier}/pmsProjectIssues")
@Api(tags = "项目管理-项目工作项相关接口")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsProjectIssuesController  extends BaseController {

    @Autowired
    private PmsProjectIssuesService pmsProjectIssuesService;

    @Autowired
    private IPmsProjectTestsheetIssuesService pmsProjectTestsheetIssuesService;

    /**
     * 查询项目工作项列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe", "pms:pmsProjectTestsheet:edit"}, logical = Logical.OR)
    @GetMapping("/list")
    @ApiOperation(value = "项目工作项列表")
    public AjaxResult list(PmsProjectIssuesSearchVo pmsProjectIssuesSearchVo)
    {
        JSONObject result = pmsProjectIssuesService.selectPmsProjectIssuesList(pmsProjectIssuesSearchVo);
        return success(result);
    }

    /**
     * 新增项目工作项
     */
    @RequiresPermissions("pms:pmsProjectIssues:add")
    @PostMapping("/add")
    @ApiOperation(value = "新增项目工作项")
    public AjaxResult add(@RequestBody PmsProjectIssuesInputVo pmsProjectIssuesInputVo)
    {
        JSONObject result = pmsProjectIssuesService.insertPmsProjectIssues(pmsProjectIssuesInputVo);
        return success(result);
    }

    /**
     * 更新项目工作项
     */
    @RequiresPermissions("pms:pmsProjectIssues:edit")
    @PostMapping("/edit/{id}")
    @ApiOperation(value = "编辑项目工作项")
    public AjaxResult edit(@PathVariable("id") Long id, @RequestBody PmsProjectIssuesInputVo pmsProjectIssuesInputVo)
    {
        JSONObject result = pmsProjectIssuesService.updatePmsProjectIssues(id, pmsProjectIssuesInputVo);
        return success(result);
    }

    /**
     * 批量更新项目工作项
     */
    @RequiresPermissions("pms:pmsProjectIssues:edit")
    @PutMapping("/edit/batch")
    @ApiOperation(value = "批量修改项目工作项")
    public AjaxResult batchUpdate(@ApiParam( "项目Id") @RequestParam(name= "pmProjectId") Long pmProjectId,
                                  @RequestBody PmsProjectIssuesBatchUpdateVo pmsProjectIssuesUpdateVo)
    {

        JSONObject result = pmsProjectIssuesService.batchUpdatePmsProjectIssues(pmProjectId, pmsProjectIssuesUpdateVo);
        return success(result);
    }

    /**
     * 查看项目工作项
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/{id}")
    public AjaxResult query(@PathVariable("id") Long id, @ApiParam( "项目Id")@RequestParam(name = "pmProjectId") Long pmProjectId)
    {

        JSONObject result = pmsProjectIssuesService.selectPmsProjectIssueById(id, pmProjectId);
        return success(result);
    }

    /**
     * 删除项目工作项
     */
    @RequiresPermissions("pms:pmsProjectIssues:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam( "工作项id数组，为-1时删除全部")@PathVariable Long[] ids, @ApiParam( "项目Id")@RequestParam(name = "pmProjectId") Long pmProjectId)
    {

        JSONObject result = pmsProjectIssuesService.deletePmsProjectIssueByIds(ids, pmProjectId);
        return success(result);
    }

    /**
     * 查询项目工作项状态列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/issueStatus")
    @ApiOperation(value = "查询项目工作项状态列表")
    public AjaxResult issueStatus()
    {

        JSONObject result = pmsProjectIssuesService.selectPmsProjectIssueStatus();
        return success(result);
    }

    /**
     * 查询项目工作项优先级列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/issuePriorities")
    @ApiOperation(value = "查询项目工作项优先级列表")
    public AjaxResult issuePriorities()
    {

        JSONObject result = pmsProjectIssuesService.selectPmsProjectIssuePriorities();
        return success(result);
    }

    /**
     * 查询初始的项目工作项标记列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe", "pms:pmsProjectIssuesTag:manage"}, logical = Logical.OR)
    @GetMapping("/issueTags")
    @ApiOperation(value = "查询初始的项目工作项标记列表")
    public AjaxResult issueTags(@ApiParam(name = "pmProjectId", value = "项目Id") Long pmProjectId,
                                @ApiParam(name = "repositoryId", value = "仓库") Long repositoryId)
    {

        JSONObject result = pmsProjectIssuesService.selectPmsProjectIssueTags(pmProjectId, repositoryId);
        return success(result);
    }

    /**
     * 查询企业下项目工作项标记列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe", "pms:pmsProjectIssuesTag:manage"}, logical = Logical.OR)
    @GetMapping("/enterpriseIssueTags")
    @ApiOperation(value = "查询企业下项目工作项标记列表")
    public AjaxResult getEnterpriseIssueTags(PmsEnterpriseIssueTagsSearchVo pmsEnterpriseIssueTagsSearchVo)
    {

        JSONObject result = pmsProjectIssuesService.selectPmsEnterpriseIssueTags(pmsEnterpriseIssueTagsSearchVo);
        return success(result);
    }

    /**
     * 创建企业下项目工作项标记
     */
    @RequiresPermissions("pms:pmsProjectIssuesTag:manage")
    @PostMapping("/enterpriseIssueTags")
    @ApiOperation(value = "创建企业下项目工作项标记")
    public AjaxResult createEnterpriseIssueTag(@RequestBody PmsEnterpriseIssueTagInputVo pmsEnterpriseIssueTagInputVo)
    {

        JSONObject result = pmsProjectIssuesService.createPmsEnterpriseIssueTag(pmsEnterpriseIssueTagInputVo);
        return success(result);
    }

    /**
     * 更新企业下项目工作项标记
     */
    @RequiresPermissions("pms:pmsProjectIssuesTag:manage")
    @PutMapping("/enterpriseIssueTags/{tagId}")
    @ApiOperation(value = "更新企业下项目工作项标记")
    public AjaxResult editEnterpriseIssueTag(@PathVariable("tagId") Long tagId, @RequestBody PmsEnterpriseIssueTagUpdateVo pmsEnterpriseIssueTagUpdateVo)
    {

        JSONObject result = pmsProjectIssuesService.editPmsEnterpriseIssueTag(tagId, pmsEnterpriseIssueTagUpdateVo);
        return success(result);
    }

    /**
     * 删除企业下项目工作项标记
     */
    @RequiresPermissions("pms:pmsProjectIssuesTag:manage")
    @DeleteMapping("/enterpriseIssueTags/{tagId}")
    @ApiOperation(value = "删除企业下项目工作项标记")
    public AjaxResult deleteEnterpriseIssueTag(@PathVariable("tagId") Long tagId)
    {

        JSONObject result = pmsProjectIssuesService.deletePmsEnterpriseIssueTag(tagId);
        return success(result);
    }

    /**
     * 查询仓库下分支列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("{repositoryId}/repoBranches")
    @ApiOperation(value = "查询仓库下分支列表")
    public AjaxResult repoBranches(@PathVariable Long repositoryId)
    {

        JSONArray result = pmsProjectIssuesService.selectPmsProjectRepoBranches(repositoryId);
        return success(result);
    }

    /**
     * 上传工作项中的附件
     */
    @RequiresPermissions(value = {"pms:pmsProjectIssues:edit", "pms:pmsProjectIssues:add"}, logical = Logical.OR)
    @PostMapping("/attachments")
    @ApiOperation(value = "上传工作项中的附件")
    public AjaxResult uploadAttachments(@RequestPart("file") MultipartFile file)
    {
        JSONObject result = pmsProjectIssuesService.uploadAttachment(file);
        return success(result);
    }

    /**
     * 删除工作项中的附件
     */
    @RequiresPermissions(value = {"pms:pmsProjectIssues:edit", "pms:pmsProjectIssues:add", "pms:pmsProjectIssues:remove"}, logical = Logical.OR)
    @DeleteMapping("/attachments/{id}")
    @ApiOperation(value = "删除工作项中的附件")
    public AjaxResult deleteAttachments(@PathVariable Long id)
    {

        JSONObject result = pmsProjectIssuesService.deleteAttachment(id);
        return success(result);
    }

    /**
     * 添加项目工作项关联
     */
    @RequiresPermissions(value = {"pms:pmsProjectIssues:edit", "pms:pmsProjectIssues:add"}, logical = Logical.OR)
    @PostMapping("/link/{issueId}")
    @ApiOperation(value = "添加项目工作项关联")
    public AjaxResult addIssueLinks(@ApiParam("工作项id") @PathVariable Long issueId,
                                    @RequestBody PmsProjectIssuesLinkVo pmsProjectIssuesLinkVo)
    {

        JSONObject result = pmsProjectIssuesService.addPmsProjectIssueLink(issueId, pmsProjectIssuesLinkVo);
        return success(result);
    }

    /**
     * 查看项目工作项关联
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/link/{issueId}")
    @ApiOperation(value = "查看项目工作项关联")
    public AjaxResult getIssueLinks(@ApiParam("工作项id") @PathVariable Long issueId,
                                    @ApiParam( "项目Id")@RequestParam(name = "pmProjectId") Long pmProjectId)
    {

        JSONObject result = pmsProjectIssuesService.getPmsProjectIssueLink(issueId, pmProjectId);
        return success(result);
    }

    /**
     * 查看与当前项目工作项未关联的工作项
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/unlink/{issueId}")
    @ApiOperation(value = "查看与当前项目工作项未关联/未绑定父子级关系的一级工作项")
    public AjaxResult getIssueUnlinks(@ApiParam("页码") Integer pageNum,
                                      @ApiParam("页数") Integer pageSize,
                                      @ApiParam("工作项id") @PathVariable Long issueId,
                                      @ApiParam("项目Id") @RequestParam(name = "pmProjectId") Long pmProjectId,
                                      @ApiParam("需求1 任务2 缺陷3,默认为 1") @RequestParam(name = "pmIssueType", defaultValue = "1") Integer pmIssueType,
                                      @ApiParam("工作项筛选类型 叶子节点工作项:leaf_issue  关联工作项:link_issue") @RequestParam(name = "issueFilterType") String issueFilterType) {

        JSONObject result = pmsProjectIssuesService.getPmsProjectIssueUnlink(issueId, pmProjectId, pmIssueType, issueFilterType, pageNum, pageSize);
        return success(result);
    }

    /**
     * 可选择的父工作项列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/optionalParentIssue/{issueId}")
    @ApiOperation(value = "可选择的父工作项列表")
    public AjaxResult getOptionalParentIssueList(
                                      @ApiParam("页码") Integer pageNum,
                                      @ApiParam("页数") Integer pageSize,
                                      @ApiParam("工作项id") @PathVariable Long issueId,
                                      @ApiParam("需求1 任务2 缺陷3") @RequestParam(name = "pmIssueType") String pmIssueType,
                                      @ApiParam("项目id") @RequestParam(name = "pmProjectId") String pmProjectId,
                                      @ApiParam("搜索关键词") @RequestParam(name = "keyword", required = false) String keyword

    ) {

        JSONObject result = pmsProjectIssuesService.getPmsProjectIssueOptionalParentIssueList(issueId, pmIssueType, pmProjectId, keyword, pageNum, pageSize);
        return success(result);
    }

    /**
     * 删除项目工作项关联
     */
    @RequiresPermissions(value = {"pms:pmsProjectIssues:edit", "pms:pmsProjectIssues:add", "pms:pmsProjectIssues:remove"}, logical = Logical.OR)
    @DeleteMapping("/link/{issueId}/{linkIssueId}")
    @ApiOperation(value = "删除项目工作项关联")
    public AjaxResult removeIssueLinks(@ApiParam("当前工作项id") @PathVariable Long issueId,
                                       @ApiParam("关联工作项id") @PathVariable String linkIssueId,
                                       @ApiParam( "项目Id")@RequestParam(name = "pmProjectId") Long pmProjectId)
    {

        JSONObject result = pmsProjectIssuesService.deletePmsProjectIssueLink(issueId, linkIssueId, pmProjectId);
        return success(result);
    }

    /**
     * 新增工作项评论
     */
    @PostMapping("/journals/{issueId}")
    @ApiOperation(value = "新增工作项评论")
    public AjaxResult addJournals(@ApiParam("工作项id") @PathVariable Long issueId,
                                  @RequestBody PmsProjectIssueJournalInputVo pmsProjectIssueJournalInputVo)
    {
        JSONObject result = pmsProjectIssuesService.addPmsProjectIssueJournals(issueId, pmsProjectIssueJournalInputVo);
        return success(result);
    }

    /**
     * 删除工作项评论
     */
    @DeleteMapping("/journals/{issueId}/{journalId}")
    @ApiOperation(value = "删除工作项评论")
    public AjaxResult removeJournals(@ApiParam("工作项id") @PathVariable Long issueId,
                                     @ApiParam("评论id") @PathVariable Long journalId,
                                     @ApiParam( "项目Id")@RequestParam(name = "pmProjectId") Long pmProjectId,
                                     @ApiParam( "工作项关联的仓库id")@RequestParam(name = "pmProjectRepositoryId", required = false) Long pmProjectRepositoryId
                                     )
    {
        JSONObject result = pmsProjectIssuesService.removePmsProjectIssueJournals(issueId, journalId, pmProjectId, pmProjectRepositoryId);
        return success(result);
    }

    /**
     * 修改工作项评论
     */
    @PutMapping("/journals/{issueId}/{journalId}")
    @ApiOperation(value = "编辑工作项评论")
    public AjaxResult editJournals(@ApiParam("工作项id") @PathVariable Long issueId,
                                   @ApiParam("评论id") @PathVariable Long journalId,
                                   @RequestBody PmsProjectIssueJournalEditVo pmsProjectIssueJournalEditVo

    ) {
        JSONObject result = pmsProjectIssuesService.editPmsProjectIssueJournals(issueId, journalId, pmsProjectIssueJournalEditVo);
        return success(result);
    }

    /**
     * 工作项评论列表
     */
    @GetMapping("/journals/{issueId}/list")
    @ApiOperation(value = "工作项评论列表")
    public AjaxResult getJournals(@ApiParam("工作项id") @PathVariable Long issueId,
                                  PmsProjectIssueJournalSearchVo pmsProjectIssueJournalSearchVo

    ) {
        JSONObject result = pmsProjectIssuesService.getPmsProjectIssueJournals(issueId, pmsProjectIssueJournalSearchVo);
        return success(result);
    }

    /**
     * 关联测试用例
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/linkTestcase/{issueId}")
    @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<PmsProjectTestsheetCaseVo> linkTestcase(@ApiParam("工作项id") @PathVariable Long issueId, @ApiParam("项目Id") @RequestParam(name = "pmProjectId") Long pmProjectId) {
        return pmsProjectTestsheetIssuesService.getProjectIssueLinkTestcaseList(issueId, pmProjectId);
    }

    @RequiresPermissions(value = {"pms:pmsProjectIssues:edit"}, logical = Logical.OR)
    @PostMapping("/import")
    @ApiOperation(value = "导入工作项")
    public AjaxResult importIssuesFromFile(@PathVariable String enterpriseIdentifier, @RequestPart("file") MultipartFile file, @ApiParam("项目Id") @RequestParam(name = "pmProjectId") Long pmProjectId) {
        return success(pmsProjectIssuesService.importIssuesFromFile(enterpriseIdentifier, file, pmProjectId));
    }

    @RequiresPermissions(value = {"pms:pmsProjectIssues:edit"}, logical = Logical.OR)
    @GetMapping("/export")
    @ApiOperation(value = "导出工作项")
    public void exportIssues(HttpServletResponse response, @ApiParam("项目Id") @RequestParam(name = "pmProjectId") Long pmProjectId) {
        pmsProjectIssuesService.exportIssues(response, pmProjectId);
    }
}
