package com.jojubanking.boot.module.system.service.carousel;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.jojubanking.boot.module.system.controller.admin.carousel.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.carousel.CarouselDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.carousel.CarouselConvert;
import com.jojubanking.boot.module.system.dal.mysql.carousel.CarouselMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 轮播图 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class CarouselServiceImpl implements CarouselService {

    @Resource
    private CarouselMapper carouselMapper;

    @Override
    public Integer createCarousel(CarouselCreateReqVO createReqVO) {
        // 插入
        CarouselDO carousel = CarouselConvert.INSTANCE.convert(createReqVO);
        carouselMapper.insert(carousel);
        // 返回
        return carousel.getId();
    }

    @Override
    public void updateCarousel(CarouselUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateCarouselExists(updateReqVO.getId());
        // 更新
        CarouselDO updateObj = CarouselConvert.INSTANCE.convert(updateReqVO);
        carouselMapper.updateById(updateObj);
    }

    @Override
    public void deleteCarousel(Integer id) {
        // 校验存在
        this.validateCarouselExists(id);
        // 删除
        carouselMapper.deleteById(id);
    }

    private void validateCarouselExists(Integer id) {
        if (carouselMapper.selectById(id) == null) {
            throw exception(CAROUSEL_NOT_EXISTS);
        }
    }

    @Override
    public CarouselDO getCarousel(Integer id) {
        return carouselMapper.selectById(id);
    }

    @Override
    public List<CarouselDO> getCarouselList(Collection<Integer> ids) {
        return carouselMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CarouselDO> getCarouselPage(CarouselPageReqVO pageReqVO) {
        return carouselMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CarouselDO> getCarouselList(CarouselExportReqVO exportReqVO) {
        return carouselMapper.selectList(exportReqVO);
    }

}
