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

import com.tmerclub.cloud.biz.dto.NotifyTemplateDTO;
import com.tmerclub.cloud.biz.mapper.NotifyTemplateMapper;
import com.tmerclub.cloud.biz.model.NotifyTemplate;
import com.tmerclub.cloud.biz.service.NotifyTemplateService;
import com.tmerclub.cloud.biz.vo.NotifyTemplateVO;
import com.tmerclub.cloud.biz.vo.SmsCodeTemplateVO;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author tmerclub
 * @date 2021-01-16 15:01:14
 */
@Service
public class NotifyTemplateServiceImpl implements NotifyTemplateService {

    @Autowired
    private NotifyTemplateMapper notifyTemplateMapper;

    @Override
    public PageVO<NotifyTemplateVO> page(PageDTO pageDTO, NotifyTemplateDTO notifyTemplate) {
        return PageUtil.doPage(pageDTO, () -> notifyTemplateMapper.list(notifyTemplate));
    }

    @Override
    public NotifyTemplateVO getByTemplateId(Long templateId) {
        return notifyTemplateMapper.getByTemplateId(templateId);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SMS_CODE_KEY, key = "#notifyTemplate.sendType")
    public void save(NotifyTemplate notifyTemplate) {
        notifyTemplateMapper.save(notifyTemplate);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.SMS_CODE_KEY, key = "#notifyTemplate.sendType")
    public void update(NotifyTemplate notifyTemplate) {
        notifyTemplateMapper.update(notifyTemplate);
    }

    @Override
    public void deleteById(Long templateId) {
        notifyTemplateMapper.deleteById(templateId);
    }

    @Override
    public NotifyTemplateVO getBySendType(Integer sendType) {
        return notifyTemplateMapper.getBySendType(sendType);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.SMS_CODE_KEY, key = "#sendType")
    public SmsCodeTemplateVO getSmsCodeTemplateBySendType(Integer sendType) {
        return notifyTemplateMapper.getSmsCodeTemplateBySendType(sendType);
    }

    @Override
    public Integer countBySendType(Integer sendType, Long templateId) {
        return notifyTemplateMapper.countBySendType(sendType, templateId);
    }

}
