package com.seer.teach.user.admin.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.user.api.GradeServiceApi;
import com.seer.teach.user.api.dto.GradeDTO;
import com.seer.teach.user.api.dto.GradeUpdateDTO;
import com.seer.teach.user.convert.GradeConvert;
import com.seer.teach.user.entity.GradeEntity;
import com.seer.teach.user.entity.UserExtendEntity;
import com.seer.teach.user.service.IGradeService;
import com.seer.teach.user.service.IUserExtendService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p >
 *
 * @author Captain
 * @since 2025-07-28 15:44
 */
@RequiredArgsConstructor
@RestController
@Slf4j
public class GradeServiceApiImpl implements GradeServiceApi {

    private final IGradeService gradeService;

    private final IUserExtendService userExtendService;
    @Override
    public String getById(Integer id) {
        GradeEntity grade = gradeService.getById(id);
        AssertUtils.notNull(grade, ResultCodeEnum.GRADE_NOT_FOUND);
        return grade.getGrade();
    }

    @Override
    public List<GradeDTO> findGradesWithIdGte(Integer id) {
        List<GradeEntity> list = gradeService.list(new LambdaQueryWrapper<GradeEntity>()
                .ge(GradeEntity::getId, id));
        return GradeConvert.INSTANCE.toGradeDTOList(list);
    }

    @Override
    public List<GradeDTO> getGradeList() {
        List<GradeEntity> list = gradeService.list();
        return GradeConvert.INSTANCE.toGradeDTOList(list);
    }

    /**
     * 按ID集合查询
     * @param ids
     * @return
     */
    @Override
    public List<GradeDTO> findGradesByIds(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<GradeEntity> list = gradeService.list(new LambdaQueryWrapper<GradeEntity>()
                .in(GradeEntity::getId, ids));
        return GradeConvert.INSTANCE.toGradeDTOList(list);
    }

    /**
     * 添加年级
     * @param status
     * @param grade
     */
    @Override
    public ResultBean addGrade(Integer status, String grade) {
        try {
            GradeEntity one = gradeService.getOne(new LambdaQueryWrapper<GradeEntity>()
                    .eq(GradeEntity::getGrade, grade));
            AssertUtils.isNull(one,ResultCodeEnum.GRADE_IS_EXIST);
            GradeEntity result = new GradeEntity();
            result.setGrade(grade);
            result.setStatus(status);
            gradeService.save(result);
            return ResultBean.success();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public ResultBean updateGradeStatus(GradeUpdateDTO params) {
        GradeEntity grade = gradeService.getById(params.getId());
        AssertUtils.notNull(grade, ResultCodeEnum.GRADE_NOT_FOUND);
        // 只有在下架年级时（status=0）检查是否有用户使用
        if (params.getStatus() == 0) {
            List<UserExtendEntity> userExtend = userExtendService.getByGradeId(params.getId());
            AssertUtils.isEmpty(userExtend, ResultCodeEnum.THE_GRADE_TO_USE_NOT_ALLOWED_TO_BE_DELETED);
        }
        grade.setStatus(params.getStatus());
        gradeService.updateById(grade);
        log.info("年级状态更新成功{}", grade);
        return ResultBean.success();
    }

    @Override
    public List<GradeDTO> list() {
        List<GradeEntity> list = gradeService.list();
        return GradeConvert.INSTANCE.toGradeDTOList(list);
    }
}
