package com.sonic.lion.service.impl;

import com.sonic.lion.dao.ChannelBlacklistDao;
import com.sonic.lion.domain.entity.ChannelBlacklist;
import com.sonic.lion.service.ChannelBlacklistService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author code
 * @Description 渠道黑名单列表
 * @Date 2024/1/23 15:06
 * @Version 1.0
 */
@Slf4j
@Service
public class ChannelBlacklistServiceImpl implements ChannelBlacklistService {

    @Autowired
    private ChannelBlacklistDao channelBlacklistDao;


    @Override
    public void addBlacklist(Long userId, String channelType) {
        try {
            if(userId == null || StringUtils.isEmpty(channelType)) {
                return;
            }
            ChannelBlacklist channelBlacklist = channelBlacklistDao.getPayerId(userId, channelType);
            if(channelBlacklist != null) {
                //更新数据
                ChannelBlacklist updateChannelBlacklist = new ChannelBlacklist();
                updateChannelBlacklist.setId(channelBlacklist.getId());
                updateChannelBlacklist.setIsDelete(false);
                updateChannelBlacklist.setBlockCount(channelBlacklist.getBlockCount() + 1);
                updateChannelBlacklist.setEditTime(LocalDateTime.now());
                channelBlacklistDao.updateById(updateChannelBlacklist);
                return;
            }
            //写入数据
            ChannelBlacklist saveChannelBlacklist = ChannelBlacklist.
                    builder().userId(userId)
                    .channelType(channelType)
                    .blockCount(1)
                    .isDelete(false)
                    .createTime(LocalDateTime.now())
                    .editTime(LocalDateTime.now()).build();
            channelBlacklistDao.insert(saveChannelBlacklist);
        } catch (Exception e) {
            log.error("===> ChannelBlacklistService save channelBlacklist error : ", e);
        }
    }
}
