package cn.iocoder.byzs.module.bjdx.service.courselabel;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.byzs.module.bjdx.controller.admin.courselabel.vo.CourseLabelPageReqVO;
import cn.iocoder.byzs.module.bjdx.controller.admin.courselabel.vo.CourseLabelSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.byzs.module.bjdx.controller.admin.courselabel.vo.*;
import cn.iocoder.byzs.module.bjdx.dal.dataobject.courselabel.CourseLabelDO;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;

import cn.iocoder.byzs.module.bjdx.dal.mysql.courselabel.CourseLabelMapper;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.byzs.module.bjdx.enums.ErrorCodeConstants.*;

/**
 * 课程-标签 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class CourseLabelServiceImpl implements CourseLabelService {

    @Resource
    private CourseLabelMapper courseLabelMapper;

    @Override
    public Long createCourseLabel(CourseLabelSaveReqVO createReqVO) {
        // 插入
        CourseLabelDO courseLabel = BeanUtils.toBean(createReqVO, CourseLabelDO.class);
        courseLabelMapper.insert(courseLabel);
        // 返回
        return courseLabel.getId();
    }

    @Override
    public void updateCourseLabel(CourseLabelSaveReqVO updateReqVO) {
        // 校验存在
        validateCourseLabelExists(updateReqVO.getId());
        // 更新
        CourseLabelDO updateObj = BeanUtils.toBean(updateReqVO, CourseLabelDO.class);
        courseLabelMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseLabel(Long id) {
        // 校验存在
        validateCourseLabelExists(id);
        // 删除
        courseLabelMapper.deleteById(id);
    }

    @Override
        public void deleteCourseLabelListByIds(List<Long> ids) {
        // 校验存在
        validateCourseLabelExists(ids);
        // 删除
        courseLabelMapper.deleteByIds(ids);
        }

    private void validateCourseLabelExists(List<Long> ids) {
        List<CourseLabelDO> list = courseLabelMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COURSE_LABEL_NOT_EXISTS);
        }
    }

    private void validateCourseLabelExists(Long id) {
        if (courseLabelMapper.selectById(id) == null) {
            throw exception(COURSE_LABEL_NOT_EXISTS);
        }
    }

    @Override
    public CourseLabelDO getCourseLabel(Long id) {
        return courseLabelMapper.selectById(id);
    }

    @Override
    public PageResult<CourseLabelDO> getCourseLabelPage(CourseLabelPageReqVO pageReqVO) {
        return courseLabelMapper.selectPage(pageReqVO);
    }

}