package com.yida.data.school.index.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.school.EduSchoolHomeMicroIndex;
import com.yida.data.common.core.entity.school.EduSchoolHomeMicroPic;
import com.yida.data.school.index.mapper.EduSchoolHomeMicroIndexMapper;
import com.yida.data.school.index.service.EduSchoolHomeMicroIndexService;
import com.yida.data.school.index.service.EduSchoolHomeMicroPicService;
import com.yida.data.school.news.service.EduSchoolMatterService;
import com.yida.data.school.vo.index.SchoolIndexVO;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class EduSchoolHomeMicroIndexServiceImpl extends ServiceImpl<EduSchoolHomeMicroIndexMapper,
        EduSchoolHomeMicroIndex> implements EduSchoolHomeMicroIndexService {

    private final EduSchoolHomeMicroPicService eduSchoolHomeMicroPicService;
    private final EduSchoolMatterService eduSchoolMatterService;

    @Override
    public void saveIndex(EduSchoolHomeMicroIndex index) {
        if (index.getId() == null) {
            EduSchoolHomeMicroIndex exist = getOne(Wrappers.lambdaQuery(new EduSchoolHomeMicroIndex())
                    .eq(EduSchoolHomeMicroIndex::getSchoolId, index.getSchoolId()));
            if (exist != null) {
                index.setId(exist.getId());
            }
        }
        saveOrUpdate(index);
        List<EduSchoolHomeMicroPic> envList = index.getEnvList();
        List<EduSchoolHomeMicroPic> oldEnv =
                eduSchoolHomeMicroPicService.list(Wrappers.lambdaQuery(new EduSchoolHomeMicroPic())
                        .eq(EduSchoolHomeMicroPic::getColId, index.getColId()));
        if (CollUtil.isNotEmpty(oldEnv)) {
            List<Long> ids = envList.stream().filter(x -> x.getId() != null).map(x -> x.getId()).collect(Collectors.toList());
            List<Long> removeIds = oldEnv.stream().filter(x -> !ids.contains(x.getId())).map(x -> x.getId()).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(removeIds)) {
                eduSchoolHomeMicroPicService.removeByIds(removeIds);
            }
        }
        List<String> picList = new ArrayList<>();
        List<String> videoList = new ArrayList<>();
        for (EduSchoolHomeMicroPic eduSchoolHomeMicroPic : envList) {
            if (StrUtil.isNotBlank(eduSchoolHomeMicroPic.getUrl())) {
                for (String url : eduSchoolHomeMicroPic.getUrl().split(",")) {
                    String suffix = FileUtil.getSuffix(url).toLowerCase(Locale.ROOT);
                    if ("avimp4".contains(suffix)) {
                        videoList.add(url);
                    } else {
                        picList.add(url);
                    }
                }
            }
        }
        // 保存到素材库
        eduSchoolMatterService.saveMatterBatch(picList, index.getSchoolId(), 0);
        // 保存到素材库
        eduSchoolMatterService.saveMatterBatch(videoList, index.getSchoolId(), 0);
        eduSchoolHomeMicroPicService.saveOrUpdateBatch(envList);
    }

    @Override
    public SchoolIndexVO getInfo(Long colId) {
        return baseMapper.getInfo(colId);
    }
}
