|
@@ -1,6 +1,8 @@
|
|
|
package com.fire.admin.service.impl;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.json.JSON;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
@@ -17,6 +19,7 @@ import com.fire.common.exception.BaseException;
|
|
|
import com.fire.common.redis.RedisAmountScript;
|
|
|
import com.fire.dto.AdditionalPayment;
|
|
|
import com.fire.dto.CustomerInfo;
|
|
|
+import com.fire.dto.TransactionFlow;
|
|
|
import com.fire.dto.enums.Status;
|
|
|
import com.fire.utils.date.DateUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
@@ -27,13 +30,19 @@ import org.springframework.stereotype.Service;
|
|
|
import redis.clients.jedis.JedisCluster;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.fire.common.constants.RocketTags.SUPPLIER_TAG;
|
|
|
+import static com.fire.common.constants.RocketTopic.TRANSACTION_TOPIC;
|
|
|
import static com.fire.common.constants.RocketTopic.UPDATE_TOPIC;
|
|
|
import static com.fire.dto.enums.RedisKey.*;
|
|
|
+import static com.fire.dto.enums.RelationType.RELATION_CUSTOMER;
|
|
|
+import static com.fire.dto.enums.ServiceType.SERVICE_FINANCE_ADD;
|
|
|
+import static com.fire.dto.enums.ServiceType.SERVICE_FINANCE_SUB;
|
|
|
|
|
|
/**
|
|
|
* @author: liuliu
|
|
@@ -45,15 +54,13 @@ import static com.fire.dto.enums.RedisKey.*;
|
|
|
@Service
|
|
|
public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentMapper, AdditionalPayment> implements AdditionalPaymentService {
|
|
|
|
|
|
- private AliyunOSSUtil aliyunOSSUtil;
|
|
|
private RedisAmountScript redisAmountScript;
|
|
|
private CustomerService customerService;
|
|
|
private JedisCluster jedisCluster;
|
|
|
private RocketMQTemplate rocketMQTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
- public AdditionalPaymentServiceImpl(AliyunOSSUtil aliyunOSSUtil, RedisAmountScript redisAmountScript, CustomerService customerService, JedisCluster jedisCluster, RocketMQTemplate rocketMQTemplate) {
|
|
|
- this.aliyunOSSUtil = aliyunOSSUtil;
|
|
|
+ public AdditionalPaymentServiceImpl(RedisAmountScript redisAmountScript, CustomerService customerService, JedisCluster jedisCluster, RocketMQTemplate rocketMQTemplate) {
|
|
|
this.redisAmountScript = redisAmountScript;
|
|
|
this.customerService = customerService;
|
|
|
this.jedisCluster = jedisCluster;
|
|
@@ -69,15 +76,34 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
*/
|
|
|
@Override
|
|
|
public void insertAdditionalPaymen(AdditionalPaymentDto additionalPaymentDto) {
|
|
|
+ // TODO 获取当前操作的人的用户名
|
|
|
+ String username = SecurityUtil.getUser().getUsername();
|
|
|
+
|
|
|
if (additionalPaymentDto.getType().equals("customer")) {
|
|
|
additionalPaymentDto.setId(jedisCluster.incr(GLOBAL_ID_INCR.key()));
|
|
|
additionalPaymentDto.setDistinguish(2);
|
|
|
+
|
|
|
// TODO 客户加款
|
|
|
- int count = this.additionalPayment(additionalPaymentDto);
|
|
|
+ int count = this.additionalPayment(additionalPaymentDto, username);
|
|
|
+
|
|
|
+ // TODO 构建流水实体对象,并转换为 json
|
|
|
+ JSONObject param = JSONUtil.parseObj(createTransactionFlow(
|
|
|
+ Long.parseLong(jedisCluster.hget(CUSTOMER_AMOUNT.key(), CUSTOMER_AMOUNT.key().concat(additionalPaymentDto.getRelationId()))),
|
|
|
+ additionalPaymentDto.getAmount().multiply(new BigDecimal(10000)).longValue(),
|
|
|
+ RELATION_CUSTOMER.type(),
|
|
|
+ Long.parseLong(additionalPaymentDto.getRelationId()),
|
|
|
+ DateUtils.strformatDatetime(LocalDateTime.now()).concat(" ".concat(username).concat(" 为客户".concat(additionalPaymentDto.getCustomerName())).concat("加款,加款金额为:")).concat(additionalPaymentDto.getAmount().setScale(4, RoundingMode.DOWN).toString()),
|
|
|
+ additionalPaymentDto.getCustomerName(),
|
|
|
+ username,
|
|
|
+ SERVICE_FINANCE_ADD.type()
|
|
|
+ ));
|
|
|
// TODO 加款操作
|
|
|
Long amount = redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(), Long.parseLong(additionalPaymentDto.getRelationId()), "add", additionalPaymentDto.getAmount().multiply(new BigDecimal(10000)).longValue(), 0L);
|
|
|
+
|
|
|
if (count > 0 && ObjectUtil.isNotEmpty(amount)) {
|
|
|
- log.info("客户加款成功,客户编号为:【{}】,加款金额为:【{}】", additionalPaymentDto.getRelationId(), additionalPaymentDto.getAmount());
|
|
|
+ log.info("客户加款成功,客户编号为:【{}】,客户名称为:【{}】,加款金额为:【{}】", additionalPaymentDto.getRelationId(),param.get("name"), additionalPaymentDto.getAmount());
|
|
|
+ log.info("加款入流水队列,加款之前的金额为:【{}】,操作金额为:【{}】,加款之后的金额为:【{}】,详情为:【{}】", new BigDecimal(param.get("beforeAmount").toString()).divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN), additionalPaymentDto.getAmount(), new BigDecimal(param.get("afterAmount").toString()).divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN), param);
|
|
|
+ rocketMQTemplate.syncSend(TRANSACTION_TOPIC, MessageBuilder.withPayload(param).build());
|
|
|
} else {
|
|
|
// TODO 客户加款失败,删除该条加款记录
|
|
|
log.info("加款失败,加款编号为:【{}】将删除此条记录,客户编号为:【{}】,加款金额为:【{}】", additionalPaymentDto.getId(), additionalPaymentDto.getRelationId(), additionalPaymentDto.getAmount());
|
|
@@ -87,7 +113,7 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
additionalPaymentDto.setId(jedisCluster.incr(GLOBAL_ID_INCR.key()));
|
|
|
additionalPaymentDto.setDistinguish(1);
|
|
|
// 供应商加款
|
|
|
- int count = this.additionalPayment(additionalPaymentDto);
|
|
|
+ int count = this.additionalPayment(additionalPaymentDto, username);
|
|
|
Long amount = redisAmountScript.changeAmount(SUPPLIER_AMOUNT.key(), Long.parseLong(additionalPaymentDto.getRelationId()), "add", additionalPaymentDto.getAmount().multiply(new BigDecimal(10000)).longValue(), 0L);
|
|
|
if (count > 0 && ObjectUtil.isNotEmpty(amount)) {
|
|
|
rocketMQTemplate.send(UPDATE_TOPIC + ":" + SUPPLIER_TAG, MessageBuilder.withPayload(SUPPLIER_TAG).build());
|
|
@@ -101,6 +127,48 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /***
|
|
|
+ *@description: TODO 构建流水对象
|
|
|
+ * @param beforeAmount: 供应商可用金额
|
|
|
+ * @param amount : 加款或者撤销的金额
|
|
|
+ * @param distinguish : 1、客户;2、供应商
|
|
|
+ * @param relationId : 供应商或者客户编号
|
|
|
+ * @param note : 描述
|
|
|
+ * @param name : 客户或者供应商名称
|
|
|
+ *@return: com.fire.dto.TransactionFlow
|
|
|
+ *@author: liuliu
|
|
|
+ *@date: 2021-07-07 16:28
|
|
|
+ */
|
|
|
+ private TransactionFlow createTransactionFlow(Long beforeAmount, Long amount, Integer distinguish, Long relationId, String note, String name, String username, Integer serviceType) {
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotEmpty(amount) && amount > 0) {
|
|
|
+ Long afterAmount = null;
|
|
|
+ // TODO 加款后金额的计算
|
|
|
+ if (serviceType.equals(SERVICE_FINANCE_ADD.type())) {
|
|
|
+ afterAmount = new BigDecimal(beforeAmount).add(new BigDecimal(amount)).longValue();
|
|
|
+ }
|
|
|
+ // TODO 款项册小后金额的计算
|
|
|
+ if (serviceType.equals(SERVICE_FINANCE_SUB.type())) {
|
|
|
+ afterAmount = new BigDecimal(beforeAmount).add(new BigDecimal(Math.negateExact(amount))).longValue();
|
|
|
+ }
|
|
|
+ TransactionFlow flow = TransactionFlow.builder().seqNo(jedisCluster.incr(GLOBAL_ID_INCR.key()))
|
|
|
+ .createTime(new Date())
|
|
|
+ .serviceType(serviceType)
|
|
|
+ .beforeAmount(beforeAmount)
|
|
|
+ .afterAmount(afterAmount)
|
|
|
+ .operatingAmount(amount)
|
|
|
+ .distinguish(distinguish)
|
|
|
+ .relationId(relationId)
|
|
|
+ .note(note)
|
|
|
+ .orderId(null)
|
|
|
+ .name(name)
|
|
|
+ .operatorName(username)
|
|
|
+ .build();
|
|
|
+ return flow;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* @Description: TODO 客户和供应商加款共用和方法
|
|
|
* @Param: [additionalPaymentDto]
|
|
@@ -108,7 +176,7 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
* @Author: liuliu
|
|
|
* @Date: 2021/6/7 16:12
|
|
|
*/
|
|
|
- private int additionalPayment(AdditionalPaymentDto additionalPaymentDto) {
|
|
|
+ private int additionalPayment(AdditionalPaymentDto additionalPaymentDto, String username) {
|
|
|
|
|
|
if (ObjectUtil.isNotEmpty(additionalPaymentDto.getType()) && ObjectUtil.isNotEmpty(additionalPaymentDto.getRelationId())) {
|
|
|
AdditionalPayment payment = AdditionalPayment.builder()
|
|
@@ -116,7 +184,7 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
.cardNo(additionalPaymentDto.getCardNo())
|
|
|
.bankDeposit(additionalPaymentDto.getBankDeposit())
|
|
|
.account(additionalPaymentDto.getAccount())
|
|
|
- .payer(SecurityUtil.getUser().getUsername())
|
|
|
+ .payer(username)
|
|
|
.amount(additionalPaymentDto.getAmount().multiply(new BigDecimal(10000)).longValue())
|
|
|
.certificate(additionalPaymentDto.getCertificate())
|
|
|
.time(DateUtils.strformatDatetime(LocalDateTime.now()))
|
|
@@ -203,7 +271,8 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
*/
|
|
|
@Override
|
|
|
public String cancelAdditionalPayment(AdditionalPaymentDto additionalPaymentDto) {
|
|
|
-
|
|
|
+ // TODO 账户册小操作人
|
|
|
+ String username = SecurityUtil.getUser().getUsername();
|
|
|
log.info("撤销参数为:【{}】", JSONUtil.parseObj(additionalPaymentDto));
|
|
|
AdditionalPayment payment = this.formatAdditionalPaymentDto(additionalPaymentDto);
|
|
|
// TODO 客户的撤销
|
|
@@ -214,13 +283,28 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
if (additionalPayment.getId().equals(payment.getId())) {
|
|
|
// TODO 获取客户授信金额
|
|
|
CustomerInfo customerInfo = customerService.getcustomerOne(Long.parseLong(payment.getRelationId()));
|
|
|
+ // TODO 构建撤销的对象
|
|
|
+ JSONObject param = JSONUtil.parseObj(createTransactionFlow(
|
|
|
+ Long.parseLong(jedisCluster.hget(CUSTOMER_AMOUNT.key(), CUSTOMER_AMOUNT.key().concat(payment.getRelationId()))),
|
|
|
+ payment.getAmount(),
|
|
|
+ RELATION_CUSTOMER.type(),
|
|
|
+ Long.parseLong(payment.getRelationId()),
|
|
|
+ DateUtils.strformatDatetime(LocalDateTime.now()).concat(" ".concat(username).concat(" 为客户".concat(payment.getRelationId())).concat("款项撤销,撤销金额为:")).concat(additionalPaymentDto.getAmount().divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN).toString()),
|
|
|
+ additionalPaymentDto.getCustomerName(),
|
|
|
+ username,
|
|
|
+ SERVICE_FINANCE_SUB.type()
|
|
|
+ ));
|
|
|
|
|
|
// TODO 进行账户撤销
|
|
|
Long amount = redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(), Long.parseLong(payment.getRelationId()), "sub", payment.getAmount(), customerInfo.getCreditAmount());
|
|
|
if (ObjectUtil.isNotEmpty(amount)) {
|
|
|
// TODO 修改记录的撤销状态
|
|
|
baseMapper.updateById(payment);
|
|
|
- log.info("客户款项撤销。 客户编号为:【{}】 撤销金额为:【{}】 ,授信额度为:【{}】 剩余额度为:【{}】", payment.getRelationId(), additionalPaymentDto.getAmount(), new BigDecimal(customerInfo.getCreditAmount()).divide(new BigDecimal(10000), 2, BigDecimal.ROUND_DOWN), new BigDecimal(amount).divide(new BigDecimal(10000), 2, BigDecimal.ROUND_DOWN));
|
|
|
+ // TODO 获取撤销的具体金额
|
|
|
+ BigDecimal canceprice = additionalPaymentDto.getAmount().divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN);
|
|
|
+ log.info("客户款项撤销入流水队列,撤销之前的金额为:【{}】,操作金额为:【{}】,撤销之后的金额为:【{}】,详情为:【{}】", new BigDecimal(param.get("beforeAmount").toString()).divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN), canceprice, new BigDecimal(param.get("afterAmount").toString()).divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN), param);
|
|
|
+ log.info("客户款项撤销。 客户编号为:【{}】,客户名称为:【{}】 撤销金额为:【{}】 ,授信额度为:【{}】 剩余额度为:【{}】", payment.getRelationId(),param.get("name"), canceprice, new BigDecimal(customerInfo.getCreditAmount()).divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN), new BigDecimal(amount).divide(new BigDecimal(10000)).setScale(4, RoundingMode.DOWN));
|
|
|
+ rocketMQTemplate.syncSend(TRANSACTION_TOPIC, MessageBuilder.withPayload(param).build());
|
|
|
return payment.getRelationId().concat("_").concat(amount.toString());
|
|
|
} else {
|
|
|
// TODO 撤销失败的处理
|
|
@@ -231,7 +315,6 @@ public class AdditionalPaymentServiceImpl extends ServiceImpl<AdditionalPaymentM
|
|
|
log.info("撤销失败,撤销不能撤销当前客户最近一次的加款记录。如果非要撤销请先加款一次再撤销本次记录");
|
|
|
throw new BaseException("577", "撤销失败");
|
|
|
}
|
|
|
-
|
|
|
} else if (ObjectUtil.isNotEmpty(additionalPaymentDto.getRelationId()) && additionalPaymentDto.getType().equals("supplier")) {
|
|
|
payment.setUndo(1);
|
|
|
AdditionalPayment additionalPayment = getAdditionalPaymentOne(additionalPaymentDto.getDistinguish(), additionalPaymentDto.getRelationId(), additionalPaymentDto.getId());
|