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

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.teacher.module.entity.ModificationLogEntity;
import com.seer.teach.teacher.module.entity.UnitKnowMappingEntity;
import com.seer.teach.teacher.module.mapper.UnitKnowMappingMapper;
import com.seer.teach.teacher.service.UnitKnowMappingService;
import com.seer.teach.teacher.util.ModificationLogUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 知识点映射表 服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-11-05
 */
@Slf4j
@Service
public class UnitKnowMappingServiceImpl extends ServiceImpl<UnitKnowMappingMapper, UnitKnowMappingEntity> implements UnitKnowMappingService {

    @Override
    public boolean updateKnowledgePointOrder(Integer knowId, Integer unitId, Integer order) {
        UnitKnowMappingEntity entity = this.getOne(new LambdaQueryWrapper<UnitKnowMappingEntity>()
                .eq(UnitKnowMappingEntity::getKnowledgePointId, knowId)
                .eq(UnitKnowMappingEntity::getUnitId, unitId));

        if (entity != null) {
            entity.setSort(order);
            entity.setUpdateTime(LocalDateTime.now());
            entity.setUpdateBy(StpUtil.getLoginIdAsString());
            boolean updated = this.updateById(entity);
            if(updated){
                UnitKnowMappingEntity after = this.getById(entity.getId());
                ModificationLogUtils.logModification(
                        ModificationLogEntity.TARGET_TYPE_KNOWLEDGE_POINT_MAPPING,
                        entity.getId(),
                        "修改排序字段",
                        ModificationLogEntity.OPERATION_TYPE_UPDATE,
                        entity,
                        after,
                        StpUtil.getLoginIdAsInt()
                );
            }
            return updated;
        }
        return false;
    }

    @Override
    public boolean updateKnowledgePointCheckAndScore(Integer knowId, Integer unitId, Integer isChecked, BigDecimal score) {
        UnitKnowMappingEntity entity = this.getOne(new LambdaQueryWrapper<UnitKnowMappingEntity>()
                .eq(UnitKnowMappingEntity::getKnowledgePointId, knowId)
                .eq(UnitKnowMappingEntity::getUnitId, unitId));
        if (entity != null) {
            entity.setIsChecked(isChecked);
            entity.setScore(score);
            entity.setUpdateTime(LocalDateTime.now());
            entity.setUpdateBy(StpUtil.getLoginIdAsString());
            boolean updated = this.updateById(entity);
            if(updated){
                UnitKnowMappingEntity after = this.getById(entity.getId());
                ModificationLogUtils.logModification(
                        ModificationLogEntity.TARGET_TYPE_KNOWLEDGE_POINT_MAPPING,
                        entity.getId(),
                        "修改是否检查和评分字段",
                        ModificationLogEntity.OPERATION_TYPE_UPDATE,
                        entity,
                        after,
                        StpUtil.getLoginIdAsInt()
                );
            }
            return updated;
        }
        return false;
    }

    @Override
    public Collection<UnitKnowMappingEntity> getListByUnitIds(Collection<Integer> unitIds) {
        if(CollectionUtil.isEmpty(unitIds)){
            return Collections.emptyList();
        }
        return super.list(new LambdaQueryWrapper<>(UnitKnowMappingEntity.class).in(UnitKnowMappingEntity::getUnitId, unitIds));
    }

    @Override
    public Collection<UnitKnowMappingEntity> getListByUnitId(Integer unitId) {
        return super.list(new LambdaQueryWrapper<>(UnitKnowMappingEntity.class).eq(UnitKnowMappingEntity::getUnitId, unitId));
    }

    @Override
    public Collection<Integer> getKnowIdListByUnitId(Integer unitId) {
        List<UnitKnowMappingEntity> unitKnowMappingEntities = super.list(new LambdaQueryWrapper<>(UnitKnowMappingEntity.class).eq(UnitKnowMappingEntity::getUnitId, unitId));
        if(CollectionUtil.isEmpty(unitKnowMappingEntities)){
            return Collections.emptyList();
        }
        return unitKnowMappingEntities.stream().map(UnitKnowMappingEntity::getKnowledgePointId).collect(Collectors.toSet());
    }

    @Override
    public Collection<Integer> getKnowIdListByUnitIds(Collection<Integer> unitIds) {
        List<UnitKnowMappingEntity> unitKnowMappingEntities = super.list(new LambdaQueryWrapper<>(UnitKnowMappingEntity.class).in(UnitKnowMappingEntity::getUnitId, unitIds));
        if(CollectionUtil.isEmpty(unitKnowMappingEntities)){
            return Collections.emptyList();
        }
        return unitKnowMappingEntities.stream().map(UnitKnowMappingEntity::getKnowledgePointId).collect(Collectors.toSet());
    }

    @Override
    public Integer getUnitIdByKnowId(Integer knowId) {
        UnitKnowMappingEntity one = super.getOne(new LambdaQueryWrapper<>(UnitKnowMappingEntity.class).eq(UnitKnowMappingEntity::getKnowledgePointId, knowId));
        if(one != null){
            return one.getUnitId();
        }
        return null;
    }

    @Override
    public Collection<Integer> getUnitIdListByKnowIds(List<Integer> knowIds) {
        Collection<UnitKnowMappingEntity> unitKnowMappingEntities = getListByKnowIds(knowIds);
        if(CollectionUtil.isEmpty(unitKnowMappingEntities)){
            return Collections.emptyList();
        }
        return unitKnowMappingEntities.stream().map(UnitKnowMappingEntity::getUnitId).collect(Collectors.toSet());
    }

    @Override
    public Collection<UnitKnowMappingEntity> getListByKnowIds(List<Integer> knowIds) {
        if(CollectionUtil.isEmpty(knowIds)){
            return Collections.emptyList();
        }
        List<UnitKnowMappingEntity> unitKnowMappingEntities = super.list(new LambdaQueryWrapper<>(UnitKnowMappingEntity.class).in(UnitKnowMappingEntity::getKnowledgePointId, knowIds));
        if(CollectionUtil.isEmpty(unitKnowMappingEntities)){
            return Collections.emptyList();
        }
        log.info("getListByKnowIds:{}", unitKnowMappingEntities);
        return unitKnowMappingEntities;
    }

    @Override
    public UnitKnowMappingEntity getOneByUnitIdAndKnowId(Integer unitId, Integer knowId) {
        return super.getOne(new LambdaQueryWrapper<>(UnitKnowMappingEntity.class).eq(UnitKnowMappingEntity::getUnitId, unitId).eq(UnitKnowMappingEntity::getKnowledgePointId, knowId));
    }
}