package org.dromara.main.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.main.domain.PaymentConfig;
import org.dromara.main.mapper.PaymentConfigMapper;
import org.dromara.main.service.IPaymentConfigService;
import org.dromara.system.domain.bo.PaymentConfigBo;
import org.dromara.system.domain.vo.PaymentConfigVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class PaymentConfigServiceImpl implements IPaymentConfigService {

    private final PaymentConfigMapper paymentConfigMapper;

    @Override
    public PaymentConfig getEnabledAlipayConfig() {
        return getEnabledReceiveConfig();
    }

    @Override
    public PaymentConfig getEnabledReceiveConfig() {
        return paymentConfigMapper.selectOne(
            Wrappers.<PaymentConfig>lambdaQuery()
                .eq(PaymentConfig::getConfigType, 1)
                .eq(PaymentConfig::getPaymentType, 1)
                .eq(PaymentConfig::getIsEnabled, 1)
                .orderByDesc(PaymentConfig::getCreateTime)
                .last("limit 1")
        );
    }

    @Override
    public PaymentConfig getEnabledTransferConfig() {
        return paymentConfigMapper.selectOne(
            Wrappers.<PaymentConfig>lambdaQuery()
                .eq(PaymentConfig::getConfigType, 2)
                .eq(PaymentConfig::getPaymentType, 1)
                .eq(PaymentConfig::getIsEnabled, 1)
                .orderByDesc(PaymentConfig::getCreateTime)
                .last("limit 1")
        );
    }

    @Override
    public PaymentConfig getEnabledWechatConfig() {
        return paymentConfigMapper.selectOne(
            Wrappers.<PaymentConfig>lambdaQuery()
                .eq(PaymentConfig::getConfigType, 1)
                .eq(PaymentConfig::getPaymentType, 2)
                .eq(PaymentConfig::getIsEnabled, 1)
                .orderByDesc(PaymentConfig::getCreateTime)
                .last("limit 1")
        );
    }

    @Override
    public PaymentConfig getEnabledWechatTransferConfig() {
        return paymentConfigMapper.selectOne(
            Wrappers.<PaymentConfig>lambdaQuery()
                .eq(PaymentConfig::getConfigType, 2)
                .eq(PaymentConfig::getPaymentType, 2)
                .eq(PaymentConfig::getIsEnabled, 1)
                .orderByDesc(PaymentConfig::getCreateTime)
                .last("limit 1")
        );
    }

    @Override
    public PaymentConfigVo getWechatPayConfig() {
        PaymentConfig config = getOrCreateWechatConfig();
        PaymentConfigVo vo = new PaymentConfigVo();
        vo.setAppId(config.getAppId());
        vo.setMchId(config.getMchId());
        vo.setApiV3Key(config.getApiV3Key());
        vo.setSerialNo(config.getSerialNo());
        vo.setPrivateKeyPath(config.getPrivateKeyPath());
        vo.setCertPath(config.getCertPath());
        vo.setNotifyUrl(config.getPayNotifyUrl());
        vo.setPublicKeyId(config.getPublicKeyId());
        vo.setPrivateKeyUploaded(config.getPrivateKeyPath() != null && !config.getPrivateKeyPath().isEmpty());
        vo.setCertUploaded(config.getCertPath() != null && !config.getCertPath().isEmpty());
        vo.setPublicKeyUploaded(config.getPublicKeyPath() != null && !config.getPublicKeyPath().isEmpty());
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWechatPayConfig(PaymentConfigBo bo) {
        PaymentConfig config = getOrCreateWechatConfig();
        config.setAppId(bo.getAppId());
        config.setMchId(bo.getMchId());
        config.setApiV3Key(bo.getApiV3Key());
        config.setSerialNo(bo.getSerialNo());
        config.setPrivateKeyPath(bo.getPrivateKeyPath());
        config.setCertPath(bo.getCertPath());
        config.setPayNotifyUrl(bo.getNotifyUrl());
        config.setPublicKeyId(bo.getPublicKeyId());
        saveOrUpdate(config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWechatPrivateKeyPath(String privateKeyPath) {
        PaymentConfig config = getOrCreateWechatConfig();
        config.setPrivateKeyPath(privateKeyPath);
        saveOrUpdate(config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWechatCertPath(String certPath) {
        PaymentConfig config = getOrCreateWechatConfig();
        config.setCertPath(certPath);
        saveOrUpdate(config);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWechatPublicKeyPath(String publicKeyPath) {
        PaymentConfig config = getOrCreateWechatConfig();
        config.setPublicKeyPath(publicKeyPath);
        saveOrUpdate(config);
    }

    private PaymentConfig getOrCreateWechatConfig() {
        PaymentConfig config = paymentConfigMapper.selectOne(
            Wrappers.<PaymentConfig>lambdaQuery()
                .eq(PaymentConfig::getConfigType, 1)
                .eq(PaymentConfig::getPaymentType, 2)
                .orderByDesc(PaymentConfig::getCreateTime)
                .last("limit 1")
        );
        if (config != null) {
            return config;
        }
        PaymentConfig add = new PaymentConfig();
        add.setConfigName("微信小程序支付配置");
        add.setConfigType(1);
        add.setPaymentType(2);
        add.setIsEnabled(1);
        paymentConfigMapper.insert(add);
        return add;
    }

    private void saveOrUpdate(PaymentConfig config) {
        if (config.getId() == null) {
            paymentConfigMapper.insert(config);
            return;
        }
        paymentConfigMapper.updateById(config);
    }
}
