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

import cn.dev33.satoken.stp.StpUtil;
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.KnowSummaryEntity;
import com.seer.teach.teacher.module.entity.KnowledgePointEntity;
import com.seer.teach.teacher.module.entity.ModificationLogEntity;
import com.seer.teach.teacher.module.mapper.KnowSummariesMapper;
import com.seer.teach.teacher.service.KnowSummaryService;
import com.seer.teach.teacher.util.ModificationLogUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-10-09 15:20:36
 */
@Service
public class KnowSummaryServiceImpl extends ServiceImpl<KnowSummariesMapper, KnowSummaryEntity> implements KnowSummaryService {

    @Override
    public KnowSummaryEntity getOneByKnowId(Integer knowId) {
        return super.getOne(new LambdaQueryWrapper<>(KnowSummaryEntity.class).eq(KnowSummaryEntity::getKnowId, knowId));
    }

    @Override
    public List<KnowSummaryEntity> getListByKnowIds(Collection<Integer> knowIds) {
        if(CollectionUtil.isEmpty(knowIds)){
            return Collections.emptyList();
        }
        return super.list(new LambdaQueryWrapper<>(KnowSummaryEntity.class).in(KnowSummaryEntity::getKnowId, knowIds));
    }

    @Override
    public boolean save(KnowSummaryEntity entity) {
        // 记录新增知识点日志
        boolean result = super.save(entity);
        if (result) {
            ModificationLogUtils.logModification(
                    ModificationLogEntity.TARGET_TYPE_KNOWLEDGE_POINT_SUMMARY,
                    entity.getId(),
                    "保存概要",
                    1,
                    null,
                    entity,
                    StpUtil.getLoginIdAsInt()
            );
        }
        return result;
    }

    @Override
    public boolean updateById(KnowSummaryEntity entity) {
        // 获取修改前的数据
        KnowSummaryEntity before = getById(entity.getId());
        // 更新数据
        boolean result = super.updateById(entity);
        KnowSummaryEntity after = getById(entity.getId());
        // 记录修改日志
        if (result) {
            ModificationLogUtils.logModification(
                    ModificationLogEntity.TARGET_TYPE_KNOWLEDGE_POINT_SUMMARY,
                    entity.getId(),
                    "修改概要",
                    2,
                    before,
                    after,
                    StpUtil.getLoginIdAsInt()
            );
        }
        return result;
    }

    @Override
    public boolean removeById(Serializable id) {
        // 获取删除前的数据
        KnowSummaryEntity before = getById(id);

        // 删除数据
        boolean result = super.removeById(id);

        // 记录删除日志
        if (result && before != null) {
            ModificationLogUtils.logModification(
                    ModificationLogEntity.TARGET_TYPE_KNOWLEDGE_POINT_SUMMARY,
                    (Integer) id,
                    "删除概要",
                    3,
                    before,
                    null,
                    StpUtil.getLoginIdAsInt()
            );
        }
        return result;
    }
}
