liulin
2025-07-08 d36ad7a2138062f891000c13d9712b4706f8627c
src/main/java/com/lunhan/water/service/PaymentRecordsService.java
@@ -46,6 +46,7 @@
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
@@ -77,6 +78,8 @@
    private HeartbeatDataMapperImpl heartbeatDataMapper;
    @Autowired
    private FacilityAlarmRecordMapperImpl facilityAlarmRecordMapper;
    @Autowired
    private WaterFacilityRecordsMapperImpl waterFacilityRecordsMapper;
    public ExecutedResult<Long> create(ReqCreatePaymentRecords request) {
        // 转换po
@@ -230,11 +233,10 @@
            return ExecutedResult.failed("余额水量不足!");
        }
        //发送mqtt取水指令
        String topic = "zundong/" + facilityPO.getFacilityCode() + "/switch";
        String topic = "zundong/" + facilityPO.getFacilityCode() + "/send";
        JsonObject data = new JsonObject();
        data.addProperty(facilityPO.getFacilityCode() + "_amount", request.getAmount().multiply(BigDecimal.valueOf(100)));
        data.addProperty(facilityPO.getFacilityCode() + "_user", userLoginPO.getUserCode());
        data.addProperty(facilityPO.getFacilityCode() + "_state", 1);
        data.addProperty(facilityPO.getFacilityCode() + "_count2", request.getAmount().multiply(BigDecimal.valueOf(100)));
        data.addProperty(facilityPO.getFacilityCode() + "_state2", 1);
        boolean send=false;
        if (Objects.nonNull(mqttServer)) {
            send = mqttServer.send(topic, data.toString());
@@ -252,6 +254,35 @@
        if(!send){
            ExecutedResult.failed("通讯断开!");
        }
        //扣除余额
        //创建消费记录
        PaymentRecordsPO recordsPO=new PaymentRecordsPO();
        recordsPO.setUserId(userLoginPO.getId());
        recordsPO.setUserName(userLoginPO.getUserName());
        recordsPO.setFacilityCode(facilityPO.getFacilityCode());
        recordsPO.setPaymentAmount(request.getAmount());
        recordsPO.setWaterAmount(request.getAmount());
        recordsPO.setPayTime(LocalDateTimeUtil.nowDateTimeStr());
        recordsPO.setPayStatus(EBillPayStatus.PAID.getValue());
        recordsPO.setComment("取水");
        recordsPO.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
        mapper.insert(recordsPO);
        //修改余额
        BigDecimal beforeMoney=userLoginPO.getBalance();
        BigDecimal subtract = beforeMoney.subtract(request.getAmount());
        userLoginPO.setBalance(subtract);
        userLoginMapper.updateById(userLoginPO);
        //生成余额变动记录
        UserCapitalChangePO changePO=new UserCapitalChangePO();
        changePO.setUserId(userLoginPO.getId());
        changePO.setBusiness(ECapitalChange.WATER_BILL_COUNTER_PAY.getValue());
        changePO.setBusinessName("取水");
        changePO.setBusinessCode("QS"+SnowFlakeUtil.getId());
        changePO.setChangeMoney(request.getAmount());
        changePO.setBeforeMoney(beforeMoney);
        changePO.setAfterMoney(subtract);
        changePO.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
        userCapitalChangeMapper.insert(changePO);
        return ExecutedResult.success("请点击设备取水按钮取水");
    }
@@ -309,50 +340,34 @@
            case "data":
                uploadData(time,facilityPO,listVo);
                break;
            case "state":
                uploadState(time,facilityPO,listVo);
                break;
//            case "state":
//                uploadState(time,facilityPO,listVo);
//                break;
        }
    }
    public void uploadData(String time,WaterFacilityPO facilityPO, List<CountVO> listVo) {
        CountVO stateVo = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_state")).findFirst().orElse(null);
        CountVO faultVo = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_fault")).findFirst().orElse(null);
        CountVO countVO = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_count")).findFirst().orElse(null);
        CountVO userVo = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_user")).findFirst().orElse(null);
        CountVO stateVo = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_state1")).findFirst().orElse(null);
        CountVO heartbeat = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_heartbeat")).findFirst().orElse(null);
        CountVO countVO = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_count2")).findFirst().orElse(null);
        switch (stateVo.getValue()) {
            case "0"://取水完成
                //扣除余额
                UserLoginPO user = userLoginMapper.get4UserCode(userVo.getValue());
               //创建消费记录
                PaymentRecordsPO recordsPO=new PaymentRecordsPO();
                recordsPO.setUserId(user.getId());
                recordsPO.setUserName(user.getUserName());
                recordsPO.setFacilityCode(facilityPO.getFacilityCode());
                BigDecimal bigDecimal = new BigDecimal(countVO.getValue()).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_DOWN);
                recordsPO.setPaymentAmount(bigDecimal);
                recordsPO.setWaterAmount(bigDecimal);
                recordsPO.setPayTime(time);
                recordsPO.setPayStatus(EBillPayStatus.PAID.getValue());
                recordsPO.setComment("取水");
                recordsPO.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
                mapper.insert(recordsPO);
                //修改余额
                BigDecimal beforeMoney=user.getBalance();
                BigDecimal subtract = beforeMoney.subtract(bigDecimal);
                user.setBalance(subtract);
                userLoginMapper.updateById(user);
                //生成余额变动记录
                UserCapitalChangePO changePO=new UserCapitalChangePO();
                changePO.setUserId(user.getId());
                changePO.setBusiness(ECapitalChange.WATER_BILL_COUNTER_PAY.getValue());
                changePO.setBusinessName("取水");
                changePO.setBusinessCode("QS"+SnowFlakeUtil.getId());
                changePO.setChangeMoney(bigDecimal);
                changePO.setBeforeMoney(beforeMoney);
                changePO.setAfterMoney(subtract);
                changePO.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
                userCapitalChangeMapper.insert(changePO);
                BigDecimal divide = new BigDecimal(countVO.getValue()).divide(new BigDecimal(100)).setScale(2,RoundingMode.DOWN);
                BigDecimal subtract=divide.subtract(facilityPO.getWaterCount());
                if(subtract.compareTo(BigDecimal.ZERO)>0){
                    facilityPO.setWaterCount(divide);
                    int i = waterFacilityMapper.updateById(facilityPO);
                    if(i>0){
                        WaterFacilityRecordsPO recordsPO=new WaterFacilityRecordsPO();
                        recordsPO.setFacilityId(facilityPO.getId());
                        recordsPO.setFacilityName(facilityPO.getFacilityName());
                        recordsPO.setWaterValue(subtract);
                        recordsPO.setUseDate(time);
                        recordsPO.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
                        recordsPO.setRemark("设备供水记录");
                        waterFacilityRecordsMapper.insert(recordsPO);
                    }
                }
                break;
            case "1"://运行
                break;
@@ -361,40 +376,19 @@
            case "3"://故障
                break;
        }
    }
    public void uploadState(String time,WaterFacilityPO facilityPO, List<CountVO> listVo){
        CountVO heartbeatVO = listVo.stream().filter(x -> x.getName().equals(facilityPO.getFacilityCode() + "_heartbeat")).findFirst().orElse(null);
        HeartbeatDataPO heartbeatDataPO = heartbeatDataMapper.getByKey(facilityPO.getFacilityCode());
        if(Objects.nonNull(heartbeatDataPO)){
            heartbeatDataPO.setDataValue(Integer.valueOf(heartbeatVO.getValue()));
            heartbeatDataPO.setDataValue(Integer.valueOf(heartbeat.getValue()));
            heartbeatDataPO.setSendValue(heartbeatDataPO.getSendValue()==0?1:0);
            heartbeatDataMapper.updateById(heartbeatDataPO);
        }else {
            HeartbeatDataPO dataPO=new HeartbeatDataPO();
            dataPO.setDataKey(facilityPO.getFacilityCode());
            dataPO.setSendValue(0);
            dataPO.setDataValue(Integer.valueOf(heartbeatVO.getValue()));
            dataPO.setDataValue(Integer.valueOf(heartbeat.getValue()));
            dataPO.setCreateTime(LocalDateTimeUtil.nowTimeStamp());
            dataPO.setRemark(LocalDateTimeUtil.nowDateTimeStr());
            heartbeatDataMapper.insert(dataPO);
        }
        //发送心跳数据
        // 发送心跳
        String topic = "zundong/"+facilityPO.getFacilityCode()+"/remotecontrol/";
        JsonObject data = new JsonObject();
        data.addProperty(facilityPO.getFacilityCode()+"_heartbeat", heartbeatDataPO.getSendValue()==0?1:0);
        if (Objects.nonNull(mqttServer)) {
            mqttServer.send(topic, data.toString());
        } else {
            try {
                System.out.println("mqttServer bean尚未初始化...");
                mqttServer = SpringUtil.getBean(MQTTServer.class);
                System.out.println("mqttServer 初始化bean成功!");
            } catch (Exception ignored) {
            }
            if (Objects.nonNull(mqttServer)) {
                mqttServer.send(topic, data.toString());
            }
        }
    }
    public ExecutedResult<List<PaymentRecordsVO>> getList(List<Long> listId) {