|
@@ -11,21 +11,25 @@ import com.bz.smart_city.dao.pay.BaseClosingAccountInfoMapper;
|
|
|
import com.bz.smart_city.dao.pay.PayBaseConfigMapper;
|
|
|
import com.bz.smart_city.dto.pay.*;
|
|
|
import com.bz.smart_city.entity.Customer;
|
|
|
+import com.bz.smart_city.entity.pay.DeviceReplaceRecord;
|
|
|
+import com.bz.smart_city.quartz.entity.QuartzEntity;
|
|
|
+import com.bz.smart_city.quartz.job.AmountSyncJob;
|
|
|
import com.bz.smart_city.quartz.service.AmountSynService;
|
|
|
import com.bz.smart_city.quartz.service.JobAndTriggerService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.BigInteger;
|
|
|
-import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.time.ZoneId;
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
-import java.util.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -36,7 +40,7 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
-public class AmountSynServiceImpl implements AmountSynService {
|
|
|
+public class AmountSynServiceImpl implements AmountSynService , InitializingBean {
|
|
|
|
|
|
@Value("1")
|
|
|
private BigInteger createBy;
|
|
@@ -51,12 +55,58 @@ public class AmountSynServiceImpl implements AmountSynService {
|
|
|
@Resource
|
|
|
private CustomerMapper customerMapper;
|
|
|
@Resource
|
|
|
- private PayBaseConfigMapper payBaseConfigMapper;
|
|
|
- @Resource
|
|
|
private JobAndTriggerService jobAndTriggerService;
|
|
|
+ @Resource
|
|
|
+ private PayBaseConfigMapper payBaseConfigMapper;
|
|
|
+
|
|
|
@Override
|
|
|
+ public void afterPropertiesSet() {
|
|
|
+ //获取系统参数
|
|
|
+ List<PayBaseConfigDto> payBaseConfigDtos = payBaseConfigMapper.getAllList("AUTO_SYNUAMOUNT_TIME",null,null,null);
|
|
|
+
|
|
|
+ //循环添加定时任务
|
|
|
+ for(PayBaseConfigDto payBaseConfigDto :payBaseConfigDtos){
|
|
|
+ saveQrtzTask(payBaseConfigDto);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void saveQrtzTask(PayBaseConfigDto payBaseConfigDto) {
|
|
|
|
|
|
+
|
|
|
+ String value = payBaseConfigDto.getValue();
|
|
|
+ if(value == null|| value==""){ //获取定时corn失败
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1,查询需要批量推送的配置项目并构建定时任务
|
|
|
+ // 2,若对应定时任务不存在则创建
|
|
|
+ QuartzEntity entity = new QuartzEntity();
|
|
|
+ entity.setJobGroup("水量同步");
|
|
|
+ entity.setJobName("AmountSyncJob"+payBaseConfigDto.getCustomerId());
|
|
|
+ entity.setDescription("AmountSyncJob"+payBaseConfigDto.getCustomerId());
|
|
|
+ boolean exists = jobAndTriggerService.isExists(entity);
|
|
|
+ if(!exists){
|
|
|
+ String[] values = value.split("_");
|
|
|
+ String cron = "0 */"+values[2]+" * "+values[0]+"-"+values[1]+" * ?";
|
|
|
+ log.info("水量同步cron:"+cron);
|
|
|
+ entity.setCronExpression(cron);
|
|
|
+ entity.setJobClassName(AmountSyncJob.class.getName());
|
|
|
+ HashMap<String, Object> jobData = new HashMap<String, Object>();
|
|
|
+ jobData.put("siteId", payBaseConfigDto.getSiteId().intValue());
|
|
|
+ jobData.put("customerId", payBaseConfigDto.getCustomerId().intValue());
|
|
|
+ entity.setJobData(jobData);
|
|
|
+ jobAndTriggerService.save(entity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public void deleteQrtzTask(BigInteger id) {
|
|
|
+ // 1,查询需要批量推送的配置项目并构建定时任务
|
|
|
+ QuartzEntity entity = new QuartzEntity();
|
|
|
+ entity.setJobGroup("水量同步");
|
|
|
+ entity.setJobName("AmountSyncJob"+id);
|
|
|
+ if(jobAndTriggerService.isExists(entity)){
|
|
|
+ jobAndTriggerService.deleteJob(entity);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -91,20 +141,25 @@ public class AmountSynServiceImpl implements AmountSynService {
|
|
|
log.info("同步水量失败:获取同步计划失败");
|
|
|
return;
|
|
|
}
|
|
|
+ //提取出水表编码集合
|
|
|
List<String> metercodes = amountWaterUsedAmountDtos.stream().map(
|
|
|
AmountWaterUsedAmountDto -> AmountWaterUsedAmountDto.getMetercode()).collect(Collectors.toList());
|
|
|
-// List<String> metercodes =amountWaterUsedAmountMapper.GetAmountSynInfo(year,month,siteId,customerId);
|
|
|
+
|
|
|
+ //获取账期内换表记录
|
|
|
+ HashMap<String, DeviceReplaceRecord> replaceRecordMap = amountWaterUsedAmountMapper.getRepalceRecordCount(year,month,siteId,customerId);
|
|
|
|
|
|
//批量更新水量信息
|
|
|
List<AmountWaterUsedAmountDto> updates = new ArrayList<>();
|
|
|
|
|
|
+ //抄表数据结果
|
|
|
List<AmountSynRespDto> lists =new ArrayList<>();
|
|
|
+
|
|
|
for(int i=0;i<metercodes.size();i+=500){
|
|
|
AmountSynParamDto amountSynParamDto= new AmountSynParamDto();
|
|
|
amountSynParamDto.setYyyymm(year+""+month);
|
|
|
amountSynParamDto.setCustomerNo(customerNo);
|
|
|
//一次查询五百条
|
|
|
- List<String> selectList = new ArrayList<>();
|
|
|
+ List<String> selectList ;
|
|
|
if(i+500 >= metercodes.size()){
|
|
|
selectList=metercodes.subList(i,metercodes.size());
|
|
|
}else{
|
|
@@ -125,107 +180,51 @@ public class AmountSynServiceImpl implements AmountSynService {
|
|
|
if(StringUtils.isNotBlank(total) && Integer.valueOf(total) > 0){//有数据
|
|
|
JSONArray jsonArray = datas.getJSONArray("list");
|
|
|
List<AmountSynRespDto> list = jsonArray.toJavaList(AmountSynRespDto.class);
|
|
|
+
|
|
|
lists.addAll(list);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
Map<String ,AmountSynRespDto> map = lists.stream().collect(Collectors.toMap(AmountSynRespDto::getFileNo, Function.identity(), (key1, key2) -> key2));
|
|
|
|
|
|
-
|
|
|
-
|
|
|
//换表记录
|
|
|
- HashMap<BigInteger, ReplaceMeterCountDto> replaceMap = new HashMap<>();
|
|
|
- LocalDateTime stTime = LocalDateTime.ofInstant(baseClosingAccountInfoDto.getStartTime().toInstant(), ZoneId.systemDefault());
|
|
|
- HashMap<BigInteger, ReplaceMeterCountDto> childReplaceMap = amountWaterUsedAmountMapper.getRepalceRecordCount(stTime,null,null,null,customerId);
|
|
|
- if(childReplaceMap.size() >0){
|
|
|
- List<ReplaceMeterDto> replaceMeterDtoList = amountWaterUsedAmountMapper.getRepalceRecordDetail(stTime,null,null,null,customerId);
|
|
|
- if(replaceMeterDtoList.size() >0){
|
|
|
- for(int i=0;i<replaceMeterDtoList.size();i++){
|
|
|
- if(childReplaceMap.containsKey(replaceMeterDtoList.get(i).getDeviceId())){
|
|
|
- ReplaceMeterCountDto replaceMeterCountDto = childReplaceMap.get(replaceMeterDtoList.get(i).getDeviceId());
|
|
|
- if(replaceMeterCountDto.getReplaceMeterDtoList() == null)
|
|
|
- {
|
|
|
- replaceMeterCountDto.setReplaceMeterDtoList(new ArrayList<>());
|
|
|
- }
|
|
|
- replaceMeterCountDto.getReplaceMeterDtoList().add(replaceMeterDtoList.get(i));
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- replaceMap.putAll(childReplaceMap);
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
for (AmountWaterUsedAmountDto amountWaterUsedAmountDto : amountWaterUsedAmountDtos) {
|
|
|
BigInteger watermeterId = amountWaterUsedAmountDto.getWatermeterId();
|
|
|
if (watermeterId != null && map.containsKey(amountWaterUsedAmountDto.getMetercode())) {
|
|
|
AmountSynRespDto amountSynRespDto = map.get(amountWaterUsedAmountDto.getMetercode());
|
|
|
- BigInteger amountId = amountWaterUsedAmountDto.getId();//水量id
|
|
|
BigDecimal readingOld = amountWaterUsedAmountDto.getLastreading();//上期止度
|
|
|
+
|
|
|
if (amountSynRespDto.getReading() != null) {
|
|
|
BigDecimal readingNew = new BigDecimal(amountSynRespDto.getReading()).setScale(0, BigDecimal.ROUND_DOWN);//本期止度
|
|
|
- BigInteger meterid = amountWaterUsedAmountDto.getWatermeterId();//水表id
|
|
|
- BigDecimal payAmount = BigDecimal.ZERO;//结算水量
|
|
|
- Date lastAmountDate = amountWaterUsedAmountDto.getLastrecorddate();
|
|
|
- BigDecimal Amount = BigDecimal.ZERO;//实际用水量
|
|
|
- BigDecimal tempReading = BigDecimal.ZERO;
|
|
|
- tempReading = readingOld;//起度或上期的止度
|
|
|
+
|
|
|
+ BigDecimal payAmount ;//结算水量
|
|
|
+
|
|
|
+ BigDecimal Amount ;//实际用水量
|
|
|
+
|
|
|
+ BigDecimal replaceAmount = BigDecimal.ZERO;//旧表用水量
|
|
|
+
|
|
|
String readDate = amountSynRespDto.getCurrentPeriodDate();
|
|
|
+
|
|
|
Integer calculateway = amountWaterUsedAmountDto.getCalculateway();
|
|
|
|
|
|
//换表记录计算
|
|
|
- boolean replace = false;
|
|
|
- ReplaceMeterCountDto replaceMeterCountDto=null;
|
|
|
- if(replaceMap.containsKey(watermeterId)){
|
|
|
- replaceMeterCountDto = replaceMap.get(watermeterId);
|
|
|
- //有换表记录且换表时间在两次同步之间 用水量=(换表中的旧表止度-上次抄表止度) + (本次止度-换表中的新表起度)
|
|
|
- if(replaceMeterCountDto != null){
|
|
|
- LocalDateTime currTime = LocalDateTime.parse(readDate, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S"));//本次抄表时间
|
|
|
- LocalDateTime previousTime = LocalDateTime.ofInstant(amountWaterUsedAmountDto.getLastrecorddate().toInstant(),ZoneId.systemDefault()); //上个帐期的结算抄表时间
|
|
|
-
|
|
|
- ReplaceMeterDto oldReplaceMeterDto = null;
|
|
|
- for(int i = 0;i<replaceMeterCountDto.getReplaceMeterDtoList().size();i++){
|
|
|
- LocalDateTime replaceTime = replaceMeterCountDto.getReplaceMeterDtoList().get(i).getReplaceTime(); //换表时间
|
|
|
-
|
|
|
- Duration duration1 = Duration.between(previousTime,replaceTime);
|
|
|
- Duration duration2 = Duration.between(replaceTime,currTime);
|
|
|
- if(duration1.toMillis() > 0 && duration2.toMinutes() > 0){
|
|
|
- BigDecimal replaceEndData = new BigDecimal(replaceMeterCountDto.getReplaceMeterDtoList().get(i).getOldEnd()).setScale(2,BigDecimal.ROUND_DOWN);//换表中的旧表止度
|
|
|
- BigDecimal replaceData = BigDecimal.ZERO;
|
|
|
- if(oldReplaceMeterDto == null)
|
|
|
- replaceData = tempReading;
|
|
|
- else
|
|
|
- replaceData = new BigDecimal(oldReplaceMeterDto.getNewBegin()).setScale(0,BigDecimal.ROUND_DOWN);//换表中的起度
|
|
|
- BigDecimal oldMeterUse = replaceEndData.subtract(replaceData); //旧表用量
|
|
|
-
|
|
|
- Amount = Amount.add(oldMeterUse);
|
|
|
- replace = true;
|
|
|
-
|
|
|
- oldReplaceMeterDto = replaceMeterCountDto.getReplaceMeterDtoList().get(i);
|
|
|
- }
|
|
|
- else {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //新表用量
|
|
|
- if(oldReplaceMeterDto != null){
|
|
|
- BigDecimal replaceNewDate = new BigDecimal(oldReplaceMeterDto.getNewBegin()).setScale(0,BigDecimal.ROUND_DOWN);//换表中的新表起度
|
|
|
- BigDecimal newMeterUse = readingNew.subtract(replaceNewDate);//新表用量
|
|
|
- Amount = Amount.add(newMeterUse);
|
|
|
- }
|
|
|
- }
|
|
|
+ if(replaceRecordMap.containsKey(amountWaterUsedAmountDto.getMetercode())){
|
|
|
+ DeviceReplaceRecord deviceReplaceRecord = replaceRecordMap.get(amountWaterUsedAmountDto.getMetercode());
|
|
|
+
|
|
|
+ replaceAmount=deviceReplaceRecord.getOldAmount();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //实际水量=本期水量+换表前水量
|
|
|
+ Amount = readingNew.subtract(readingOld).add(replaceAmount);
|
|
|
|
|
|
- if(!replace){
|
|
|
- if (readingNew.compareTo(tempReading) > 0)
|
|
|
- Amount = Amount.add(readingNew.subtract(tempReading));
|
|
|
- }
|
|
|
|
|
|
//计量方式判断
|
|
|
if (calculateway.equals(1))//按实际用量
|
|
@@ -238,17 +237,13 @@ public class AmountSynServiceImpl implements AmountSynService {
|
|
|
if (payAmount.compareTo(BigDecimal.ZERO) < 0) {
|
|
|
continue;
|
|
|
}
|
|
|
-
|
|
|
amountWaterUsedAmountDto.setReading(readingNew);
|
|
|
amountWaterUsedAmountDto.setRecorddate(DateTimeUtil.parseDate(readDate, DateTimeUtil.DATE_TIME_FORMAT_SECOND));
|
|
|
amountWaterUsedAmountDto.setAmount(Amount);
|
|
|
amountWaterUsedAmountDto.setPayamount(payAmount);
|
|
|
-
|
|
|
updates.add(amountWaterUsedAmountDto);
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
if (updates.size() > 0) {
|
|
|
//List<List<AmountWaterUsedAmountDto>> lists = Lists.partition(updates, 500);
|
|
@@ -256,10 +251,6 @@ public class AmountSynServiceImpl implements AmountSynService {
|
|
|
amountWaterUsedAmountMapper.batchUpdate(amountWaterUsedAmount);
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
log.info("水量同步结束:" + LocalDateTime.now());
|
|
|
-
|
|
|
- }
|
|
|
+ }
|
|
|
}
|