package com.xzx.gc.user.service; import cn.hutool.core.convert.Convert; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.json.JSONUtil; import com.xzx.gc.common.constant.CommonEnum; import com.xzx.gc.common.constant.Constants; import com.xzx.gc.common.constant.OrderEnum; import com.xzx.gc.common.utils.BusinessUtil; import com.xzx.gc.entity.AccountInfo; import com.xzx.gc.entity.AccountLog; import com.xzx.gc.entity.PartnerAccount; import com.xzx.gc.entity.PartnerAccountLog; import com.xzx.gc.model.comon.account.AllAcountParamDto; import com.xzx.gc.service.BaseAccountService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.Iterator; import java.util.List; @Service @Transactional @Slf4j public class LimitChangeService extends BaseAccountService { @Autowired private AccountService accountService; @Autowired private BusinessUtil businessUtil; @Autowired private PartnerAccountLogService partnerAccountLogService; @Autowired private PartnerAccountService partnerAccountService; @Autowired private AccountLogService accountLogService; @Override public Object account(AllAcountParamDto allAcountParamDto) { String now=DateUtil.now(); //计算额度之间的差额 BigDecimal overBalance=BigDecimal.ZERO; for (Iterator iterator = allAcountParamDto.getAccountInfoList().iterator(); iterator.hasNext(); ) { AccountInfo accountInfo1=iterator.next(); AccountInfo accountInfo = accountService.findById(accountInfo1.getAccountId()); BigDecimal overdraftLimit = Convert.toBigDecimal(accountInfo1.getOverdraftLimit(),Constants.MONEY_INIT); BigDecimal fixLimit = Convert.toBigDecimal(accountInfo1.getFixedLimit(),Constants.MONEY_INIT); BigDecimal oldOverdraftLimit = Convert.toBigDecimal(accountInfo.getOverdraftLimit(), Constants.MONEY_INIT); BigDecimal oldFixLimit = Convert.toBigDecimal(accountInfo.getFixedLimit(), Constants.MONEY_INIT); BigDecimal sub=NumberUtil.sub(oldOverdraftLimit,overdraftLimit); BigDecimal fix=NumberUtil.sub(oldFixLimit,fixLimit); //无任何额度改变 if(sub.compareTo(BigDecimal.ZERO)==0){ iterator.remove(); continue; } accountInfo1.setOldFixLimit(oldFixLimit); accountInfo1.setOldOverdraftLimit(oldOverdraftLimit); accountInfo1.setOverBalance(sub); accountInfo1.setOverFix(fix); overBalance=NumberUtil.add(overBalance,sub); } //总的差额 allAcountParamDto.setOverBlance(overBalance); log.debug("准备进行额度管理操作:{}",JSONUtil.toJsonPrettyStr(allAcountParamDto)); //打包员额度管理 打包员账户增加或减少,打包站增加或减少,先增减额度再增减余额 List accountInfoList = allAcountParamDto.getAccountInfoList(); BigDecimal overBlance = allAcountParamDto.getOverBlance(); String partnerAccountId = allAcountParamDto.getPartnerAccountId(); for (AccountInfo info : accountInfoList) { accountService.updateOverLimitByAccountId( info.getAccountId(),Convert.toBigDecimal(info.getOverdraftLimit())); accountService.updateFixLimitByAccountId( info.getAccountId(),Convert.toBigDecimal(info.getFixedLimit())); AccountLog accountLog = new AccountLog(); accountLog.setCreateTime(now); accountLog.setAccountId(info.getAccountId()); accountLog.setCreateUserId(info.getUserId()); overBalance = info.getOverBalance(); BigDecimal overFix = info.getOverFix(); if(overBalance.compareTo(BigDecimal.ZERO)>0) { accountLog.setChannelType(Convert.toShort(CommonEnum.额度降低.getValue())); }else if(overBalance.compareTo(BigDecimal.ZERO)<0){ accountLog.setChannelType(Convert.toShort(CommonEnum.额度申请.getValue())); } if(overFix.compareTo(BigDecimal.ZERO)!=0){ accountLog.setOldLimit(businessUtil.changeMoney(info.getOldOverdraftLimit())); accountLog.setNewLimit(businessUtil.changeMoney(info.getOverdraftLimit())); accountLog.setOldFixedLimit(businessUtil.changeMoney(info.getOldFixLimit())); accountLog.setNewFixedLimit(businessUtil.changeMoney(info.getFixedLimit())); }else if(overBalance.compareTo(BigDecimal.ZERO)!=0){ accountLog.setOldLimit(businessUtil.changeMoney(info.getOldOverdraftLimit())); accountLog.setNewLimit(businessUtil.changeMoney(info.getOverdraftLimit())); } accountLog.setOrderId(partnerAccountId); accountLogService.add(accountLog); } //修改打包站账户 先扣额度再扣环保金 PartnerAccountLog partnerAccountLog1=partnerAccountService.updateAll(partnerAccountId, overBlance); PartnerAccountLog partnerAccountLog = new PartnerAccountLog(); partnerAccountLog.setAccountId(partnerAccountId); partnerAccountLog.setFlowNo(partnerAccountId); partnerAccountLog.setType(OrderEnum.额度调整.getValue()); partnerAccountLog.setMoney(partnerAccountLog1.getMoney()); partnerAccountLog.setManageLimit(partnerAccountLog1.getManageLimit()); partnerAccountLog.setManageLimitFix(partnerAccountLog1.getManageLimitFix()); PartnerAccount partnerAccount=new PartnerAccount(); partnerAccount.setHbb(partnerAccountLog1.getAccountMoney()); partnerAccount.setOverdraftLimit(partnerAccountLog1.getOldLimit()); partnerAccount.setFixedLimit(partnerAccountLog1.getOldLimitFix()); partnerAccountLog.setPartnerAccount(partnerAccount); partnerAccountLogService.add(partnerAccountLog); log.debug("额度管理操作已完成"); return null; } }