package com.xzx.gc.pay.service;
|
|
import cn.hutool.core.convert.Convert;
|
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.util.NumberUtil;
|
import cn.hutool.core.util.StrUtil;
|
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 RestoreLimitService 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;
|
|
|
/**
|
* 打包站标识
|
*/
|
private static final String DBZ="1";
|
|
@Override
|
public Object account(AllAcountParamDto allAcountParamDto) {
|
//额度恢复 恢复入库员的固定额度 同时要减去合伙人的金额 打包员则 需要 减去 打包站的额度(按额度优先原则 )
|
|
//打包站恢复额度则需要减去合伙人的环保币
|
|
String partnerId=null;
|
if(StrUtil.isBlank(allAcountParamDto.getUserRealType())) {
|
AccountInfo byUserId = accountService.findByUserId(allAcountParamDto.getUserId());
|
if (byUserId == null) {
|
throw new RestException(-1, "账户被禁用");
|
}
|
OtherUserInfo byId = otherUserService.findById(allAcountParamDto.getUserId());
|
partnerId = byId.getPartnerId();
|
allAcountParamDto.setFixedLimit(byUserId.getFixedLimit());
|
allAcountParamDto.setAccountId(byUserId.getAccountId());
|
allAcountParamDto.setOverdraftLimit(byUserId.getOverdraftLimit());
|
CityPartner byId1 = cityPartnerService.queryById(partnerId);
|
//1:合伙人。2:打包站
|
String partnerType = byId1.getPartnerType();
|
allAcountParamDto.setPartnerType(partnerType);
|
allAcountParamDto.setPartnerId(partnerId);
|
allAcountParamDto.setPartnerAccountId(byId1.getAccountNo());
|
|
}else if(DBZ.equals(allAcountParamDto.getUserRealType())){
|
String userId=allAcountParamDto.getUserId();
|
//根据主打包员查询所属的打包站ID
|
CityPartner byuserId = cityPartnerService.findByUserId(userId, OrderEnum.打包站.getValue());
|
if(byuserId==null){
|
throw new RestException("找不到对应的打包站");
|
}
|
partnerId=byuserId.getId()+"";
|
//查询打包站的账户
|
PartnerAccount accountInfo1 = partnerAccountService.findByPartnerId(partnerId);
|
allAcountParamDto.setFixedLimit(accountInfo1.getFixedLimit());
|
|
//打包站的账户
|
allAcountParamDto.setAccountId(accountInfo1.getAccount());
|
allAcountParamDto.setOverdraftLimit(accountInfo1.getOverdraftLimit());
|
|
CityPartner byId1 = cityPartnerService.queryById(byuserId.getPackingStation());
|
allAcountParamDto.setPartnerId(byId1.getId().toString());
|
//合伙人的账户
|
allAcountParamDto.setPartnerAccountId(byId1.getAccountNo());
|
}
|
|
|
log.debug("准备进行额度恢复操作:{}",JSONUtil.toJsonPrettyStr(allAcountParamDto));
|
//额度恢复 入库员加钱 合伙人扣钱
|
BigDecimal limit=Convert.toBigDecimal(allAcountParamDto.getOverdraftLimit(),Constants.MONEY_INIT);
|
//实际恢复的额度
|
BigDecimal realLimit=NumberUtil.sub(Convert.toBigDecimal(allAcountParamDto.getFixedLimit()),limit);
|
|
if(StrUtil.isBlank(allAcountParamDto.getUserRealType())) {
|
AccountInfo accountInfo = new AccountInfo();
|
accountInfo.setAccountId(allAcountParamDto.getAccountId());
|
accountInfo.setOverdraftLimit(allAcountParamDto.getFixedLimit());
|
accountInfo.setUpdateTime(DateUtil.now());
|
accountService.update(accountInfo);
|
|
AccountLog accountLog = new AccountLog();
|
accountLog.setAccountId(allAcountParamDto.getAccountId());
|
accountLog.setCreateUserId(allAcountParamDto.getUserId());
|
accountLog.setChannelType(Convert.toShort(CommonEnum.额度恢复.getValue()));
|
accountLog.setOrderId(allAcountParamDto.getFlowNo());
|
accountLog.setOldLimit(allAcountParamDto.getOverdraftLimit());
|
accountLog.setNewLimit(allAcountParamDto.getFixedLimit());
|
accountLog.setOldFixedLimit(accountLog.getNewLimit());
|
accountLog.setNewFixedLimit(accountLog.getNewLimit());
|
accountLogService.add(accountLog);
|
|
|
//合伙人
|
if("1".equals(allAcountParamDto.getPartnerType())){
|
PartnerAccountLog partnerAccountLog=new PartnerAccountLog();
|
partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId());
|
partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
|
partnerAccountLog.setType(OrderEnum.额度调整.getValue());
|
partnerAccountLog.setMoney("-"+realLimit.toString());
|
partnerAccountLogService.add(partnerAccountLog);
|
partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(),realLimit);
|
}else if("2".equals(allAcountParamDto.getPartnerType())){
|
//打包站 先扣额度再扣钱
|
PartnerAccountLog partnerAccountLog1=partnerAccountService.updateAll(allAcountParamDto.getPartnerAccountId(), Convert.toBigDecimal("-"+realLimit.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);
|
}
|
|
|
}else if(DBZ.equals(allAcountParamDto.getUserRealType())){
|
|
//打包站可用额度恢复
|
PartnerAccountLog partnerAccountLog = new PartnerAccountLog();
|
partnerAccountLog.setAccountId(allAcountParamDto.getAccountId());
|
partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
|
partnerAccountLog.setType(OrderEnum.恢复额度.getValue());
|
partnerAccountLog.setManageLimit(realLimit.toString());
|
PartnerAccount partnerAccount=new PartnerAccount();
|
partnerAccount.setOverdraftLimit(allAcountParamDto.getOverdraftLimit());
|
partnerAccountLog.setPartnerAccount(partnerAccount);
|
partnerAccountLogService.add(partnerAccountLog);
|
|
PartnerAccount accountInfo = new PartnerAccount();
|
accountInfo.setAccount(allAcountParamDto.getAccountId());
|
accountInfo.setOverdraftLimit(allAcountParamDto.getFixedLimit());
|
accountInfo.setUpdateTime(DateUtil.now());
|
partnerAccountService.updateByAccount(accountInfo);
|
|
|
|
//合伙人扣除环保币
|
partnerAccountLog=new PartnerAccountLog();
|
partnerAccountLog.setPartnerId(allAcountParamDto.getPartnerId());
|
partnerAccountLog.setFlowNo(allAcountParamDto.getFlowNo());
|
partnerAccountLog.setType(OrderEnum.恢复额度.getValue());
|
partnerAccountLog.setMoney("-"+realLimit.toString());
|
partnerAccountLogService.add(partnerAccountLog);
|
partnerAccountService.updateReduceMoneyByPartnerId(allAcountParamDto.getPartnerId(),realLimit);
|
}
|
|
|
|
log.debug("额度恢复操作已完成");
|
return null;
|
}
|
}
|