作者 shenhailong

电费充值 接口完善 电表支付成功 充值失败 插入临时表

... ... @@ -127,7 +127,7 @@ public class PayRseponeController {
String eeId = payRecordsMapper.findOrderNumber(ORDERID);
if (!"".equals(eeId)){
// 通知充值电费
int eeResult = electricityMeterService.rechargeDevices(eeId, PAYMENT, "0");
int eeResult = electricityMeterService.rechargeDevices(eeId, PAYMENT, "0", ORDERID);
if (eeResult > 0){
// 修改订单状态
int i1 = payRecordsMapper.updateByPrimaryKey(payRecords);
... ... @@ -144,6 +144,12 @@ public class PayRseponeController {
jsonObject.put("success", "N");
webSocketServer.broadcast(jsonObject.toJSONString());
}
}else {
WebSocketServer webSocketServer = new WebSocketServer();
JSONObject jsonObject = new JSONObject();
jsonObject.put("orderNumber", ORDERID);
jsonObject.put("success", "S");
webSocketServer.broadcast(jsonObject.toJSONString());
}
}
}else {
... ...
package com.sunyo.energy.location.dao;
import com.sunyo.energy.location.model.ElectricityInfo;
import java.util.List;
public interface ElectricityInfoMapper {
int deleteByPrimaryKey(String orderNumber);
int insert(ElectricityInfo record);
int insertSelective(ElectricityInfo record);
ElectricityInfo selectByPrimaryKey(String orderNumber);
List<ElectricityInfo> selectAll();
int updateByPrimaryKeySelective(ElectricityInfo record);
int updateByPrimaryKey(ElectricityInfo record);
}
\ No newline at end of file
... ...
... ... @@ -25,4 +25,6 @@ public interface PayRecordsMapper {
String findOrderNumber(String orderId);
boolean selectType(String orderId);
int updateStatus(String orderNumber);
}
\ No newline at end of file
... ...
package com.sunyo.energy.location.model;
import java.math.BigDecimal;
import java.util.Date;
public class ElectricityInfo {
private String orderNumber;
private String actionType;
private String deviceId;
private BigDecimal money;
private String ipAddress;
private String secret;
private String status;
private Date opertTime;
private Date editTime;
private String infoStatus;
public String getOrderNumber() {
return orderNumber;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber == null ? null : orderNumber.trim();
}
public String getActionType() {
return actionType;
}
public void setActionType(String actionType) {
this.actionType = actionType == null ? null : actionType.trim();
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId == null ? null : deviceId.trim();
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress == null ? null : ipAddress.trim();
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret == null ? null : secret.trim();
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status == null ? null : status.trim();
}
public Date getOpertTime() {
return opertTime;
}
public void setOpertTime(Date opertTime) {
this.opertTime = opertTime;
}
public Date getEditTime() {
return editTime;
}
public void setEditTime(Date editTime) {
this.editTime = editTime;
}
public String getInfoStatus() {
return infoStatus;
}
public void setInfoStatus(String infoStatus) {
this.infoStatus = infoStatus == null ? null : infoStatus.trim();
}
@Override
public String toString() {
return "ElectricityInfo{" +
"orderNumber='" + orderNumber + '\'' +
", actionType='" + actionType + '\'' +
", deviceId='" + deviceId + '\'' +
", money=" + money +
", ipAddress='" + ipAddress + '\'' +
", secret='" + secret + '\'' +
", status='" + status + '\'' +
", opertTime=" + opertTime +
", editTime=" + editTime +
", infoStatus='" + infoStatus + '\'' +
'}';
}
}
\ No newline at end of file
... ...
... ... @@ -14,4 +14,6 @@ public class RechargeDevicesResult {
private String errmessage;
private RechargeDevicesResultDatas data;
}
... ...
package com.sunyo.energy.location.model;
import lombok.Data;
/**
* @author shenhailong
* <p>
* 2020/6/4/14:32
*/
@Data
public class RechargeDevicesResultData {
/**
* 设备id
*/
private String deviceId;
/**
* 充值金额
*/
private Number money;
/**
* 充值状态
*/
private Boolean success;
}
... ...
package com.sunyo.energy.location.model;
import lombok.Data;
import java.util.List;
/**
* @author shenhailong
* <p>
* 2020/6/4/14:29
*/
@Data
public class RechargeDevicesResultDatas {
private List<RechargeDevicesResultData> datas;
}
... ...
... ... @@ -28,6 +28,6 @@ public interface ElectricityMeterService {
* @param actionType
* @return
*/
int rechargeDevices(String deviceId, String money, String actionType);
int rechargeDevices(String deviceId, String money, String actionType, String orderNumber);
}
... ...
package com.sunyo.energy.location.service.imp;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.sun.deploy.net.HttpUtils;
import com.sunyo.energy.location.controller.response.ResultJson;
import com.sunyo.energy.location.dao.ElectricityInfoMapper;
import com.sunyo.energy.location.dao.ElectricityMeterMapper;
import com.sunyo.energy.location.dao.PayRecordsMapper;
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 sun.security.provider.MD5;
import java.awt.*;
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
ElectricityMeterMapper electricityMeterMapper;
/**
* 获取房间与设备信息
*/
private final static String electricityUrl = "/api/emcs/getRoomAndDevice";
/**
* 获取所有电表实时通断电状态
*/
private final static String electricityStatus = "/api/emcs/getAllDeviceControlStatus";
@Autowired
ElectricityInfoMapper electricityInfoMapper;
@Autowired
PayRecordsMapper payRecordsMapper;
/**
* 获取电表实时数据/余额
*/
... ... @@ -93,38 +90,94 @@ public class ElectricityMeterServiceImp implements ElectricityMeterService {
}
@Override
public int rechargeDevices(String deviceId, String money, String actionType) {
public int rechargeDevices(String deviceId, String money, String actionType, String orderNumber) {
Map<String, Object> map = new HashMap<>();
map.put("actionType", actionType);
map.put("deviceId", deviceId);
map.put("money", money);
map.put("ipAddress", ipAddress);
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);
map.put("secret", secret);
stringObjectMap.put("secret", secret);
log.info("电表充值信息{}",stringObjectMap.toString());
try {
String result = HttpsUtils.sendPost(rechargeDevicesUrl, map);
String result = HttpsUtils.sendPost(rechargeDevicesUrl, stringObjectMap);
log.info("电表充值反馈结果:{}",result);
RechargeDevicesResult rechargeDevicesResult = JSON.parseObject(result, RechargeDevicesResult.class);
if ("0".equals(rechargeDevicesResult.getErrcode())){
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);
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;
}
return 0;
}
public Map<String, Object> eeInfo(String deviceId){
Map<String, Object> datas = new HashMap<>();
datas.put("deviceId", deviceId);
return datas;
}
/**
* 定时读取电表临时表 发起充值
*/
@Scheduled(fixedDelay = 5000)
public void electricityInfo(){
List<ElectricityInfo> electricityInfos = electricityInfoMapper.selectAll();
if (electricityInfos.size() > 0){
for (ElectricityInfo electricityInfo: electricityInfos){
Map<String, Object> stringObjectMap = mapCommon(electricityInfo.getActionType(),
electricityInfo.getDeviceId(),
String.valueOf(electricityInfo.getMoney()),
electricityInfo.getIpAddress());
stringObjectMap.put("secret", electricityInfo.getSecret());
String result = HttpsUtils.sendPost(rechargeDevicesUrl, stringObjectMap);
log.info("电表临时表充值请求信息:{}", electricityInfo.toString());
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){
payRecordsMapper.updateStatus(electricityInfo.getOrderNumber());
electricityInfo.setStatus("0");
electricityInfoMapper.updateByPrimaryKeySelective(electricityInfo);
}
}
}
}
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;
}
}
... ...
<?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.ElectricityInfoMapper" >
<resultMap id="BaseResultMap" type="com.sunyo.energy.location.model.ElectricityInfo" >
<id column="order_number" property="orderNumber" jdbcType="VARCHAR" />
<result column="action_type" property="actionType" jdbcType="VARCHAR" />
<result column="device_id" property="deviceId" jdbcType="VARCHAR" />
<result column="money" property="money" jdbcType="DECIMAL" />
<result column="ip_address" property="ipAddress" jdbcType="VARCHAR" />
<result column="secret" property="secret" jdbcType="VARCHAR" />
<result column="status" property="status" jdbcType="VARCHAR" />
<result column="opert_time" property="opertTime" jdbcType="TIMESTAMP" />
<result column="edit_time" property="editTime" jdbcType="TIMESTAMP" />
<result column="info_status" property="infoStatus" jdbcType="VARCHAR" />
</resultMap>
<sql id="Base_Column_List" >
order_number, action_type, device_id, money, ip_address, secret, status, opert_time,
edit_time, info_status
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from electricity_info
where order_number = #{orderNumber,jdbcType=VARCHAR}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from electricity_info
where status = '1'
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from electricity_info
where order_number = #{orderNumber,jdbcType=VARCHAR}
</delete>
<insert id="insert" parameterType="com.sunyo.energy.location.model.ElectricityInfo" >
insert into electricity_info (order_number, action_type, device_id,
money, ip_address, secret,
status, opert_time, edit_time,
info_status)
values (#{orderNumber,jdbcType=VARCHAR}, #{actionType,jdbcType=VARCHAR}, #{deviceId,jdbcType=VARCHAR},
#{money,jdbcType=DECIMAL}, #{ipAddress,jdbcType=VARCHAR}, #{secret,jdbcType=VARCHAR},
#{status,jdbcType=VARCHAR}, #{opertTime,jdbcType=TIMESTAMP}, #{editTime,jdbcType=TIMESTAMP},
#{infoStatus,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.sunyo.energy.location.model.ElectricityInfo" >
insert into electricity_info
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="orderNumber != null" >
order_number,
</if>
<if test="actionType != null" >
action_type,
</if>
<if test="deviceId != null" >
device_id,
</if>
<if test="money != null" >
money,
</if>
<if test="ipAddress != null" >
ip_address,
</if>
<if test="secret != null" >
secret,
</if>
<if test="status != null" >
status,
</if>
<if test="opertTime != null" >
opert_time,
</if>
<if test="editTime != null" >
edit_time,
</if>
<if test="infoStatus != null" >
info_status,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="orderNumber != null" >
#{orderNumber,jdbcType=VARCHAR},
</if>
<if test="actionType != null" >
#{actionType,jdbcType=VARCHAR},
</if>
<if test="deviceId != null" >
#{deviceId,jdbcType=VARCHAR},
</if>
<if test="money != null" >
#{money,jdbcType=DECIMAL},
</if>
<if test="ipAddress != null" >
#{ipAddress,jdbcType=VARCHAR},
</if>
<if test="secret != null" >
#{secret,jdbcType=VARCHAR},
</if>
<if test="status != null" >
#{status,jdbcType=VARCHAR},
</if>
<if test="opertTime != null" >
#{opertTime,jdbcType=TIMESTAMP},
</if>
<if test="editTime != null" >
#{editTime,jdbcType=TIMESTAMP},
</if>
<if test="infoStatus != null" >
#{infoStatus,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.sunyo.energy.location.model.ElectricityInfo" >
update electricity_info
<set >
<if test="actionType != null" >
action_type = #{actionType,jdbcType=VARCHAR},
</if>
<if test="deviceId != null" >
device_id = #{deviceId,jdbcType=VARCHAR},
</if>
<if test="money != null" >
money = #{money,jdbcType=DECIMAL},
</if>
<if test="ipAddress != null" >
ip_address = #{ipAddress,jdbcType=VARCHAR},
</if>
<if test="secret != null" >
secret = #{secret,jdbcType=VARCHAR},
</if>
<if test="status != null" >
status = #{status,jdbcType=VARCHAR},
</if>
<if test="opertTime != null" >
opert_time = #{opertTime,jdbcType=TIMESTAMP},
</if>
<if test="editTime != null" >
edit_time = #{editTime,jdbcType=TIMESTAMP},
</if>
<if test="infoStatus != null" >
info_status = #{infoStatus,jdbcType=VARCHAR},
</if>
</set>
where order_number = #{orderNumber,jdbcType=VARCHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.sunyo.energy.location.model.ElectricityInfo" >
update electricity_info
set action_type = #{actionType,jdbcType=VARCHAR},
device_id = #{deviceId,jdbcType=VARCHAR},
money = #{money,jdbcType=DECIMAL},
ip_address = #{ipAddress,jdbcType=VARCHAR},
secret = #{secret,jdbcType=VARCHAR},
status = #{status,jdbcType=VARCHAR},
opert_time = #{opertTime,jdbcType=TIMESTAMP},
edit_time = #{editTime,jdbcType=TIMESTAMP},
info_status = #{infoStatus,jdbcType=VARCHAR}
where order_number = #{orderNumber,jdbcType=VARCHAR}
</update>
</mapper>
\ No newline at end of file
... ...
... ... @@ -61,6 +61,12 @@
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateStatus" parameterType="string">
update pay_records
set payStatus = 1
where orderNumber = #{value, jdbcType=VARCHAR}
</update>
<insert id="insert" parameterType="com.sunyo.energy.location.model.PayRecords" >
insert into pay_records (id, payFees, payUserId,
payTime, payLocationId, payStatus,
... ...