思平 停车场

订单结束之后,我会发送mq消息到小程序后端得微服务,你在微服务中处理调用停车场接口。
有几点要注意:
    1.最好发送http前记录一笔减免记录。
    2.因为调用公网的http接口可能会存在网络问题,延迟啊超时啊,导致事物时间拉长,所以你看下要不要将减免记录新增和
      停车场http接口调用事物分开处理
    3.然后减免记录估计会有失败重试的功能,你看下可能会加定时器

一。保证接收到停车场的减免记录会有记录。不要因为特殊异常或者停车场接口问题 导致这笔减免丢失

在这里插入图片描述

二。独立service出来做定时服务

package com.nebula.microservice.appbackend.parkdiscounts.service;

import com.nebula.microservice.appbackend.parkdiscounts.domain.OperationReductionRecord;
import com.nebula.microservice.appbackend.parkdiscounts.web.dto.*;
import com.nebula.microservice.stype.SParkReductionRecord;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.sql.SQLQueryFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;

@Service
public class ParkDiscountsCronService {

    private final SQLQueryFactory sqlQueryFactory;
    private final ParkDiscountsService parkDiscountsService;;

    public ParkDiscountsCronService(SQLQueryFactory sqlQueryFactory,ParkDiscountsService parkDiscountsService) {
        this.sqlQueryFactory = sqlQueryFactory;
        this.parkDiscountsService = parkDiscountsService;
    }

    /**
     * 定时任务
     *
     * 根据减免记录状态,失败的记录重新调用停车场接口
     */
    @Scheduled(cron = "0 0 23 * * ?")
    @Transactional
    public void recall () {
        //1.搜索所有减免记录
        List<ParkReductionRecordDto> parkReductionRecordDtoList = this.getParkReductionRecordDto();
        if (!CollectionUtils.isEmpty(parkReductionRecordDtoList)){
            for (ParkReductionRecordDto recordDto: parkReductionRecordDtoList){
                //2.遍历减免记录集合,状态失败的重新调用接口
                if (!recordDto.getCode().equals(Integer.parseInt("10000")) && !recordDto.getCode().equals(Integer.parseInt("10002")) && !recordDto.getCode().equals(Integer.parseInt("10003")) && !recordDto.getCode().equals(Integer.parseInt("10006")) && !recordDto.getCode().equals(Integer.parseInt("10007"))){
                    ParkReduceAPIRequestDto requestDto = new ParkReduceAPIRequestDto();
                    if (recordDto.getPlateNo() != null) requestDto.setPlateNo(recordDto.getPlateNo());                                 //车牌号
                    if (recordDto.getParkCode() != null) requestDto.setMerchId(recordDto.getParkCode());                               //停车场唯一标识
                    if (recordDto.getReductionTime() != null) requestDto.setDuration(recordDto.getReductionTime());                    //减免时长
                    if (recordDto.getReductionOrderNo() != null) requestDto.setBizld(recordDto.getReductionOrderNo());                 //减免流水号
                    if (recordDto.getStartChargingTime() != null) requestDto.setStartChargingTime(recordDto.getStartChargingTime());   //充电开始时间
                    if (recordDto.getStopChargingTime() != null) requestDto.setStopChargingTime(recordDto.getStopChargingTime());      //充电结束时间
                    if (recordDto.getSign() != null) requestDto.setSign(recordDto.getSign());                                          //签名
                    ParkReduceAPIResultDto resultDto = parkDiscountsService.callPark(requestDto);
                    //更新结果
                    if (resultDto != null){
                        Integer code = Integer.parseInt(resultDto.get("code").toString());      //调用公网返回状态码
                        String msg = resultDto.get("msg").toString();                           //调用公网返回消息
                        String data = resultDto.get("data").toString();                         //调用公网返回数据
                        SParkReductionRecord sParkReductionRecord = SParkReductionRecord.parkReductionRecord;
                        sqlQueryFactory.update(sParkReductionRecord)
                                .set(sParkReductionRecord.prrCode,code)
                                .set(sParkReductionRecord.prrMsg,msg)
                                .set(sParkReductionRecord.prrData,data)
                                .where(sParkReductionRecord.prrParkId.eq(recordDto.getId()))
                                .execute();
                    }
                }
            }
        }
    }

    /**
     * 减免记录集合查询
     *
     * @return  减免记录集合
     */
    private List<ParkReductionRecordDto> getParkReductionRecordDto() {
        SParkReductionRecord sParkReductionRecord = SParkReductionRecord.parkReductionRecord;

        Map<String, Expression<?>> resultMap = new HashMap<>();
        resultMap.put("id", sParkReductionRecord.prrParkId);
        resultMap.put("stationCode", sParkReductionRecord.prrStationCode);
        resultMap.put("stationName", sParkReductionRecord.prrStationName);
        resultMap.put("parkName", sParkReductionRecord.prrParkName);
        resultMap.put("plateNo", sParkReductionRecord.prrPlateNo);
        resultMap.put("reductionOrderNo", sParkReductionRecord.prrReductionOrderNo);
        resultMap.put("startChargingTime", sParkReductionRecord.prrStartChargingTime.stringValue());
        resultMap.put("stopChargingTime", sParkReductionRecord.prrStopChargingTime.stringValue());
        resultMap.put("reductionTime", sParkReductionRecord.prrReductionTime);
        resultMap.put("code", sParkReductionRecord.prrCode);
        resultMap.put("parkCode", sParkReductionRecord.prrParkCode);
        resultMap.put("sign", sParkReductionRecord.prrSign);

        List<Predicate> predicateList = new ArrayList<>();
        List<ParkReductionRecordDto> parkReductionRecordList = sqlQueryFactory
                .select(Projections.bean(ParkReductionRecordDto.class, resultMap))
                .from(sParkReductionRecord)
                .where(predicateList.toArray(new Predicate[0])).fetch();
        return parkReductionRecordList;
    }
}

三.MQ的发送

package com.nebula.microservice.operationmanage.rabbitmq.provider;

import com.nebula.microservice.operationmanage.organization.web.dto.StationWeatherDTO;
import com.nebula.microservice.operationmanage.rabbitmq.DefaultProcess;
import com.nebula.module.common.message.ManualStopChargeMessage;
import com.nebula.module.common.message.RefundMessage;
import com.nebula.module.common.message.ServerUpdateMessage;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;

import javax.inject.Inject;

/**
 * mq消息生产者实现
 * Created by lisp
 * 2019/3/15
 */
@EnableBinding(DefaultProcess.class)
public class MessageProvider {

    private final MessageChannel refundChannel;
    private final MessageChannel pileUpgradeChannel;
    private final MessageChannel manualCloseOrderChannel;
    private final MessageChannel weatherOutputChannel;
    @Inject
    public MessageProvider(@Qualifier(value = DefaultProcess.REFUND_OUTPUT) MessageChannel refundChannel,
                           @Qualifier(value = DefaultProcess.PILE_UPGRADE_OUTPUT) MessageChannel pileUpgradeChannel,
                           @Qualifier(value = DefaultProcess.MANUAL_CLOSE_ORDER_OUTPUT) MessageChannel manualCloseOrderChannel,
                           @Qualifier(value = DefaultProcess.STATION_WEATHER_OUTPUT) MessageChannel weatherOutputChannel) {
        this.refundChannel = refundChannel;
        this.pileUpgradeChannel = pileUpgradeChannel;
        this.manualCloseOrderChannel = manualCloseOrderChannel;
        this.weatherOutputChannel=weatherOutputChannel;
    }

    /**
     * 发送退款消息
     *
     * @param refundMessage 退款消息
     */
    public void sendRefundMessage(RefundMessage refundMessage) {
        refundChannel.send(MessageBuilder.withPayload(refundMessage).build());
    }

    /**
     * 发送桩体升级通知
     *
     * @param serverUpdateMessage 桩体升级消息
     */
    public void sendPileUpgrade(ServerUpdateMessage serverUpdateMessage) {
        pileUpgradeChannel.send(MessageBuilder.withPayload(serverUpdateMessage).build());
    }

    /**
     * 人工关闭订单通知
     *
     * @param stopChargeMessage 停止充电消息
     */
    public void sendManualCloseOrder(ManualStopChargeMessage stopChargeMessage) {
        manualCloseOrderChannel.send(MessageBuilder.withPayload(stopChargeMessage).build());
    }

    /**
     * 发送站点天气消息
     * @param weatherDTO 天气dto
     */
    public void sendStationWeather(StationWeatherDTO weatherDTO){
        weatherOutputChannel.send(MessageBuilder.withPayload(weatherDTO).build());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飘然生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值