package com.njcn.msgpush.module.push.service.blacklist;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.msgpush.framework.common.util.object.PageUtils;
import com.njcn.msgpush.module.push.controller.admin.blacklist.vo.BlacklistReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.blacklist.BlacklistDO;
import com.njcn.msgpush.module.push.dal.dataobject.message.MessageRecordDO;
import com.njcn.msgpush.module.push.dal.mysql.blacklist.BlacklistMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caozehui
 * @data 2026-03-12
 */
@Service
public class BlacklistServiceImpl extends ServiceImpl<BlacklistMapper, BlacklistDO> implements BlacklistService {
    @Override
    public Page<BlacklistDO> getPage(BlacklistReqVO reqVO) {
        QueryWrapper<BlacklistDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BlacklistDO::getChannel, reqVO.getChannel());
        return this.page(new Page<>(PageUtils.getPageNum(reqVO), PageUtils.getPageSize(reqVO)), wrapper);
    }


    @Override
    public boolean check(MessageRecordDO messageRecordDO) {
        String receiver = messageRecordDO.getReceiver();
        String[] split = receiver.split(String.valueOf(StrUtil.C_COMMA));
        BlacklistDO blacklistDO = this.lambdaQuery()
                .eq(BlacklistDO::getChannel, messageRecordDO.getChannel())
                .in(split.length > 0, BlacklistDO::getTarget, split).one();

        if (ObjectUtil.isNotNull(blacklistDO)) {
            blacklistDO.setHitCount(blacklistDO.getHitCount() + 1);
            this.updateById(blacklistDO);
            return false;
        }
        return true;
    }
}
