ElectricityMeterServiceImp.java 6.2 KB
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;
    }


}