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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.admin.convert.KnowSummariesConvert;
import com.seer.teach.admin.request.KnowSummariesQueryReq;
import com.seer.teach.admin.request.KnowSummariesReq;
import com.seer.teach.admin.response.KnowSummaryResp;
import com.seer.teach.admin.service.AdminKnowSummaryService;
import com.seer.teach.teacher.module.entity.KnowSummaryEntity;
import com.seer.teach.teacher.service.KnowSummaryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 知识点摘要管理服务实现类
 * </p>
 *
 * @author ZS
 * @since 2025-10-24
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminKnowSummaryServiceImpl implements AdminKnowSummaryService {

    private final KnowSummaryService knowSummaryService;

    /**
     * 分页查询知识点摘要
     *
     * @param req 查询条件
     * @return 知识点摘要分页结果
     */
    @Override
    public IPage<KnowSummaryResp> pageKnowSummaries(KnowSummariesQueryReq req) {
        log.info("开始分页查询知识点摘要, 查询条件: {}", req);
        Page<KnowSummaryEntity> page = new Page<>(req.getPageNo(), req.getPageSize());
        LambdaQueryWrapper<KnowSummaryEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (req.getKnowId() != null) {
            queryWrapper.eq(KnowSummaryEntity::getKnowId, req.getKnowId());
        }
        if (req.getSummary() != null && !req.getSummary().isEmpty()) {
            queryWrapper.like(KnowSummaryEntity::getSummary, req.getSummary());
        }
        IPage<KnowSummaryEntity> entityPage = knowSummaryService.page(page, queryWrapper);
        log.info("知识点摘要分页查询完成, 共查询到 {} 条记录", entityPage.getTotal());
        return entityPage.convert(KnowSummariesConvert.INSTANCE::toResp);
    }

    /**
     * 根据ID获取知识点摘要详情
     *
     * @param id 知识点摘要ID
     * @return 知识点摘要详情
     */
    @Override
    public KnowSummaryResp getKnowSummaryById(Integer id) {
        log.info("开始根据ID获取知识点摘要详情, ID: {}", id);
        KnowSummaryEntity entity = knowSummaryService.getById(id);
        KnowSummaryResp resp = KnowSummariesConvert.INSTANCE.toResp(entity);
        log.info("知识点摘要详情获取完成, ID: {}, 结果: {}", id, resp);
        return resp;
    }

    /**
     * 添加知识点摘要
     *
     * @param req 知识点摘要信息
     * @return 是否添加成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addKnowSummary(KnowSummariesReq req) {
        log.info("开始添加知识点摘要, 请求参数: {}", req);
        KnowSummaryEntity entity = KnowSummariesConvert.INSTANCE.toEntity(req);
        boolean result = knowSummaryService.save(entity);
        log.info("知识点摘要添加完成, 结果: {}", result);
        return result;
    }

    /**
     * 更新知识点摘要
     *
     * @param id  知识点摘要ID
     * @param req 知识点摘要信息
     * @return 是否更新成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateKnowSummary(Integer id, KnowSummariesReq req) {
        log.info("开始更新知识点摘要, ID: {}, 请求参数: {}", id, req);
        KnowSummaryEntity entity = knowSummaryService.getById(id);
        if (Objects.isNull(entity)) {
            log.warn("更新知识点摘要失败, 未找到对应记录, ID: {}", id);
            return false;
        }
        KnowSummariesConvert.INSTANCE.toEntity(req);
        knowSummaryService.updateById(entity);
        log.info("知识点摘要更新完成, ID: {}", id);
        return true;
    }

    /**
     * 删除知识点摘要
     *
     * @param id 知识点摘要ID
     * @return 是否删除成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteKnowSummary(Integer id) {
        log.info("开始删除知识点摘要, ID: {}", id);
        boolean result = knowSummaryService.removeById(id);
        log.info("知识点摘要删除完成, ID: {}, 结果: {}", id, result);
        return result;
    }
}