package jnpf.notice.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.model.notice.domain.FtbNoticeFiles;
import jnpf.model.notice.dto.FtbNoticeFilesDto;
import jnpf.model.notice.enums.NoticeEnums;
import jnpf.model.notice.req.announcement.NoticeFilesVo;
import jnpf.notice.mapper.FtbNoticeFilesMapper;
import jnpf.notice.service.FtbNoticeFilesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 许贵林
 * @description 针对表【ftb_notice_files(公告附件表)】的数据库操作Service实现
 * @createDate 2024-05-08 09:42:09
 */
@Service
public class FtbNoticeFilesServiceImpl extends ServiceImpl<FtbNoticeFilesMapper, FtbNoticeFiles>
        implements FtbNoticeFilesService {

    /**
     * 添加附件
     * @param announcementId 公告id
     * @param fileList       附件列表
     */
    @Override
    public void addFiles(String announcementId, List<NoticeFilesVo> fileList) {
        if (StringUtils.isEmpty(announcementId) || CollectionUtil.isEmpty(fileList)) {
            return;
        }
        Map<String, FtbNoticeFiles> oldMap = new HashMap<>();
        LambdaQueryWrapper<FtbNoticeFiles> wrapper = new LambdaQueryWrapper<FtbNoticeFiles>()
                .eq(FtbNoticeFiles::getBusinessId, announcementId);
        List<FtbNoticeFiles> oldFiles = baseMapper.selectList(wrapper);
        if (CollectionUtil.isEmpty(oldFiles)) {
            for (FtbNoticeFiles file : oldFiles) {
                StringBuilder sb = new StringBuilder();
                sb.append(file.getFileName());
                sb.append(file.getUrl());
                sb.append(file.getExtension());
                sb.append(file.getSize());
                sb.append(file.getPreUrl());
                String key = sb.toString();
                oldMap.put(key, file);
            }
        }


        List<FtbNoticeFiles> addList = new ArrayList<>();
        List<FtbNoticeFiles> updateList = new ArrayList<>();

        for (NoticeFilesVo item : fileList) {
            FtbNoticeFiles file = new FtbNoticeFiles();
            file.setBusinessId(announcementId);
            file.setFileName(item.getFileName());
            file.setUrl(item.getFileUrl());
            file.setSize(item.getFilesize());
            file.setExtension(item.getFileSuffix());
            file.setEnabledMark(NoticeEnums.DeleteType.VALID.getCode());
            file.setCreatorTime(new Date());
            file.setPreUrl(item.getPreUrl());


            StringBuilder sb = new StringBuilder();
            sb.append(file.getFileName());
            sb.append(file.getUrl());
            sb.append(file.getExtension());
            sb.append(file.getSize());
            sb.append(file.getPreUrl());
            String key = sb.toString();
            FtbNoticeFiles ftbNoticeFiles = oldMap.get(key);
            if (null == ftbNoticeFiles) {
                addList.add(file);
            } else {
                file.setId(ftbNoticeFiles.getId());
                updateList.add(file);
            }

        }
        if (CollectionUtil.isNotEmpty(updateList)) {
            updateBatchById(updateList);
        }
        if (CollectionUtil.isNotEmpty(addList)) {
            saveBatch(addList);
        }
    }

    /**
     * 删除公告附件
     * @param announcementId 公告id
     */
    @Override
    public void deleteFiles(String announcementId) {
        LambdaUpdateWrapper<FtbNoticeFiles> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(FtbNoticeFiles::getBusinessId, announcementId);
        updateWrapper.set(FtbNoticeFiles::getEnabledMark, NoticeEnums.DeleteType.INVALID.getCode());
        baseMapper.update(new FtbNoticeFiles(), updateWrapper);
    }

    /**
     * 获取公告附件列表
     * @param announcementId 公告id
     * @return
     */
    @Override
    public List<FtbNoticeFilesDto> getFiles(String announcementId) {
        LambdaQueryWrapper<FtbNoticeFiles> wrapper = new LambdaQueryWrapper<FtbNoticeFiles>()
                .eq(FtbNoticeFiles::getEnabledMark, NoticeEnums.DeleteType.VALID.getCode())
                .eq(FtbNoticeFiles::getBusinessId, announcementId).orderByAsc(FtbNoticeFiles::getCreatorTime);
        List<FtbNoticeFiles> list = baseMapper.selectList(wrapper);
        if (CollectionUtil.isEmpty(list)) {
            return new ArrayList<>();
        }
        List<FtbNoticeFilesDto> retList = new ArrayList<>();
        for (FtbNoticeFiles ftbNoticeFiles : list) {
            FtbNoticeFilesDto dto = new FtbNoticeFilesDto();
            dto.setId(ftbNoticeFiles.getId());
            dto.setFileName(ftbNoticeFiles.getFileName());
            dto.setFileUrl(ftbNoticeFiles.getUrl());
            dto.setFileSuffix(ftbNoticeFiles.getExtension());
            dto.setFilesize(ftbNoticeFiles.getSize());
            dto.setPreUrl(ftbNoticeFiles.getPreUrl());
            retList.add(dto);
        }
        return retList;
    }
}




