MatchDictServiceImpl.java 7.51 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.dto.match.MatchDictDTO;
import com.zhongzhi.dto.match.SelectListPageDTO;
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 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.List;

/**
 * <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, ProjectType.SEED_TRACK);
        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);

        if (matchDictDTO.getSchedule().size() > 0) {
            for (MatchScheduleDO matchScheduleDO : matchDictDTO.getSchedule()) {
                MatchScheduleDO matchSchedule = new MatchScheduleDO();
                BeanUtils.copyProperties(matchScheduleDO, matchSchedule);
                matchSchedule.setMatchId(matchDictDO.getId());
                matchScheduleService.save(matchSchedule);
            }
        }
    }

    @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)
                .eq(MatchDictDO::getMatchType, ProjectType.VOCATIONAL));
    }

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

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

    @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, ProjectType.VOCATIONAL);
        for (MatchDictVO record : iPage.getRecords()) {
            List<MatchScheduleDO> list = matchScheduleService.list(new QueryWrapper<MatchScheduleDO>()
                    .lambda()
                    .eq(MatchScheduleDO::getMatchId, record.getId()));
            record.setSchedule(list);
        }
        return iPage;
    }
}