From a071c70355a0ac580ff124e6fbc4bf4c07d9efb0 Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期三, 06 八月 2025 12:30:27 +0800
Subject: [PATCH] 所有代码

---
 ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ChargeOrderServiceImpl.java |  150 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 127 insertions(+), 23 deletions(-)

diff --git a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ChargeOrderServiceImpl.java b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ChargeOrderServiceImpl.java
index f836ffc..0560b15 100644
--- a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ChargeOrderServiceImpl.java
+++ b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/ChargeOrderServiceImpl.java
@@ -1,15 +1,19 @@
 package com.ruoyi.order.service.impl;
 
 import cn.hutool.core.util.IdUtil;
-import com.alibaba.fastjson2.JSONObject;
+import cn.hutool.json.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.account.api.feignClient.AppUserClient;
 import com.ruoyi.account.api.feignClient.UserPointClient;
 import com.ruoyi.account.api.model.AppUser;
 import com.ruoyi.account.api.model.UserPoint;
+import com.ruoyi.chargingPile.api.feignClient.SiteClient;
+import com.ruoyi.chargingPile.api.model.Site;
 import com.ruoyi.common.core.constant.ExpressCompanyMap;
 import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.dto.ChargingOrderGroup;
+import com.ruoyi.common.core.dto.ChargingPercentProvinceDto;
 import com.ruoyi.common.core.exception.ServiceException;
 import com.ruoyi.common.core.utils.StringUtils;
 import com.ruoyi.common.core.web.page.PageInfo;
@@ -21,14 +25,22 @@
 import com.ruoyi.order.service.ChargeOrderService;
 import com.ruoyi.order.service.OrderService;
 import com.ruoyi.order.vo.ConfirmDelivery;
+import com.ruoyi.other.api.domain.IntegralRecord;
 import com.ruoyi.other.api.domain.Region;
+import com.ruoyi.other.api.domain.TIntegralRule;
+import com.ruoyi.other.api.feignClient.OtherClient;
 import com.ruoyi.system.api.domain.SysConfig;
+import com.ruoyi.system.api.domain.SysUser;
 import com.ruoyi.system.api.feignClient.SysConfigClient;
+import com.ruoyi.system.api.feignClient.SysUserClient;
+import io.swagger.models.auth.In;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import java.io.IOException;
@@ -36,6 +48,8 @@
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.time.LocalDateTime;
 import java.time.LocalTime;
@@ -53,28 +67,39 @@
     private OrderService orderService;
     @Resource
     private SysConfigClient sysConfigClient;
-
+    @Resource
+    private OtherClient otherClient;
     @Resource
     private AppUserClient appUserClient;
     @Resource
     private UserPointClient userPointClient;
     @Resource
     private ChargeOrderService chargeOrderService;
-    private static final String DEFAULT_AVATAR_URL = "http://qijishenghuiyuan.obs.cn-southwest-2.myhuaweicloud.com/admin/aedfbbb41280471f8d9fa7905298b65f.png";
+    @Resource
+    private SiteClient siteClient;
+
+    @Resource
+    private SysUserClient sysUserClient;
+    private static final String DEFAULT_AVATAR_URL = "http://qijishenghuiyuan.obs.cn-southwest-2.myhuaweicloud.com/admin/58084a0ce8c34dc79f9d8b4c0bc3774a.png";
 
 
     @Override
-    public void importExpress(String url) {
+    public R<JSONObject> importExpress(MultipartFile file) {
+ /*       System.out.println("导入订单url:"+url);
         URL url1 = null;
         try {
-            url1 = new URL(url);
+            String encodedUrl = new URI(url).toASCIIString();
+            url1 = new URL(encodedUrl);
         } catch (MalformedURLException e) {
             throw new RuntimeException(e);
-        }
+        } catch (URISyntaxException e) {
+            throw new RuntimeException(e);
+        }*/
         List<String> orderNumberList=new ArrayList<>();
-
-        try (InputStream fileInputStream = url1.openStream()) {
-            Workbook workbook = new XSSFWorkbook(fileInputStream);
+        JSONObject result = new JSONObject();
+        try (InputStream inputStream = file.getInputStream();
+             Workbook workbook = new XSSFWorkbook(inputStream)) {
+//            Workbook workbook = new XSSFWorkbook(fileInputStream);
             Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet
             int lastRowNum = sheet.getLastRowNum();
             for (int i = 1; i <= lastRowNum; i++) {//第二行开始
@@ -129,7 +154,7 @@
                 }
 
                 // 结束时间
-                Cell endTimeCell = row.getCell(6);
+                Cell endTimeCell = row.getCell(7);
                 if (endTimeCell == null) {
                     throw new ServiceException("第" + (i + 1) + "行开始时间为空", 500);
                 }
@@ -229,14 +254,18 @@
 
                 //检查订单号是否重复
                 if (orderNumberList.contains(orderNum)) {
-                    throw new ServiceException("订单号重复:" + orderNum, 500);
+//                    throw new ServiceException("订单号重复:" + orderNum, 500);
+                    result.append("订单号:[", orderNum+"]重复");
+                    continue;
                 }
                 //数据库中是否存在
                 ChargeOrder order = chargeOrderService.getOne(new LambdaQueryWrapper<ChargeOrder>()
                         .eq(ChargeOrder::getOrderNumber, orderNum)
                 );
                 if (order != null) {
-                    throw new ServiceException("数据库中订单已存在:" + orderNum, 500);
+//                    throw new ServiceException("数据库中订单已存在:" + orderNum, 500);
+                    result.append("订单号:[", orderNum+"]数据库中订单已存在");
+                    continue;
                 }
                 orderNumberList.add(orderNum);//添加到订单集合中
 
@@ -270,6 +299,10 @@
             e.printStackTrace();
             throw new ServiceException(e.getMessage());
         }
+        if(!result.isEmpty()){
+            return R.ok(result);
+        }
+        return R.ok();
     }
 
     @Override
@@ -280,11 +313,19 @@
         return pageInfo;
     }
 
-    private R importData(ChargeOrder chargeOrder) {
-        //先计算出积分数
-        Integer point = getPoint(chargeOrder.getTotalAmount());
-        //获取用户,更改积分数
+    @Override
+    public List<ChargingOrderGroup> chargingOrderGroup(ChargingPercentProvinceDto chargingPercentProvinceDto) {
+
+        return this.baseMapper.chargingOrderGroup(chargingPercentProvinceDto);
+    }
+
+    @Transactional
+    public R importData(ChargeOrder chargeOrder) {
+        Site site = siteClient.getSiteAll().getData().stream().filter(e -> e.getId()
+                .equals(chargeOrder.getPowerStationId())).findFirst().orElse(null);
+        Integer point=0;
         AppUser appuser = appUserClient.getAppUserByPhone1(chargeOrder.getPhone()).getData();
+        //获取用户,更改积分数
         if (null==appuser){
             //创建用户
             appuser = new AppUser();
@@ -295,19 +336,41 @@
             appuser.setAvatar( DEFAULT_AVATAR_URL);
             appuser.setCreateTime(LocalDateTime.now());
             appuser.setDelFlag(false);
+            appuser.setName(appuser.getPhone().substring(0, 3) + "****" + appuser.getPhone().substring(7));
         }
-        Integer chargePoint=appuser.getRechargePoint();//充值积分
-        Integer availablePoint=appuser.getAvailablePoint();//可用积分
-        Integer totalPoint=appuser.getTotalPoint();//总积分
-        appuser.setRechargePoint(chargePoint==null?0:chargePoint +point);//充值积分增加
-        appuser.setAvailablePoint(availablePoint==null?0:availablePoint+point);//可用积分增加
-        appuser.setTotalPoint(totalPoint==null?0:totalPoint+point);//总积分增加
+        Integer chargePoint=appuser.getRechargePoint()==null?0:appuser.getRechargePoint();//充值积分
+        Integer availablePoint=appuser.getAvailablePoint()==null?0:appuser.getAvailablePoint();//可用积分
+        Integer totalPoint=appuser.getTotalPoint()==null?0:appuser.getTotalPoint();//总积分
+        appuser.setRechargePoint(chargePoint +point);//充值积分增加
+        appuser.setAvailablePoint(availablePoint+point);//可用积分增加
+        appuser.setTotalPoint(totalPoint+point);//总积分增加
 
         Long userId = appUserClient.saveOrUpdateAppUser(appuser);
         if (userId==null){
             return R.fail("导入订单-保存用户失败");
         }
-
+        if (site==null){
+            //先计算出积分数
+            point = getPoint(chargeOrder.getTotalAmount());
+        }else{
+            point = getPointSite(chargeOrder.getTotalAmount(),site.getId());
+            // 增加站点端积分明细
+            IntegralRecord integralRecord = new IntegralRecord();
+            integralRecord.setSiteId(chargeOrder.getPowerStationId());
+            integralRecord.setAppUserId(appuser.getId());
+            integralRecord.setIntegralType(2);
+            integralRecord.setIntegralCount(point);
+            integralRecord.setDelFlag(0);
+            integralRecord.setCreateTime(LocalDateTime.now());
+            otherClient.saveIntegralRecord(integralRecord);
+            // 扣除站点系统用户的积分数
+            List<SysUser> data = sysUserClient.getSysUserAll().getData();
+            SysUser sysUser = data.stream().filter(e -> e.getSiteId().equals(site.getId())).findFirst().orElse(null);
+            if (sysUser != null) {
+                sysUser.setIntegral(sysUser.getIntegral()==null?0:sysUser.getIntegral()-point);
+                sysUserClient.updateSysUser(sysUser);
+            }
+        }
         //添加用户积分流水
         //转入用户
         UserPoint userPoint = new UserPoint();
@@ -323,6 +386,7 @@
             return R.fail("导入订单-保存用户积分流水失败");
         }
         //保存到数据库
+        chargeOrder.setPoint(point);
         chargeOrderService.save(chargeOrder);
 
         return R.ok();
@@ -364,5 +428,45 @@
         }
 
     }
+    /**
+     * 获取现金兑换的积分数
+     * @param cash
+     * @return
+     */
+    public Integer getPointSite(BigDecimal cash,Integer siteId){
+        if (cash == null || cash.compareTo(BigDecimal.ZERO) < 0) {
+            throw new IllegalArgumentException("金额不能为null或负数");
+        }
+        // 获取积分兑换比例配置
+        TIntegralRule data = otherClient.getSetBySiteId(siteId).getData();
+        if (data == null) {
+            throw new RuntimeException("获取积分兑换比例配置失败");
+
+        }
+        if (org.springframework.util.StringUtils.hasLength(data.getChargeCredit())) {
+            throw new RuntimeException("获取积分兑换比例配置失败");
+        }
+        String chargeCredit = data.getChargeCredit();
+        if (StringUtils.isBlank(chargeCredit)) {
+            throw new RuntimeException("积分兑换比例配置值为空");
+        }
+        try {
+            com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(chargeCredit);
+            Integer num1 = jsonObject.getInteger("num1");
+
+            if (num1 <= 0) {
+                throw new RuntimeException("积分兑换比例必须大于0");
+            }
+            // 计算积分并向下取整(Floor)
+            int cashInt = cash.intValue();
+            return cashInt*=num1;
+
+        } catch (NumberFormatException e) {
+            throw new RuntimeException("积分兑换比例配置值格式错误", e);
+        } catch (ArithmeticException e) {
+            throw new RuntimeException("积分计算结果溢出", e);
+        }
+
+    }
 
 }

--
Gitblit v1.7.1