package com.yida.data.school.secondClass.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.constant.CachePrefixConstant;
import com.yida.data.common.core.entity.school.secondClass.EduSecondClassBanner;
import com.yida.data.school.secondClass.mapper.EduSecondClassBannerMapper;
import com.yida.data.school.secondClass.service.EduSecondClassBannerService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class EduSecondClassBannerServiceImpl extends ServiceImpl<EduSecondClassBannerMapper, EduSecondClassBanner> implements
    EduSecondClassBannerService {

    @Override
    @CacheEvict(value = CachePrefixConstant.SECOND_CLASS_BANNER, key = "#banner.deptId+'-'+#banner.type")
    public void saveBanner(EduSecondClassBanner banner) {
        EduSecondClassBanner exist = getOne(
            Wrappers.<EduSecondClassBanner>lambdaQuery().eq(EduSecondClassBanner::getDeptId, banner.getDeptId())
                .eq(EduSecondClassBanner::getType, banner.getType()));
        if (exist != null) {
            banner.setId(exist.getId());
        }
        saveOrUpdate(banner);
    }

    @Override
    @Cacheable(value = CachePrefixConstant.SECOND_CLASS_BANNER, key = "#deptId+'-'+#type")
    public EduSecondClassBanner getBanner(Long deptId, Integer type) {
        EduSecondClassBanner res = getOne(Wrappers.<EduSecondClassBanner>lambdaQuery().eq(EduSecondClassBanner::getDeptId, deptId)
            .eq(EduSecondClassBanner::getType, type)
            .select(EduSecondClassBanner::getId, EduSecondClassBanner::getType, EduSecondClassBanner::getNum,
                EduSecondClassBanner::getEdgeMargin, EduSecondClassBanner::getTime, EduSecondClassBanner::getUrl,
                EduSecondClassBanner::getLinkUrl));
        return res == null ? new EduSecondClassBanner() : res;
    }

    @Override
    @CacheEvict(value = CachePrefixConstant.SECOND_CLASS_BANNER, key = "#deptId+'-'+#type")
    public void removeBanner(Long deptId, Integer type) {
        remove(Wrappers.<EduSecondClassBanner>lambdaQuery().eq(EduSecondClassBanner::getDeptId, deptId)
            .eq(EduSecondClassBanner::getType, type));  
    }
}
