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

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.payalipayconfig.entity.PayAlipayConfig;
import com.jeesharp.business.modules.pay.payalipayconfig.enums.PayAlipayConfigStatusEnum;
import com.jeesharp.business.modules.pay.payalipayconfig.mapper.PayAlipayConfigMapper;
import com.jeesharp.business.modules.pay.payalipayconfig.service.PayAlipayConfigService;
import com.jeesharp.business.modules.pay.payalipayconfig.vo.PayAlipayConfigQueryVO;
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.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 PayAlipayConfigServiceImpl extends CrudService<PayAlipayConfigMapper, PayAlipayConfig, PayAlipayConfigQueryVO>
        implements PayAlipayConfigService {

    private final PayChannelConfigService payChannelConfigService;

    @Override
    @Transactional(readOnly = false)
    public boolean delete(String id) {
        PayAlipayConfig 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) {
            PayAlipayConfig row = get(id);
            PayChannelTenantSupport.assertDeletableTenantCode(row != null ? row.getTenantCode() : null);
        }
        return super.batchDelete(ids);
    }

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

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

    @Override
    public PayAlipayConfig 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,
                PayAlipayConfigStatusEnum.ENABLED.getValue(),
                PayAlipayConfigStatusEnum.DISABLED.getValue());
    }
}
