فهرست منبع

饲料消耗查询

yjj 2 سال پیش
والد
کامیت
cb5c8c26ef

+ 71 - 17
huimv-env-platform/huimv-env-produce/src/main/java/com/huimv/env/produce/service/impl/FeedDayServiceImpl.java

@@ -1,9 +1,12 @@
 package com.huimv.env.produce.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.huimv.common.utils.Result;
 import com.huimv.common.utils.ResultCode;
+import com.huimv.env.common.utils.DateUtil;
 import com.huimv.env.produce.entity.FeedDay;
+import com.huimv.env.produce.entity.FeedMonth;
 import com.huimv.env.produce.mapper.FeedDayMapper;
 import com.huimv.env.produce.mapper.FeedMonthMapper;
 import com.huimv.env.produce.service.FeedDayService;
@@ -18,7 +21,7 @@ import java.util.Map;
 
 /**
  * <p>
- *  服务实现类
+ * 服务实现类
  * </p>
  *
  * @author zn
@@ -30,50 +33,101 @@ public class FeedDayServiceImpl extends ServiceImpl<FeedDayMapper, FeedDay> impl
     private FeedDayMapper feedDayMapper;
     @Autowired
     private FeedMonthMapper feedMonthMapper;
+    @Autowired
+    private static final String globalFarmCode = "14";
+    @Autowired
+    private DateUtil dateUtil;
+
 
     @Override
     public Result getFeedCount(Map<String, String> paramsMap) {
-        JSONObject resultJo = new JSONObject();
+
+
         //计算今日消耗量、环比
         BigDecimal todayCountBd = new BigDecimal(0);
         BigDecimal todayRatioBd = new BigDecimal(0);
         // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 这里写代码计算今日消耗量、环比
+        String farmCode = paramsMap.get("farm_code");
+        if (farmCode == null) {
+            farmCode = globalFarmCode;
+        }
+        String index = paramsMap.get("index");
+        if (index==""||index==null) {
+            index = "6";
+        }
+        QueryWrapper<FeedDay> queryWrapper = new QueryWrapper();
+        queryWrapper.eq("farm_code", farmCode).orderByDesc("id").last("limit ".concat(index));
+        List<FeedDay> feedDays = feedDayMapper.selectList(queryWrapper);
+        if (!feedDays.isEmpty()) {
+            todayCountBd = feedDays.get(0).getConsumption();
+            if (feedDays.size() < 2) {
+                todayRatioBd = BigDecimal.ZERO;
+            }
+            BigDecimal yesterdayCountBd = feedDays.get(1).getConsumption();
+            todayRatioBd = (todayCountBd.subtract(yesterdayCountBd)).divide(yesterdayCountBd);
+        } else {
+            todayCountBd = BigDecimal.ZERO;
+            todayRatioBd = BigDecimal.ZERO;
+        }
 
         //计算本月消耗量、环比
         BigDecimal thisMonthCountBd = new BigDecimal(0);
         BigDecimal thisMonthRatioBd = new BigDecimal(0);
         // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 这里写代码计算本月消耗量、环比
+        QueryWrapper<FeedMonth> queryWrapper1 = new QueryWrapper<>();
+        queryWrapper1.eq("farm_code", farmCode).orderByDesc("id").last("limit ".concat(index));
+        List<FeedMonth> feedMonths = feedMonthMapper.selectList(queryWrapper1);
+        if (!feedMonths.isEmpty()) {
+            thisMonthCountBd = feedMonths.get(0).getConsumption();
+            if (feedDays.size() < 2) {
+                thisMonthRatioBd = BigDecimal.ZERO;
+            }
+            BigDecimal LastMonthCountBd = feedDays.get(1).getConsumption();
+            thisMonthRatioBd = (thisMonthRatioBd.subtract(LastMonthCountBd)).divide(LastMonthCountBd);
+        } else {
+            thisMonthCountBd = BigDecimal.ZERO;
+            thisMonthRatioBd = BigDecimal.ZERO;
+        }
 
+        JSONObject resultJo = new JSONObject();
         //计算历史记录
-        List<Map<String,Object>> consumptionList = _getFeedHistoryConsumption(paramsMap);
+        List<Object> consumptionList = _getFeedHistoryConsumption(paramsMap);
 
         //今日消耗量
-        resultJo.put("todayCount",todayCountBd.toString());
+        resultJo.put("todayCount", todayCountBd.toString());
         //今日环比
-        resultJo.put("todayRatio",todayRatioBd.toString());
+        resultJo.put("todayRatio", todayRatioBd.toString());
         //本月消耗量
-        resultJo.put("thisMonthCount",thisMonthCountBd.toString());
+        resultJo.put("thisMonthCount", thisMonthCountBd.toString());
         //本月环比
-        resultJo.put("thisMonthRatio",thisMonthRatioBd.toString());
+        resultJo.put("thisMonthRatio", thisMonthRatioBd.toString());
         //每月消耗记录
-        resultJo.put("consumptionList",consumptionList);
-        return new Result(ResultCode.SUCCESS,resultJo);
+        resultJo.put("consumptionList", consumptionList);
+        return new Result(ResultCode.SUCCESS, resultJo);
     }
 
-    private List<Map<String, Object>> _getFeedHistoryConsumption(Map<String, String> paramsMap) {
+    private List<Object> _getFeedHistoryConsumption(Map<String, String> paramsMap) {
         String months = paramsMap.get("months");
         String days = paramsMap.get("days");
-
-        List<Map<String, Object>> consumptionList = new ArrayList<>();
-        if(months == null && days != null){
+        String farmCode = paramsMap.get("farm_code");
+        if (farmCode == null) {
+            farmCode = globalFarmCode;
+        }
+        List<Object> consumptionList = new ArrayList<>();
+        if (months == null && days != null) {
             //读取N天历史记录,以正序返回
             //+++++++++++++++++++++++++++++++++++++++++++++++
-
-
-        }else if(months != null && days == null){
+            QueryWrapper<FeedDay> queryWrapper = new QueryWrapper<>();
+            queryWrapper.eq("farm_code", farmCode).orderByAsc("id").last("limit ".concat(days));
+            List<FeedDay> feedDays = feedDayMapper.selectList(queryWrapper);
+            consumptionList.addAll(feedDays);
+        } else if (months != null && days == null) {
             //读取N月历史记录,以正序返回
             //+++++++++++++++++++++++++++++++++++++++++++++++
-
+            QueryWrapper<FeedMonth> queryWrapper2 = new QueryWrapper<>();
+            queryWrapper2.eq("farm_code", farmCode).orderByAsc("id").last("limit ".concat(months));
+            List<FeedMonth> feedMonths = feedMonthMapper.selectList(queryWrapper2);
+            consumptionList.addAll(feedMonths);
 
         }
         return consumptionList;

+ 4 - 4
huimv-env-platform/huimv-env-produce/src/test/java/com/huimv/env/produce/util/DateTest.java

@@ -22,7 +22,7 @@ public class DateTest {
         System.out.println("this="+dateUtil.getThisMonthAndYear());
     }
 
-    @Test
+   /* @Test
     public void test2() {
         DateUtil dateUtil = new DateUtil();
         String month = dateUtil.getBeforeMonthAndYear(0);
@@ -32,8 +32,8 @@ public class DateTest {
         String month2 = dateUtil.getBeforeMonthAndYear(2);
         System.out.println("month2="+month2);
 
-    }
-
+    }*/
+/*
     @Test
     public void test3() {
         DateUtil dateUtil = new DateUtil();
@@ -43,5 +43,5 @@ public class DateTest {
             System.out.println("month="+month);
         }
 
-    }
+    }*/
 }