package com.microservices.pms.project.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.constant.HttpStatus;
import com.microservices.common.core.constant.SecurityConstants;
import com.microservices.common.core.constant.UserConstants;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.httpClient.util.GitLinkRequestHelper;
import com.microservices.pms.common.service.IPmsCommonService;
import com.microservices.pms.enterprise.domain.PmsEnterprise;
import com.microservices.pms.enterprise.service.IPmsEnterpriseService;
import com.microservices.pms.product.domain.PmsProduct;
import com.microservices.pms.product.service.IPmsProductService;
import com.microservices.pms.project.domain.PmsProject;
import com.microservices.pms.project.domain.PmsProjectMembers;
import com.microservices.pms.project.domain.PmsProjectRepository;
import com.microservices.pms.project.domain.vo.*;
import com.microservices.pms.project.mapper.*;
import com.microservices.pms.project.service.IPmsProjectMembersService;
import com.microservices.pms.project.service.IPmsProjectRepositoryService;
import com.microservices.pms.project.service.IPmsProjectService;
import com.microservices.pms.project.service.PmsProjectIssuesService;
import com.microservices.pms.utils.PmsGitLinkRequestUrl;
import com.microservices.pms.utils.PmsUtils;
import com.microservices.system.api.RemoteDeptService;
import com.microservices.system.api.RemoteNoticeService;
import com.microservices.system.api.domain.SimpleSysUser;
import com.microservices.system.api.domain.SysNotice;
import com.microservices.system.api.domain.SysUser;
import com.microservices.system.api.utils.FeignUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.microservices.common.core.utils.PageUtils.startPage;
import static com.microservices.common.core.utils.PageUtils.toPage;
import static com.microservices.common.httpClient.constant.GitLinkConstants.DATA_KEY;

/**
 * 项目Service业务层处理
 * 
 * @author wanjia
 */
@Service
public class PmsProjectServiceImpl implements IPmsProjectService 
{
    @Resource
    private PmsProjectMapper pmsProjectMapper;

    @Autowired
    private IPmsProjectRepositoryService pmsProjectRepositoryService;

    @Autowired
    private IPmsProjectMembersService pmsProjectMembersService;

    @Autowired
    private PmsProjectMembersMapper pmsProjectMembersMapper;

    @Resource
    private PmsProjectRepositoryMapper pmsProjectRepositoryMapper;

    @Autowired
    private PmsProjectIssuesService pmsProjectIssuesService;

    @Autowired
    private IPmsCommonService pmsCommonService;

    @Autowired
    private IPmsProductService pmsProductService;

    @Autowired
    @Lazy
    private IPmsEnterpriseService pmsEnterpriseService;

    @Autowired
    private RemoteNoticeService remoteNoticeService;

    @Autowired
    private GitLinkRequestHelper gitLinkRequestHelper;

    @Autowired
    private RemoteDeptService remoteDeptService;
    @Resource
    private PmsProjectSprintMapper pmsProjectSprintMapper;
    @Resource
    private PmsProjectTestcaseMapper pmsProjectTestcaseMapper;
    @Resource
    private PmsProjectTestsheetMapper pmsProjectTestsheetMapper;
    @Resource
    private PmsProjectTestReportMapper pmsProjectTestReportMapper;

    /**
     * 查询项目
     * 
     * @param id 项目主键
     * @return 项目
     */
    @Override
    public PmsProjectDetailVo selectPmsProjectById(Long id)
    {
        PmsProject pmsProject = selectAndCheckPmsProjectById(id);
        PmsProjectDetailVo pmsProjectDetailVo = new PmsProjectDetailVo();
        BeanUtils.copyProperties(pmsProject,pmsProjectDetailVo);
        pmsProjectDetailVo.setProjectMemberCount(pmsProjectMembersMapper.countMemberByProjectId(pmsProject.getId()));
        pmsProjectDetailVo.setProjectRepositoryCount(pmsProjectRepositoryMapper.countRepositoryByProjectId(pmsProject.getId()));
        pmsProjectDetailVo.setProjectAssignee(pmsCommonService.getSimpleSysUserByGitlinkUserId(pmsProject.getProjectAssigneeId()));
        pmsProjectDetailVo.setProjectMembers(pmsProjectMembersService.selectPmsProjectMemberInfoListByProjectId(pmsProject.getId()));
        pmsProjectDetailVo.setProjectRepos(pmsProjectRepositoryService.selectPmsProjectRepoInfoListByProjectId(pmsProject.getId(), pmsProject.getPmsEnterpriseId()));
        if (pmsProject.getPmsProductId() != null) {
            pmsProjectDetailVo.setPmsProduct(pmsProductService.selectPmsProductById(pmsProject.getPmsProductId()));
        }
        return pmsProjectDetailVo;
    }

    /**
     * 查询项目列表
     * 
     * @param pmsProject 项目
     * @return 项目
     */
    @Override
    public List<PmsProject> selectPmsProjectList(PmsProject pmsProject)
    {
        return pmsProjectMapper.selectPmsProjectList(pmsProject);
    }

    @Override
    public GenericsTableDataInfo<PmsProjectDetailVo> selectPmsProjectList(PmsProjectSearchVo pmsProjectSearchVo) {
        startPage();
        List<PmsProject> pmsProjectList = pmsProjectMapper.selectPmsProjectListBySearchInput(pmsProjectSearchVo);
        List<String> projectIdList = pmsProjectList.stream()
                .map(p -> String.valueOf(p.getId()))
                .collect(Collectors.toList());
        JSONObject projectTotalCount = new JSONObject();
        if (StringUtils.isNotEmpty(pmsProjectList)) {
            projectTotalCount = getProjectIssuesTotalCountByGitLink(projectIdList);
        }
        JSONObject finalProjectTotalCount = projectTotalCount;
        return toPage(pmsProjectList, pmsProject -> buildPmsProjectDetailVo(pmsProject, finalProjectTotalCount));
    }

    private PmsProjectDetailVo buildPmsProjectDetailVo(PmsProject pmsProject, JSONObject projectTotalCount) {

        PmsProjectDetailVo pmsProjectDetailVo = new PmsProjectDetailVo();
        BeanUtils.copyProperties(pmsProject, pmsProjectDetailVo);
        pmsProjectDetailVo.setProjectMemberCount(pmsProjectMembersMapper.countMemberByProjectId(pmsProject.getId()));
        pmsProjectDetailVo.setProjectRepositoryCount(pmsProjectRepositoryMapper.countRepositoryByProjectId(pmsProject.getId()));
        pmsProjectDetailVo.setProjectIssuesCount(projectTotalCount.getInteger(pmsProject.getId().toString()));
        pmsProjectDetailVo.setProjectAssignee(pmsCommonService.getSimpleSysUserByGitlinkUserId(pmsProject.getProjectAssigneeId()));
        if (pmsProject.getPmsProductId() != null) {
            pmsProjectDetailVo.setPmsProduct(pmsProductService.selectPmsProductById(pmsProject.getPmsProductId()));
        }

        return pmsProjectDetailVo;
    }

    /**
     * 新增项目
     * 
     * @param pmsProjectInputVo 项目
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPmsProject(PmsProjectInputVo pmsProjectInputVo)
    {
        PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseById(pmsProjectInputVo.getPmsEnterpriseId());
        PmsUtils.validStartTimeAndEndTime(pmsProjectInputVo.getStartTime(), pmsProjectInputVo.getEndTime());
        if (pmsProjectInputVo.getPmsProductId() != null) {
            PmsProduct pmsProduct = pmsProductService.selectPmsProductById(pmsProjectInputVo.getPmsProductId());
            if (pmsProduct == null) {
                throw new RuntimeException("关联的产品不存在");
            }
        }
        PmsProject pmsProject = pmsProjectInputVo.toPmsProject();
        pmsProject.setDeptId(pmsEnterprise.getDeptId());
        pmsProjectMapper.insertPmsProject(pmsProject);
        //同步新增项目-仓库数据
        if (StringUtils.isNotEmpty(pmsProjectInputVo.getProjectRepositoryIds())) {
            List<String> repositoryIdList = Arrays.asList(pmsProjectInputVo.getProjectRepositoryIds().split(","));
            if (!repositoryIdList.isEmpty()) {
                for (String repoId : repositoryIdList) {
                    PmsProjectRepository pmsProjectRepository = new PmsProjectRepository();
                    pmsProjectRepository.setProjectRepositoryId(Long.valueOf(repoId));
                    pmsProjectRepository.setPmsProjectId(pmsProject.getId());
                    pmsProjectRepositoryService.insertPmsProjectRepository(pmsProjectRepository);
                }
            }
        }
        // 项目负责人需要自动添加到项目成员中
        SysUser productAssigneeUser = pmsCommonService.getSysUserByGitlinkId(pmsProjectInputVo.getProjectAssigneeId());
        Boolean hasPerms = FeignUtils.getReturnData(
                remoteDeptService.checkUserInDeptHasPerms(pmsEnterprise.getDeptId(), productAssigneeUser.getUserId(), UserConstants.PMS_PERMS_PROJECT_EDIT, SecurityConstants.INNER)
        );
        if (hasPerms == null || !hasPerms) {
            throw new ServiceException("该用户没有项目编辑权限，无法被选定为项目负责人(用户名[%s])", productAssigneeUser.getUserName());
        }
        PmsProjectMembers pmsProjectMembers = new PmsProjectMembers();
        pmsProjectMembers.setPmsProjectMemberId(pmsProjectInputVo.getProjectAssigneeId());
        pmsProjectMembers.setPmsProjectId(pmsProject.getId());
        pmsProjectMembersService.insertPmsProjectMembers(pmsProjectMembers);
        ////同步新增项目-成员数据
        if (StringUtils.isNotEmpty(pmsProjectInputVo.getPmsProjectMemberIds())) {
            List<String> memberIdList = Arrays.asList(pmsProjectInputVo.getPmsProjectMemberIds().split(","));
            if (!memberIdList.isEmpty()) {
                for (String memberId : memberIdList) {
                    pmsProjectMembers = new PmsProjectMembers();
                    pmsProjectMembers.setPmsProjectMemberId(Long.valueOf(memberId));
                    pmsProjectMembers.setPmsProjectId(pmsProject.getId());
                    pmsProjectMembersService.insertPmsProjectMembers(pmsProjectMembers);
                }
            }
        }
        return pmsProject.getId().intValue();
    }

    /**
     * 批量删除项目
     * 
     * @param ids 需要删除的项目主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePmsProjectByIds(Long[] ids)
    {
        for (Long projectId : ids) {
            selectAndCheckPmsProjectById(projectId);
            // 删除项目关联仓库
            pmsProjectRepositoryMapper.deletePmsProjectRepositoryByProjectId(projectId);
            // 删除项目关联成员
            pmsProjectMembersMapper.deletePmsProjectMembersByProjectId(projectId);
            // 删除项目关联工作项
            pmsProjectIssuesService.deletePmsProjectIssueByProjectId(projectId);
            // 删除项目关联迭代
            pmsProjectSprintMapper.deletePmsProjectSprintByProjectId(projectId);
            // 删除项目关联测试用例
            pmsProjectTestcaseMapper.deletePmsProjectTestcaseByProjectId(projectId);
            // 删除项目关联测试单
            pmsProjectTestsheetMapper.deletePmsProjectTestsheetByProjectId(projectId);
            // 删除项目关联测试报告
            pmsProjectTestReportMapper.deletePmsProjectTestReportByProjectId(projectId);
            // 需要先删除项目关联项，最后删除项目
            pmsProjectMapper.deletePmsProjectById(projectId);
        }

        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePmsProject(JSONObject pmsProjectUpdateVoJson) {
        PmsProjectUpdateVo pmsProjectUpdateVo = transferPmsProjectUpdateJsonToVo(pmsProjectUpdateVoJson);
        Long updatedPmsProjectId = pmsProjectUpdateVo.getId();
        PmsProject oldProject = selectAndCheckPmsProjectById(updatedPmsProjectId);
        PmsUtils.validStartTimeAndEndTime(pmsProjectUpdateVo.getStartTime(), pmsProjectUpdateVo.getEndTime());
        pmsProjectUpdateVo.setUpdateTime(DateUtils.getNowDate());
        int count = pmsProjectMapper.updatePmsProjectByUpdateInput(pmsProjectUpdateVo);

        //同步更新项目-仓库数据
        if (pmsProjectUpdateVo.getIsResetRepositoryIdsToNull() != null && pmsProjectUpdateVo.getIsResetRepositoryIdsToNull()) {
            pmsProjectRepositoryMapper.deletePmsProjectRepositoryByProjectId(updatedPmsProjectId);
        } else
            if (StringUtils.isNotEmpty(pmsProjectUpdateVo.getProjectRepositoryIds())) {
                List<String> repositoryIdList = Arrays.asList(pmsProjectUpdateVo.getProjectRepositoryIds().split(","));
                if (!repositoryIdList.isEmpty()) {
                    pmsProjectRepositoryMapper.deletePmsProjectRepositoryByProjectId(updatedPmsProjectId);
                    for (String repoId : repositoryIdList) {
                        PmsProjectRepository pmsProjectRepository = new PmsProjectRepository();
                        pmsProjectRepository.setProjectRepositoryId(Long.valueOf(repoId));
                        pmsProjectRepository.setPmsProjectId(updatedPmsProjectId);
                        pmsProjectRepositoryService.insertPmsProjectRepository(pmsProjectRepository);
                    }
                }
            }

        //同步更新项目-成员数据
        if (StringUtils.isNotEmpty(pmsProjectUpdateVo.getPmsProjectMemberIds())) {
            List<String> memberIdList = getMemberIdList(pmsProjectUpdateVo, oldProject);
            if (!memberIdList.isEmpty()) {
                pmsProjectMembersMapper.deletePmsProjectMembersByProjectId(updatedPmsProjectId);
                for (String memberId : memberIdList) {
                    PmsProjectMembers pmsProjectMembers= new PmsProjectMembers();
                    pmsProjectMembers.setPmsProjectMemberId(Long.valueOf(memberId));
                    pmsProjectMembers.setPmsProjectId(updatedPmsProjectId);
                    pmsProjectMembersService.insertPmsProjectMembersWithoutAuthCheck(pmsProjectMembers);
                }
            }
        }
        return count;
    }

    private PmsProjectUpdateVo transferPmsProjectUpdateJsonToVo(JSONObject pmsProjectUpdateVo) {
        PmsProjectUpdateVo pmsProjectUpdateVoInput = JSONObject.parseObject(pmsProjectUpdateVo.toJSONString(), PmsProjectUpdateVo.class);
        if (pmsProjectUpdateVo.containsKey("projectRepositoryIds") && pmsProjectUpdateVo.getString("projectRepositoryIds") == null) {
            pmsProjectUpdateVoInput.setIsResetRepositoryIdsToNull(true);
        }
        if (pmsProjectUpdateVo.containsKey("projectDescription") && pmsProjectUpdateVo.getString("projectDescription") == null) {
            pmsProjectUpdateVoInput.setIsResetDescriptionToNull(true);
        }
        if (pmsProjectUpdateVo.containsKey("pmsProductId") && pmsProjectUpdateVo.getString("pmsProductId") == null) {
            pmsProjectUpdateVoInput.setIsResetPmsProductIdToNull(true);
        }
        if (pmsProjectUpdateVo.containsKey("startTime") && pmsProjectUpdateVo.getString("startTime") == null) {
            pmsProjectUpdateVoInput.setIsResetStartTimeToNull(true);
        }
        if (pmsProjectUpdateVo.containsKey("endTime") && pmsProjectUpdateVo.getString("endTime") == null) {
            pmsProjectUpdateVoInput.setIsResetEndTimeToNull(true);
        }
        return pmsProjectUpdateVoInput;
    }

    // 需校验移除的成员中是否包含项目负责人
    private List<String> getMemberIdList(PmsProjectUpdateVo pmsProjectUpdateVo, PmsProject oldProject) {
        List<String> memberIdList = new ArrayList<>(Arrays.asList(pmsProjectUpdateVo.getPmsProjectMemberIds().split(",")));
        if (pmsProjectUpdateVo.getProjectAssigneeId() != null
                && !pmsProjectUpdateVo.getProjectAssigneeId().equals(oldProject.getProjectAssigneeId())) {
            SysUser productAssigneeUser = pmsCommonService.getSysUserByGitlinkId(pmsProjectUpdateVo.getProjectAssigneeId());
            Boolean hasPerms = FeignUtils.getReturnData(
                    remoteDeptService.checkUserInDeptHasPerms(oldProject.getDeptId(), productAssigneeUser.getUserId(), UserConstants.PMS_PERMS_PROJECT_EDIT, SecurityConstants.INNER)
            );
            if (hasPerms == null || !hasPerms) {
                throw new ServiceException("该用户没有项目编辑权限，无法被选定为项目负责人(用户名[%s])", productAssigneeUser.getUserName());
            }
            memberIdList.add(pmsProjectUpdateVo.getProjectAssigneeId().toString());
        } else {
            if (!memberIdList.contains(oldProject.getProjectAssigneeId().toString())) {
                SimpleSysUser sysUser = pmsCommonService.getSimpleSysUserByGitlinkUserId(oldProject.getProjectAssigneeId());
                throw new ServiceException("该用户为当前项目负责人，不允许从成员中移出(用户名[%s])", sysUser.getUserName());
            }
        }
        return memberIdList;
    }

    /**
     * 根据项目Id获取项目，校验数据权限
     *
     * @param id 项目id
     * @return 项目
     */
    @Override
    public PmsProject selectAndCheckPmsProjectById(Long id) {
        PmsProject pmsProjectSearch = new PmsProject();
        pmsProjectSearch.setId(id);
        List<PmsProject> projectList = pmsProjectMapper.selectPmsProjectList(pmsProjectSearch);
        PmsProject pmsProject = projectList
                .stream()
                .filter(x -> id.equals(x.getId()))
                .findFirst()
                .orElseThrow(() -> new ServiceException(HttpStatus.FORBIDDEN, "该项目不存在或无权限访问(项目ID[%s])", id));
        pmsEnterpriseService.selectPmsEnterpriseById(pmsProject.getPmsEnterpriseId());
        return pmsProject;
    }

    @Override
    public PmsProject selectAndCheckPmsProjectById(String idStr) {
        if (StringUtils.isEmpty(idStr)) {
            return null;
        }
        long id;
        try {
            id = Long.parseLong(idStr);
        } catch (Exception e) {
            throw new ServiceException("项目Id格式不正确(项目Id[%s])", idStr);
        }
        return selectAndCheckPmsProjectById(id);
    }

    @Override
    public PmsProjectSimpleVo getSimpleProject(Long pmsProjectId) {
        PmsProject pmsProject = selectAndCheckPmsProjectById(pmsProjectId);
        PmsProjectSimpleVo pmsProjectSimpleVo = new PmsProjectSimpleVo();
        BeanUtils.copyProperties(pmsProject, pmsProjectSimpleVo);
        return pmsProjectSimpleVo;
    }

    @Override
    public int addProjectNotice(PmsProjectNoticeInputVo pmsProjectNoticeInputVo) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(pmsProjectNoticeInputVo, sysNotice);
        sysNotice.setStatus("0"); //公告状态（0正常 1关闭）
        Long noticeId = FeignUtils.getReturnData(remoteNoticeService.addNotice(sysNotice, SecurityConstants.INNER));
        PmsProject pmsProject = new PmsProject();
        pmsProject.setId(pmsProjectNoticeInputVo.getPmsProjectId());
        pmsProject.setNoticeId(noticeId);
        return pmsProjectMapper.updatePmsProject(pmsProject);
    }

    @Override
    public Integer removeProjectNotice(Long[] noticeIds) {
        return FeignUtils.getReturnData(remoteNoticeService.deleteNotice(noticeIds, SecurityConstants.INNER));
    }

    @Override
    public Integer editProjectNotice(PmsProjectNoticeUpdateVo pmsProjectNoticeUpdateVo) {
        SysNotice sysNotice = new SysNotice();
        BeanUtils.copyProperties(pmsProjectNoticeUpdateVo, sysNotice);
        return FeignUtils.getReturnData(remoteNoticeService.editNotice(sysNotice, SecurityConstants.INNER));
    }

    @Override
    public SysNotice getProjectNotice(Long noticeId) {
        SysNotice sysNotice = FeignUtils.getReturnData(remoteNoticeService.getNotice(noticeId, SecurityConstants.INNER));
        if (sysNotice != null) {
            SimpleSysUser createSysUser = pmsCommonService.getSimpleSysUserByUsername(sysNotice.getCreateBy());
            sysNotice.setCreateBy(createSysUser.getNickName());
            SimpleSysUser updateSysUser = pmsCommonService.getSimpleSysUserByUsername(sysNotice.getUpdateBy());
            sysNotice.setUpdateBy(updateSysUser.getNickName());
        }
        return sysNotice;
    }

    @Override
    public void clearPmsProjectAndProductAssociations(Long productId) {
        PmsProject pmsProjectSearch = new PmsProject();
        pmsProjectSearch.setPmsProductId(productId);
        List<PmsProject> pmsProjectList = pmsProjectMapper.selectPmsProjectListByNoDataScope(pmsProjectSearch);
        if (pmsProjectList != null && !pmsProjectList.isEmpty()) {
            List<Long> projectIdList = pmsProjectList.stream().map(PmsProject::getId).collect(Collectors.toList());
            pmsProjectMapper.updatePmsProjectNoProduct(projectIdList);
        }
    }


    @Override
    public JSONObject getPmsProjectStatistics(Long pmProjectId) {
        // 校验数据权限
        selectAndCheckPmsProjectById(pmProjectId);
        JSONObject result = gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_PROJECT_STATISTICS(pmProjectId));
        return result.getJSONObject(DATA_KEY);
    }

    @Override
    public JSONObject getPmsProjectPolyline(Long pmProjectId) {
        // 校验数据权限
        selectAndCheckPmsProjectById(pmProjectId);
        JSONObject result = gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_PROJECT_POLYLINE(pmProjectId));
        return result.getJSONObject(DATA_KEY);
    }

    private JSONObject getProjectIssuesTotalCountByGitLink(List<String> projectIdList) {
        JSONObject result = pmsProjectIssuesService.getProjectIssuesCount(projectIdList, "all", "all");
        JSONObject dataObj = result.getJSONObject(DATA_KEY);
        return dataObj.keySet().stream()
                .map(key -> new AbstractMap.SimpleEntry<>(key, dataObj.getJSONObject(key).getInteger("total")))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, JSONObject::new));
    }

    public IssueTypeTotalCountVo getIssueTypeProjectIssuesTotalCount(List<String> projectIdList) {
        JSONObject result = pmsProjectIssuesService.getProjectIssuesCount(projectIdList, "assignedme", "opened");
        JSONObject dataObj = result.getJSONObject(DATA_KEY);
        int requirementCount = dataObj.keySet().stream().map(key -> new AbstractMap.SimpleEntry<>(key, dataObj.getJSONObject(key).getInteger("requirement"))).mapToInt(Map.Entry::getValue).sum();
        int taskCount = dataObj.keySet().stream().map(key -> new AbstractMap.SimpleEntry<>(key, dataObj.getJSONObject(key).getInteger("task"))).mapToInt(Map.Entry::getValue).sum();
        int bugCount = dataObj.keySet().stream().map(key -> new AbstractMap.SimpleEntry<>(key, dataObj.getJSONObject(key).getInteger("bug"))).mapToInt(Map.Entry::getValue).sum();
        int assignedmeCount = dataObj.keySet().stream().map(key -> new AbstractMap.SimpleEntry<>(key, dataObj.getJSONObject(key).getInteger("assignedme"))).mapToInt(Map.Entry::getValue).sum();
        int authoredmeCount = dataObj.keySet().stream().map(key -> new AbstractMap.SimpleEntry<>(key, dataObj.getJSONObject(key).getInteger("authoredme"))).mapToInt(Map.Entry::getValue).sum();
        IssueTypeTotalCountVo issueTypeTotalCountVo = new IssueTypeTotalCountVo();
        issueTypeTotalCountVo.setProjectRequirementCount(requirementCount);
        issueTypeTotalCountVo.setProjectTaskCount(taskCount);
        issueTypeTotalCountVo.setProjectBugCount(bugCount);
        issueTypeTotalCountVo.setAssignedmeCount(assignedmeCount);
        issueTypeTotalCountVo.setAuthoredmeCount(authoredmeCount);
        return issueTypeTotalCountVo;
    }

    @Override
    public GenericsTableDataInfo<SimpleSysUser> getUserList(String enterpriseIdentifier, String search) {
        return pmsEnterpriseService.getUserListByPerms(enterpriseIdentifier, search, UserConstants.PMS_PERMS_PROJECT_EDIT);
    }

}
