MatchDictServiceImpl.java 9.52 KB
package com.zhongzhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongzhi.common.constant.ProjectType;
import com.zhongzhi.common.utils.DateFormatUtil;
import com.zhongzhi.common.utils.ExcelUtil;
import com.zhongzhi.dto.match.MatchDictDTO;
import com.zhongzhi.dto.match.SelectListPageDTO;
import com.zhongzhi.dto.project.ProjectSummaryDTO;
import com.zhongzhi.model.MatchDictDO;
import com.zhongzhi.dao.MatchDictDAO;
import com.zhongzhi.model.MatchScheduleDO;
import com.zhongzhi.service.MatchDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongzhi.service.MatchScheduleService;
import com.zhongzhi.vo.match.MatchDictVO;
import com.zhongzhi.vo.match.ProjectSummaryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 赛事年份管理 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2021-05-17
 */
@Service
public class MatchDictServiceImpl extends ServiceImpl<MatchDictDAO, MatchDictDO> implements MatchDictService {

    @Autowired
    private MatchScheduleService matchScheduleService;

    @Override
    public MatchDictDO getMainTrackMatch() {
        return this.baseMapper.selectOne(new QueryWrapper<MatchDictDO>()
                .lambda()
                .eq(MatchDictDO::getMatchType, ProjectType.MAIN_TRACK)
                .eq(MatchDictDO::getStatus, 1));
    }

    @Override
    public MatchDictDO getSeedTrackMatch() {
        return this.baseMapper.selectOne(new QueryWrapper<MatchDictDO>()
                .lambda()
                .eq(MatchDictDO::getMatchType, ProjectType.SEED_TRACK)
                .eq(MatchDictDO::getStatus, 1));
    }

    @Override
    public void updateMatch(MatchDictDTO matchDictDTO) {
        MatchDictDO matchDict = this.baseMapper.selectById(matchDictDTO.getId());
        MatchDictDO match = this.baseMapper.selectOne(new QueryWrapper<MatchDictDO>()
                .lambda()
                .eq(MatchDictDO::getMatchType, matchDict.getMatchType())
                .eq(MatchDictDO::getStatus, 1));
        if (matchDictDTO.getStatus() != null) {
            if (match != null && !match.getId().equals(matchDictDTO.getId())) {
                match.setStatus(0);
                this.baseMapper.updateById(match);
            }
        }

        MatchDictDO matchDictDO = new MatchDictDO();
        BeanUtils.copyProperties(matchDictDTO, matchDictDO);
        this.baseMapper.updateById(matchDictDO);

        if (matchDictDTO.getSchedule() != null && matchDictDTO.getSchedule().size() > 0) {
            List<MatchScheduleDO> list = matchScheduleService.list(new QueryWrapper<MatchScheduleDO>()
                    .lambda()
                    .eq(MatchScheduleDO::getMatchId, matchDictDTO.getId()));
            if (list.size() > 0) {
                for (MatchScheduleDO matchScheduleDO : list) {
                    if (matchDictDTO.getSchedule().stream().filter(ms -> ms.getScheduleTime().equals(matchScheduleDO.getScheduleTime()) &&
                            ms.getExplains().equals(matchScheduleDO.getExplains())).findAny().isPresent()) {
                        continue;
                    } else {
                        matchScheduleService.removeById(matchScheduleDO.getId());
                    }
                }
            }

            for (MatchScheduleDO matchScheduleDO : matchDictDTO.getSchedule()) {
                if (list.stream().filter(ms -> ms.getExplains().equals(matchScheduleDO.getExplains()) &&
                        DateFormatUtil.format(ms.getScheduleTime(), DateFormatUtil.FMT_sdf_yMd).equals(DateFormatUtil.format(matchScheduleDO.getScheduleTime(), DateFormatUtil.FMT_sdf_yMd))).findAny().isPresent()) {
                    MatchScheduleDO matchSchedule = matchScheduleService.getOne(new QueryWrapper<MatchScheduleDO>()
                            .lambda()
                            .eq(MatchScheduleDO::getMatchId, matchScheduleDO.getMatchId())
                            .eq(MatchScheduleDO::getExplains, matchScheduleDO.getExplains())
                            .eq(MatchScheduleDO::getScheduleTime, DateFormatUtil.format(matchScheduleDO.getScheduleTime(), DateFormatUtil.FMT_sdf_yMd)));
                    if (matchSchedule != null) {
                        matchScheduleDO.setId(matchSchedule.getId());
                        matchScheduleService.updateById(matchScheduleDO);
                    }
                } else {
                    matchScheduleDO.setMatchId(matchDictDTO.getId());
                    matchScheduleService.save(matchScheduleDO);
                }
            }
        }
    }

    @Override
    public IPage<MatchDictVO> getSeedTrackMatchPage(SelectListPageDTO selectListPageDTO) {
        Page page = new Page(selectListPageDTO.getPageNo(), selectListPageDTO.getPageSize());
        IPage<MatchDictVO> iPage = this.baseMapper.getSeedTrackMatchPage(page);
        for (MatchDictVO record : iPage.getRecords()) {
            List<MatchScheduleDO> list = matchScheduleService.list(new QueryWrapper<MatchScheduleDO>()
                    .lambda()
                    .eq(MatchScheduleDO::getMatchId, record.getId()));
            record.setSchedule(list);
        }
        return iPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createMatch(MatchDictDTO matchDictDTO) {
        MatchDictDO matchDictDO = new MatchDictDO();
        BeanUtils.copyProperties(matchDictDTO, matchDictDO);
        this.baseMapper.insert(matchDictDO);
    }

    @Override
    public List<MatchDictDO> getMatch() {
        return this.baseMapper.selectList(new QueryWrapper<MatchDictDO>()
                .lambda()
                .eq(MatchDictDO::getStatus, 1));
    }

    @Override
    public MatchDictDO getVocationalMatch() {
        return this.baseMapper.selectOne(new QueryWrapper<MatchDictDO>()
                .lambda()
                .eq(MatchDictDO::getStatus, 1));
    }

    public void updateMatchDate(MatchDictDO matchDictDO) {
        this.baseMapper.updateById(matchDictDO);
    }

    public MatchDictDO getMatch(MatchDictDO matchDictDO) {
        return this.baseMapper.selectById(matchDictDO.getId());
    }

    @Override
    public List<ProjectSummaryVO> projectSummary(ProjectSummaryDTO projectSummaryDTO) {

        // ===================== 1. 固定顺序(必须和图2一致)=====================
        List<String> ALL_COLLEGES = Arrays.asList(
                "会计学院",
                "金融学院",
                "工商管理学院",
                "国际经贸学院",
                "财税与公共管理学院",
                "统计与数学学院",
                "计算机与人工智能学院",
                "外国语学院",
                "保险学院",
                "法学院",
                "金融科技学院",
                "人文艺术学院",
                "序伦书院"
        );
        List<ProjectSummaryVO> projectSummaryVOS = this.baseMapper.projectSummary(projectSummaryDTO.getMatchId(), projectSummaryDTO.getStartDate(), projectSummaryDTO.getEndDate());

        // ===================== 3. 关键代码:补全所有学院,没有数据则为0 =====================
        // 把SQL结果转成 Map<学院名, 数据>,方便快速查找
        Map<String, ProjectSummaryVO> dataMap = projectSummaryVOS.stream()
                .collect(Collectors.toMap(ProjectSummaryVO::getXueyuan, d -> d));

        // 遍历固定顺序列表 → 有数据用数据,没数据补0
        List<ProjectSummaryVO> finalResult = ALL_COLLEGES.stream()
                .map(college -> {
                    ProjectSummaryVO data = dataMap.get(college);
                    if (data != null) {
                        return data;
                    } else {
                        // 没有数据 → 构造 0 值对象
                        return new ProjectSummaryVO(college, 0, 0);
                    }
                })
                .collect(Collectors.toList());

        int i = 1;

        String[] split = "159,114,62,72,89,59,93,33,37,34,36,8,4".split(",");


        for (ProjectSummaryVO projectSummaryVO : finalResult){
            projectSummaryVO.setFixCnt(Integer.valueOf(split[i-1]));
            projectSummaryVO.setId(i ++);
        }

        return finalResult;
    }

    @Override
    public void exportProjectSummary(ProjectSummaryDTO projectSummaryDTO) {
        List<ProjectSummaryVO> projectSummaryVOS = projectSummary(projectSummaryDTO);
        ExcelUtil.writeExcel(projectSummaryVOS,ProjectSummaryVO.class);
    }

    @Override
    public List<MatchDictDO> getList(String projectType, String projectGroup) {
        return this.baseMapper.getList(projectType, projectGroup);
    }

    @Override
    public IPage getVocationalPage(SelectListPageDTO selectListPageDTO) {
        Page page = new Page(selectListPageDTO.getPageNo(), selectListPageDTO.getPageSize());
        IPage<MatchDictVO> iPage = this.baseMapper.getSeedTrackMatchPage(page);
//        for (MatchDictVO record : iPage.getRecords()) {
//            List<MatchScheduleDO> list = matchScheduleService.list(new QueryWrapper<MatchScheduleDO>()
//                    .lambda()
//                    .eq(MatchScheduleDO::getMatchId, record.getId()));
//            record.setSchedule(list);
//        }
        return iPage;
    }
}