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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.msgpush.module.push.controller.admin.retry.vo.RetryStrategyConfigReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.retry.RetryStrategyConfigDO;
import com.njcn.msgpush.module.push.dal.mysql.retry.RetryStrategyConfigMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author caozehui
 * @data 2026-03-05
 */
@Service
public class RetryStrategyConfigServiceImpl extends ServiceImpl<RetryStrategyConfigMapper, RetryStrategyConfigDO> implements RetryStrategyConfigService {
    @Override
    public RetryStrategyConfigDO getStrategyConfig(String channel) {
        return this.lambdaQuery().eq(RetryStrategyConfigDO::getChannel, channel)
                .eq(RetryStrategyConfigDO::getEnabled, true)
                .one();
    }

    @Override
    public List<RetryStrategyConfigDO> listAll() {
        return this.lambdaQuery()
                .eq(RetryStrategyConfigDO::getEnabled, true)
                .list();
    }

    @Override
    public boolean updateStrategyConfig(RetryStrategyConfigReqVO strategyConfigReqVO) {
        RetryStrategyConfigDO retryStrategyConfigDO = BeanUtil.copyProperties(strategyConfigReqVO, RetryStrategyConfigDO.class);
        return this.updateById(retryStrategyConfigDO);
    }

    @Override
    public boolean toggleEnableField(String id) {
        RetryStrategyConfigDO retryStrategyConfigDO = this.getById(id);
        retryStrategyConfigDO.setEnabled(retryStrategyConfigDO.getEnabled() ^ 0X0001);
        return this.updateById(retryStrategyConfigDO);
    }
}
