package com.microservices.pms.project.controller;

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.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
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.IPmsProjectSprintService;
import com.microservices.pms.project.service.PmsProjectIssuesService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 项目迭代Controller
 *
 * @author wanjia
 * @date 2023-10-11
 */
@RestController
@RequestMapping("/{enterpriseIdentifier}/pmsProjectSprint")
@Api(tags = "项目管理-项目迭代接口")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsProjectSprintController extends BaseController {
    @Autowired
    private IPmsProjectSprintService pmsProjectSprintService;

    @Autowired
    private PmsProjectIssuesService pmsProjectIssuesService;

    /**
     * 查询项目迭代列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/list")
    @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<PmsProjectSprintDetailVo> list(PmsProjectSprintSearchVo pmsProjectSprintSearchVo) {
        return pmsProjectSprintService.selectPmsProjectSprintList(pmsProjectSprintSearchVo);
    }

    /**
     * 查询与项目迭代已关联/未关联工作项列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/issues")
    @ApiOperation(value = "查询与项目迭代已关联/未关联工作项列表")
    public AjaxResult issueList(PmsProjectSprintLinkIssuesSearchVo pmsProjectSprintLinkIssuesSearchVo) {
        JSONObject result = pmsProjectIssuesService.getPmsProjectSprintLinkIssues(pmsProjectSprintLinkIssuesSearchVo);
        return success(result);
    }

    /**
     * 获取项目迭代详细信息
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id,
                              PmsProjectSprintIssuesSearchVo pmsProjectSprintIssuesSearchVo) {
        return success(pmsProjectSprintService.selectPmsProjectSprint(id, pmsProjectSprintIssuesSearchVo));
    }

    /**
     * 获取项目迭代下工作项统计信息
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/statistics")
    @ApiOperation(value = "获取项目迭代下工作项统计信息")
    public AjaxResult getStatistics(@ApiParam("迭代id，多个用逗号隔开") @RequestParam String pmSprintIds) {
        return success(pmsProjectSprintService.getPmsProjectSprintStatistics(pmSprintIds));
    }

    /**
     * 获取项目迭代燃尽图
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/burnDownChart")
    @ApiOperation(value = "获取项目迭代下燃尽图")
    public AjaxResult getBurndownChart(@ApiParam("迭代id") @RequestParam Long pmSprintId) {
        return success(pmsProjectSprintService.getPmsProjectSprintBurnDownChart(pmSprintId));
    }

    /**
     * 完成项目迭代，并批量处理工作项
     */
    @RequiresPermissions("pms:pmsProjectSprint:edit")
    @PostMapping(value = "/complete")
    @ApiOperation(value = "完成项目迭代，并批量处理工作项")
    public AjaxResult completeSprint(@RequestBody PmsProjectSprintCompleteVo pmsProjectSprintCompleteVo) {
        return toAjax(pmsProjectSprintService.completeSprint(pmsProjectSprintCompleteVo));
    }

    /**
     * 新增项目迭代
     */
    @RequiresPermissions("pms:pmsProjectSprint:add")
    @Log(title = "项目迭代", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated PmsProjectSprintInputVo pmsProjectSprintInputVo) {
        return toAjax(pmsProjectSprintService.insertPmsProjectSprint(pmsProjectSprintInputVo.toPmsProjectSprint()));
    }

    @RequiresPermissions("pms:pmsProjectSprint:add")
    @Log(title = "项目迭代", businessType = BusinessType.INSERT)
    @PostMapping(value = "/findOrCreate")
    @ApiOperation(value = "查询或新增项目迭代（迭代名、负责人、项目id相同的会返回该迭代）")
    public AjaxResult findOrCreate(@RequestBody @Validated PmsProjectSprintInputVo pmsProjectSprintInputVo) {
        return success(pmsProjectSprintService.findOrCreatePmsProjectSprint(pmsProjectSprintInputVo.toPmsProjectSprint()));
    }

    /**
     * 修改项目迭代
     */
    @RequiresPermissions("pms:pmsProjectSprint:edit")
    @Log(title = "项目迭代", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated PmsProjectSprintUpdateVo pmsProjectSprintUpdateVo) {
        return toAjax(pmsProjectSprintService.updatePmsProjectSprint(pmsProjectSprintUpdateVo.toPmsProjectSprint()));
    }

    /**
     * 删除项目迭代
     */
    @RequiresPermissions("pms:pmsProjectSprint:remove")
    @Log(title = "项目迭代", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsProjectSprintService.deletePmsProjectSprintByIds(ids));
    }
}
