Quellcode durchsuchen

增加 返回失败信息带具体描述

zhangchenglong vor 2 Jahren
Ursprung
Commit
e601e6a65e

+ 23 - 23
in-passback/src/main/java/com/jiayue/passback/controller/InApiController.java

@@ -48,7 +48,7 @@ public class InApiController {
         // 如果校验结果为失败
         if (JsonResultUtil.Type.failure.value().equals(checkResult.get(JsonResultUtil.CODE_TAG))) {
             log.info("获取用户信息失败");
-            return JsonResultUtil.failure();
+            return checkResult;
         }
         // 根据用户名密码获取token
         JSONObject result = apiService.getToken(checkResult.getJSONObject(JsonResultUtil.DATA_TAG));
@@ -67,7 +67,7 @@ public class InApiController {
         JSONObject checkResult = checkRequest(request,data);
         // 如果校验结果为失败
         if (JsonResultUtil.Type.failure.value().equals(checkResult.get(JsonResultUtil.CODE_TAG))) {
-            return JsonResultUtil.failure();
+            return checkResult;
         }
         // 返回短期数据json格式
         JSONObject result = apiService.getShortWpfData(data);
@@ -86,7 +86,7 @@ public class InApiController {
         JSONObject checkResult = checkRequest(request,data);
         // 如果校验结果为失败
         if (JsonResultUtil.Type.failure.value().equals(checkResult.get(JsonResultUtil.CODE_TAG))) {
-            return JsonResultUtil.failure();
+            return checkResult;
         }
         JSONObject result = apiService.putShortWpfData(data);
         // 返回短期数据json格式
@@ -102,10 +102,10 @@ public class InApiController {
     @PostMapping("/getSShortWpfData")
     public JSONObject getSShortWpfData(HttpServletRequest request, @RequestBody JSONObject data) {
         //校验token
-        JSONObject checkResult = checkToken(request);
+        JSONObject checkResult = checkRequest(request,data);
         // 如果校验结果为失败
         if (JsonResultUtil.Type.failure.value().equals(checkResult.get(JsonResultUtil.CODE_TAG))) {
-            return JsonResultUtil.failure();
+            return checkResult;
         }
         // 返回超短期数据json格式
         return apiService.getSShortWpfData(data);
@@ -120,10 +120,10 @@ public class InApiController {
     @PostMapping("/putSShortWpfData")
     public JSONObject putSShortWpfData(HttpServletRequest request, @RequestBody JSONObject data) {
         //校验token
-        JSONObject checkResult = checkToken(request);
+        JSONObject checkResult = checkRequest(request,data);
         // 如果校验结果为失败
         if (JsonResultUtil.Type.failure.value().equals(checkResult.get(JsonResultUtil.CODE_TAG))) {
-            return JsonResultUtil.failure();
+            return checkResult;
         }
         // 返回短期数据json格式
         return apiService.putSShortWpfData(data);
@@ -139,13 +139,13 @@ public class InApiController {
         String authorization = request.getHeader(CommonStant.AUTHORIZATION);
         // 如果授权信息不存在,返回失败
         if (authorization == null) {
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("授权信息不存在");
         }
         // 将"用户名:密码"格式的数据拆分
         String[] authorizationArr = authorization.split(CommonStant.CHARACTER_COLON);
         // 如果根据冒号拆分后的数据长度小于2,则返回失败
         if (authorizationArr.length < 2) {
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("用户密码格式不正确");
         }
         // 放入用户信息
         JSONObject userInfo = new JSONObject();
@@ -166,7 +166,7 @@ public class InApiController {
         JSONObject checkResult = checkAuthorization(request);
         // 如果校验结果为失败
         if (JsonResultUtil.Type.failure.value().equals(checkResult.get(JsonResultUtil.CODE_TAG))) {
-            return JsonResultUtil.failure();
+            return checkResult;
         }
         // 如果授权格式校验成功,获取用户名和token
         JSONObject userInfo = checkResult.getJSONObject(JsonResultUtil.DATA_TAG);
@@ -179,7 +179,7 @@ public class InApiController {
         // 如果token不同,校验失败
         if (!requestToken.equals(token)) {
             log.error("====================token验证失败====================");
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("token验证失败");
         }
         return JsonResultUtil.success();
     }
@@ -193,29 +193,29 @@ public class InApiController {
         // 验证token
         JSONObject tokenResult = checkToken(httpRequest);
         if (JsonResultUtil.Type.failure.value().equals(tokenResult.get(JsonResultUtil.CODE_TAG))) {
-            return JsonResultUtil.failure();
+            return tokenResult;
         }
         // 如果入参为空
         if (requestJson == null) {
             log.info("接收到的数据为空");
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("接收到的数据为空");
         }
         // 获取场站id
         String stationCode = requestJson.getStr("wfId");
         // 如果场站id为空
         if (stationCode == null || "".equals(stationCode)) {
             log.info("场站id为空");
-            return JsonResultUtil.failure();
-        }
-        // 根据场站编号查询场站信息
-        QueryWrapper<ElectricField> wrapper = new QueryWrapper();
-        wrapper.eq("C_STATION_CODE",stationCode);
-        ElectricField electricField = electricFieldMapper.selectOne(wrapper);
-        // 如果场站信息为空
-        if (electricField == null) {
-            log.info("场站信息为空");
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("场站id为空");
         }
+//        // 根据场站编号查询场站信息
+//        QueryWrapper<ElectricField> wrapper = new QueryWrapper();
+//        wrapper.eq("C_STATION_CODE",stationCode);
+//        ElectricField electricField = electricFieldMapper.selectOne(wrapper);
+//        // 如果场站信息为空
+//        if (electricField == null) {
+//            log.info("场站信息为空");
+//            return JsonResultUtil.failureDesc("场站信息为空");
+//        }
         return JsonResultUtil.success();
     }
 }

+ 44 - 29
in-passback/src/main/java/com/jiayue/passback/service/impl/InApiServiceImpl.java

@@ -26,7 +26,6 @@ import lombok.extern.slf4j.Slf4j;
 import net.jodah.expiringmap.ExpirationPolicy;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import org.springframework.util.DigestUtils;
 import javax.annotation.Resource;
 import java.math.BigDecimal;
@@ -34,7 +33,6 @@ import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
-import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
 
@@ -67,7 +65,7 @@ public class InApiServiceImpl implements InApiService {
      */
     @Override
     public JSONObject getToken(JSONObject userInfo) {
-        CacheUtil.cacheMap.entrySet().forEach(entry ->log.info("==========user:" + entry.getKey() + "token" + entry.getValue()+"=========="));
+        CacheUtil.cacheMap.entrySet().forEach(entry -> log.info("==========user:" + entry.getKey() + "token" + entry.getValue() + "=========="));
         JSONObject result;
         //获取用户名密码
         String requestUserName = userInfo.getStr(CommonStant.REQUEST_USER_NAME);
@@ -78,7 +76,7 @@ public class InApiServiceImpl implements InApiService {
         SysParameter systemParameter = systemParameterMapper.selectOne(wrapper);
         if (systemParameter == null || systemParameter.getSysValue() == null || "".equals(systemParameter.getSysValue()) || systemParameter.getSysValue().split(CommonStant.CHARACTER_COLON).length != 2) {
             log.error("系统参数TOKEN_USER不正常");
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("系统参数TOKEN_USER不正常");
         }
         String tokenUser = systemParameter.getSysValue();
         // 根据用户名获取密码
@@ -89,7 +87,7 @@ public class InApiServiceImpl implements InApiService {
         // 如果用户名或密码不匹配
         if (!requestUserName.equals(userName) || !requestUserPass.equals(passwordMD5t)) {
             log.error("账户密码不匹配");
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("账户密码不匹配");
         }
         // 根据用户名去缓存中获取token,如果token已存在,则返回token,如果token不存在,则创建新token并返回
         // 根据用户名获取token
@@ -110,7 +108,7 @@ public class InApiServiceImpl implements InApiService {
             result.set(CommonStant.TOKEN, token);
             // 将生成的token放入缓存中
             CacheUtil.cacheMap.put(userName, token, ExpirationPolicy.CREATED, 1440, TimeUnit.MINUTES);
-            CacheUtil.cacheMap.entrySet().forEach(entry ->log.info("==========user:" + entry.getKey() + "token" + entry.getValue()+"=========="));
+            CacheUtil.cacheMap.entrySet().forEach(entry -> log.info("==========user:" + entry.getKey() + "token" + entry.getValue() + "=========="));
         } else {
             // 将缓存中查到的token返回
             result = JsonResultUtil.success();
@@ -130,11 +128,22 @@ public class InApiServiceImpl implements InApiService {
     public JSONObject getShortWpfData(JSONObject requestJson) {
         // 获取场站id
         String stationCode = requestJson.getStr("wfId");
-        // 获取明日短期预测数据
-        List<ForecastPowerShortTerm> shortList = getShortArr();
-        //解析短期文件
-        JSONObject result = fileAnalysis(stationCode, shortList, null, "dq", requestJson.getStr("beginDate"), requestJson.getStr("endDate"));
-        return result;
+        String startTime = requestJson.getStr("beginDate");
+        String endTime = requestJson.getStr("endDate");
+        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
+        try {
+            long startTimeL = sdfDate.parse(startTime).getTime();
+            long oneDay = 24*60*60*1000-1000;
+            long endTimeL = sdfDate.parse(endTime).getTime() + oneDay;
+            // 获取明日短期预测数据
+            List<ForecastPowerShortTerm> shortList = getShortArr(startTimeL, endTimeL);
+            //解析短期文件
+            JSONObject result = fileAnalysis(stationCode, shortList, null, "dq", requestJson.getStr("beginDate"), requestJson.getStr("endDate"));
+            return result;
+        } catch (ParseException e) {
+            log.error(String.valueOf(e));
+            return JsonResultUtil.failure("获取超短期数据失败");
+        }
     }
 
     /**
@@ -149,7 +158,7 @@ public class InApiServiceImpl implements InApiService {
         // 功率json
         JSONObject values = JSONUtil.parseObj(requestJson.getJSONArray("values").get(0));
         if (values == null || values.getJSONArray("datas").size() == 0) {
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("修正短期数据为空");
         }
         // 明日修正后数据Arr
         JSONArray datas = values.getJSONArray("datas");
@@ -157,7 +166,7 @@ public class InApiServiceImpl implements InApiService {
         List<ForecastPowerShortTerm> dqList = getDqListMap(datas);
         if (dqList == null || dqList.size() == 0 || dqList.size() > 96) {
             log.error("修正数据大于96条");
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("修正数据大于96条");
         }
         // 更新
         updateShortTerm(dqList);
@@ -193,7 +202,7 @@ public class InApiServiceImpl implements InApiService {
         // 所有日期的数据Arr
         JSONArray datas = values.getJSONArray("datas");
         if (datas.size() == 0) {
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("修正超短期数据为空");
         }
         // 将json数据转化为ListMap
         List<ForecastPowerUltraShortTerm> cdqListMap = getCdqListMap(datas);
@@ -236,6 +245,10 @@ public class InApiServiceImpl implements InApiService {
             SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             // 如果是短期
             if ("dq".equals(type)) {
+                if (shortList == null || shortList.size() == 0) {
+                    log.error("短期实时数据为空");
+                    return JsonResultUtil.failureDesc("短期实时数据为空");
+                }
                 for (int i = 0; i < shortList.size(); i++) {
                     ForecastPowerShortTerm shortTerm = shortList.get(i);
                     //当条的日期
@@ -274,8 +287,9 @@ public class InApiServiceImpl implements InApiService {
                 //返回短期数据JSONArray
                 resultArr.add(powerJson);
             } else if ("cdq".equals(type)) {
-                if(ultraShortList==null || ultraShortList.size()==0){
-                    return JsonResultUtil.failure();
+                if (ultraShortList == null || ultraShortList.size() == 0) {
+                    log.error("超短期实时数据为空");
+                    return JsonResultUtil.failureDesc("超短期实时数据为空");
                 }
                 cdqFileStartTime = sdfDateTime.format(ultraShortList.get(0).getForecastTime());
                 datas = new JSONArray();
@@ -302,8 +316,8 @@ public class InApiServiceImpl implements InApiService {
                 resultArr.add(powerJson);
             }
         } catch (Exception e) {
-            log.error(stationCode + " 解析当天DQ文件错误:", e);
-            return JsonResultUtil.failure();
+            log.error(stationCode + " 解析当天DQ数据错误:", e);
+            return JsonResultUtil.failureDesc("解析当天DQ数据错误");
         }
         //如果解析成功
         return JsonResultUtil.success(resultArr);
@@ -331,7 +345,7 @@ public class InApiServiceImpl implements InApiService {
                 forecastPowerShortTerm.setPredictionModelEnum(PredictionModelEnum.E11);
                 forecastPowerShortTerm.setForecastTime(sdf.parse(oneJson.getStr("time")).getTime());
                 forecastPowerShortTerm.setGenDate(now);
-                forecastPowerShortTerm.setFpValue(new BigDecimal(oneJson.getStr("data")).divide(new BigDecimal("1000"),2,BigDecimal.ROUND_HALF_UP));
+                forecastPowerShortTerm.setFpValue(new BigDecimal(oneJson.getStr("data")).divide(new BigDecimal("1000"), 2, BigDecimal.ROUND_HALF_UP));
                 shortTermList.add(forecastPowerShortTerm);
             }
         } catch (ParseException e) {
@@ -357,7 +371,7 @@ public class InApiServiceImpl implements InApiService {
                 // 初始化短期数据实体
                 forecastPowerUltraShortTerm.setGenDate(now);
                 forecastPowerUltraShortTerm.setForecastTime(sdf.parse(oneJson.getStr("time")).getTime());
-                forecastPowerUltraShortTerm.setFpValue(new BigDecimal(oneJson.getStr("data")).divide(new BigDecimal("1000"),2,BigDecimal.ROUND_HALF_UP));
+                forecastPowerUltraShortTerm.setFpValue(new BigDecimal(oneJson.getStr("data")).divide(new BigDecimal("1000"), 2, BigDecimal.ROUND_HALF_UP));
                 forecastPowerUltraShortTerm.setPredictionModelEnum(PredictionModelEnum.E11);
                 ultraShortTermList.add(forecastPowerUltraShortTerm);
             }
@@ -372,14 +386,12 @@ public class InApiServiceImpl implements InApiService {
      *
      * @return
      */
-    public List<ForecastPowerShortTerm> getShortArr() {
+    public List<ForecastPowerShortTerm> getShortArr(long startTime, long endTime) {
         // 获取短期预测数据
         LambdaQueryWrapper<ForecastPowerShortTerm> lambdaQueryWrapper = new LambdaQueryWrapper<>();
-        // 获取明天0点到23:59:59的预测数据
-        long startTime = DateUtil.beginOfDay(DateUtil.tomorrow()).getTime();
-        long endTime = DateUtil.endOfDay(DateUtil.tomorrow()).getTime();
-        lambdaQueryWrapper.between(ForecastPowerShortTerm::getForecastTime, startTime,endTime);
+        lambdaQueryWrapper.between(ForecastPowerShortTerm::getForecastTime, startTime, endTime);
         List<ForecastPowerShortTerm> shortList = forecastPowerShortTermMapper.selectList(lambdaQueryWrapper);
+        shortList.sort(Comparator.comparing(ForecastPowerShortTerm::getForecastTime));
         return shortList;
     }
 
@@ -398,13 +410,14 @@ public class InApiServiceImpl implements InApiService {
         long endDateL = 0;
         try {
             beginDateL = sdf.parse(beginDate).getTime();
-            endDateL = sdf.parse(endDate).getTime()-1;
+            endDateL = sdf.parse(endDate).getTime();
         } catch (ParseException e) {
             e.printStackTrace();
         }
         // 获取明天日期
         lambdaQueryWrapper.between(ForecastPowerUltraShortTerm::getForecastTime, beginDateL, endDateL);
         List<ForecastPowerUltraShortTerm> ultraShortList = forecastPowerUltraShortTermMapper.selectList(lambdaQueryWrapper);
+        ultraShortList.sort(Comparator.comparing(ForecastPowerUltraShortTerm::getForecastTime));
         return ultraShortList;
     }
 
@@ -420,7 +433,8 @@ public class InApiServiceImpl implements InApiService {
         long endTime = DateUtil.endOfDay(DateUtil.tomorrow()).getTime();
         // 根据开始时间和结束时间作为删除的查询条件
         LambdaQueryWrapper<ForecastPowerShortTerm> lambdaQueryWrapper = new LambdaQueryWrapper<>();
-        lambdaQueryWrapper.between(ForecastPowerShortTerm::getForecastTime, startTime,endTime);
+        lambdaQueryWrapper.between(ForecastPowerShortTerm::getForecastTime, startTime, endTime);
+        shortTermList.sort(Comparator.comparing(ForecastPowerShortTerm::getForecastTime));
         // 删除明天的预测数据
         forecastPowerShortTermMapper.delete(lambdaQueryWrapper);
         // 保存明天的预测数据
@@ -438,10 +452,11 @@ public class InApiServiceImpl implements InApiService {
         LambdaQueryWrapper<ForecastPowerUltraShortTerm> lambdaQueryWrapper = new LambdaQueryWrapper<>();
         long startTimeL = ultraShortTermList.stream().mapToLong(ForecastPowerUltraShortTerm::getForecastTime).min().getAsLong();
         long endTimeL = ultraShortTermList.stream().mapToLong(ForecastPowerUltraShortTerm::getForecastTime).max().getAsLong();
-        if(ultraShortTermList.size()>16){
+        if (ultraShortTermList.size() > 16) {
             log.info("====================超短期个数大于16====================");
-            return JsonResultUtil.failure();
+            return JsonResultUtil.failureDesc("超短期个数大于16");
         }
+        ultraShortTermList.sort(Comparator.comparing(ForecastPowerUltraShortTerm::getForecastTime));
         lambdaQueryWrapper.between(ForecastPowerUltraShortTerm::getForecastTime, startTimeL, endTimeL);
         forecastPowerUltraShortTermMapper.delete(lambdaQueryWrapper);
         forecastPowerUltraShortTermService.saveBatch(ultraShortTermList);

+ 40 - 12
in-passback/src/main/java/com/jiayue/passback/util/JsonResultUtil.java

@@ -21,6 +21,9 @@ public class JsonResultUtil {
     /** 数据对象 */
     public static final String DATA_TAG = "data";
 
+    /** 数据对象 */
+    public static final String DESC_TAG = "desc";
+
     /**
      * 状态类型
      */
@@ -57,18 +60,18 @@ public class JsonResultUtil {
         }
     }
 
-    /**
-     * 存入code和msg
-     *
-     * @param type 状态类型
-     */
-    public static JSONObject jsonResult(Type type)
-    {
-        JSONObject result = new JSONObject();
-        result.set(CODE_TAG,type.value);
-        result.set(MSG_TAG,type.msg);
-        return result;
-    }
+//    /**
+//     * 存入code和msg
+//     *
+//     * @param type 状态类型
+//     */
+//    public static JSONObject jsonResult(Type type)
+//    {
+//        JSONObject result = new JSONObject();
+//        result.set(CODE_TAG,type.value);
+//        result.set(MSG_TAG,type.msg);
+//        return result;
+//    }
 
     /**
      * 存入code、data
@@ -88,6 +91,21 @@ public class JsonResultUtil {
     }
 
     /**
+     * 获取带描述的结果
+     * @param type
+     * @param desc
+     * @return
+     */
+    public static JSONObject jsonResultsDesc(Type type, String desc)
+    {
+        JSONObject result = new JSONObject();
+        result.set(CODE_TAG, type.value);
+        result.set(MSG_TAG, type.msg);
+        result.set(DESC_TAG, desc);
+        return result;
+    }
+
+    /**
      * 返回成功消息
      *
      * @return 成功消息
@@ -169,4 +187,14 @@ public class JsonResultUtil {
     {
         return jsonResult(Type.failure,data);
     }
+
+    /**
+     * 返回错误信息内容
+     *
+     * @return
+     */
+    public static JSONObject failureDesc(String desc)
+    {
+        return jsonResultsDesc(Type.failure,desc);
+    }
 }