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

import cn.hutool.core.collection.CollectionUtil;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.admin.convert.AdminTeachGoalConvert;
import com.seer.teach.admin.request.TeachMethodsReq;
import com.seer.teach.admin.response.TeachMethodsResp;
import com.seer.teach.admin.service.AdminTeachMethodsService;
import com.seer.teach.teacher.module.entity.TeachMethodsEntity;
import com.seer.teach.teacher.module.mapper.TeachMethodsMapper;
import com.seer.teach.teacher.service.TeachMethodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 教学方法管理服务实现类
 * </p>
 *
 * @author Lingma
 * @since 2025-12-04
 */
@Slf4j
@Service
public class AdminTeachMethodsServiceImpl extends ServiceImpl<TeachMethodsMapper, TeachMethodsEntity> implements AdminTeachMethodsService {

    @Autowired
    private TeachMethodsService teachMethodsService;

    @Override
    public IPage<TeachMethodsResp> pageTeachMethods(Integer pageNo, Integer pageSize) {
        log.info("分页查询教学方法列表, 页码: {}, 每页数量: {}", pageNo, pageSize);
        Page<TeachMethodsEntity> page = new Page<>(pageNo, pageSize);
        page(page);

        // 转换为响应对象
        IPage<TeachMethodsResp> respPage = page.convert(AdminTeachGoalConvert.INSTANCE::toTeachMethodsResp);

        log.info("分页查询教学方法列表成功, 总数: {}", respPage.getTotal());
        return respPage;
    }

    @Override
    public List<TeachMethodsResp> getTeachMethodList(String methodName) {
        List<TeachMethodsEntity> list = teachMethodsService.list(new LambdaQueryWrapper<>(TeachMethodsEntity.class).like(StringUtils.isNotBlank(methodName), TeachMethodsEntity::getName, methodName));
        if(CollectionUtil.isNotEmpty(list)){
            return AdminTeachGoalConvert.INSTANCE.toTeachMethodsRespList(list);
        }
        return Collections.emptyList();
    }

    @Override
    public TeachMethodsResp getTeachMethodById(Integer id) {
        log.info("根据ID查询教学方法详情, ID: {}", id);
        TeachMethodsEntity entity = teachMethodsService.getById(id);
        if (entity == null) {
            return null;
        }
        
        TeachMethodsResp resp = AdminTeachGoalConvert.INSTANCE.toTeachMethodsResp(entity);
        log.info("查询教学方法详情成功, ID: {}", id);
        return resp;
    }

    @Override
    public boolean addTeachMethod(TeachMethodsReq req) {
        log.info("新增教学方法, 请求参数: {}", req);
        TeachMethodsEntity entity = AdminTeachGoalConvert.INSTANCE.toTeachMethodsEntity(req);
        boolean result = save(entity);
        log.info("新增教学方法结果: {}, ID: {}", result, entity.getId());
        return result;
    }

    @Override
    public boolean updateTeachMethod(Integer id, TeachMethodsReq req) {
        log.info("更新教学方法, ID: {}, 请求参数: {}", id, req);
        TeachMethodsEntity entity = AdminTeachGoalConvert.INSTANCE.toTeachMethodsEntity(req);
        entity.setId(id);
        boolean result = updateById(entity);
        log.info("更新教学方法结果: {}", result);
        return result;
    }

    @Override
    public boolean deleteTeachMethod(Integer id) {
        log.info("删除教学方法, ID: {}", id);
        boolean result = removeById(id);
        log.info("删除教学方法结果: {}", result);
        return result;
    }
}