package com.seer.teach.teacher.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.teacher.module.entity.MaterialEntity;
import com.seer.teach.teacher.module.entity.UnitKnowMappingEntity;
import com.seer.teach.teacher.module.entity.UnitsEntity;
import com.seer.teach.teacher.module.entity.VersionsEntity;
import com.seer.teach.teacher.module.mapper.UnitsMapper;
import com.seer.teach.teacher.module.mapper.VersionsMapper;
import com.seer.teach.teacher.service.MaterialService;
import com.seer.teach.teacher.service.UnitKnowMappingService;
import com.seer.teach.teacher.service.VersionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 教材版本表 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-10-09 15:20:37
 */
@Service
@RequiredArgsConstructor
public class VersionsServiceImpl extends ServiceImpl<VersionsMapper, VersionsEntity> implements VersionsService {

    private final UnitsMapper unitsMapper;

    private final MaterialService materialService;

    private final UnitKnowMappingService unitKnowMappingService;

    @Override
    public Integer getDefaultVersionId(Integer subjectId) {
        VersionsEntity defaultVersion = list(new LambdaQueryWrapper<VersionsEntity>()
                .eq(VersionsEntity::getSubjectId, subjectId)).get(0);
        return defaultVersion.getId();
    }

    @Override
    public Integer getPrimarySchool(Integer subjectId) {
        VersionsEntity defaultVersion = list(new LambdaQueryWrapper<VersionsEntity>()
                .eq(VersionsEntity::getSubjectId, subjectId)
                .ge(VersionsEntity::getId,1)
                .le(VersionsEntity::getId,29)).get(0);
        return defaultVersion.getId();
    }

    @Override
    public Integer getMiddleSchool(Integer subjectId) {
        VersionsEntity defaultVersion = list(new LambdaQueryWrapper<VersionsEntity>()
                .eq(VersionsEntity::getSubjectId, subjectId)
                .ge(VersionsEntity::getId,49)
                .le(VersionsEntity::getId,93)).get(0);
        return defaultVersion.getId();
    }

    @Override
    public Integer getHighSchool(Integer subjectId) {
        VersionsEntity defaultVersion = list(new LambdaQueryWrapper<VersionsEntity>()
                .eq(VersionsEntity::getSubjectId, subjectId)
                .ge(VersionsEntity::getId,50)
                .le(VersionsEntity::getId,115)).get(0);
        return defaultVersion.getId();
    }

    @Override
    public Integer getSubjectIdByKnowID(Integer knowId) {
        Integer unitId = unitKnowMappingService.getUnitIdByKnowId(knowId);
        UnitsEntity unit = unitsMapper.selectById(unitId);
        MaterialEntity material = materialService.getById(unit.getMaterialId());
        VersionsEntity version = getById(material.getVersionId());
        return version.getSubjectId();
    }

    @Override
    public HashMap<Integer, Integer> getSubjectIdsByKnowIds(List<Integer> knowIds) {
        HashMap<Integer, Integer> resultMap = new HashMap<>();

        Collection<UnitKnowMappingEntity> mappings = unitKnowMappingService.getListByKnowIds(knowIds);

        Map<Integer, Integer> unitToKnowMap = mappings.stream().collect(
                Collectors.toMap(
                        UnitKnowMappingEntity::getUnitId,
                        UnitKnowMappingEntity::getKnowledgePointId,
                        (a, b) -> a
                ));

        List<UnitsEntity> unitList = unitsMapper.selectByIds(unitToKnowMap.keySet());

        Map<Integer, Integer> materialToUnitMap = unitList.stream().collect(
                Collectors.toMap(
                        UnitsEntity::getMaterialId,
                        UnitsEntity::getId,
                        (a, b) -> a
                ));

        List<MaterialEntity> materialList = materialService.listByIds(materialToUnitMap.keySet());

        Map<Integer, Integer> versionToMaterialMap = materialList.stream()
                .filter(m -> m.getVersionId() != null)
                .collect(Collectors.toMap(
                        MaterialEntity::getVersionId,
                        MaterialEntity::getId,
                        (a, b) -> a
                ));

        List<VersionsEntity> versionList = listByIds(versionToMaterialMap.keySet());
        for (VersionsEntity version : versionList) {
            Integer subjectId = version.getSubjectId();
            Integer materialId = versionToMaterialMap.get(version.getId());
            Integer unitId = materialToUnitMap.get(materialId);
            Integer knowId = unitToKnowMap.get(unitId);
            if (knowId != null && subjectId != null) {
                resultMap.put(knowId, subjectId);
            }
        }

        return resultMap;
    }

    @Override
    public List<VersionsEntity> getListBySubjectId(Integer subjectId) {
        return super.list(new LambdaQueryWrapper<>(VersionsEntity.class).eq(VersionsEntity::getSubjectId, subjectId));
    }

    @Override
    public List<VersionsEntity> getListBySubjectIds(Collection<Integer> subjectIds) {
        if(CollectionUtil.isEmpty(subjectIds)){
            return Collections.emptyList();
        }
        return super.list(new LambdaQueryWrapper<>(VersionsEntity.class).in(VersionsEntity::getSubjectId, subjectIds));
    }
}
