You've already forked FrameTour-BE
IProfitSharing
This commit is contained in:
@ -0,0 +1,84 @@
|
||||
package com.ycwl.basic.profitsharing.biz;
|
||||
|
||||
import com.ycwl.basic.profitsharing.entity.ProfitSharingConfig;
|
||||
import com.ycwl.basic.profitsharing.entity.ProfitSharingRecord;
|
||||
import com.ycwl.basic.profitsharing.entity.ProfitSharingUser;
|
||||
import com.ycwl.basic.profitsharing.mapper.ProfitSharingConfigMapper;
|
||||
import com.ycwl.basic.profitsharing.mapper.ProfitSharingRecordMapper;
|
||||
import com.ycwl.basic.profitsharing.repository.ProfitSharingRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class ProfitSharingBiz {
|
||||
@Autowired
|
||||
private ProfitSharingRepository repository;
|
||||
|
||||
@Autowired
|
||||
private ProfitSharingRecordMapper recordMapper;
|
||||
|
||||
public void processProfitSharing(Long scenicId, Long orderId, BigDecimal amount) {
|
||||
ProfitSharingConfig config = repository.getScenicConfig(scenicId);
|
||||
if (config == null || config.getUsers() == null || config.getUsers().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<ProfitSharingRecord> records = new ArrayList<>();
|
||||
BigDecimal totalPercentage = BigDecimal.ZERO;
|
||||
for (ProfitSharingUser user : config.getUsers()) {
|
||||
totalPercentage = totalPercentage.add(user.getRealRate());
|
||||
}
|
||||
if (totalPercentage.compareTo(BigDecimal.valueOf(100)) > 0) {
|
||||
throw new RuntimeException("分账比例总和超过100%");
|
||||
}
|
||||
BigDecimal myPercentage = BigDecimal.valueOf(100).subtract(totalPercentage);
|
||||
|
||||
for (ProfitSharingUser user : config.getUsers()) {
|
||||
BigDecimal userAmount = amount.multiply(user.getRealRate()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_DOWN);
|
||||
BigDecimal wxAmount = amount.multiply(user.getWxRate()).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_DOWN);
|
||||
BigDecimal manualAmount = userAmount.subtract(wxAmount);
|
||||
ProfitSharingRecord record = new ProfitSharingRecord();
|
||||
record.setScenicId(scenicId);
|
||||
record.setOrderId(orderId);
|
||||
record.setUserId(user.getId());
|
||||
record.setUserName(user.getName());
|
||||
record.setAmount(userAmount);
|
||||
record.setWxAmount(wxAmount);
|
||||
record.setManualAmount(manualAmount);
|
||||
record.setWxRate(user.getWxRate());
|
||||
record.setRealRate(user.getRealRate());
|
||||
record.setOrderAmount(amount);
|
||||
record.setCreateTime(new Date());
|
||||
records.add(record);
|
||||
}
|
||||
BigDecimal remainAmount = amount;
|
||||
for (ProfitSharingRecord record : records) {
|
||||
remainAmount = remainAmount.subtract(record.getAmount());
|
||||
}
|
||||
|
||||
if (remainAmount.compareTo(BigDecimal.ZERO) > 0) {
|
||||
ProfitSharingRecord record = new ProfitSharingRecord();
|
||||
record.setScenicId(scenicId);
|
||||
record.setOrderId(orderId);
|
||||
record.setUserId(0L);
|
||||
record.setUserName("自己");
|
||||
record.setAmount(remainAmount);
|
||||
record.setWxAmount(BigDecimal.ZERO);
|
||||
record.setManualAmount(remainAmount);
|
||||
record.setWxRate(BigDecimal.ZERO);
|
||||
record.setRealRate(myPercentage);
|
||||
record.setOrderAmount(amount);
|
||||
record.setCreateTime(new Date());
|
||||
records.add(record);
|
||||
}
|
||||
|
||||
recordMapper.batchInsert(records);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user