package com.kingdee.eas.custom.managedormsys.utils; import com.kingdee.bos.BOSException; import com.kingdee.bos.Context; import com.kingdee.bos.metadata.entity.EntityViewInfo; import com.kingdee.bos.metadata.entity.FilterInfo; import com.kingdee.bos.metadata.entity.FilterItemCollection; import com.kingdee.bos.metadata.entity.FilterItemInfo; import com.kingdee.bos.metadata.query.util.CompareType; import com.kingdee.eas.custom.managedormsys.IWaterPowerMeterReading; import com.kingdee.eas.custom.managedormsys.WaterPowerMeterReadingCollection; import com.kingdee.eas.custom.managedormsys.WaterPowerMeterReadingFactory; import com.kingdee.eas.custom.managedormsys.WaterPowerMeterReadingInfo; import com.kingdee.shr.base.syssetting.exception.ShrWebBizException; import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.time.YearMonth; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author qingwu * @date 2025/4/22 * @apiNote */ public class WaterPowerUtil { static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM"); public static void verifyDatEexists(Context ctx, Date years, String dormNumber, String id) throws ShrWebBizException { try { FilterInfo filterInfo = new FilterInfo(); FilterItemCollection filterItems = filterInfo.getFilterItems(); filterItems.add(new FilterItemInfo("years", years)); filterItems.add(new FilterItemInfo("dormNumber", dormNumber)); if (id != "") { filterItems.add(new FilterItemInfo("id", id, CompareType.NOTEQUALS)); } IWaterPowerMeterReading iWaterPowerMeterReading = WaterPowerMeterReadingFactory.getLocalInstance(ctx); boolean exists = iWaterPowerMeterReading.exists(filterInfo); if (exists) { throw new ShrWebBizException(dormNumber + "房间已经存在" + formatter.format(years) + "月的抄表信息!"); } } catch ( Exception e) { e.printStackTrace(); throw new ShrWebBizException(e); } } public static Map getThisMonthWaterPowerTons(Context ctx, String years, String dormNumber, BigDecimal totalWaterTons, BigDecimal totalElecDegrees) throws BOSException { Map dateMap = getDate(years); String previousMonth = dateMap.get("previousMonth"); String twoMonthsAgo = dateMap.get("twoMonthsAgo"); //获取上个月的水电总吨数 //水总吨数 BigDecimal water = BigDecimal.ZERO; //电总吨数 BigDecimal power = BigDecimal.ZERO; //当月水吨数 ?本月实际用水量,计算(本月-上月,如上月无数据-上上月,如还无数据则直接显示“用水总吨数”); BigDecimal curMonthWater = BigDecimal.ZERO; //当月电度数 ?本月实际用电量,计算(本月-上月,如上月无数据-上上月,如还无数据则直接显示“用电总度数”); BigDecimal curMonthElecDegrees = BigDecimal.ZERO; Map previousMonthMap = getWaterPower(ctx, previousMonth, dormNumber); if (previousMonthMap.size() > 0) { water = previousMonthMap.get("water"); power = previousMonthMap.get("power"); curMonthWater = totalWaterTons.subtract(water); curMonthElecDegrees = totalElecDegrees.subtract(power); } else { //获取上上个月的水电总吨数 previousMonthMap = getWaterPower(ctx, twoMonthsAgo, dormNumber); if (previousMonthMap.size() > 0) { water = previousMonthMap.get("water"); power = previousMonthMap.get("power"); curMonthWater = totalWaterTons.subtract(water); curMonthElecDegrees = totalElecDegrees.subtract(power); } curMonthWater = totalWaterTons; curMonthElecDegrees = totalElecDegrees; } Map result = new HashMap(); result.put("curMonthWater", curMonthWater.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : curMonthWater);//当月水吨数 result.put("curMonthElecDegrees", curMonthElecDegrees.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : curMonthElecDegrees);//当月电度数 return result; } /** * 获取上月用水电吨数 * * @param date * @param dormNumber * @return * @throws BOSException */ public static Map getWaterPower(Context ctx, String date, String dormNumber) throws BOSException { Map map = new HashMap(); FilterInfo filterInfo = new FilterInfo(); FilterItemCollection filterItems = filterInfo.getFilterItems(); //上个月:previousMonth 上上个月: filterItems.add(new FilterItemInfo("years", date + "-01 00:00:00")); filterItems.add(new FilterItemInfo("dormNumber", dormNumber)); EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null); //水电每月抄表信息 IWaterPowerMeterReading iWaterPowerMeterReading = WaterPowerMeterReadingFactory.getLocalInstance(ctx); WaterPowerMeterReadingCollection waterPowerMeterReadingCollection = iWaterPowerMeterReading.getWaterPowerMeterReadingCollection(entityViewInfo); for (int i = 0; i < waterPowerMeterReadingCollection.size(); i++) { WaterPowerMeterReadingInfo waterPowerMeterReadingInfo = waterPowerMeterReadingCollection.get(i); map.put("water", waterPowerMeterReadingInfo.getTotalWaterTons()); map.put("power", waterPowerMeterReadingInfo.getTotalElecDegrees()); } return map; } public static Map getDate(String dateStr) { Map map = new HashMap(); // 定义日期格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM"); // 将字符串转换为 YearMonth 对象 YearMonth targetDate = YearMonth.parse(dateStr, formatter); // 获取上个月 YearMonth previousMonth = targetDate.minusMonths(1); map.put("previousMonth", formatter.format(previousMonth)); // 获取上上个月 YearMonth twoMonthsAgo = targetDate.minusMonths(2); map.put("twoMonthsAgo", formatter.format(twoMonthsAgo)); return map; } }