ElectricityMeterServiceImp.java
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.sunyo.energy.location.service.imp;
import com.alibaba.fastjson.JSON;
import com.sunyo.energy.location.dao.*;
import com.sunyo.energy.location.model.*;
import com.sunyo.energy.location.service.ElectricityMeterService;
import com.sunyo.energy.location.utils.HttpsUtils;
import com.sunyo.energy.location.utils.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ElectricityMeterServiceImp implements ElectricityMeterService {
@Autowired
ElectricityInfoMapper electricityInfoMapper;
@Autowired
PayRecordsMapper payRecordsMapper;
@Autowired
LocationMapper locationMapper;
@Autowired
USERSMapper usersMapper;
/**
* 获取电表实时数据/余额
*/
@Value("${eeUrl.electricityBanlanceUrl}")
private String electricityBanlanceUrl;
/**
* 电表充值接口读取配置文件ip地址
*/
@Value("${eeid.ipAddress}")
private String ipAddress;
/**
* 电表充值接口地址
*/
@Value("${eeUrl.rechargeDevicesUrl}")
private String rechargeDevicesUrl;
@Value("${eeUrl.remoteControlDevices}")
private String remoteControlDevices;
/**
* 实施获取电表数据
*/
@Override
public ElectricityMeter getEnergyInfoForRealTime(String deviceId) {
try {
if (!"".equals(deviceId)) {
Map<String, Object> stringObjectMap = eeInfo(deviceId);
String infoForRealTime = HttpsUtils.sendPost(electricityBanlanceUrl, stringObjectMap);
ElectricityBalanceOne electricityBalanceOne = JSON.parseObject(infoForRealTime, ElectricityBalanceOne.class);
List<ElectricityMeter> infoForRealTimeList = electricityBalanceOne.getData().getDatas();
for (ElectricityMeter electricityBalanceThree : infoForRealTimeList) {
if (electricityBalanceThree != null) {
return electricityBalanceThree;
}
}
}
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 电表充值
*
* @param deviceId 设备编号
* @param money 充值金额
* @param actionType 充值类型 0 充值 1 扣费
* @param orderNumber 订单号
* @return
*/
@Override
public int rechargeDevices(String deviceId, String money, String actionType, String orderNumber) {
Map<String, Object> stringObjectMap = mapCommon(actionType, deviceId, money, ipAddress);
// 设置secret值 设备id 跟金额 盐 需要进行加密
BigDecimal bigDecimal = new BigDecimal(money).setScale(2, RoundingMode.HALF_UP);
String moneyString = String.valueOf(bigDecimal);
String secret = Md5Utils.getMD5(deviceId + moneyString + "tiansu", true, 32);
stringObjectMap.put("secret", secret);
log.info("电表充值信息{}", stringObjectMap.toString());
try {
String result = HttpsUtils.sendPost(rechargeDevicesUrl, stringObjectMap);
log.info("电表充值反馈结果:{}", result);
RechargeDevicesResult rechargeDevicesResult = JSON.parseObject(result, RechargeDevicesResult.class);
List<RechargeDevicesResultData> data = rechargeDevicesResult.getData().getDatas();
Boolean message = null;
for (RechargeDevicesResultData rechargeDevicesResultData : data) {
message = rechargeDevicesResultData.getSuccess();
}
if ("0".equals(rechargeDevicesResult.getErrcode()) && message == true) {
log.info("电表充值成功,充值额度:{}", money);
/**
* 充值成功送电
*/
ElectricityMeter energyInfoForRealTime = getEnergyInfoForRealTime(deviceId);
if (energyInfoForRealTime.getBalance() != null){
int i = energyInfoForRealTime.getBalance().compareTo(BigDecimal.ZERO);
if (i > 0){
Map<String, Object> map = new HashMap<>();
map.put("deviceId", deviceId);
map.put("action", "1");
HttpsUtils.sendPost(remoteControlDevices, map);
}
}
return 1;
} else {
log.info("电表充值失败,充值额度:{}", money);
/**
* 插入临时表 电表接口充值因为硬件问题可能失败
*/
ElectricityInfo electricityInfo = new ElectricityInfo();
electricityInfo.setOrderNumber(orderNumber);
electricityInfo.setActionType(actionType);
electricityInfo.setDeviceId(deviceId);
electricityInfo.setMoney(new BigDecimal(money).setScale(2, RoundingMode.HALF_UP));
electricityInfo.setIpAddress(ipAddress);
electricityInfo.setSecret(secret);
int i = electricityInfoMapper.insertSelective(electricityInfo);
log.info("插入电表临时表成功:{},插入数据{}", i, electricityInfo);
return 0;
}
} catch (Exception e) {
e.printStackTrace();
log.error("电表充值失败,充值额度:{}", money);
return 0;
}
}
public Map<String, Object> eeInfo(String deviceId) {
Map<String, Object> datas = new HashMap<>();
datas.put("deviceId", deviceId);
return datas;
}
public Map<String, Object> mapCommon(String actionType, String deviceId, String money, String ip_address) {
Map<String, Object> map = new HashMap<>();
map.put("actionType", actionType);
map.put("deviceId", deviceId);
map.put("money", money);
map.put("ipAddress", ip_address);
return map;
}
}