ProjectReviewServiceImpl.java 14.7 KB
package com.zhongzhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongzhi.common.constant.ProjectReview;
import com.zhongzhi.common.constant.ProjectType;
import com.zhongzhi.common.constant.SMSTemplate;
import com.zhongzhi.common.exception.HttpException;
import com.zhongzhi.common.utils.AliyunSmsUtil;
import com.zhongzhi.common.utils.DateFormatUtil;
import com.zhongzhi.dao.ProjectDAO;
import com.zhongzhi.dto.review.CancelReviewDTO;
import com.zhongzhi.dto.review.ProjectReviewDTO;
import com.zhongzhi.dto.review.SetReviewDTO;
import com.zhongzhi.model.*;
import com.zhongzhi.dao.ProjectReviewDAO;
import com.zhongzhi.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongzhi.vo.judge.ProjectReviewVO;
import com.zhongzhi.vo.review.SendNoticeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 项目评审 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-05-18
 */
@Service
public class ProjectReviewServiceImpl extends ServiceImpl<ProjectReviewDAO, ProjectReviewDO> implements ProjectReviewService {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectJudgeService projectJudgeService;

    @Autowired
    private MatchDictService matchDictService;

    @Autowired
    private ProjectReviewPeriodService projectReviewPeriodService;

    @Autowired
    private AliyunSmsUtil aliyunSmsUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setReview(SetReviewDTO setReviewDTO) {
        if (setReviewDTO.getProjectId().size() == 0) {
            throw new HttpException(10047);
        }

        if (setReviewDTO.getJudgeId().size() == 0) {
            throw new HttpException(10048);
        }

//        Date startTime = null;
//        Date endTime = null;

        for (Long judgeId : setReviewDTO.getJudgeId()) {
            ProjectJudgeDO projectJudgeDO = projectJudgeService.getById(judgeId);
            if (projectJudgeDO != null) {
                for (Long projectId : setReviewDTO.getProjectId()) {
                    ProjectDO projectDO = projectService.getById(projectId);
                    MatchDictDO matchDictDO = matchDictService.getById(projectDO.getMatchId());
                    if ("种子赛道".equals(matchDictDO.getMatchType())) {
                        if (projectDO != null) {
                            ProjectReviewPeriodDO projectReviewPeriodDO = projectReviewPeriodService.getOne(new QueryWrapper<ProjectReviewPeriodDO>()
                                    .lambda()
                                    .eq(ProjectReviewPeriodDO::getMatchId, projectDO.getMatchId())
                                    .eq(ProjectReviewPeriodDO::getProjectGroup, projectDO.getProjectProgress())
                                    .eq(ProjectReviewPeriodDO::getProjectSchedule, projectDO.getProjectSchedule()));
//                        startTime = projectReviewPeriodDO.getStartTime();
//                        endTime = projectReviewPeriodDO.getEndTime();
                            if (projectReviewPeriodDO == null) {
                                throw new HttpException(10076);
                            }

                            if (projectDO.getProjectAssigned().equals(ProjectReview.UNASSIGNED)) {
                                ProjectDO project = new ProjectDO();
                                project.setId(projectId);
                                project.setProjectAssigned(ProjectReview.ASSIGNED);
                                if (projectDO.getProjectReview().equals(ProjectReview.SUBMITTED)) {
                                    project.setProjectReview(ProjectReview.TO_REVIEWED);
                                }
                                projectService.updateById(project);
                            }

                            ProjectReviewDO projectReview = this.baseMapper.selectOne(new QueryWrapper<ProjectReviewDO>()
                                    .lambda()
                                    .eq(ProjectReviewDO::getProjectId, projectId)
                                    .eq(ProjectReviewDO::getJudgeId, judgeId)
                                    .eq(ProjectReviewDO::getProjectSchedule, projectDO.getProjectSchedule()));
                            if (projectReview != null) {
                                throw new HttpException(10053);
                            }

                            ProjectReviewDO projectReviewDO = new ProjectReviewDO();
                            projectReviewDO.setProjectId(projectId);
                            projectReviewDO.setJudgeId(judgeId);
                            projectReviewDO.setProjectSchedule(projectDO.getProjectSchedule());
                            projectReviewDO.setReviewStatus(ProjectReview.ASSIGNED);
                            this.baseMapper.insert(projectReviewDO);
                        }
                    }
                    else {
                        if (projectDO != null) {
                            ProjectReviewPeriodDO projectReviewPeriodDO = projectReviewPeriodService.getOne(new QueryWrapper<ProjectReviewPeriodDO>()
                                    .lambda()
                                    .eq(ProjectReviewPeriodDO::getMatchId, projectDO.getMatchId())
                                    .eq(ProjectReviewPeriodDO::getProjectGroup, projectDO.getProjectGroup())
                                    .eq(ProjectReviewPeriodDO::getProjectSchedule, projectDO.getProjectSchedule()));
//                        startTime = projectReviewPeriodDO.getStartTime();
//                        endTime = projectReviewPeriodDO.getEndTime();
                            if (projectReviewPeriodDO == null) {
                                throw new HttpException(10076);
                            }

                            if (projectDO.getProjectAssigned().equals(ProjectReview.UNASSIGNED)) {
                                ProjectDO project = new ProjectDO();
                                project.setId(projectId);
                                project.setProjectAssigned(ProjectReview.ASSIGNED);
                                if (projectDO.getProjectReview().equals(ProjectReview.SUBMITTED)) {
                                    project.setProjectReview(ProjectReview.TO_REVIEWED);
                                }
                                projectService.updateById(project);
                            }

                            ProjectReviewDO projectReview = this.baseMapper.selectOne(new QueryWrapper<ProjectReviewDO>()
                                    .lambda()
                                    .eq(ProjectReviewDO::getProjectId, projectId)
                                    .eq(ProjectReviewDO::getJudgeId, judgeId)
                                    .eq(ProjectReviewDO::getProjectSchedule, projectDO.getProjectSchedule()));
                            if (projectReview != null) {
                                throw new HttpException(10053);
                            }

                            ProjectReviewDO projectReviewDO = new ProjectReviewDO();
                            projectReviewDO.setProjectId(projectId);
                            projectReviewDO.setJudgeId(judgeId);
                            projectReviewDO.setProjectSchedule(projectDO.getProjectSchedule());
                            projectReviewDO.setReviewStatus(ProjectReview.ASSIGNED);
                            this.baseMapper.insert(projectReviewDO);
                        }
                    }
                }


                if (setReviewDTO.getSmsMessage() == 1) {
                    MatchDictDO matchDictDO = matchDictService.getById(setReviewDTO.getMatchId());
                    int count = this.baseMapper.selectCount(new QueryWrapper<ProjectReviewDO>()
                            .lambda()
                            .eq(ProjectReviewDO::getReviewStatus, ProjectReview.ASSIGNED)
                            .eq(ProjectReviewDO::getJudgeId, judgeId));
                    String msg = "";
                    if (matchDictDO != null) {
//                        msg = matchDictDO.getMatchName() +","+count+","+ DateFormatUtil.format(startTime,DateFormatUtil.FMT_sdf14_L)+"~"+DateFormatUtil.format(endTime,DateFormatUtil.FMT_sdf14_L);
                        msg = matchDictDO.getMatchName() + "," + count;
                    }

                    aliyunSmsUtil.sendTemplateMsg(msg, projectJudgeDO.getTelephone(), SMSTemplate.REVIEW_INVITATION_OLD);
                }
            }
        }
    }

    @Override
    public void sendNotice(SendNoticeVO sendNoticeVO) {
        MatchDictDO matchDictDO = matchDictService.getById(sendNoticeVO.getMatchId());
        if (matchDictDO != null) {
            for (Long id : sendNoticeVO.getId()) {
                ProjectJudgeDO projectJudgeDO = projectJudgeService.getById(id);
                if (projectJudgeDO != null) {
                    int count = this.baseMapper.selectCount(new QueryWrapper<ProjectReviewDO>()
                            .lambda()
                            .eq(ProjectReviewDO::getReviewStatus, ProjectReview.ASSIGNED)
                            .eq(ProjectReviewDO::getJudgeId, id));
                    List<ProjectReviewPeriodDO> list = projectReviewPeriodService.list(new QueryWrapper<ProjectReviewPeriodDO>()
                            .lambda()
                            .eq(ProjectReviewPeriodDO::getMatchId, matchDictDO.getId()));

                    StringBuffer stringBuffer = new StringBuffer();
                    for (int i = 0; i < list.size(); i++) {
                        stringBuffer.append(list.get(i).getProjectGroup() + ":" + DateFormatUtil.format(list.get(i).getStartTime(), DateFormatUtil.FMT_sdf14_L) + "~" + DateFormatUtil.format(list.get(i).getEndTime(), DateFormatUtil.FMT_sdf14_L));
                        if (i != list.size()) {
                            stringBuffer.append(";");
                        }
                    }

                    String msg = matchDictDO.getMatchName() + "," + count + "," + stringBuffer.toString();
                    aliyunSmsUtil.sendTemplateMsgP(msg, projectJudgeDO.getTelephone(), SMSTemplate.REVIEW_INVITATION_NEW);
                }
            }
        }
    }

    @Override
    public Integer selectCount(Long judgeId, Long matchId) {
        return this.baseMapper.selectCountByMatchId(judgeId, matchId);
    }

    @Override
    public Integer selectNotReviewCount(Long judgeId, Long matchId) {
        return this.baseMapper.selectNotReviewCount(judgeId, matchId);
    }

    @Override
    public List<ProjectReviewVO> getList(String projectSchedule, Long projectId) {
        return this.baseMapper.getList(projectSchedule, projectId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cancelReview(CancelReviewDTO cancelReviewDTO) {
        if (cancelReviewDTO.getProjectId() == null) {
            throw new HttpException(10047);
        }

        if (cancelReviewDTO.getJudgeId().size() == 0) {
            throw new HttpException(10048);
        }

        for (Long judgeId : cancelReviewDTO.getJudgeId()) {
            ProjectJudgeDO projectJudgeDO = projectJudgeService.getById(judgeId);
            if (projectJudgeDO == null) {
                throw new HttpException(10070);
            }

            ProjectDO projectDO = projectService.getById(cancelReviewDTO.getProjectId());
            if (projectDO != null) {
                ProjectReviewPeriodDO projectReviewPeriodDO = projectReviewPeriodService.getOne(new QueryWrapper<ProjectReviewPeriodDO>()
                        .lambda()
                        .eq(ProjectReviewPeriodDO::getMatchId, projectDO.getMatchId())
                        .eq(ProjectReviewPeriodDO::getProjectGroup, projectDO.getProjectGroup())
                        .eq(ProjectReviewPeriodDO::getProjectSchedule, projectDO.getProjectSchedule()));
                if (projectReviewPeriodDO.getStartTime().before(new Date())) {
                    throw new HttpException(10075);
                }

                if (projectDO != null) {
                    Integer count = this.baseMapper.selectCount(new QueryWrapper<ProjectReviewDO>()
                            .lambda()
                            .eq(ProjectReviewDO::getProjectId, projectDO.getId())
                            .eq(ProjectReviewDO::getProjectSchedule, projectDO.getProjectSchedule()));
                    /*
                        只有最后一个评委是撤销修改项目分配状态
                     */
                    if (count == 1) {
                        ProjectDO project = new ProjectDO();
                        project.setId(projectDO.getId());
                        project.setProjectAssigned(ProjectReview.UNASSIGNED);
                        projectService.updateById(project);
                    }

                    ProjectReviewDO projectReview = this.baseMapper.selectOne(new QueryWrapper<ProjectReviewDO>()
                            .lambda()
                            .eq(ProjectReviewDO::getProjectId, projectDO.getId())
                            .eq(ProjectReviewDO::getJudgeId, judgeId)
                            .eq(ProjectReviewDO::getProjectSchedule, projectDO.getProjectSchedule()));
                    if (projectReview == null) {
                        throw new HttpException(10071);
                    }

                    if (projectReview.getReviewStatus().equals(ProjectReview.SUBMITTED)) {
                        throw new HttpException(10059);
                    }

                    this.baseMapper.deleteById(projectReview.getId());
                }

                aliyunSmsUtil.sendTemplateMsg(projectDO.getProjectName(), projectJudgeDO.getTelephone(), SMSTemplate.CANCEL_REVIEW);
            }
        }
    }

    @Override
    public List<ProjectJudgeDO> getProjectReview(ProjectReviewDTO projectReviewDTO) {
        List<ProjectJudgeDO> data = new ArrayList<>();

        ProjectDO projectDO = projectService.getById(projectReviewDTO.getProjectId());
        if (projectDO != null) {
            List<ProjectReviewDO> list = this.baseMapper.selectList(new QueryWrapper<ProjectReviewDO>()
                    .lambda()
                    .eq(ProjectReviewDO::getProjectSchedule, projectDO.getProjectSchedule())
                    .eq(ProjectReviewDO::getProjectId, projectReviewDTO.getProjectId()));
            for (ProjectReviewDO projectReviewDO : list) {
                ProjectJudgeDO projectJudgeDO = projectJudgeService.getById(projectReviewDO.getJudgeId());
                if (projectJudgeDO != null) {
                    data.add(projectJudgeDO);
                }

            }
        }
        return data;
    }
}