mirror of https://github.com/jeecgboot/jeecg-boot
fix2 : credit editing now correctly updates balance + allow only editing of last credit per currency
parent
1efb88c7a1
commit
6978554075
|
@ -24,6 +24,7 @@ import io.swagger.annotations.Api;
|
|||
import io.swagger.annotations.ApiOperation;
|
||||
import org.jeecg.common.aspect.annotation.AutoLog;
|
||||
|
||||
import static org.jeecg.modules.business.entity.Balance.OperationType;
|
||||
/**
|
||||
* @Description: credit
|
||||
* @Author: jeecg-boot
|
||||
|
@ -88,9 +89,31 @@ public class CreditController extends JeecgController<Credit, ICreditService> {
|
|||
@Transactional
|
||||
@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
|
||||
public Result<String> edit(@RequestBody Credit credit) {
|
||||
log.info("editing credit");
|
||||
Credit lastCredit = creditService.getLastCredit(credit.getCurrencyId());
|
||||
boolean isAmountUpdated = true;
|
||||
// if the last credit is not the one being edited, then the amount cannot be edited
|
||||
if(!lastCredit.getId().equals(credit.getId())) {
|
||||
isAmountUpdated = false;
|
||||
Credit creditToEdit = creditService.getById(credit.getId());
|
||||
if(creditToEdit.getAmount().compareTo(credit.getAmount()) != 0) {
|
||||
log.error("Credit amount cannot be edited ! Only the last record can be edited !");
|
||||
return Result.error("Credit amount cannot be edited ! Only the last record can be edited !");
|
||||
}
|
||||
}
|
||||
creditService.updateById(credit);
|
||||
balanceService.editBalance(credit.getId(), "Credit", credit.getClientId() ,credit.getAmount(), credit.getCurrencyId());
|
||||
return Result.OK("编辑成功!");
|
||||
log.info("credit edited successfully !");
|
||||
if(isAmountUpdated) {
|
||||
try {
|
||||
balanceService.editBalance(credit.getId(), OperationType.Credit.name(), credit.getClientId(), credit.getAmount(), credit.getCurrencyId());
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.error("Error while editing balance : " + e.getMessage());
|
||||
return Result.error("Error while editing balance : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
return Result.OK("Credit edited successfully !");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -105,7 +128,7 @@ public class CreditController extends JeecgController<Credit, ICreditService> {
|
|||
@DeleteMapping(value = "/delete")
|
||||
public Result<String> delete(@RequestParam(name="id") String id) {
|
||||
creditService.removeById(id);
|
||||
balanceService.deleteBalance(id, "Credit");
|
||||
balanceService.deleteBalance(id, OperationType.Credit.name());
|
||||
return Result.OK("删除成功!");
|
||||
}
|
||||
|
||||
|
@ -122,7 +145,7 @@ public class CreditController extends JeecgController<Credit, ICreditService> {
|
|||
public Result<String> deleteBatch(@RequestParam(name="ids") String ids) {
|
||||
List<String> idList = Arrays.asList(ids.split(","));
|
||||
creditService.removeByIds(idList);
|
||||
balanceService.deleteBatchBalance(idList, "Credit");
|
||||
balanceService.deleteBatchBalance(idList,OperationType.Credit.name());
|
||||
return Result.OK("批量删除成功!");
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,7 @@ import java.util.regex.Pattern;
|
|||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.jeecg.modules.business.entity.Balance.OperationType;
|
||||
/**
|
||||
* @Description: 物流发票
|
||||
* @Author: jeecg-boot
|
||||
|
@ -512,7 +513,7 @@ public class ShippingInvoiceController {
|
|||
savRefundService.cancelInvoice(invoiceNumber);
|
||||
shippingInvoiceService.delMain(id);
|
||||
log.info("Updating balance ...");
|
||||
balanceService.deleteBalance(id, "Debit");
|
||||
balanceService.deleteBalance(id, OperationType.Debit.name());
|
||||
log.info("Deleting invoice files ...");
|
||||
String invoiceEntity = clientService.getClientEntity(clientId);
|
||||
List<Path> invoicePathList = getPath(INVOICE_LOCATION, invoiceNumber, invoiceEntity);
|
||||
|
@ -575,7 +576,7 @@ public class ShippingInvoiceController {
|
|||
savRefundService.cancelBatchInvoice(invoiceNumbers);
|
||||
shippingInvoiceService.delBatchMain(ids);;
|
||||
log.info("Updating balances ...");
|
||||
balanceService.deleteBatchBalance(ids, "Debit");
|
||||
balanceService.deleteBatchBalance(ids, OperationType.Debit.name());
|
||||
log.info("Deleting invoice files ...");
|
||||
|
||||
for(int i = 0; i < ids.size(); i++) {
|
||||
|
|
|
@ -71,6 +71,11 @@ public class Balance implements Serializable {
|
|||
@ApiModelProperty(value = "balance amount")
|
||||
private java.math.BigDecimal amount;
|
||||
|
||||
public enum OperationType {
|
||||
Credit,
|
||||
Debit,
|
||||
Init,
|
||||
}
|
||||
public Balance() {
|
||||
|
||||
}
|
||||
|
|
|
@ -29,4 +29,6 @@ public interface BalanceMapper extends BaseMapper<Balance> {
|
|||
@Param("clientId") String clientId,
|
||||
@Param("amount") BigDecimal amount,
|
||||
@Param("currencyId") String currencyId);
|
||||
|
||||
Balance getBalanceByOperation(@Param("operationId")String operationId, @Param("operationType") String operationType);
|
||||
}
|
||||
|
|
|
@ -15,5 +15,5 @@ import org.springframework.stereotype.Repository;
|
|||
*/
|
||||
@Repository
|
||||
public interface CreditMapper extends BaseMapper<Credit> {
|
||||
|
||||
Credit getLastCredit(@Param("currencyId") String currencyId);
|
||||
}
|
||||
|
|
|
@ -11,6 +11,12 @@
|
|||
ORDER BY b.create_time DESC
|
||||
LIMIT 1
|
||||
</select>
|
||||
<select id="getBalanceByOperation" resultType="org.jeecg.modules.business.entity.Balance">
|
||||
SELECT id
|
||||
FROM balance b
|
||||
WHERE operation_id = #{operationId}
|
||||
AND operation_type = #{operationType}
|
||||
</select>
|
||||
<delete id="deleteBalance">
|
||||
DELETE
|
||||
FROM balance
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.jeecg.modules.business.mapper.CreditMapper">
|
||||
|
||||
<select id="getLastCredit" resultType="org.jeecg.modules.business.entity.Credit">
|
||||
SELECT * FROM credit
|
||||
WHERE currency_id = #{currencyId}
|
||||
ORDER BY create_time DESC LIMIT 1;
|
||||
</select>
|
||||
</mapper>
|
|
@ -34,7 +34,7 @@ public interface IBalanceService extends IService<Balance> {
|
|||
* @param amount
|
||||
* @param currencyId
|
||||
*/
|
||||
void editBalance(String operationId, String operationType, String clientId, BigDecimal amount, String currencyId);
|
||||
void editBalance(String operationId, String operationType, String clientId, BigDecimal amount, String currencyId) throws Exception;
|
||||
|
||||
void deleteBatchBalance(List<String> operationIds, String operationType);
|
||||
}
|
||||
|
|
|
@ -10,5 +10,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
* @Version: V1.0
|
||||
*/
|
||||
public interface ICreditService extends IService<Credit> {
|
||||
|
||||
Credit getLastCredit(String currencyId);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.jeecg.modules.business.service.impl;
|
||||
|
||||
import org.checkerframework.checker.units.qual.A;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.modules.business.entity.Balance;
|
||||
import org.jeecg.modules.business.entity.PlatformOrder;
|
||||
import org.jeecg.modules.business.entity.PlatformOrderContent;
|
||||
|
@ -28,6 +28,7 @@ import java.util.stream.Collectors;
|
|||
* @Version: V1.0
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> implements IBalanceService {
|
||||
@Autowired
|
||||
private BalanceMapper balanceMapper;
|
||||
|
@ -62,7 +63,7 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> impl
|
|||
currentBalance = currentBalance.add(purchaseFees);
|
||||
}
|
||||
SysUser sysUser = new SysUser();
|
||||
Balance balance = Balance.of(sysUser.getUsername(), clientId, invoice.getCurrencyId(), "Debit", invoice.getId(), currentBalance);
|
||||
Balance balance = Balance.of(sysUser.getUsername(), clientId, invoice.getCurrencyId(), Balance.OperationType.Debit.name(), invoice.getId(), currentBalance);
|
||||
balanceMapper.insert(balance);
|
||||
}
|
||||
|
||||
|
@ -72,7 +73,7 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> impl
|
|||
BigDecimal previousBalance = getBalanceByClientIdAndCurrency(clientId, currency);
|
||||
BigDecimal currentBalance = previousBalance.add(amount);
|
||||
SysUser sysUser = new SysUser();
|
||||
Balance balance = Balance.of(sysUser.getUsername(), clientId, currencyId, "Credit", CreditId, currentBalance);
|
||||
Balance balance = Balance.of(sysUser.getUsername(), clientId, currencyId, Balance.OperationType.Credit.name(), CreditId, currentBalance);
|
||||
balanceMapper.insert(balance);
|
||||
}
|
||||
|
||||
|
@ -86,9 +87,22 @@ public class BalanceServiceImpl extends ServiceImpl<BalanceMapper, Balance> impl
|
|||
balanceMapper.deleteBatchBalance(operationIds, operationType);
|
||||
}
|
||||
@Override
|
||||
public void editBalance(String operationId, String operationType, String clientId, BigDecimal amount, String currencyId) {
|
||||
public void editBalance(String operationId, String operationType, String clientId, BigDecimal amount, String currencyId) throws Exception {
|
||||
log.info("editing balance");
|
||||
SysUser sysUser = new SysUser();
|
||||
balanceMapper.editBalance(operationId, operationType, sysUser.getUsername(), clientId, amount, currencyId);
|
||||
String currency = currencyService.getCodeById(currencyId);
|
||||
Balance balance = balanceMapper.getBalanceByOperation(operationId, operationType);
|
||||
System.out.println("balance exist ? : " + balance);
|
||||
if(balance == null) {
|
||||
throw new Exception("Balance not found !");
|
||||
}
|
||||
balanceMapper.deleteBalance(operationId, operationType);
|
||||
BigDecimal currentBalance = balanceMapper.getBalanceByClientIdAndCurrency(clientId, currency);
|
||||
System.out.println("current balance : " + currentBalance);
|
||||
BigDecimal finalBalance = operationType.equals(Balance.OperationType.Credit.name()) ? currentBalance.add(amount) : currentBalance.subtract(amount);
|
||||
System.out.println("final balance : " + finalBalance);
|
||||
Balance newBalance = Balance.of(sysUser.getUsername(), clientId, currencyId, operationType, operationId, finalBalance);
|
||||
balanceMapper.insert(newBalance);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import org.jeecg.modules.business.entity.Credit;
|
||||
import org.jeecg.modules.business.mapper.CreditMapper;
|
||||
import org.jeecg.modules.business.service.ICreditService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
|
@ -17,5 +18,9 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||
@Service
|
||||
@Slf4j
|
||||
public class CreditServiceImpl extends ServiceImpl<CreditMapper, Credit> implements ICreditService {
|
||||
|
||||
@Autowired private CreditMapper creditMapper;
|
||||
@Override
|
||||
public Credit getLastCredit(String currencyId) {
|
||||
return creditMapper.getLastCredit(currencyId);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue