
package com.pavis.admin.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.provider.config.BaseConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.pavis.admin.common.enums.DisEnableStatusEnum;
import com.pavis.admin.system.config.sms.SmsConfigUtil;
import com.pavis.admin.system.mapper.SmsConfigMapper;
import com.pavis.admin.system.model.entity.SmsConfigDO;
import com.pavis.admin.system.model.query.SmsConfigQuery;
import com.pavis.admin.system.model.req.SmsConfigReq;
import com.pavis.admin.system.model.resp.SmsConfigResp;
import com.pavis.admin.system.service.SmsConfigService;
import top.continew.starter.core.validation.CheckUtils;
import top.continew.starter.extension.crud.service.BaseServiceImpl;

import java.util.List;

/**
 * 短信配置业务实现
 *
 * 
 * @since 2025/03/15 18:41
 */
@Service
@RequiredArgsConstructor
public class SmsConfigServiceImpl extends BaseServiceImpl<SmsConfigMapper, SmsConfigDO, SmsConfigResp, SmsConfigResp, SmsConfigQuery, SmsConfigReq> implements SmsConfigService {

    @Override
    public void afterCreate(SmsConfigReq req, SmsConfigDO entity) {
        this.load(entity);
    }

    @Override
    public void afterUpdate(SmsConfigReq req, SmsConfigDO entity) {
        // 重新加载配置
        // 先卸载
        this.unload(entity.getId().toString());
        // 再加载
        this.load(entity);
    }

    @Override
    public void afterDelete(List<Long> ids) {
        for (Long id : ids) {
            this.unload(id.toString());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void setDefaultConfig(Long id) {
        SmsConfigDO smsConfig = super.getById(id);
        if (Boolean.TRUE.equals(smsConfig.getIsDefault())) {
            return;
        }
        // 启用状态才能设为默认配置
        CheckUtils.throwIfNotEqual(DisEnableStatusEnum.ENABLE, smsConfig.getStatus(), "请先启用所选配置");
        baseMapper.lambdaUpdate().eq(SmsConfigDO::getIsDefault, true).set(SmsConfigDO::getIsDefault, false).update();
        baseMapper.lambdaUpdate().eq(SmsConfigDO::getId, id).set(SmsConfigDO::getIsDefault, true).update();
    }

    @Override
    public SmsConfigDO getDefaultConfig() {
        return baseMapper.lambdaQuery()
            .eq(SmsConfigDO::getIsDefault, true)
            .eq(SmsConfigDO::getStatus, DisEnableStatusEnum.ENABLE)
            .one();
    }

    /**
     * 加载配置
     *
     * @param entity 配置信息
     */
    private void load(SmsConfigDO entity) {
        SmsConfigResp smsConfig = this.get(entity.getId());
        BaseConfig config = SmsConfigUtil.from(smsConfig);
        if (config != null) {
            SmsFactory.createSmsBlend(config);
        }
    }

    /**
     * 卸载配置
     *
     * @param configId 配置 ID
     */
    private void unload(String configId) {
        if (SmsFactory.getSmsBlend(configId) != null) {
            SmsFactory.unregister(configId);
        }
    }
}