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

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.constant.SecurityConstants;
import com.microservices.common.core.enums.SystemRole;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.common.httpClient.util.GitLinkRequestHelper;
import com.microservices.pms.common.service.IPmsCommonAsyncService;
import com.microservices.pms.common.service.IPmsCommonService;
import com.microservices.pms.enterprise.domain.PmsEnterprise;
import com.microservices.pms.enterprise.domain.vo.GitLinkUserSimpleInfoVo;
import com.microservices.pms.enterprise.domain.vo.GitLinkUserToEnterpriseInputVo;
import com.microservices.pms.enterprise.domain.vo.GitlinkOrganizationTeamVo;
import com.microservices.pms.enterprise.service.IPmsEnterpriseService;
import com.microservices.pms.enums.ProjectIssuePriority;
import com.microservices.pms.enums.ProjectIssueStatus;
import com.microservices.pms.product.domain.PmsProductRequirement;
import com.microservices.pms.product.service.IPmsProductRequirementService;
import com.microservices.pms.project.domain.*;
import com.microservices.pms.project.domain.vo.PmsProjectIssuesInputVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestReportInputVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestsheetCaseInputVo;
import com.microservices.pms.project.mapper.PmsProjectMapper;
import com.microservices.pms.project.mapper.PmsProjectSprintMapper;
import com.microservices.pms.project.service.*;
import com.microservices.pms.utils.CustomExecutorFactory;
import com.microservices.pms.utils.PmsConstants;
import com.microservices.pms.utils.PmsGitLinkRequestUrl;
import com.microservices.system.api.RemoteUserService;
import com.microservices.system.api.domain.SysRole;
import com.microservices.system.api.domain.SysUser;
import com.microservices.system.api.utils.FeignUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

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

/**
 * 项目管理企业Service业务层处理
 *
 * @author otto
 * @date 2023-10-10
 */
@Service
public class PmsCommonAsyncServiceImpl implements IPmsCommonAsyncService {
    private static final Logger logger = LoggerFactory.getLogger(PmsCommonAsyncServiceImpl.class);
    @Autowired
    private GitLinkRequestHelper gitLinkRequestHelper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private IPmsCommonService pmsCommonService;
    @Lazy
    @Autowired
    private IPmsEnterpriseService pmsEnterpriseService;
    @Resource
    private PmsProjectMapper pmsProjectMapper;

    @Autowired
    private IPmsProjectMembersService pmsProjectMembersService;
    @Resource
    private PmsProjectSprintMapper pmsProjectSprintMapper;
    @Autowired
    private PmsProjectIssuesService pmsProjectIssuesService;
    @Autowired
    @Lazy
    private IPmsProjectTestcaseService pmsProjectTestcaseService;
    @Autowired
    private IPmsProjectTestsheetService pmsProjectTestsheetService;
    @Autowired
    @Lazy
    private IPmsProjectTestsheetCasesService pmsProjectTestsheetCasesService;
    @Autowired
    private IPmsProjectTestReportService pmsProjectTestReportService;
    @Autowired
    @Lazy
    private IPmsProductRequirementService pmsProductRequirementService;


    private static final String INITIALIZE_NAME = "【示例%s】研发示例%s";

    @Override
    public void syncChangeUserGitlinkRole(Long userId, Long deptId, Long oldRoleId, Long newRoleId) {
        // 使用线程池异步处理
        CustomExecutorFactory.threadPoolExecutor.execute(() -> {
            PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseByDeptId(deptId);
            if (pmsEnterprise == null) {
                throw new ServiceException("该企业不存在(企业组织Id[%d])", deptId);
            }
            SysUser sysUser = FeignUtils.getReturnData(remoteUserService.getSysUserByUserId(userId, SecurityConstants.INNER));
            if (sysUser == null || sysUser.getGitlinkUserId() == null) {
                return;
            }
            SysRole oldRole = null;
            if (oldRoleId != null) {
                oldRole = FeignUtils.getReturnData(
                        remoteUserService.getRoleInfoByInner(oldRoleId, SecurityConstants.INNER)
                );
            }
            SysRole newRole = null;
            if (newRoleId != null) {
                newRole = FeignUtils.getReturnData(
                        remoteUserService.getRoleInfoByInner(newRoleId, SecurityConstants.INNER)
                );
            }

            if (oldRole == null && newRole != null) {
                String gitlinkOrgTeamKey = PmsConstants.getGitlinkOrgTeamKeyByRoleKey(newRole.getRoleKey());
                addUserToGitlinkOrgTeam(pmsEnterprise, gitlinkOrgTeamKey, sysUser.getUserName());
                return;
            }
            if (oldRole != null && newRole == null) {
                String gitlinkOrgTeamKey = PmsConstants.getGitlinkOrgTeamKeyByRoleKey(oldRole.getRoleKey());
                Long teamId = getGitlinkOrgTeamIdByTeamKey(pmsEnterprise, gitlinkOrgTeamKey);
                if (checkUserInGitlinkTeam(sysUser, pmsEnterprise, teamId)) {
                    gitLinkRequestHelper.doDelete(PmsGitLinkRequestUrl.DELETE_USER_FROM_ORGANIZATION_TEAM(
                            pmsEnterprise.getEnterpriseIdentifier(),
                            teamId,
                            sysUser.getUserName()
                    ));
                }

                return;
            }
            if (oldRole != null && !Objects.equals(oldRoleId, newRoleId)) {
                // 移除旧组织
                String gitlinkOrgTeamKey = PmsConstants.getGitlinkOrgTeamKeyByRoleKey(oldRole.getRoleKey());
                // 移出前先检查用户是否存在于该团队中
                Long teamId = getGitlinkOrgTeamIdByTeamKey(pmsEnterprise, gitlinkOrgTeamKey);
                if (checkUserInGitlinkTeam(sysUser, pmsEnterprise, teamId)) {
                    gitLinkRequestHelper.doDelete(PmsGitLinkRequestUrl.DELETE_USER_FROM_ORGANIZATION_TEAM(
                            pmsEnterprise.getEnterpriseIdentifier(),
                            teamId,
                            sysUser.getUserName()
                    ));
                }

                // 加入新组织
                gitlinkOrgTeamKey = PmsConstants.getGitlinkOrgTeamKeyByRoleKey(newRole.getRoleKey());
                addUserToGitlinkOrgTeam(pmsEnterprise, gitlinkOrgTeamKey, sysUser.getUserName());
            }
        });
    }

    @Override
    public void asyncClearProductRequirementTag(Long tagId) {
        CustomExecutorFactory.threadPoolExecutor.execute(() -> {
            String tagIdStr = String.valueOf(tagId);
            PmsProductRequirement pmsProductRequirementSearch = new PmsProductRequirement();
            pmsProductRequirementSearch.setTagId(tagIdStr);
            List<PmsProductRequirement> pmsProductRequirementList = pmsProductRequirementService.selectPmsProductRequirementList(pmsProductRequirementSearch);
            for (PmsProductRequirement pmsProductRequirement : pmsProductRequirementList) {
                String tagIdsStr = pmsProductRequirement.getTagIds();
                List<String> tagIds = new ArrayList<>(Arrays.asList(tagIdsStr.split(",")));
                tagIds.remove(tagIdStr);
                pmsProductRequirement.setTags(String.join(",", tagIds));
                pmsProductRequirementService.updatePmsProductRequirement(pmsProductRequirement);
            }
        });
    }

    @Override
    public void asyncClearTestCaseTag(Long tagId) {
        CustomExecutorFactory.threadPoolExecutor.execute(() -> {
            String tagIdStr = String.valueOf(tagId);
            PmsProjectTestcase pmsProjectTestcaseSearch = new PmsProjectTestcase();
            pmsProjectTestcaseSearch.setTagId(tagIdStr);
            List<PmsProjectTestcase> pmsProjectTestcaseList = pmsProjectTestcaseService.selectPmsProjectTestcaseList(pmsProjectTestcaseSearch);
            for (PmsProjectTestcase pmsProjectTestcase : pmsProjectTestcaseList) {
                String tagIdsStr = pmsProjectTestcase.getTagIds();
                List<String> tagIds = new ArrayList<>(Arrays.asList(tagIdsStr.split(",")));
                tagIds.remove(tagIdStr);
                pmsProjectTestcase.setTags(String.join(",", tagIds));
                pmsProjectTestcaseService.updatePmsProjectTestcase(pmsProjectTestcase);
            }
        });
    }

    private void addUserToGitlinkOrgTeam(PmsEnterprise pmsEnterprise, String gitlinkOrgTeamKey, String username) {
        JSONObject addUserToTeam = new JSONObject();
        addUserToTeam.put("username", username);
        Long teamId = getGitlinkOrgTeamIdByTeamKey(pmsEnterprise, gitlinkOrgTeamKey);
        gitLinkRequestHelper.doPost(
                PmsGitLinkRequestUrl.ADD_USER_TO_ORGANIZATION_TEAM(pmsEnterprise.getEnterpriseIdentifier(), teamId), addUserToTeam);
    }

    private Boolean checkUserInGitlinkTeam(SysUser sysUser, PmsEnterprise pmsEnterprise, Long teamId) {
        List<GitLinkUserSimpleInfoVo> gitlinkUserList = pmsCommonService.getAllGitlinkUsersByOrgTeam(pmsEnterprise.getEnterpriseIdentifier(), teamId);
        return gitlinkUserList.stream().anyMatch(x -> x.getUserId().equals(sysUser.getGitlinkUserId()));
    }

    private Long getGitlinkOrgTeamIdByTeamKey(PmsEnterprise pmsEnterprise, String teamKey) {
        // 获取组织下所有团队
        List<GitlinkOrganizationTeamVo> gitlinkOrganizationTeamVoList = pmsCommonService.getAllGitlinkOrgTeams(pmsEnterprise.getGitlinkOrgId());
        Long teamId = null;
        switch (teamKey) {
            case PmsConstants.GITLINK_ORGANIZATION_TEAM_OWNER: {
                teamId = gitlinkOrganizationTeamVoList
                        .stream()
                        .filter(x -> PmsConstants.GITLINK_ORGANIZATION_TEAM_OWNER.equals(x.getAuthorize()))
                        .map(GitlinkOrganizationTeamVo::getId)
                        .findFirst()
                        .orElseThrow(() -> new ServiceException("Gitlink组织下Owner团队不存在请及时处理"));
                break;
            }
            case PmsConstants.GITLINK_ORGANIZATION_TEAM_ADMIN: {
                teamId = gitlinkOrganizationTeamVoList
                        .stream()
                        .filter(x -> PmsConstants.GITLINK_ORGANIZATION_TEAM_ADMIN.equals(x.getName()))
                        .map(GitlinkOrganizationTeamVo::getId)
                        .findFirst()
                        .orElse(null);
                if (teamId == null) {
                    teamId = pmsEnterpriseService.initGitlinkOrgAdminTeam(pmsEnterprise.getEnterpriseIdentifier());
                }
                break;
            }
            case PmsConstants.GITLINK_ORGANIZATION_TEAM_WRITE: {
                teamId = gitlinkOrganizationTeamVoList
                        .stream()
                        .filter(x -> PmsConstants.GITLINK_ORGANIZATION_TEAM_WRITE.equals(x.getName()))
                        .map(GitlinkOrganizationTeamVo::getId)
                        .findFirst()
                        .orElse(null);
                if (teamId == null) {
                    teamId = pmsEnterpriseService.initGitlinkOrgWriteTeam(pmsEnterprise.getEnterpriseIdentifier());
                }
                break;
            }
        }
        if (teamId == null) {
            throw new ServiceException("Gitlink组织下团队获取异常，请联系管理员");
        }
        return teamId;
    }

    /**
     * 同步Forge组织中成员到工作台中（未同步的用户需在Forge中移除）
     *
     * @param enterpriseId                       企业id
     * @param gitLinkUserToEnterpriseInputVoList 需要同步到企业的用户列表
     */
    @Override
    public void syncGitlinkOrganizationUserToPmsEnterprise(Long enterpriseId, List<GitLinkUserToEnterpriseInputVo> gitLinkUserToEnterpriseInputVoList) {
        CustomExecutorFactory.threadPoolExecutor.execute(() -> {
            PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseById(enterpriseId);
            try {
                pmsEnterpriseService.increaseOpenEnterpriseProcessCount(pmsEnterprise.getGitlinkOrgId());
                // 获取组织下所有团队
                List<GitlinkOrganizationTeamVo> gitlinkOrganizationTeamVoList = pmsCommonService.getAllGitlinkOrgTeams(pmsEnterprise.getGitlinkOrgId());
                // 根据权限标识判断是否为Owner团队
                Long ownerTeamId = getGitlinkOrgTeamIdByTeamKey(pmsEnterprise, PmsConstants.GITLINK_ORGANIZATION_TEAM_OWNER);
                Long pmsProjectAdminTeamId = getGitlinkOrgTeamIdByTeamKey(pmsEnterprise, PmsConstants.GITLINK_ORGANIZATION_TEAM_ADMIN);
                Long pmsUserTeamId = getGitlinkOrgTeamIdByTeamKey(pmsEnterprise, PmsConstants.GITLINK_ORGANIZATION_TEAM_WRITE);

                // 获取原Owner团队中成员列表
                List<GitLinkUserSimpleInfoVo> oldOwnerUserList = pmsCommonService.getAllGitlinkUsersByOrgTeam(pmsEnterprise.getEnterpriseIdentifier(), ownerTeamId);
                List<Long> newOwnerUserIdList = gitLinkUserToEnterpriseInputVoList
                        .stream()
                        .filter(x -> x.getRoleKey().equals(SystemRole.PMS_ORG_ADMIN.getRoleKey()))
                        .map(GitLinkUserToEnterpriseInputVo::getUserId)
                        .collect(Collectors.toList());

                // 删除原Owner团队中不在新Owner团队中的成员
                List<GitLinkUserSimpleInfoVo> removeUserIdList = oldOwnerUserList
                        .stream()
                        .filter(x -> !newOwnerUserIdList.contains(x.getUserId()))
                        .collect(Collectors.toList());
                for (GitLinkUserSimpleInfoVo gitLinkUserSimpleInfoVo : removeUserIdList) {
                    gitLinkRequestHelper.doDelete(
                            PmsGitLinkRequestUrl.DELETE_USER_FROM_ORGANIZATION_TEAM(pmsEnterprise.getEnterpriseIdentifier(), ownerTeamId, gitLinkUserSimpleInfoVo.getUsername()));
                }


                JSONObject addUserToTeam = new JSONObject();
                // 将用户添加到微服务部门中并调整到对应团队中
                for (GitLinkUserToEnterpriseInputVo gitLinkUserToEnterpriseInputVo : gitLinkUserToEnterpriseInputVoList) {
                    SysUser sysUser = pmsCommonService.getSysUserByGitlinkId(gitLinkUserToEnterpriseInputVo.getUserId());
                    // 将用户添加到该部门下的身份
                    remoteUserService.addUserIdentify(pmsEnterprise.getDeptId(), sysUser.getUserId(), gitLinkUserToEnterpriseInputVo.getRoleKey(), SecurityConstants.INNER);
                    // 将用户添加到对应团队中
                    addUserToTeam.put("username", sysUser.getUserName());
                    SystemRole systemRole = SystemRole.getSystemRole(gitLinkUserToEnterpriseInputVo.getRoleKey());
                    switch (systemRole) {
                        case PMS_ORG_ADMIN: {
                            gitLinkRequestHelper.doPost(
                                    PmsGitLinkRequestUrl.ADD_USER_TO_ORGANIZATION_TEAM(pmsEnterprise.getEnterpriseIdentifier(), ownerTeamId), addUserToTeam);
                            break;
                        }
                        case PMS_PROJECT_MANAGER: {
                            gitLinkRequestHelper.doPost(
                                    PmsGitLinkRequestUrl.ADD_USER_TO_ORGANIZATION_TEAM(pmsEnterprise.getEnterpriseIdentifier(), pmsProjectAdminTeamId), addUserToTeam);
                            break;
                        }
                        default: {
                            gitLinkRequestHelper.doPost(
                                    PmsGitLinkRequestUrl.ADD_USER_TO_ORGANIZATION_TEAM(pmsEnterprise.getEnterpriseIdentifier(), pmsUserTeamId), addUserToTeam);
                            break;
                        }
                    }
                }

                //删除除Owner团队、项目管理员团队和普通成员团队外的所有团队
                gitlinkOrganizationTeamVoList = gitlinkOrganizationTeamVoList
                        .stream()
                        .filter(x -> !ownerTeamId.equals(x.getId()) && !pmsProjectAdminTeamId.equals(x.getId()) && !pmsUserTeamId.equals(x.getId()))
                        .collect(Collectors.toList());
                for (GitlinkOrganizationTeamVo gitlinkOrganizationTeamVo : gitlinkOrganizationTeamVoList) {
                    gitLinkRequestHelper.doDelete(PmsGitLinkRequestUrl.DELETE_ORGANIZATION_TEAM(pmsEnterprise.getGitlinkOrgId(), gitlinkOrganizationTeamVo.getId()));
                }
            } catch (Exception e) {
                logger.error("同步Gitlink组织用户到企业失败：{0}", e);
            } finally {
                pmsEnterpriseService.decreaseOpenEnterpriseProcessCount(pmsEnterprise.getGitlinkOrgId());
            }
        });
    }

    @Override
    public void syncInitPmsEnterpriseData(Long id, Long creatorGitLinkUserId) {
        CustomExecutorFactory.threadPoolExecutor.execute(() -> {
            PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseById(id);
            try {
                pmsEnterpriseService.increaseOpenEnterpriseProcessCount(pmsEnterprise.getGitlinkOrgId());
                Long pmsProjectId = initializePmsProject(id, creatorGitLinkUserId);
                initializePmsProjectIssues(pmsProjectId, creatorGitLinkUserId);
                Long pmsProjectSprintId = initializePmsProjectSprint(pmsProjectId, creatorGitLinkUserId);
                Long pmsTestcaseId = initializePmsTestcase(pmsProjectId, creatorGitLinkUserId);
                Long pmsTestSheetId = initializePmsTestSheet(pmsProjectId, creatorGitLinkUserId, pmsProjectSprintId, pmsTestcaseId);
                initializePmsTestReport(pmsProjectId, pmsTestSheetId);
            } catch (Exception e) {
                logger.error("初始化企业数据失败：{}", e.getMessage());
            } finally {
                pmsEnterpriseService.decreaseOpenEnterpriseProcessCount(pmsEnterprise.getGitlinkOrgId());
            }
        });
    }

    private void initializePmsTestReport(Long pmsProjectId, Long pmsTestSheetId) {
        PmsProjectTestReportInputVo pmsProjectTestReportInputVo = new PmsProjectTestReportInputVo();
        String name = "测试报告";
        pmsProjectTestReportInputVo.setName(String.format(INITIALIZE_NAME, name, name));
        pmsProjectTestReportInputVo.setPmsProjectId(pmsProjectId);
        pmsProjectTestReportInputVo.setTestSheetIds(String.valueOf(pmsTestSheetId));
        pmsProjectTestReportService.insertPmsProjectTestReportByInputVo(pmsProjectTestReportInputVo);
    }

    private Long initializePmsTestSheet(Long pmsProjectId, Long creatorGitLinkUserId, Long pmsProjectSprintId, Long pmsTestcaseId) {
        PmsProjectTestsheet pmsProjectTestsheet = new PmsProjectTestsheet();
        pmsProjectTestsheet.setPmsProjectId(pmsProjectId);
        String name = "测试单";
        pmsProjectTestsheet.setName(String.format(String.format(INITIALIZE_NAME, name, name)));
        pmsProjectTestsheet.setAssigneeId(creatorGitLinkUserId);
        pmsProjectTestsheet.setProjectSprintId(pmsProjectSprintId);
        pmsProjectTestsheetService.insertPmsProjectTestsheet(pmsProjectTestsheet);

        PmsProjectTestsheetCaseInputVo pmsProjectTestsheetCaseInputVo = new PmsProjectTestsheetCaseInputVo();
        pmsProjectTestsheetCaseInputVo.setProjectTestsheetId(pmsProjectTestsheet.getId());
        List<Long> testCaseIdList = new ArrayList<>();
        testCaseIdList.add(pmsTestcaseId);
        pmsProjectTestsheetCaseInputVo.setIdList(testCaseIdList);
        pmsProjectTestsheetCasesService.insertPmsProjectTestsheetCases(pmsProjectTestsheetCaseInputVo);
        return pmsProjectTestsheet.getId();
    }

    private Long initializePmsTestcase(Long pmsProjectId, Long creatorGitLinkUserId) {
        PmsProjectTestcase pmsProjectTestcase = new PmsProjectTestcase();
        String name = "测试用例";
        pmsProjectTestcase.setTitle(String.format(INITIALIZE_NAME, name, name));
        pmsProjectTestcase.setAssigneeGitlinkId(creatorGitLinkUserId);
        pmsProjectTestcase.setPmsProjectId(pmsProjectId);
        List<PmsProjectTestcaseStep> pmsProjectTestcaseSteps = new ArrayList<>();
        PmsProjectTestcaseStep pmsProjectTestcaseStep = new PmsProjectTestcaseStep();
        pmsProjectTestcaseStep.setIndex(0L);
        pmsProjectTestcaseSteps.add(pmsProjectTestcaseStep);
        pmsProjectTestcase.setTestcaseStepList(pmsProjectTestcaseSteps);
        pmsProjectTestcaseService.insertPmsProjectTestcase(pmsProjectTestcase);
        return pmsProjectTestcase.getId();
    }

    private void initializePmsProjectIssues(Long pmsProjectId, Long creatorGitLinkUserId) {
        PmsProjectIssuesInputVo pmsProjectIssuesInputVo = new PmsProjectIssuesInputVo();
        pmsProjectIssuesInputVo.setPmProjectId(pmsProjectId);
        //状态-待评审
        pmsProjectIssuesInputVo.setStatusId(ProjectIssueStatus.NEW.getId());
        //优先级-正常
        pmsProjectIssuesInputVo.setPriorityId(ProjectIssuePriority.NORMAL.getId());
        List<Long> assignerIds = new ArrayList<>();
        assignerIds.add(creatorGitLinkUserId);
        pmsProjectIssuesInputVo.setAssignerIds(assignerIds);
        pmsProjectIssuesInputVo.setAttachmentIds(new ArrayList<>());
        pmsProjectIssuesInputVo.setIssueTagIds(new ArrayList<>());

        String name = "需求";
        pmsProjectIssuesInputVo.setSubject(String.format(INITIALIZE_NAME, name, name));
        pmsProjectIssuesInputVo.setPmIssueType(Integer.valueOf(PmsConstants.PROJECT_ISSUE_TYPE_REQUIREMENT));
        pmsProjectIssuesService.insertPmsProjectIssues(pmsProjectIssuesInputVo);

        name = "任务";
        pmsProjectIssuesInputVo.setSubject(String.format(INITIALIZE_NAME, name, name));
        pmsProjectIssuesInputVo.setPmIssueType(Integer.valueOf(PmsConstants.PROJECT_ISSUE_TYPE_TASK));
        pmsProjectIssuesService.insertPmsProjectIssues(pmsProjectIssuesInputVo);

        name = "缺陷";
        pmsProjectIssuesInputVo.setSubject(String.format(INITIALIZE_NAME, name, name));
        pmsProjectIssuesInputVo.setPmIssueType(Integer.valueOf(PmsConstants.PROJECT_ISSUE_TYPE_BUG));
        pmsProjectIssuesService.insertPmsProjectIssues(pmsProjectIssuesInputVo);
    }

    private Long initializePmsProjectSprint(Long pmsProjectId, Long creatorGitLinkUserId) {
        PmsProjectSprint pmsProjectSprint = new PmsProjectSprint();
        String name = "迭代";
        pmsProjectSprint.setSprintName(String.format(INITIALIZE_NAME, name, name));
        pmsProjectSprint.setPmsProjectId(pmsProjectId);
        pmsProjectSprint.setSprintAssigneeId(creatorGitLinkUserId);
        pmsProjectSprint.setStatus(PmsConstants.SPRINT_TO_BE_STARTED_STATUS);
        pmsProjectSprint.setStartTime(new Date());
        pmsProjectSprint.setEndTime(DateUtils.addDays(new Date(), 30));
        pmsProjectSprintMapper.insertPmsProjectSprint(pmsProjectSprint);
        return pmsProjectSprint.getId();
    }

    private Long initializePmsProject(Long id, Long creatorGitLinkUserId) {
        PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseById(id);
        PmsProject pmsProject = new PmsProject();
        pmsProject.setPmsEnterpriseId(id);
        String name = "项目";
        pmsProject.setProjectName(String.format(INITIALIZE_NAME, name, name));
        pmsProject.setProjectAssigneeId(creatorGitLinkUserId);
        pmsProject.setDeptId(pmsEnterprise.getDeptId());
        pmsProjectMapper.insertPmsProject(pmsProject);
        PmsProjectMembers pmsProjectMembers = new PmsProjectMembers();
        pmsProjectMembers.setPmsProjectMemberId(creatorGitLinkUserId);
        pmsProjectMembers.setPmsProjectId(pmsProject.getId());
        pmsProjectMembersService.insertPmsProjectMembers(pmsProjectMembers);
        return pmsProject.getId();
    }
}
