作者 shenhailong

解决 充值电费后 送电不成功问题

package com.sunyo.energy.location.dao;
import com.sunyo.energy.location.model.ElectrifyInfo;
import java.util.List;
public interface ElectrifyInfoMapper {
int deleteByPrimaryKey(Integer id);
int insert(ElectrifyInfo record);
int insertSelective(ElectrifyInfo record);
ElectrifyInfo selectByPrimaryKey(Integer id);
List<ElectrifyInfo> selectAll();
int updateByPrimaryKeySelective(ElectrifyInfo record);
int updateByPrimaryKey(ElectrifyInfo record);
}
\ No newline at end of file
... ...
package com.sunyo.energy.location.model;
public class ElectrifyInfo {
private Integer id;
private String deviceid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDeviceid() {
return deviceid;
}
public void setDeviceid(String deviceid) {
this.deviceid = deviceid == null ? null : deviceid.trim();
}
}
\ No newline at end of file
... ...
package com.sunyo.energy.location.model;
import lombok.Data;
/**
* @author shenhailong
* <p>
* 2020/7/20/09:47
*/
@Data
public class ProwerResult {
private String errcode;
private String errmessage;
private ProwerResultData data;
}
... ...
package com.sunyo.energy.location.model;
import lombok.Data;
import java.util.List;
/**
* @author shenhailong
* <p>
* 2020/7/20/09:48
*/
@Data
public class ProwerResultData {
private List<ProwerResultDatas> datas;
}
... ...
package com.sunyo.energy.location.model;
import lombok.Data;
/**
* @author shenhailong
* <p>
* 2020/7/20/09:48
*/
@Data
public class ProwerResultDatas {
private Integer deviceId;
private Integer action;
private Boolean success;
@Override
public String toString() {
return "ProwerResultDatas{" +
"deviceId=" + deviceId +
", action=" + action +
", success=" + success +
'}';
}
}
... ...
package com.sunyo.energy.location.service.imp;
import com.alibaba.fastjson.JSON;
import com.sun.deploy.net.HttpUtils;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIGlobalBinding;
import com.sunyo.energy.location.dao.*;
import com.sunyo.energy.location.model.*;
... ... @@ -27,6 +28,9 @@ public class ElectricityMeterServiceImp implements ElectricityMeterService {
@Autowired
PayRecordsMapper payRecordsMapper;
@Autowired
ElectrifyInfoMapper electrifyInfoMapper;
/**
* 电表充值接口地址
*/
... ... @@ -67,16 +71,11 @@ public class ElectricityMeterServiceImp implements ElectricityMeterService {
message = rechargeDevicesResultData.getSuccess();
}
if ("0".equals(rechargeDevicesResult.getErrcode()) && message == true) {
ElectricityMeter energyInfoForRealTime = getEnergyInfoForRealTime(electricityInfo.getDeviceId());
if (energyInfoForRealTime.getBalance() != null){
int i = energyInfoForRealTime.getBalance().compareTo(BigDecimal.ZERO);
if (i > 0){
Map<String, Object> map = new HashMap<>();
map.put("deviceId", electricityInfo.getDeviceId());
map.put("action", "1");
HttpsUtils.sendPost(remoteControlDevices, map);
}
}
// 插入送电临时表
ElectrifyInfo electrifyInfo1 = new ElectrifyInfo();
electrifyInfo1.setDeviceid(electricityInfo.getDeviceId());
electrifyInfoMapper.insertSelective(electrifyInfo1);
// 修改订单状态
payRecordsMapper.updateStatus(electricityInfo.getOrderNumber());
// 成功 删除该订单
electricityInfoMapper.deleteByPrimaryKey(electricityInfo.getOrderNumber());
... ... @@ -129,4 +128,50 @@ public class ElectricityMeterServiceImp implements ElectricityMeterService {
}
/**
* 定时读取电表临时表 送电
*/
@Scheduled(fixedDelay = 30000)
public void electrifyInfo() {
try {
List<ElectrifyInfo> electrifyInfoList = electrifyInfoMapper.selectAll();
for (ElectrifyInfo electrifyInfo: electrifyInfoList ){
ElectricityMeter energyInfoForRealTime = getEnergyInfoForRealTime(electrifyInfo.getDeviceid());
if (energyInfoForRealTime.getBalance() != null){
int i = energyInfoForRealTime.getBalance().compareTo(BigDecimal.ZERO);
log.info("查询该设备余额是否为正数{}", i);
if (i > 0){
log.info("进入送电接口----------");
Map<String, Object> map = new HashMap<>();
map.put("deviceId", electrifyInfo.getDeviceid());
map.put("action", "1");
String postResult = HttpsUtils.sendPost(remoteControlDevices, map);
ProwerResult prowerResult = JSON.parseObject(postResult, ProwerResult.class);
List<ProwerResultDatas> datas = prowerResult.getData().getDatas();
if ("0".equals(prowerResult.getErrcode())){
log.info("送电成功-------------------");
log.info("success-info-RechargeDevicesResult");
System.out.println(datas);
// 成功 删除该订单
electrifyInfoMapper.deleteByPrimaryKey(electrifyInfo.getId());
}else {
// 失败插入临时表
ElectrifyInfo electrifyInfo1 = new ElectrifyInfo();
electrifyInfo1.setDeviceid(electrifyInfo.getDeviceid());
electrifyInfoMapper.insertSelective(electrifyInfo1);
log.info("送电失败插入临时表--------------------------------");
log.info("送电信息插入success---info");
}
}
}
log.info("定时任务处理完成");
}
}catch (Exception e){
e.printStackTrace();
}
}
}
... ...
... ... @@ -184,6 +184,8 @@ public class HttpsUtils {
// 打开URL连接
java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL
.openConnection();
httpConn.setConnectTimeout(50000);
httpConn.setReadTimeout(50000);
// 设置通用属性
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Connection", "Keep-Alive");
... ...
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sunyo.energy.location.dao.ElectrifyInfoMapper" >
<resultMap id="BaseResultMap" type="com.sunyo.energy.location.model.ElectrifyInfo" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="deviceId" property="deviceid" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
id, deviceId
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from electrify_info
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultType="com.sunyo.energy.location.model.ElectrifyInfo">
select
<include refid="Base_Column_List" />
from electrify_info
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from electrify_info
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.sunyo.energy.location.model.ElectrifyInfo" >
insert into electrify_info (id, deviceId)
values (#{id,jdbcType=INTEGER}, #{deviceid,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.sunyo.energy.location.model.ElectrifyInfo" >
insert into electrify_info
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="deviceid != null" >
deviceId,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="deviceid != null" >
#{deviceid,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.sunyo.energy.location.model.ElectrifyInfo" >
update electrify_info
<set >
<if test="deviceid != null" >
deviceId = #{deviceid,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.sunyo.energy.location.model.ElectrifyInfo" >
update electrify_info
set deviceId = #{deviceid,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
\ No newline at end of file
... ...