Browse Source

计算计费金额

guoshuai 1 month ago
parent
commit
f9abb8489e

+ 66 - 3
src/main/java/com/qmrb/system/controller/OrderController.java

@@ -9,13 +9,16 @@ import com.qmrb.system.common.result.PageResult;
 import com.qmrb.system.common.result.Result;
 import com.qmrb.system.common.result.ResultCode;
 import com.qmrb.system.framework.resubmit.Resubmit;
+import com.qmrb.system.mq.PickCarProducer;
 import com.qmrb.system.pojo.dto.LoginParam;
 import com.qmrb.system.pojo.dto.UserParam;
 import com.qmrb.system.pojo.entity.Order;
 import com.qmrb.system.pojo.entity.ToolWxConfig;
+import com.qmrb.system.pojo.form.BarnRecordForm;
 import com.qmrb.system.pojo.form.PayOrderForm;
 import com.qmrb.system.pojo.query.PayOrderQuery;
 import com.qmrb.system.pojo.vo.*;
+import com.qmrb.system.service.IBarnRecordService;
 import com.qmrb.system.service.OrderService;
 import com.qmrb.system.service.ToolWxConfigService;
 import com.qmrb.system.service.UserService;
@@ -33,13 +36,19 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement;
 import jakarta.servlet.http.HttpServletRequest;
 import jakarta.validation.Valid;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.rocketmq.client.exception.MQBrokerException;
+import org.apache.rocketmq.client.exception.MQClientException;
+import org.apache.rocketmq.remoting.exception.RemotingException;
 import org.springdoc.core.annotations.ParameterObject;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.math.BigDecimal;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
@@ -67,6 +76,18 @@ public class OrderController extends CommonController {
 
     @Autowired
     private ToolWxConfigService toolWxConfigService;
+    
+    @Autowired
+    private PickCarProducer pickCarProducer;
+    
+    
+    @Autowired
+    private IBarnRecordService barnRecordService;
+    
+    @Value("${pickCarProducer.topic}")
+    private String topic;
+    @Value("${pickCarProducer.tags}")
+    private String tags;
 
 
     @Operation(summary = "订单表分页列表", security = {@SecurityRequirement(name = "Authorization")})
@@ -77,10 +98,22 @@ public class OrderController extends CommonController {
         Page<PayOrderVO> result = orderService.getPage(queryParams);
         return PageResult.success(result);
     }
+    
+    @Operation(summary = "订单表分页列表", security = {@SecurityRequirement(name = "Authorization")})
+    @GetMapping("/getPageByUserId")
+    public PageResult<PayOrderVO> getPageByUserId(
+            @ParameterObject PayOrderQuery queryParams
+    ) {
+        if(StrUtil.isNotBlank(queryParams.getUserId())){
+            Page<PayOrderVO> result = orderService.getPageByUserId(queryParams);
+            return PageResult.success(result);
+        }
+        
+        return PageResult.success(null);
+    }
 
     @Operation(summary = "新增订单表", security = {@SecurityRequirement(name = "Authorization")})
     @PostMapping
-    @PreAuthorize("@ss.hasPerm('order:add')")
     @Resubmit
     public Result<PayOrderForm> saveForm(
             @RequestBody @Valid PayOrderForm form
@@ -100,7 +133,6 @@ public class OrderController extends CommonController {
 
     @Operation(summary = "修改订单表", security = {@SecurityRequirement(name = "Authorization")})
     @PutMapping(value = "/{id}")
-    @PreAuthorize("@ss.hasPerm('order:edit')")
     public Result<?> updateForm(
             @Parameter(description = "订单表ID") @PathVariable Long id,
             @RequestBody @Validated PayOrderForm form) {
@@ -111,7 +143,6 @@ public class OrderController extends CommonController {
 
     @Operation(summary = "删除订单表", security = {@SecurityRequirement(name = "Authorization")})
     @DeleteMapping("/{ids}")
-    @PreAuthorize("@ss.hasPerm('order:delete')")
     public Result<?> deleteUsers(
             @Parameter(description = "订单表ID,多个以英文逗号(,)分割") @PathVariable String ids
     ) {
@@ -210,6 +241,38 @@ public class OrderController extends CommonController {
         orderService.closeOrder(toolWxConfigService.findConf(),outTradeNo);
     }
 
+    @Operation(summary = "根据岗亭端停车记录数据计费", security = {@SecurityRequirement(name = "Authorization")})
+    @GetMapping("/{id}/charging")
+    public CommonResult<?> pickUp(
+            @Parameter(description = "岗亭端停车记录ID") @PathVariable Long id,
+            @ParameterObject String openId
+    ) throws MQBrokerException, RemotingException, UnsupportedEncodingException, InterruptedException, MQClientException {
+        //todo 调用计费模块,计算是否有应付费用
+        BigDecimal bigDecimal = new BigDecimal(1);
+
+        BarnRecordForm formData = barnRecordService.chargingByBarnId(id);
+        if(formData != null && formData.getPendingAmount() != null && formData.getPendingAmount().compareTo(BigDecimal.ZERO) >0){
+            ToCreateOrderVO toCreateOrderVO = new ToCreateOrderVO();
+            toCreateOrderVO.setTotal(formData.getPendingAmount().multiply(new BigDecimal(100)).intValueExact());
+            toCreateOrderVO.setDescription("停车缴费");
+            toCreateOrderVO.setOpenId(openId);
+            
+            //订单入库
+            orderService.save(new Order().setAmountsPayable(toCreateOrderVO.getTotal())
+                    .setPayDescription(toCreateOrderVO.getDescription())
+                    .setId(id)
+                    .setPayStatus(SystemConstant.NUM_ZERO));
+            return process(() -> orderService.createOrder(toolWxConfigService.findConf(),toCreateOrderVO));
+        }else if(formData != null && formData.getPendingAmount() != null && formData.getPendingAmount().compareTo(BigDecimal.ZERO) <= 0){
+            HashMap<String, Object> paramsMap = new HashMap<>();
+            paramsMap.put("channelId",formData.getOutChannelId());
+            paramsMap.put("carNumber",formData.getCarNumber());
+            // 发送取车指令
+            pickCarProducer.send(topic,tags, JSONUtil.toJsonStr(paramsMap));
+        }
+        return CommonResult.success(null);
+    }
+
     @PostMapping("/wechatRefundBack")
     public String wechatRefundBack(HttpServletRequest request) {
         ToolWxConfig wxConfig = toolWxConfigService.findConf();

+ 3 - 0
src/main/java/com/qmrb/system/mapper/BarnRecordMapper.java

@@ -2,7 +2,9 @@ package com.qmrb.system.mapper;
 
 import com.qmrb.system.pojo.entity.BarnRecord;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.qmrb.system.pojo.form.BarnRecordForm;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * <p>
@@ -15,4 +17,5 @@ import org.apache.ibatis.annotations.Mapper;
 @Mapper
 public interface BarnRecordMapper extends BaseMapper<BarnRecord> {
 
+    BarnRecordForm chargingByBarnId(@Param("orderId") Long orderId);
 }

+ 5 - 0
src/main/java/com/qmrb/system/mapper/OrderMapper.java

@@ -1,8 +1,12 @@
 package com.qmrb.system.mapper;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.qmrb.system.pojo.entity.Order;
+import com.qmrb.system.pojo.query.PayOrderQuery;
+import com.qmrb.system.pojo.vo.PayOrderVO;
 import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
 
 /**
  * <p>
@@ -15,4 +19,5 @@ import org.apache.ibatis.annotations.Mapper;
 @Mapper
 public interface OrderMapper extends BaseMapper<Order> {
 
+    Page<PayOrderVO> getPageByUserId(Page<Object> objectPage,@Param("queryParams") PayOrderQuery queryParams);
 }

+ 2 - 0
src/main/java/com/qmrb/system/pojo/query/PayOrderQuery.java

@@ -22,6 +22,8 @@ public class PayOrderQuery extends BasePageQuery{
 
  	@Schema(description="关键字")
     private String keywords;
+     
+     private String userId;
 
     /**
      * 本系统生成的订单

+ 13 - 0
src/main/java/com/qmrb/system/pojo/vo/PayOrderVO.java

@@ -3,6 +3,8 @@ package com.qmrb.system.pojo.vo;
 import java.math.BigDecimal;
 import java.util.Date;
 import java.util.List;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.Data;
 
@@ -26,6 +28,7 @@ public class PayOrderVO {
      * 下单时间
      */
 	@Schema(description = "下单时间",type="Date")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date orderTime;
     /**
      * 支付金额
@@ -61,17 +64,27 @@ public class PayOrderVO {
      * 支付时间
      */
 	@Schema(description = "支付时间",type="Date")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date payTime;
     /**
      * 退款时间
      */
 	@Schema(description = "退款时间",type="Date")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date refundTime;
     /**
      * 取消时间
      */
 	@Schema(description = "取消时间",type="Date")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date cancelTime;
+    
+    /**
+     * 新增时间
+     */
+	@Schema(description = "新增时间",type="Date")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date createTime;
     /**
      * 支付机构(缩写之类的)
      */

+ 3 - 0
src/main/java/com/qmrb/system/pojo/vo/UserInfoVO.java

@@ -23,6 +23,9 @@ public class UserInfoVO {
 
     @Schema(description="头像地址")
     private String avatar;
+    
+    @Schema(description="手机号")
+    private String phone;
 
     @Schema(description="用户角色编码集合")
     private Set<String> roles;

+ 9 - 0
src/main/java/com/qmrb/system/service/IBarnRecordService.java

@@ -41,4 +41,13 @@ public interface IBarnRecordService extends IService<BarnRecord> {
 
 
 	List<BarnRecordForm> saveBatchOrUpdate(@Valid List<BarnRecordForm> list);
+
+	/**
+	 * 计算停车记录的剩余应付金额
+	 * @param id
+	 * @return
+	 */
+	BarnRecordForm chargingByBarnId(Long id);
+
+	BarnRecordForm getByOrderId(String orderNo);
 }

+ 3 - 0
src/main/java/com/qmrb/system/service/OrderService.java

@@ -11,6 +11,7 @@ import com.qmrb.system.pojo.vo.ReCreateOrderVO;
 import com.qmrb.system.pojo.vo.ToCreateOrderVO;
 import com.qmrb.system.pojo.vo.WxLoginVO;
 import jakarta.validation.Valid;
+import org.springframework.web.bind.annotation.RequestBody;
 
 /**
  * <p>
@@ -136,5 +137,7 @@ public interface OrderService extends IService<Order> {
     String queryRefundOrder(ToolWxConfig wxConfig, String refundNo);
 
 
+    Page<PayOrderVO> getPageByUserId(PayOrderQuery queryParams);
 
+    void createOrderByRecord(ToCreateOrderVO toCreatOrderVO);
 }

+ 19 - 1
src/main/java/com/qmrb/system/service/impl/BarnRecordServiceImpl.java

@@ -5,14 +5,19 @@ import java.time.LocalDateTime;
 import java.time.ZoneId;
 import java.time.temporal.ChronoUnit;
 import java.util.*;
+import java.util.stream.Collectors;
 
 import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.bean.copier.CopyOptions;
 import cn.hutool.core.date.DateUnit;
 import cn.hutool.core.date.DateUtil;
 import com.qmrb.system.pojo.entity.CarPark;
 import com.qmrb.system.pojo.entity.MyCarPlateNumber;
+import com.qmrb.system.pojo.vo.ToCreateOrderVO;
 import com.qmrb.system.service.ICarParkService;
 import com.qmrb.system.service.IMyCarPlateNumberService;
+import org.springframework.beans.BeanUtils;
+import com.qmrb.system.service.OrderService;
 import org.springframework.beans.factory.annotation.Autowired;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
@@ -190,7 +195,6 @@ public class BarnRecordServiceImpl extends ServiceImpl<BarnRecordMapper, BarnRec
     public List<BarnRecordForm> saveBatchOrUpdate(@Valid List<BarnRecordForm> list) {
         List<BarnRecord> saveList = new ArrayList<>();
         List<BarnRecord> updateList = new ArrayList<>();
-        List<BarnRecord> result = new ArrayList<>();
         for (BarnRecordForm barnRecordForm : list) {
             BarnRecord barnRecord = new BarnRecord();
             BeanUtil.copyProperties(barnRecordForm, barnRecord);
@@ -213,6 +217,20 @@ public class BarnRecordServiceImpl extends ServiceImpl<BarnRecordMapper, BarnRec
         return list;
     }
 
+    @Override
+    public BarnRecordForm chargingByBarnId(Long orderId) {
+        if(orderId == null){
+            return null;
+        }
+        BarnRecordForm barnRecordForm = baseMapper.chargingByBarnId(orderId);
+        return barnRecordForm;
+    }
+
+    @Override
+    public BarnRecordForm getByOrderId(String orderNo) {
+        return null;
+    }
+
 
     /**
      * 计算两个时间差(年,月,星期,日,时,分,秒)

+ 44 - 1
src/main/java/com/qmrb/system/service/impl/OrderServiceImpl.java

@@ -3,6 +3,7 @@ package com.qmrb.system.service.impl;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.lang.Assert;
 import cn.hutool.core.text.StrFormatter;
+import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.RandomUtil;
 import cn.hutool.core.util.StrUtil;
@@ -21,13 +22,16 @@ import com.qmrb.system.converter.PayOrderConverter;
 import com.qmrb.system.mapper.OrderMapper;
 import com.qmrb.system.pojo.entity.Order;
 import com.qmrb.system.pojo.entity.ToolWxConfig;
+import com.qmrb.system.pojo.form.BarnRecordForm;
 import com.qmrb.system.pojo.form.PayOrderForm;
 import com.qmrb.system.pojo.query.PayOrderQuery;
 import com.qmrb.system.pojo.vo.PayOrderVO;
 import com.qmrb.system.pojo.vo.ReCreateOrderVO;
 import com.qmrb.system.pojo.vo.ToCreateOrderVO;
 import com.qmrb.system.pojo.vo.WxLoginVO;
+import com.qmrb.system.service.IBarnRecordService;
 import com.qmrb.system.service.OrderService;
+import com.qmrb.system.service.ToolWxConfigService;
 import com.qmrb.system.utils.WxPayUtil;
 import com.qmrb.system.wxpayback.Enum.WxApiType;
 import com.qmrb.system.wxpayback.Enum.WxPayStatusEnum;
@@ -50,9 +54,11 @@ import org.springframework.stereotype.Service;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.net.URISyntaxException;
 import java.nio.charset.StandardCharsets;
 import java.security.PrivateKey;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.locks.ReentrantLock;
@@ -74,7 +80,11 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
 
     private final ReentrantLock reentrantLock2 = new ReentrantLock();
 
-
+    @Autowired
+    private ToolWxConfigService toolWxConfigService;
+    
+    @Autowired
+    private IBarnRecordService barnRecordService;
     @Autowired
     private PayOrderConverter converter;
 
@@ -276,6 +286,12 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
                     if(WxPayUtil.verifyMoney(node, queryAmountByOrderNo(orderNo))) {
                         //2.对比成功 账单状态 已支付
                         updateStatus(SystemConstant.NUM_ONE,orderNo);
+                        //   更新计费金额,已收金额
+                        BarnRecordForm barnRecordForm = barnRecordService.getByOrderId(orderNo);
+                        Order order = this.getById(orderNo);
+                        barnRecordForm.setReceivedAmount(barnRecordForm.getReceivedAmount().add(new BigDecimal(order.getAmountsPayable())));
+                        barnRecordForm.setBillingStartTime(new Date());
+                        barnRecordService.updateForm(barnRecordForm.getId(),barnRecordForm);
                     }
                     //支付成功 就返回true
                     return true;
@@ -310,6 +326,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
             httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
             CloseableHttpResponse response = WxPayUtil.getHttpClient(wxConfig, WxPayUtil.getVerifier(wxConfig)).execute(httpPost);
             log.info("=====关单结果======={}====",response);
+            // 删除订单
+            this.removeById(outTradeNo);
         } catch (Exception e) {
             log.error("关单失败" + outTradeNo + e);
         }
@@ -436,4 +454,29 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
         }
         return null;
     }
+
+    @Override
+    public Page<PayOrderVO> getPageByUserId(PayOrderQuery queryParams) {
+        // 查询参数
+        int pageNum = queryParams.getPageNum();
+        int pageSize = queryParams.getPageSize();
+        //String keywords = queryParams.getKeywords();
+        
+
+        // 实体转换
+        Page<PayOrderVO> pageResult = baseMapper.getPageByUserId(new Page<>(pageNum, pageSize),queryParams);
+        return pageResult;
+    }
+
+    @Override
+    public void createOrderByRecord(ToCreateOrderVO toCreatOrderVO) {
+        Long id = IdUtil.getSnowflake().nextId();
+        toCreatOrderVO.setOutTradeNo(id.toString());
+        //订单入库
+        this.save(new Order().setAmountsPayable(toCreatOrderVO.getTotal())
+                .setPayDescription(toCreatOrderVO.getDescription())
+                .setId(id)
+                .setPayStatus(SystemConstant.NUM_ZERO));
+        this.createOrder(toolWxConfigService.findConf(),toCreatOrderVO);
+    }
 }

+ 1 - 0
src/main/java/com/qmrb/system/service/impl/SysUserServiceImpl.java

@@ -255,6 +255,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
         Set<String> roles = SecurityUtils.getRoles();
         userInfoVO.setRoles(roles);
 
+        userInfoVO.setPhone(user.getMobile());
         // 用户权限集合
         @SuppressWarnings("unchecked")
 		Set<String> perms = (Set<String>) redisTemplate.opsForValue().get(SecurityConstants.USER_PERMS_CACHE_PREFIX+ user.getId());

+ 4 - 0
src/main/resources/mapper/BarnRecordMapper.xml

@@ -2,4 +2,8 @@
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.qmrb.system.mapper.BarnRecordMapper">
 
+    <select id="chargingByBarnId" resultType="com.qmrb.system.pojo.form.BarnRecordForm"
+            parameterType="java.lang.Long">
+        SELECT br.* FROM barn_record br INNER JOIN pay_order po on po.good_id = br.id where po.id = #{orderId}
+    </select>
 </mapper>

+ 11 - 1
src/main/resources/mapper/OrderMapper.xml

@@ -1,5 +1,15 @@
 <?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.pay.wxpayback.mapper.OrderMapper">
+<mapper namespace="com.qmrb.system.mapper.OrderMapper">
 
+    <select id="getPageByUserId" parameterType="com.qmrb.system.pojo.query.PayOrderQuery" 
+            resultType="com.qmrb.system.pojo.vo.PayOrderVO">
+        select 
+            po.* 
+        from 
+            pay_order po INNER JOIN barn_record br on po.good_id = br.id 
+        where br.car_number = (
+        select plate_number from my_car_plate_number where user_id = #{queryParams.userId} order by create_time desc limit 1
+        )
+    </select>
 </mapper>