CollegeFileServiceImpl.java 4.53 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.college.FileManagementDTO;
import com.subsidy.mapper.MatchDictMapper;
import com.subsidy.model.CollegeFileDO;
import com.subsidy.mapper.CollegeFileMapper;
import com.subsidy.model.CollegesDictDO;
import com.subsidy.model.MatchDictDO;
import com.subsidy.service.CollegeFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.vo.college.CollegeInfoVO;
import com.subsidy.vo.college.FileManagementVO;
import com.subsidy.vo.college.RecentFileVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2025-01-09
 */
@Service
public class CollegeFileServiceImpl extends ServiceImpl<CollegeFileMapper, CollegeFileDO> implements CollegeFileService {

    @Autowired
    private MatchDictMapper matchDictMapper;

    public String submitFile(CollegeFileDO collegeFileDO) throws Exception {

        //时间
        MatchDictDO matchDictDO = matchDictMapper.selectById(collegeFileDO.getMatchId());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        if (null != matchDictDO.getFileStartTime() && new Date().before(simpleDateFormat.parse(matchDictDO.getFileStartTime()))){
            throw new HttpException(10007);
        }

        if ( null != matchDictDO.getFileEndTime() && new Date().after(simpleDateFormat.parse(matchDictDO.getFileEndTime()))) {
            throw new HttpException(10007);
        }

        CollegeFileDO collegeFileDO1 = this.baseMapper.selectOne(new QueryWrapper<CollegeFileDO>()
                .lambda()
                .eq(CollegeFileDO::getMatchId, collegeFileDO.getMatchId())
                .eq(CollegeFileDO::getCollegeId, collegeFileDO.getCollegeId()));

        if (null == collegeFileDO1) {
            this.baseMapper.insert(collegeFileDO);
        } else {
            copyPropertiesIgnoreNull(collegeFileDO, collegeFileDO1);
            this.baseMapper.updateById(collegeFileDO1);
        }

        return ConstantUtils.SUBMIT_SUCCESS;
    }

    public RecentFileVO recentFile(CollegeFileDO collegeFileDO) {
        RecentFileVO recentFileVO = this.baseMapper.recentFile(collegeFileDO.getCollegeId(),collegeFileDO.getMatchId());

        if (null != recentFileVO) {
            return recentFileVO;
        } else {
            return new RecentFileVO();
        }
    }

    public IPage<FileManagementVO> fileManagement(FileManagementDTO fileManagementDTO) {

        Page page = new Page(fileManagementDTO.getPageNum(), fileManagementDTO.getPageSize());

        IPage<FileManagementVO> fileManagementVOIPage = this.baseMapper.fileManagement(page, fileManagementDTO.getMatchId(), fileManagementDTO.getProjectGroup(), fileManagementDTO.getName());

        List<FileManagementVO> records = fileManagementVOIPage.getRecords();
        for (FileManagementVO fileManagementVO : records){
            //复赛项目数,决赛项目数,市级铜奖,市级银奖,市级金奖
            CollegeInfoVO collegeInfoVO = this.baseMapper.collegeInfo(fileManagementVO.getId(), fileManagementDTO.getMatchId());
            BeanUtils.copyProperties(collegeInfoVO,fileManagementVO);
        }
        return fileManagementVOIPage;
    }

    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
        Set<String> emptyNames = new HashSet<>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    public static void copyPropertiesIgnoreNull(Object src, Object target) {
        BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
    }
}