cloudroam
2024-10-23 b4aa242d1b818a8ad3a285166caedb699bbcb460
src/main/java/com/mzl/flower/service/impl/wallet/WalletServiceImpl.java
@@ -1,10 +1,21 @@
package com.mzl.flower.service.impl.wallet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.response.supplier.SupplierDTO;
import com.mzl.flower.entity.coupon.CouponTemplateDO;
import com.mzl.flower.entity.wallet.WalletDO;
import com.mzl.flower.enums.TrueOrFalseEnum;
import com.mzl.flower.mapper.wallet.WalletMapper;
import com.mzl.flower.service.supplier.SupplierService;
import com.mzl.flower.service.wallet.WalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
/**
 * <p>
@@ -17,4 +28,54 @@
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, WalletDO> implements WalletService {
    @Autowired
    private SupplierService supplierService;
    @Override
    public WalletDO getBySupplierId() {
        final SupplierDTO currentSupplier = supplierService.getCurrentSupplier();
        // 根据供应商ID获取钱包信息
            // 查询的条件的deleted的字段得为0
            // 下面的单独封装成一个方法
            WalletDO walletDO = getBySupplierId(currentSupplier.getId());
            if(null==walletDO){
                // 先创建一个钱包
                walletDO=new WalletDO();
                walletDO.setUserId(SecurityUtils.getUserId());
                walletDO.setSupplierId(currentSupplier.getId());
                walletDO.setTotalAmount(BigDecimal.ZERO);
                walletDO.setWithdrawableAmount(BigDecimal.ZERO);
                //把所有涉及BigDecimal的金额都设置为0
                walletDO.setWithdrawingAmount(BigDecimal.ZERO);
                walletDO.setWithdrawnAmount(BigDecimal.ZERO);
                walletDO.setSettlingAmount(BigDecimal.ZERO);
                walletDO.setTotalDeduction(BigDecimal.ZERO);
                walletDO.setTotalTransactionAmount(BigDecimal.ZERO);
                walletDO.create(SecurityUtils.getUserId());
                baseMapper.insert(walletDO);
                // 将再次查询的结果返回
                walletDO=getBySupplierId(currentSupplier.getId());
            }
        return walletDO;
    }
    public WalletDO getBySupplierId(Long supplierId){
        // 不用getOne,用获取list的第一个元素
        List<WalletDO> walletDOS = baseMapper.selectList(new LambdaQueryWrapper<WalletDO>()
                .eq(WalletDO::getDeleted, TrueOrFalseEnum.FALSE.isFlag())
                .eq(WalletDO::getSupplierId, supplierId));
        if(!CollectionUtils.isEmpty(walletDOS)){
            return walletDOS.get(0);
        }else return null;
    }
}