修复精度bug
This commit is contained in:
@@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.seeyon.apps.src_leasebill.config.FieldDisplayNameQueryService;
|
||||
import com.seeyon.ctp.common.AppContext;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
@@ -29,6 +31,11 @@ public class LeaseBillUtil {
|
||||
*/
|
||||
private final DateUtil dateUtil = new DateUtil();
|
||||
|
||||
/**
|
||||
* 金额精度:保留2位小数
|
||||
*/
|
||||
private static final int MONEY_SCALE = 2;
|
||||
|
||||
// ======================== 【1】主方法:生成租金账单(支持一次性/周期) ========================
|
||||
|
||||
/**
|
||||
@@ -137,21 +144,21 @@ public class LeaseBillUtil {
|
||||
nextYearStart.add(Calendar.DATE, 1);
|
||||
|
||||
int monthCount = dateUtil.betweenMonthByTwoCalendar(yearBaseCal, nextYearStart);
|
||||
double rent = calculateRent(monthCount, params, chargeType);
|
||||
String rent = calculateRent(monthCount, params, chargeType);
|
||||
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), String.valueOf(rent));
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), rent);
|
||||
bill.put(fieldDisplayNameService.getDisplayName("结束日期"), sdf.format(tempStart.getTime()));
|
||||
|
||||
yearBaseCal = (Calendar) tempStart.clone();
|
||||
} else {
|
||||
// 最后一个年度 → 不足一年
|
||||
int monthCount = dateUtil.betweenMonthByTwoCalendar(yearBaseCal, endTime);
|
||||
double rent = calculateRent(monthCount, params, chargeType);
|
||||
String rent = calculateRent(monthCount, params, chargeType);
|
||||
|
||||
Calendar realEnd = (Calendar) endTime.clone();
|
||||
realEnd.add(Calendar.DATE, -1);
|
||||
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), String.valueOf(rent));
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), rent);
|
||||
bill.put(fieldDisplayNameService.getDisplayName("结束日期"), sdf.format(realEnd.getTime()));
|
||||
}
|
||||
|
||||
@@ -177,7 +184,7 @@ public class LeaseBillUtil {
|
||||
private Map<String, String> createOneTimeBill(Calendar startTime, Calendar endTime, int totalMonths,
|
||||
JSONObject params, String chargeType) {
|
||||
Map<String, String> bill = new HashMap<>();
|
||||
double rent = calculateRent(totalMonths, params, chargeType);
|
||||
String rent = calculateRent(totalMonths, params, chargeType);
|
||||
|
||||
bill.put(fieldDisplayNameService.getDisplayName("开始日期"), sdf.format(startTime.getTime()));
|
||||
|
||||
@@ -186,7 +193,7 @@ public class LeaseBillUtil {
|
||||
realEnd.add(Calendar.DATE, -1);
|
||||
bill.put(fieldDisplayNameService.getDisplayName("结束日期"), sdf.format(realEnd.getTime()));
|
||||
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), String.valueOf(rent));
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), rent);
|
||||
return bill;
|
||||
}
|
||||
|
||||
@@ -220,18 +227,18 @@ public class LeaseBillUtil {
|
||||
|
||||
if (periodEnd.before(endTime)) {
|
||||
// 正常完整周期
|
||||
double rent = calculateRent(payCycleMonths, params, chargeType);
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), String.valueOf(rent));
|
||||
String rent = calculateRent(payCycleMonths, params, chargeType);
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), rent);
|
||||
bill.put(fieldDisplayNameService.getDisplayName("结束日期"), sdf.format(periodEnd.getTime()));
|
||||
} else {
|
||||
// 最后一期:不足一个缴费周期
|
||||
int realMonths = dateUtil.betweenMonthByTwoCalendar(tempStart, endTime);
|
||||
double rent = calculateRent(realMonths, params, chargeType);
|
||||
String rent = calculateRent(realMonths, params, chargeType);
|
||||
|
||||
Calendar realEnd = (Calendar) endTime.clone();
|
||||
realEnd.add(Calendar.DATE, -1);
|
||||
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), String.valueOf(rent));
|
||||
bill.put(fieldDisplayNameService.getDisplayName("租费"), rent);
|
||||
bill.put(fieldDisplayNameService.getDisplayName("结束日期"), sdf.format(realEnd.getTime()));
|
||||
}
|
||||
|
||||
@@ -256,24 +263,44 @@ public class LeaseBillUtil {
|
||||
* @param months 计费月数
|
||||
* @param params 表单参数
|
||||
* @param chargeType 计费方式
|
||||
* @return 计算后租金金额
|
||||
* @return 计算后租金金额(保留2位小数)
|
||||
*/
|
||||
private double calculateRent(int months, JSONObject params, String chargeType) {
|
||||
double unitPrice = 0;
|
||||
private String calculateRent(int months, JSONObject params, String chargeType) {
|
||||
BigDecimal rent = BigDecimal.ZERO;
|
||||
boolean isAreaCharge = "面积计费".equals(chargeType);
|
||||
boolean isFixedRent = "固定租金".equals(chargeType);
|
||||
|
||||
if (isAreaCharge) {
|
||||
// 面积计费:租金 = 面积 × 每平单价
|
||||
double area = Double.parseDouble(params.getString("mj"));
|
||||
double pricePerArea = dateUtil.stringToNum(params.getString("mjzj"));
|
||||
unitPrice = area * pricePerArea;
|
||||
BigDecimal area = parseBigDecimal(params.getString("mj"));
|
||||
BigDecimal pricePerArea = parseBigDecimal(params.getString("mjzj"));
|
||||
rent = area.multiply(pricePerArea);
|
||||
} else if (isFixedRent) {
|
||||
// 固定租金:直接取固定金额
|
||||
unitPrice = dateUtil.stringToNum(params.getString("gdzj"));
|
||||
rent = parseBigDecimal(params.getString("gdzj"));
|
||||
}
|
||||
|
||||
// 固定租金不乘月数,其他计费方式 × 月数
|
||||
return isFixedRent ? unitPrice : unitPrice * months;
|
||||
if (!isFixedRent) {
|
||||
rent = rent.multiply(new BigDecimal(months));
|
||||
}
|
||||
|
||||
// 保留2位小数,四舍五入
|
||||
return rent.setScale(MONEY_SCALE, RoundingMode.HALF_UP).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串解析为BigDecimal,处理逗号分隔符
|
||||
*
|
||||
* @param str 数字字符串(可能包含逗号分隔符)
|
||||
* @return BigDecimal对象
|
||||
*/
|
||||
private BigDecimal parseBigDecimal(String str) {
|
||||
if (str == null || str.trim().isEmpty()) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
// 移除逗号分隔符
|
||||
String cleanStr = str.replace(",", "").trim();
|
||||
return new BigDecimal(cleanStr);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user