package com.jeesharp.business.modules.pay.paypufaconfig.service.impl;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.paychannelconfig.entity.PayChannelConfig;
import com.jeesharp.business.modules.pay.paychannelconfig.enums.PayChannelCodeEnum;
import com.jeesharp.business.modules.pay.paychannelconfig.service.PayChannelConfigService;
import com.jeesharp.business.modules.pay.paychannelconfig.support.PayChannelConfigMergeSupport;
import com.jeesharp.business.modules.pay.paychannelconfig.support.PayChannelTenantSupport;
import com.jeesharp.business.modules.pay.paypufaconfig.entity.PayPufaConfig;
import com.jeesharp.business.modules.pay.paypufaconfig.enums.PayPufaConfigStatusEnum;
import com.jeesharp.business.modules.pay.paypufaconfig.mapper.PayPufaConfigMapper;
import com.jeesharp.business.modules.pay.paypufaconfig.service.PayPufaConfigService;
import com.jeesharp.business.modules.pay.paypufaconfig.vo.PayPufaConfigQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 浦发配置领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-25
 */
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class PayPufaConfigServiceImpl extends CrudService<PayPufaConfigMapper, PayPufaConfig, PayPufaConfigQueryVO>
        implements PayPufaConfigService {

    private final PayChannelConfigService payChannelConfigService;

    @Override
    @Transactional(readOnly = false)
    public boolean delete(String id) {
        PayPufaConfig row = get(id);
        PayChannelTenantSupport.assertDeletableTenantCode(row != null ? row.getTenantCode() : null);
        return super.delete(id);
    }

    @Override
    @Transactional(readOnly = false)
    public boolean batchDelete(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return true;
        }
        for (String id : ids) {
            PayPufaConfig row = get(id);
            PayChannelTenantSupport.assertDeletableTenantCode(row != null ? row.getTenantCode() : null);
        }
        return super.batchDelete(ids);
    }

    @Override
    public PayPufaConfig findActive(String tenantId) {
        String tid = PayChannelTenantSupport.normalize(tenantId);
        PayChannelConfig channel = payChannelConfigService.findActiveByChannelCode(
                PayChannelCodeEnum.PFA.getCode(), tid);
        PayPufaConfig pufa = null;
        if (channel != null && StrUtil.isNotBlank(channel.getId())) {
            pufa = getBaseMapper().findByChannelConfigId(channel.getId());
        }
        if (pufa == null) {
            if (tid != null) {
                pufa = getBaseMapper().findActiveByTenantId(tid);
            }
            if (pufa == null) {
                pufa = getBaseMapper().findActivePlatform();
            }
            if (channel == null) {
                channel = payChannelConfigService.findActiveByChannelCode(
                        PayChannelCodeEnum.PFA.getCode(), tid);
            }
        }
        return PayChannelConfigMergeSupport.mergePufaNotify(pufa, channel);
    }

    @Override
    public PayPufaConfig findByConfigCode(String configCode, String tenantId) {
        if (StrUtil.isBlank(configCode)) {
            return null;
        }
        String tid = PayChannelTenantSupport.normalize(tenantId);
        PayPufaConfig row = null;
        if (tid != null) {
            row = getBaseMapper().findByConfigCodeAndTenant(configCode.trim(), tid);
        }
        if (row == null) {
            row = getBaseMapper().findByConfigCode(configCode.trim());
        }
        return row;
    }

    @Override
    public PayPufaConfig findByChannelConfigId(String channelConfigId) {
        if (StrUtil.isBlank(channelConfigId)) {
            return null;
        }
        return getBaseMapper().findByChannelConfigId(channelConfigId.trim());
    }

    @Override
    @Transactional(readOnly = false)
    public void disableOtherActiveExcept(String tenantId, String keepId) {
        getBaseMapper().disableOtherActiveExcept(
                PayChannelTenantSupport.normalize(tenantId),
                keepId,
                PayPufaConfigStatusEnum.ENABLED.getValue(),
                PayPufaConfigStatusEnum.DISABLED.getValue());
    }
}
