package com.xzx.gc.order.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.exception.RestException; import com.xzx.gc.entity.*; import com.xzx.gc.model.comon.account.AllAcountParamDto; import com.xzx.gc.pay.service.*; 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; @Service @Transactional @Slf4j public class LimitApplyService extends BaseAccountService { @Autowired private CityPartnerService cityPartnerService; @Autowired private OtherUserService otherUserService; @Autowired private AccountService accountService; @Autowired private PartnerAccountService partnerAccountService; @Autowired private PartnerAccountLogService partnerAccountLogService; @Autowired private AccountLogService accountLogService; @Override public Object account(AllAcountParamDto allAcountParamDto) { AccountInfo byUserId = accountService.findByUserIdForbidden(allAcountParamDto.getUserId()); if("1".equals(byUserId.getIsProhibit())){ throw new RestException(-1,"账户被禁用"); } String partnerId = otherUserService.findById(allAcountParamDto.getUserId()).getPartnerId(); CityPartner byId1 = cityPartnerService.queryById(partnerId); //1:合伙人。2:打包站 String partnerType = byId1.getPartnerType(); String accountId = byUserId.getAccountId(); allAcountParamDto.setAccountId(accountId); allAcountParamDto.setOverdraftLimit(byUserId.getOverdraftLimit()); allAcountParamDto.setFixedLimit(byUserId.getFixedLimit()); allAcountParamDto.setPartnerId(partnerId); allAcountParamDto.setPartnerType(partnerType); allAcountParamDto.setPartnerAccountId(byId1.getAccountNo()); log.debug("准备进行额度申请操作:{}",JSONUtil.toJsonPrettyStr(allAcountParamDto)); //合伙人扣钱或打包站先扣额度再扣钱 平台用户加钱 accountId = allAcountParamDto.getAccountId(); BigDecimal overdraftLimit = Convert.toBigDecimal(allAcountParamDto.getOverdraftLimit(),Constants.MONEY_INIT); BigDecimal fixLimit = Convert.toBigDecimal(allAcountParamDto.getFixedLimit(),Constants.MONEY_INIT); AccountInfo accountInfo=new AccountInfo(); accountInfo.setAccountId(accountId); accountInfo.setOverdraftLimit(Convert.toStr(NumberUtil.add(overdraftLimit,allAcountParamDto.getMoney()))); accountInfo.setFixedLimit(Convert.toStr(NumberUtil.add(fixLimit,allAcountParamDto.getMoney()))); accountInfo.setUpdateTime(DateUtil.now()); accountService.update(accountInfo); AccountLog accountLog=new AccountLog(); accountLog.setAccountId(accountId); accountLog.setCreateUserId(allAcountParamDto.getUserId()); accountLog.setChannelType(Convert.toShort(CommonEnum.额度申请.getValue())); accountLog.setOrderId(allAcountParamDto.getFlowNo()); accountLog.setOldLimit(Convert.toStr(overdraftLimit)); accountLog.setOldFixedLimit(Convert.toStr(fixLimit)); accountLog.setNewLimit(Convert.toStr(NumberUtil.add(overdraftLimit,allAcountParamDto.getMoney()))); accountLog.setNewFixedLimit(Convert.toStr(NumberUtil.add(fixLimit,allAcountParamDto.getMoney()))); accountLogService.add(accountLog); //平台操作 partnerType = allAcountParamDto.getPartnerType(); //合伙人 if("1".equals(partnerType)){ PartnerAccountLog partnerAccountLog=new PartnerAccountLog(); partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId()); partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo()); partnerAccountLog.setType(OrderEnum.额度调整.getValue()); partnerAccountLog.setMoney("-"+allAcountParamDto.getMoney().toString()); partnerAccountLogService.add(partnerAccountLog); partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(),allAcountParamDto.getMoney()); }else if("2".equals(partnerType)){ //打包站 先扣额度再扣钱 PartnerAccountLog partnerAccountLog1=partnerAccountService.updateAll(allAcountParamDto.getPartnerAccountId(), Convert.toBigDecimal("-"+allAcountParamDto.getMoney().toString())); PartnerAccountLog partnerAccountLog = new PartnerAccountLog(); partnerAccountLog.setAccountId(allAcountParamDto.getPartnerAccountId()); partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo()); 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("额度申请操作已完成:用户:{},额度:{}",allAcountParamDto.getUserId(),allAcountParamDto.getMoney().toString()); return null; } }