package com.jeesharp.business.modules.pay.paychannelconfig.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.PayChannelConfigStatusEnum;
import com.jeesharp.business.modules.pay.paychannelconfig.mapper.PayChannelConfigMapper;
import com.jeesharp.business.modules.pay.paychannelconfig.service.PayChannelConfigService;
import com.jeesharp.business.modules.pay.paychannelconfig.support.PayChannelTenantSupport;
import com.jeesharp.business.modules.pay.paychannelconfig.vo.PayChannelConfigQueryVO;
import com.jeesharp.framework.exception.BizErrorException;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 支付渠道公共配置领域服务实现。
 *
 * @author jeesharp
 * @since 2026-05-25
 */
@Service
@Transactional(readOnly = true)
public class PayChannelConfigServiceImpl extends CrudService<PayChannelConfigMapper, PayChannelConfig, PayChannelConfigQueryVO>
        implements PayChannelConfigService {

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

    @Override
    public PayChannelConfig findActiveByChannelCode(String channelCode, String tenantId) {
        if (StrUtil.isBlank(channelCode)) {
            return null;
        }
        String code = channelCode.trim();
        String tid = PayChannelTenantSupport.normalize(tenantId);
        PayChannelConfig cfg = null;
        if (tid != null) {
            cfg = getBaseMapper().findActiveByChannelCodeAndTenant(code, tid);
        }
        if (cfg == null) {
            cfg = getBaseMapper().findActivePlatformByChannelCode(code);
        }
        return cfg;
    }

    @Override
    public PayChannelConfig findByChannelAndConfigCode(String channelCode, String configCode, String tenantId) {
        if (StrUtil.isBlank(channelCode) || StrUtil.isBlank(configCode)) {
            return null;
        }
        String tid = PayChannelTenantSupport.normalize(tenantId);
        if (tid != null) {
            PayChannelConfig tenantRow = getBaseMapper().findByChannelConfigCodeAndTenant(
                    channelCode.trim(), configCode.trim(), tid);
            if (tenantRow != null) {
                return tenantRow;
            }
        }
        return getBaseMapper().findByChannelConfigCodePlatform(channelCode.trim(), configCode.trim());
    }

    @Override
    @Transactional(readOnly = false)
    public void disableOtherActiveExcept(String channelCode, String tenantId, String keepId) {
        if (StrUtil.isBlank(channelCode)) {
            return;
        }
        getBaseMapper().disableOtherActiveExcept(
                channelCode.trim(),
                PayChannelTenantSupport.normalize(tenantId),
                keepId,
                PayChannelConfigStatusEnum.ENABLED.getValue(),
                PayChannelConfigStatusEnum.DISABLED.getValue());
    }

    @Override
    @Transactional(readOnly = false)
    public PayChannelConfig createPayChannelConfig(PayChannelConfig entity) {
        if (StrUtil.isNotBlank(entity.getId())) {
            throw new BizErrorException("新增不应携带主键");
        }
        if (entity.getStatus() == null) {
            entity.setStatus(PayChannelConfigStatusEnum.ENABLED.getValue());
        }
        if (entity.getSort() == null) {
            entity.setSort(0);
        }
        if (PayChannelConfigStatusEnum.ENABLED.getValue().equals(entity.getStatus())) {
            disableOtherActiveExcept(entity.getChannelCode(), entity.getTenantId(), null);
        }
        save(entity);
        return entity;
    }

    @Override
    @Transactional(readOnly = false)
    public void updatePayChannelConfig(PayChannelConfig entity) {
        if (StrUtil.isBlank(entity.getId())) {
            throw new BizErrorException("更新必须携带主键");
        }
        PayChannelConfig existing = get(entity.getId());
        if (existing == null) {
            throw new BizErrorException("配置不存在");
        }
        if (PayChannelTenantSupport.isPlatformTenantCode(existing.getTenantCode())) {
            existing.setNotifyBaseUrl(entity.getNotifyBaseUrl());
            existing.setPayCenterNotifyPath(entity.getPayCenterNotifyPath());
            getBaseMapper().update(existing);
            return;
        }
        if (entity.getStatus() == null) {
            entity.setStatus(PayChannelConfigStatusEnum.ENABLED.getValue());
        }
        if (entity.getSort() == null) {
            entity.setSort(0);
        }
        if (PayChannelConfigStatusEnum.ENABLED.getValue().equals(entity.getStatus())) {
            disableOtherActiveExcept(entity.getChannelCode(), entity.getTenantId(), entity.getId());
        }
        getBaseMapper().update(entity);
    }
}
