
package com.tmerclub.cloud.multishop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.multishop.dto.IndexImgDTO;
import com.tmerclub.cloud.multishop.mapper.IndexImgMapper;
import com.tmerclub.cloud.multishop.model.IndexImg;
import com.tmerclub.cloud.multishop.service.IndexImgService;
import com.tmerclub.cloud.multishop.vo.IndexImgVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播图
 *
 * @author YXF
 * @date 2020-11-24 16:38:32
 */
@Service
public class IndexImgServiceImpl implements IndexImgService {

    @Autowired
    private IndexImgMapper indexImgMapper;

    @Override
    public PageVO<IndexImgVO> page(PageDTO pageDTO, IndexImgDTO indexImgDTO) {
        return PageUtil.doPage(pageDTO, () -> indexImgMapper.list(indexImgDTO));
    }

    @Override
    public IndexImgVO getByImgId(Long imgId) {
        return indexImgMapper.getByImgId(imgId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#indexImg.shopId+ ':' + #indexImg.imgType")
    public void save(IndexImg indexImg) {
        int count = indexImgMapper.countByParam(indexImg);
        if (count >= Constant.MAX_INDEX_IMG_NUM) {
            throw new LuckException("该平台的轮播图已达到最大数量，不能再进行新增操作");
        }
        indexImgMapper.save(indexImg);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#indexImg.shopId+ ':' + 0"),
            @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#indexImg.shopId+ ':' + 1")
    })
    public void update(IndexImg indexImg) {
        int count = indexImgMapper.countByParam(indexImg);
        if (count >= Constant.MAX_INDEX_IMG_NUM) {
            throw new LuckException("该平台的轮播图已达到最大数量，不能再进行新增操作");
        }
        indexImgMapper.update(indexImg);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#shopId + ':' + #imgType")
    public void deleteById(Long imgId, Long shopId, Integer imgType) {
        indexImgMapper.deleteByIdAndShopId(imgId, shopId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#shopId + ':' + #imgType", sync = true)
    public List<IndexImgVO> getListByShopId(Long shopId, Integer imgType) {
        return indexImgMapper.getListByShopId(shopId, imgType);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#shopId+ ':' + 0"),
            @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#shopId+ ':' + 1")
    })
    public void offlineSpuBySpuIds(Long shopId, List<Long> spuIds) {
        if (CollUtil.isEmpty(spuIds)) {
            return;
        }
        indexImgMapper.offlineSpuBySpuIds(shopId, spuIds);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#shopId+ ':' + 0"),
            @CacheEvict(cacheNames = CacheNames.INDEX_IMG_KEY, key = "#shopId+ ':' + 1")
    })
    public void batchUpdate(Long spuId, Long shopId) {
        indexImgMapper.batchUpdate(spuId);
    }
}
