package com.microservices.pms.dashboard.controller;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.utils.bean.BeanUtils;
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.utils.SecurityUtils;
import com.microservices.pms.dashboard.domain.vo.MyDashboardVo;
import com.microservices.pms.dashboard.domain.vo.MyProjectIssuesSearchVo;
import com.microservices.pms.product.domain.PmsProduct;
import com.microservices.pms.product.domain.PmsProductRequirement;
import com.microservices.pms.product.domain.vo.PmsProductSearchVo;
import com.microservices.pms.product.service.IPmsProductRequirementService;
import com.microservices.pms.product.service.IPmsProductService;
import com.microservices.pms.project.domain.PmsProject;
import com.microservices.pms.project.domain.vo.*;
import com.microservices.pms.project.service.IPmsProjectService;
import com.microservices.pms.project.service.IPmsProjectSprintService;
import com.microservices.pms.project.service.PmsProjectIssuesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/{enterpriseIdentifier}/dashboard")
@Api(tags = "项目管理-工作台")
@ApiImplicitParams({@ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")})
public class PmsDashboardController extends BaseController {

    @Autowired
    private IPmsProjectService pmsProjectService;

    @Autowired
    private PmsProjectIssuesService pmsProjectIssuesService;

    @Autowired
    private IPmsProjectSprintService pmsProjectSprintService;

    @Autowired
    private IPmsProductRequirementService pmsProductRequirementService;

    @Autowired
    private IPmsProductService pmsProductService;


    /**
     * 我的工作台统计信息
     */
    @ApiOperation(value = "我的工作台统计信息")
    @GetMapping("/statistics")
    public AjaxResult statistics(@PathVariable String enterpriseIdentifier) {
        List<PmsProject> projectList = pmsProjectService.selectPmsProjectList(new PmsProject());
        PmsProductSearchVo pmsProductSearchVo = new PmsProductSearchVo();
        pmsProductSearchVo.setPmsEnterpriseIdentifier(enterpriseIdentifier);
        List<PmsProduct> pmsProductList = pmsProductService.selectPmsProductList(pmsProductSearchVo);
        MyDashboardVo myDashboardVo = new MyDashboardVo();
        if (!projectList.isEmpty()) {
            IssueTypeTotalCountVo issueTypeTotalCountVo = pmsProjectService.getIssueTypeProjectIssuesTotalCount(projectList.stream().map(PmsProject::getId).map(String::valueOf).collect(Collectors.toList()));
            BeanUtils.copyProperties(issueTypeTotalCountVo, myDashboardVo);
        }
        if (!pmsProductList.isEmpty()) {
            PmsProductRequirement pmsProductRequirement = new PmsProductRequirement();
            pmsProductRequirement.setProductIdentifierList(pmsProductList.stream().map(PmsProduct::getProductIdentifier).collect(Collectors.toList()));
            pmsProductRequirement.setCreateBy(SecurityUtils.getUsername());
            Integer requirementCount = pmsProductRequirementService.selectPmsProductRequirementCount(pmsProductRequirement);
            myDashboardVo.setProductRequirementCount(requirementCount);
        }
        return success(myDashboardVo);
    }

    /**
     * 我的工作项
     */
    @GetMapping("/myProjectIssues")
    @ApiOperation(value = "我的工作项")
    public AjaxResult myProjectIssues(MyProjectIssuesSearchVo myProjectIssuesSearchVo) {
        PmsProjectIssuesSearchVo pmsProjectIssuesSearchVo = new PmsProjectIssuesSearchVo();
        BeanUtils.copyProperties(myProjectIssuesSearchVo, pmsProjectIssuesSearchVo);
        List<PmsProject> projectList = pmsProjectService.selectPmsProjectList(new PmsProject());
        if(projectList.isEmpty()){
            return success();
        }
        pmsProjectIssuesSearchVo.setPmProjectIds(StringUtils.join(projectList.stream().map(PmsProject::getId).toArray(), ","));
        // 不包含周报
        pmsProjectIssuesSearchVo.setPmIssueTypes("1,2,3");
        JSONObject result = pmsProjectIssuesService.selectPmsProjectIssuesList(pmsProjectIssuesSearchVo);
        return success(result);
    }

    /**
     * 我的项目
     */
    @ApiOperation(value = "我的项目")
    @GetMapping("/myProjects")
    @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<PmsProjectDetailVo> myProjects(PmsProjectSearchVo pmsProjectSearchVo) {
        return pmsProjectService.selectPmsProjectList(pmsProjectSearchVo);
    }


    /**
     * 我的迭代
     */
    @ApiOperation(value = "我的迭代")
    @GetMapping("/myProjectSprints")
    @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> myProjectSprints(PmsProjectSprintSearchVo pmsProjectSprintSearchVo) {
        List<PmsProject> projectList = pmsProjectService.selectPmsProjectList(new PmsProject());
        if(projectList.isEmpty()){
            return new GenericsTableDataInfo<>();
        }
        pmsProjectSprintSearchVo.setPmsProjectIds(StringUtils.join(projectList.stream().map(PmsProject::getId).toArray(), ","));
        return pmsProjectSprintService.selectAllPmsProjectSprintList(pmsProjectSprintSearchVo);
    }
}
