WaterPowerUtil.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.kingdee.eas.custom.managedormsys.utils;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.bos.metadata.entity.EntityViewInfo;
  5. import com.kingdee.bos.metadata.entity.FilterInfo;
  6. import com.kingdee.bos.metadata.entity.FilterItemCollection;
  7. import com.kingdee.bos.metadata.entity.FilterItemInfo;
  8. import com.kingdee.bos.metadata.query.util.CompareType;
  9. import com.kingdee.eas.custom.managedormsys.IWaterPowerMeterReading;
  10. import com.kingdee.eas.custom.managedormsys.WaterPowerMeterReadingCollection;
  11. import com.kingdee.eas.custom.managedormsys.WaterPowerMeterReadingFactory;
  12. import com.kingdee.eas.custom.managedormsys.WaterPowerMeterReadingInfo;
  13. import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
  14. import java.math.BigDecimal;
  15. import java.text.SimpleDateFormat;
  16. import java.time.YearMonth;
  17. import java.time.format.DateTimeFormatter;
  18. import java.util.Date;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. /**
  22. * @author qingwu
  23. * @date 2025/4/22
  24. * @apiNote
  25. */
  26. public class WaterPowerUtil {
  27. static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
  28. public static void verifyDatEexists(Context ctx, Date years, String dormNumber, String id) throws ShrWebBizException {
  29. try {
  30. FilterInfo filterInfo = new FilterInfo();
  31. FilterItemCollection filterItems = filterInfo.getFilterItems();
  32. filterItems.add(new FilterItemInfo("years", years));
  33. filterItems.add(new FilterItemInfo("dormNumber", dormNumber));
  34. if (id != "") {
  35. filterItems.add(new FilterItemInfo("id", id, CompareType.NOTEQUALS));
  36. }
  37. IWaterPowerMeterReading iWaterPowerMeterReading = WaterPowerMeterReadingFactory.getLocalInstance(ctx);
  38. boolean exists = iWaterPowerMeterReading.exists(filterInfo);
  39. if (exists) {
  40. throw new ShrWebBizException(dormNumber + "房间已经存在" + formatter.format(years) + "月的抄表信息!");
  41. }
  42. } catch (
  43. Exception e) {
  44. e.printStackTrace();
  45. throw new ShrWebBizException(e);
  46. }
  47. }
  48. public static Map getThisMonthWaterPowerTons(Context ctx, String years, String dormNumber, BigDecimal totalWaterTons, BigDecimal totalElecDegrees) throws BOSException {
  49. Map<String, String> dateMap = getDate(years);
  50. String previousMonth = dateMap.get("previousMonth");
  51. String twoMonthsAgo = dateMap.get("twoMonthsAgo");
  52. //获取上个月的水电总吨数
  53. //水总吨数
  54. BigDecimal water = BigDecimal.ZERO;
  55. //电总吨数
  56. BigDecimal power = BigDecimal.ZERO;
  57. //当月水吨数 ?本月实际用水量,计算(本月-上月,如上月无数据-上上月,如还无数据则直接显示“用水总吨数”);
  58. BigDecimal curMonthWater = BigDecimal.ZERO;
  59. //当月电度数 ?本月实际用电量,计算(本月-上月,如上月无数据-上上月,如还无数据则直接显示“用电总度数”);
  60. BigDecimal curMonthElecDegrees = BigDecimal.ZERO;
  61. Map<String, BigDecimal> previousMonthMap = getWaterPower(ctx, previousMonth, dormNumber);
  62. if (previousMonthMap.size() > 0) {
  63. water = previousMonthMap.get("water");
  64. power = previousMonthMap.get("power");
  65. curMonthWater = totalWaterTons.subtract(water);
  66. curMonthElecDegrees = totalElecDegrees.subtract(power);
  67. } else {
  68. //获取上上个月的水电总吨数
  69. previousMonthMap = getWaterPower(ctx, twoMonthsAgo, dormNumber);
  70. if (previousMonthMap.size() > 0) {
  71. water = previousMonthMap.get("water");
  72. power = previousMonthMap.get("power");
  73. curMonthWater = totalWaterTons.subtract(water);
  74. curMonthElecDegrees = totalElecDegrees.subtract(power);
  75. }
  76. curMonthWater = totalWaterTons;
  77. curMonthElecDegrees = totalElecDegrees;
  78. }
  79. Map result = new HashMap();
  80. result.put("curMonthWater", curMonthWater.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : curMonthWater);//当月水吨数
  81. result.put("curMonthElecDegrees", curMonthElecDegrees.compareTo(BigDecimal.ZERO) <= 0 ? BigDecimal.ZERO : curMonthElecDegrees);//当月电度数
  82. return result;
  83. }
  84. /**
  85. * 获取上月用水电吨数
  86. *
  87. * @param date
  88. * @param dormNumber
  89. * @return
  90. * @throws BOSException
  91. */
  92. public static Map getWaterPower(Context ctx, String date, String dormNumber) throws BOSException {
  93. Map<String, BigDecimal> map = new HashMap();
  94. FilterInfo filterInfo = new FilterInfo();
  95. FilterItemCollection filterItems = filterInfo.getFilterItems();
  96. //上个月:previousMonth 上上个月:
  97. filterItems.add(new FilterItemInfo("years", date + "-01 00:00:00"));
  98. filterItems.add(new FilterItemInfo("dormNumber", dormNumber));
  99. EntityViewInfo entityViewInfo = EntityViewInfo.getInstance(filterInfo, null, null);
  100. //水电每月抄表信息
  101. IWaterPowerMeterReading iWaterPowerMeterReading = WaterPowerMeterReadingFactory.getLocalInstance(ctx);
  102. WaterPowerMeterReadingCollection waterPowerMeterReadingCollection = iWaterPowerMeterReading.getWaterPowerMeterReadingCollection(entityViewInfo);
  103. for (int i = 0; i < waterPowerMeterReadingCollection.size(); i++) {
  104. WaterPowerMeterReadingInfo waterPowerMeterReadingInfo = waterPowerMeterReadingCollection.get(i);
  105. map.put("water", waterPowerMeterReadingInfo.getTotalWaterTons());
  106. map.put("power", waterPowerMeterReadingInfo.getTotalElecDegrees());
  107. }
  108. return map;
  109. }
  110. public static Map getDate(String dateStr) {
  111. Map map = new HashMap();
  112. // 定义日期格式
  113. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
  114. // 将字符串转换为 YearMonth 对象
  115. YearMonth targetDate = YearMonth.parse(dateStr, formatter);
  116. // 获取上个月
  117. YearMonth previousMonth = targetDate.minusMonths(1);
  118. map.put("previousMonth", formatter.format(previousMonth));
  119. // 获取上上个月
  120. YearMonth twoMonthsAgo = targetDate.minusMonths(2);
  121. map.put("twoMonthsAgo", formatter.format(twoMonthsAgo));
  122. return map;
  123. }
  124. }