package com.ruoyi.cai.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cai.domain.PayConfig;
import com.ruoyi.cai.mapper.PayConfigMapper;
import com.ruoyi.cai.pay.PayConfigManager;
import com.ruoyi.cai.pay.PayTypeEnum;
import com.ruoyi.cai.service.PayConfigService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 支付配置Service业务层处理
 *
 * @author 77
 * @date 2024-01-20
 */
@Service
public class PayConfigServiceImpl extends ServiceImpl<PayConfigMapper,PayConfig> implements PayConfigService {

    @Autowired
    private PayConfigManager payConfigManager;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addPayConfig(PayConfig payConfig){
        if(payConfig.getPayType().equals(PayTypeEnum.ALI.getCode())){
            boolean exists = this.exists(Wrappers.lambdaQuery(PayConfig.class)
                    .eq(PayConfig::getAppid, payConfig.getAppid()));
            if(exists){
                throw new ServiceException("检测到已经存在的支付宝APPID，无法重复添加");
            }
        }
        if(payConfig.getPayType().equals(PayTypeEnum.WX.getCode())){
            boolean exists = this.exists(Wrappers.lambdaQuery(PayConfig.class)
                    .eq(PayConfig::getWxMcid, payConfig.getWxMcid()));
            if(exists){
                throw new ServiceException("检测到已经存在的微信商户ID，无法重复添加");
            }
        }
        boolean save = this.save(payConfig);
        if(save){
            payConfigManager.addPayConfig(payConfig);
        }
        return save;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePayConfig(PayConfig payConfig){
        if(payConfig.getPayType().equals(PayTypeEnum.ALI.getCode())){
            boolean exists = this.exists(Wrappers.lambdaQuery(PayConfig.class)
                    .eq(PayConfig::getAppid, payConfig.getAppid())
                    .ne(PayConfig::getId,payConfig.getId()));
            if(exists){
                throw new ServiceException("检测到已经存在的支付宝APPID，无法重复添加");
            }
        }
        if(payConfig.getPayType().equals(PayTypeEnum.WX.getCode())){
            boolean exists = this.exists(Wrappers.lambdaQuery(PayConfig.class)
                    .eq(PayConfig::getWxMcid, payConfig.getWxMcid())
                    .ne(PayConfig::getId,payConfig.getId()));
            if(exists){
                throw new ServiceException("检测到已经存在的微信商户ID，无法重复添加");
            }
        }
        boolean update = this.updateById(payConfig);
        if(update){
            payConfig = this.getById(payConfig.getId());
            payConfigManager.updatePayConfig(payConfig);
        }
        return update;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deletePayConfig(Long id){
        PayConfig payConfig = this.getById(id);
        if(payConfig == null){
            return false;
        }
        boolean b = this.removeById(id);
        if(b){
            payConfigManager.deletePayConfig(payConfig);
        }
        return b;
    }


}
