zhuoning 3 роки тому
батько
коміт
2be098ad66

+ 19 - 0
huimv-farm-produce/pom.xml

@@ -11,5 +11,24 @@
 
     <artifactId>huimv-farm-produce</artifactId>
 
+    <dependencies>
+        <!-- JPA -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.7.11</version>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>huimv-farm-admin</artifactId>
+            <version>2.6.1</version>
+            <scope>compile</scope>
+        </dependency>
+    </dependencies>
 
 </project>

+ 120 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/controller/WeightController.java

@@ -0,0 +1,120 @@
+package com.huimv.produce.produce.controller;
+
+import com.huimv.common.utils.Result;
+import com.huimv.produce.produce.service.IWeight;
+import com.huimv.produce.produce.utils.DateUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.text.ParseException;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@RestController
+@RequestMapping(value = "/weight")
+@Slf4j
+public class WeightController {
+    @Autowired
+    private IWeight weight;
+    @Autowired
+    private DateUtil dateUtil;
+
+    //指定查询日期期间
+    @RequestMapping(value = "/getWeight")
+    public Result getDayWeight(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "startDate", required = true) String startDate, @RequestParam(value = "endDate", required = true) String endDate) {
+        log.info("farmId>>" + farmId);
+        log.info("startDate>>" + startDate);
+        log.info("endDate>>" + endDate);
+        //
+        return weight.getDayWeight(farmId, startDate, endDate);
+    }
+
+    //近7次卖猪数据
+    @RequestMapping(value = "/getWeightLastNtimes")
+    public Result getDayWeightLastNtimes(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "days", required = true) Integer days) {
+        log.info("farmId>>" + farmId);
+        log.info("days>>" + days);
+        //
+        return weight.getDayWeightLastNtimes(farmId, days);
+    }
+
+    //本月统计
+    @RequestMapping(value = "/getWeightInOneMonth")
+    public Result getDayWeightInOneMonth(@RequestParam(value = "farmId", required = true) Integer farmId) {
+        log.info("farmId>>" + farmId);
+        String startDateText = dateUtil.getMonthStart();
+        log.info("startDateText>>" + startDateText);
+        String endDateText = dateUtil.getMonthEnd();
+        log.info("endDateText>>" + endDateText);
+        //
+        return weight.getDayWeight(farmId, startDateText, endDateText);
+    }
+
+    //当天统计
+    @RequestMapping(value = "/getWeightOnToday")
+    public Result getDayWeightOnToday(@RequestParam(value = "farmId", required = true) Integer farmId) throws ParseException {
+        log.info("farmId>>" + farmId);
+        String todayDateText = dateUtil.getTodayDateText();
+        log.info("startDate>>" + todayDateText);
+        log.info("endDate>>" + todayDateText);
+        //
+        return weight.getDayWeight(farmId, todayDateText, todayDateText);
+    }
+
+    //查询所有记录带分页
+    @RequestMapping(value = "/getWeightOnPage")
+    public Result getWeightOnPage(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "pageSize", required = true) Integer pageSize,
+                                  @RequestParam(value = "pageNo", required = true) Integer pageNo) throws ParseException {
+        log.info("farmId>>" + farmId);
+        String startDate = dateUtil.getYearStart();
+        String endDate = dateUtil.getYearEnd();
+        log.info("startDate>>" + startDate);
+        log.info("endDate>>" + endDate);
+        log.info("pageNo>>" + pageNo);
+        log.info("pageSize>>" + pageSize);
+        //
+        return weight.getWeightOnPage(farmId, dateUtil.parseDate(startDate), dateUtil.parseDate(endDate), pageNo, pageSize);
+    }
+
+    //分页查询批次重量记录
+    @RequestMapping(value = "/getBatchWeightOnPage")
+    public Result getBatchWeightOnPage(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "pageSize", required = true) Integer pageSize,
+                                       @RequestParam(value = "pageNo", required = true) Integer pageNo,
+                                       @RequestParam(value = "addTime", required = false) String addTime) throws ParseException {
+        log.info("farmId>>" + farmId);
+        log.info("pageNo>>" + pageNo);
+        log.info("pageSize>>" + pageSize);
+        //
+        return weight.getBatchWeightOnPage(farmId, dateUtil.parseDate(addTime), pageNo, pageSize);
+    }
+
+    //分页查询批次重量记录
+    @RequestMapping(value = "/getBatchWeightOnPage2")
+    public Result getBatchWeightOnPage2(@RequestParam(value = "farmId", required = true) Integer farmId, @RequestParam(value = "pageSize", required = true) Integer pageSize,
+                                        @RequestParam(value = "pageNo", required = true) Integer pageNo,
+                                        @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate) throws ParseException {
+        log.info("farmId>>" + farmId);
+        log.info("pageNo>>" + pageNo);
+        log.info("pageSize>>" + pageSize);
+        if(startDate == null){
+            startDate = dateUtil.getYearStart();
+        }
+        if(endDate == null){
+            endDate = dateUtil.getYearEnd();
+        }
+        log.info("startDate>>" + startDate);
+        log.info("endDate>>" + endDate);
+        //
+        return weight.getBatchWeightOnPage2(farmId, dateUtil.parseDate(startDate),dateUtil.parseDate(endDate), pageNo, pageSize);
+//        return weight.getBatchWeightOnPage4(farmId, startDate,endDate, pageNo, pageSize);
+    }
+}

+ 128 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/repo/ProdBatchWeightEntity.java

@@ -0,0 +1,128 @@
+package com.huimv.produce.produce.repo;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "prod_batch_weight")
+public class ProdBatchWeightEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "batch_code")
+    private String batchCode;
+
+    @Column(name = "batch_sort")
+    private Integer batchSort;
+
+    @Column(name = "gross_weight")
+    private Float grossWeight;
+
+    @Column(name = "tare_weight")
+    private Float tareWeight;
+
+    @Column(name = "net_weight")
+    private Float netWeight;
+
+    @Column(name = "trade_id")
+    private Integer tradeId;
+
+    @Column(name = "farm_id")
+    private Integer farmId;
+
+    @Column(name = "add_time")
+    private Timestamp addTime;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setBatchCode(String batchCode) {
+        this.batchCode = batchCode;
+    }
+
+    public String getBatchCode() {
+        return batchCode;
+    }
+
+    public void setBatchSort(Integer batchSort) {
+        this.batchSort = batchSort;
+    }
+
+    public Integer getBatchSort() {
+        return batchSort;
+    }
+
+    public void setGrossWeight(Float grossWeight) {
+        this.grossWeight = grossWeight;
+    }
+
+    public Float getGrossWeight() {
+        return grossWeight;
+    }
+
+    public void setTareWeight(Float tareWeight) {
+        this.tareWeight = tareWeight;
+    }
+
+    public Float getTareWeight() {
+        return tareWeight;
+    }
+
+    public void setNetWeight(Float netWeight) {
+        this.netWeight = netWeight;
+    }
+
+    public Float getNetWeight() {
+        return netWeight;
+    }
+
+    public void setTradeId(Integer tradeId) {
+        this.tradeId = tradeId;
+    }
+
+    public Integer getTradeId() {
+        return tradeId;
+    }
+
+    public void setFarmId(Integer farmId) {
+        this.farmId = farmId;
+    }
+
+    public Integer getFarmId() {
+        return farmId;
+    }
+
+    public void setAddTime(Timestamp addTime) {
+        this.addTime = addTime;
+    }
+
+    public Timestamp getAddTime() {
+        return addTime;
+    }
+
+    @Override
+    public String toString() {
+        return "ProdBatchWeightEntity{" +
+                "id=" + id + '\'' +
+                "batchCode=" + batchCode + '\'' +
+                "batchSort=" + batchSort + '\'' +
+                "grossWeight=" + grossWeight + '\'' +
+                "tareWeight=" + tareWeight + '\'' +
+                "netWeight=" + netWeight + '\'' +
+                "tradeId=" + tradeId + '\'' +
+                "farmId=" + farmId + '\'' +
+                "addTime=" + addTime + '\'' +
+                '}';
+    }
+}

+ 16 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/repo/ProdBatchWeightRepo.java

@@ -0,0 +1,16 @@
+package com.huimv.produce.produce.repo;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.Date;
+
+public interface ProdBatchWeightRepo extends JpaRepository<ProdBatchWeightEntity, Integer>, JpaSpecificationExecutor<ProdBatchWeightEntity> {
+
+    @Query(nativeQuery = true,value = "SELECT * FROM prod_batch_weight WHERE farm_id=?1 AND DATE_FORMAT(add_time,'%Y-%m-%d')>=DATE_FORMAT(?2,'%Y-%m-%d') AND DATE_FORMAT(add_time,'%Y-%m-%d')<=DATE_FORMAT(?3,'%Y-%m-%d') ORDER BY id DESC",
+            countQuery = "SELECT COUNT(*) FROM prod_batch_weight WHERE farm_id=?1 AND DATE_FORMAT(add_time,'%Y-%m-%d')>=DATE_FORMAT(?2,'%Y-%m-%d') AND DATE_FORMAT(add_time,'%Y-%m-%d')<=DATE_FORMAT(?3,'%Y-%m-%d')")
+    Page<ProdBatchWeightEntity> findByFarmIdAndStartDateAndEndDate(Integer farmId, Date startDate, Date endDate, Pageable pageable);
+}

+ 91 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/repo/ProdDayWeightEntity.java

@@ -0,0 +1,91 @@
+package com.huimv.produce.produce.repo;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "prod_day_weight")
+public class ProdDayWeightEntity implements Serializable {
+
+private static final long serialVersionUID = 1L;
+
+@Id
+@GeneratedValue(strategy = GenerationType.IDENTITY)@Column(name = "id", nullable = false)
+private Integer id;
+
+@Column(name = "add_time")
+private Timestamp addTime;
+
+@Column(name = "farm_id")
+private Integer farmId;
+
+@Column(name = "gross_weight")
+private Float grossWeight;
+
+@Column(name = "net_weight")
+private Float netWeight;
+
+@Column(name = "tare_weight")
+private Float tareWeight;
+
+public  void  setId(Integer id) {
+this.id = id;
+}
+
+public Integer getId() {
+return id;
+}
+
+public  void  setAddTime(Timestamp addTime) {
+this.addTime = addTime;
+}
+
+public Timestamp getAddTime() {
+return addTime;
+}
+
+public  void  setFarmId(Integer farmId) {
+this.farmId = farmId;
+}
+
+public Integer getFarmId() {
+return farmId;
+}
+
+public  void  setGrossWeight(Float grossWeight) {
+this.grossWeight = grossWeight;
+}
+
+public Float getGrossWeight() {
+return grossWeight;
+}
+
+public  void  setNetWeight(Float netWeight) {
+this.netWeight = netWeight;
+}
+
+public Float getNetWeight() {
+return netWeight;
+}
+
+public  void  setTareWeight(Float tareWeight) {
+this.tareWeight = tareWeight;
+}
+
+public Float getTareWeight() {
+return tareWeight;
+}
+
+@Override
+public String toString() {
+return "ProdDayWeightEntity{" +
+    "id=" + id + '\'' +
+    "addTime=" + addTime + '\'' +
+    "farmId=" + farmId + '\'' +
+    "grossWeight=" + grossWeight + '\'' +
+    "netWeight=" + netWeight + '\'' +
+    "tareWeight=" + tareWeight + '\'' +
+    '}';
+}
+}

+ 17 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/repo/ProdDayWeightRepo.java

@@ -0,0 +1,17 @@
+package com.huimv.produce.produce.repo;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
+
+public interface ProdDayWeightRepo extends JpaRepository<ProdDayWeightEntity, Integer>, JpaSpecificationExecutor<ProdDayWeightEntity> {
+    //
+    @Query(nativeQuery = true,value = "SELECT * FROM prod_day_weight WHERE farm_id=?1 AND DATE_FORMAT(add_time,'%Y-%m-%d') >= DATE_FORMAT(?2,'%Y-%m-%d') AND DATE_FORMAT(add_time,'%Y-%m-%d') <= DATE_FORMAT(?3,'%Y-%m-%d')")
+    List<ProdDayWeightEntity> findByFarmIdAndStartAndEndDate(Integer farmId, String startDate, String endDate);
+
+    //
+    @Query(nativeQuery = true,value = "SELECT * FROM prod_day_weight WHERE farm_id=?1 ORDER BY id DESC LIMIT ?2")
+    List<ProdDayWeightEntity> findByFarmIdAndDays(Integer farmId, Integer days);
+}

+ 31 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/service/IWeight.java

@@ -0,0 +1,31 @@
+package com.huimv.produce.produce.service;
+
+
+
+import com.huimv.common.utils.Result;
+
+import java.text.ParseException;
+import java.util.Date;
+
+public interface IWeight {
+    //查询称重
+    Result getDayWeight(Integer farmId, String startDate, String endDate);
+
+    //查询近几次卖猪记录
+    Result getDayWeightLastNtimes(Integer farmId, Integer days);
+
+    //分页查询称重记录
+    Result getWeightOnPage(Integer farmId, Date startDate, Date endDate, Integer pageNo, Integer pageSize);
+
+    //分页查询批次称重记录
+    Result getBatchWeightOnPage(Integer farmId, Date parseDate, Integer pageNo, Integer pageSize);
+
+    //分页查询批次称重记录
+    Result getBatchWeightOnPage(Integer farmId, Date parseDate, Date parseDate1, Integer pageNo, Integer pageSize) throws ParseException;
+
+    Result getBatchWeightOnPage2(Integer farmId, Date startDate, Date endDate, Integer pageNo, Integer pageSize) throws ParseException;
+
+    Result getBatchWeightOnPage3(Integer farmId, Date parseDate, Date parseDate1, Integer pageNo, Integer pageSize);
+
+    Result getBatchWeightOnPage4(Integer farmId, String startDate, String endDate, Integer pageNo, Integer pageSize);
+}

+ 331 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/service/impl/WeightImpl.java

@@ -0,0 +1,331 @@
+package com.huimv.produce.produce.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.common.utils.Result;
+import com.huimv.common.utils.ResultCode;
+import com.huimv.produce.produce.repo.ProdBatchWeightEntity;
+import com.huimv.produce.produce.repo.ProdBatchWeightRepo;
+import com.huimv.produce.produce.repo.ProdDayWeightEntity;
+import com.huimv.produce.produce.repo.ProdDayWeightRepo;
+import com.huimv.produce.produce.service.IWeight;
+import com.huimv.produce.produce.utils.DateUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.*;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import javax.persistence.criteria.Order;
+import javax.persistence.criteria.Predicate;
+import java.math.BigDecimal;
+import java.text.ParseException;
+import java.util.*;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Service
+public class WeightImpl implements IWeight {
+    @Autowired
+    private ProdDayWeightRepo dayWeightRepo;
+    @Autowired
+    private ProdBatchWeightRepo batchWeightRepo;
+    @Autowired
+    private DateUtil dateUtil;
+
+    /**
+     * @Method : getDayWeight
+     * @Description :
+     * @Params : [farmId, startDate, endDate]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/7
+     * @Time : 13:58
+     */
+    @Override
+    public Result getDayWeight(Integer farmId, String startDate, String endDate) {
+        //
+        List<ProdDayWeightEntity> dayWeightEntityList = dayWeightRepo.findByFarmIdAndStartAndEndDate(farmId, startDate, endDate);
+        if (dayWeightEntityList.size() > 0) {
+            BigDecimal totalBd = new BigDecimal(0);
+            for (int a = 0; a < dayWeightEntityList.size(); a++) {
+                ProdDayWeightEntity dayWeightEntity = dayWeightEntityList.get(a);
+                totalBd = new BigDecimal(dayWeightEntity.getNetWeight()).add(totalBd);
+            }
+            JSONObject outJo = new JSONObject();
+            outJo.put("total", totalBd.toString());
+            outJo.put("weight", dayWeightEntityList);
+            return new Result(ResultCode.SUCCESS, outJo);
+        } else {
+            return new Result(10001, "暂无数据.", false);
+        }
+    }
+
+    /**
+     * @Method : getDayWeightLastNtimes
+     * @Description :
+     * @Params : [farmId, days]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/7
+     * @Time : 13:58
+     */
+    @Override
+    public Result getDayWeightLastNtimes(Integer farmId, Integer days) {
+        //
+        List<ProdDayWeightEntity> dayWeightEntityList = dayWeightRepo.findByFarmIdAndDays(farmId, days);
+        if (dayWeightEntityList.size() > 0) {
+            //排序
+            List outList = new ArrayList();
+            for (int a = dayWeightEntityList.size() - 1; a >= 0; a--) {
+                outList.add(dayWeightEntityList.get(a));
+            }
+            //计算总重
+            BigDecimal totalBd = new BigDecimal(0);
+            for (int a = 0; a < dayWeightEntityList.size(); a++) {
+                ProdDayWeightEntity dayWeightEntity = dayWeightEntityList.get(a);
+                totalBd = new BigDecimal(dayWeightEntity.getNetWeight()).add(totalBd);
+            }
+            JSONObject outJo = new JSONObject();
+            outJo.put("total", totalBd.toString());
+            outJo.put("weight", outList);
+            return new Result(ResultCode.SUCCESS, outJo);
+        } else {
+            return new Result(10001, "暂无数据.", false);
+        }
+    }
+
+    /**
+     * @Method : getWeightOnPage
+     * @Description : 分页查询称重记录
+     * @Params : [farmId, startDate, endDate, pageNo, pageSize]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/7
+     * @Time : 14:56
+     */
+    @Override
+    public Result getWeightOnPage(Integer farmId, Date startDate, Date endDate, Integer pageNo, Integer pageSize) {
+        Specification<ProdDayWeightEntity> sf = (Specification<ProdDayWeightEntity>) (root, criteriaQuery, criteriaBuilder) -> {
+            //
+            List<Predicate> predList = new ArrayList<>();
+            if (null != farmId) {
+                predList.add(criteriaBuilder.equal(root.get("farmId").as(Integer.class), farmId));
+            }
+            if (null != startDate) {
+                predList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("addTime").as(Date.class), startDate));
+            }
+            if (null != endDate) {
+                predList.add(criteriaBuilder.lessThanOrEqualTo(root.get("addTime").as(Date.class), endDate));
+            }
+            //
+            Predicate[] pred = new Predicate[predList.size()];
+            Predicate and = criteriaBuilder.and(predList.toArray(pred));
+            criteriaQuery.where(and);
+            //
+            List<Order> orders = new ArrayList<>();
+            orders.add(criteriaBuilder.desc(root.get("id")));
+            return criteriaQuery.orderBy(orders).getRestriction();
+        };
+        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
+//        return new Result(ResultCode.SUCCESS,applyRepo.listApply(pageable,applyId));
+        return new Result(ResultCode.SUCCESS, dayWeightRepo.findAll(sf, pageable));
+    }
+
+    /**
+     * @Method : getBatchWeightOnPage
+     * @Description : 分页查询批次重量记录
+     * @Params : [farmId, addTime, pageNo, pageSize]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/7
+     * @Time : 20:31
+     */
+    @Override
+    public Result getBatchWeightOnPage(Integer farmId, Date addTime, Integer pageNo, Integer pageSize) {
+        Specification<ProdBatchWeightEntity> sf = (Specification<ProdBatchWeightEntity>) (root, criteriaQuery, criteriaBuilder) -> {
+            //
+            List<Predicate> predList = new ArrayList<>();
+            if (null != farmId) {
+                predList.add(criteriaBuilder.equal(root.get("farmId").as(Integer.class), farmId));
+            }
+            if (null != addTime) {
+                predList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("addTime").as(Date.class), addTime));
+            }
+            //
+            Predicate[] pred = new Predicate[predList.size()];
+            Predicate and = criteriaBuilder.and(predList.toArray(pred));
+            criteriaQuery.where(and);
+            //
+            List<Order> orders = new ArrayList<>();
+            orders.add(criteriaBuilder.desc(root.get("id")));
+            return criteriaQuery.orderBy(orders).getRestriction();
+        };
+        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
+        return new Result(ResultCode.SUCCESS, batchWeightRepo.findAll(sf, pageable));
+    }
+
+    /**
+     * @Method : getBatchWeightOnPage
+     * @Description :
+     * @Params : [farmId, startDate, endDate, pageNo, pageSize]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/7
+     * @Time : 20:36
+     */
+    @Override
+    public Result getBatchWeightOnPage(Integer farmId, Date startDate, Date endDate, Integer pageNo, Integer pageSize) throws ParseException {
+        System.out.println("startDate 2>>" + startDate + " " + dateUtil.formatDateText(startDate));
+        System.out.println("endDate 2>>" + endDate + " " + dateUtil.formatDateText(endDate));
+        Specification<ProdBatchWeightEntity> sf = (Specification<ProdBatchWeightEntity>) (root, criteriaQuery, criteriaBuilder) -> {
+            //
+            List<Predicate> predList = new ArrayList<>();
+            if (null != farmId) {
+                predList.add(criteriaBuilder.equal(root.get("farmId").as(Integer.class), farmId));
+            }
+            if (null != startDate) {
+                predList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("addTime").as(Date.class), startDate));
+            }
+            if (null != endDate) {
+                predList.add(criteriaBuilder.lessThanOrEqualTo(root.get("addTime").as(Date.class), endDate));
+            }
+//            //
+            Predicate[] pred = new Predicate[predList.size()];
+            Predicate and = criteriaBuilder.and(predList.toArray(pred));
+            criteriaQuery.where(and);
+            //
+            List<Order> orders = new ArrayList<>();
+            orders.add(criteriaBuilder.desc(root.get("id")));
+            return criteriaQuery.orderBy(orders).getRestriction();
+        };
+        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
+        return new Result(ResultCode.SUCCESS, batchWeightRepo.findAll(sf, pageable));
+    }
+
+    /**
+     * @Method : getBatchWeightOnPage2
+     * @Description :
+     * @Params : [farmId, startDate, endDate, pageNo, pageSize]
+     * @Return : com.huimv.common.utils.Result
+     * @Author : ZhuoNing
+     * @Date : 2021/12/8
+     * @Time : 10:43
+     */
+    @Override
+    public Result getBatchWeightOnPage2(Integer farmId, Date startDate, Date endDate, Integer pageNo, Integer pageSize) throws ParseException {
+        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "id");
+        //
+        return new Result(ResultCode.SUCCESS, batchWeightRepo.findByFarmIdAndStartDateAndEndDate(farmId, startDate, endDate, pageable));
+    }
+
+    @PersistenceContext
+    EntityManager entityManager;
+
+    @Override
+    public Result getBatchWeightOnPage3(Integer farmId, Date startDate, Date endDate, Integer pageNo, Integer pageSize) {
+        StringBuilder countSelectSql = new StringBuilder();
+        countSelectSql.append("select count(*) from ProdBatchWeightEntity bw where 1=1 ");
+
+        StringBuilder selectSql = new StringBuilder();
+        selectSql.append("from ProdBatchWeightEntity bw where 1=1 ");
+
+        Map<String, Object> params = new HashMap<>();
+        StringBuilder whereSql = new StringBuilder();
+        //farmId
+        if (farmId != null) {
+            whereSql.append(" and farmId=:farmId ");
+            params.put("farmId", farmId);
+        }
+        //startDate
+        if(startDate != null){
+            whereSql.append(" and addTime>=:startDate ");
+            params.put("startDate", startDate);
+        }
+        //endDate
+        if(endDate != null){
+            whereSql.append(" and addTime<=:endDate ");
+            params.put("endDate", endDate);
+        }
+        //加入排序顺序
+        whereSql.append(" ORDER BY id DESC");
+        String countSql = new StringBuilder().append(countSelectSql).append(whereSql).toString();
+        Query countQuery = this.entityManager.createQuery(countSql, Long.class);
+        this.setParameters(countQuery, params);
+        Long count = (Long) countQuery.getSingleResult();
+
+        String querySql = new StringBuilder().append(selectSql).append(whereSql).toString();
+        Query query = this.entityManager.createQuery(querySql, ProdBatchWeightEntity.class);
+        this.setParameters(query, params);
+        query.setFirstResult((pageNo - 1) * pageSize);
+        query.setMaxResults(pageSize);
+        List<ProdBatchWeightEntity> batchWeightList = query.getResultList();
+
+//        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "id");
+        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
+        Page<ProdBatchWeightEntity> incomeDailyPage = new PageImpl<ProdBatchWeightEntity>(batchWeightList, pageable, count);
+
+        return new Result(ResultCode.SUCCESS, incomeDailyPage);
+    }
+
+    private void setParameters(Query query, Map<String, Object> params) {
+        for (Map.Entry<String, Object> entry : params.entrySet()) {
+            query.setParameter(entry.getKey(), entry.getValue());
+        }
+    }
+
+    @Override
+    public Result getBatchWeightOnPage4(Integer farmId, String startDate, String endDate, Integer pageNo, Integer pageSize) {
+        StringBuilder countSelectSql = new StringBuilder();
+        countSelectSql.append("select count(*) from prod_batch_weight bw where 1=1 ");
+
+        StringBuilder selectSql = new StringBuilder();
+        selectSql.append("from prod_batch_weight bw where 1=1 ");
+
+        Map<String, Object> params = new HashMap<>();
+        StringBuilder whereSql = new StringBuilder();
+        //farmId
+        if (farmId != null) {
+            whereSql.append(" AND farm_id= :farmId");
+            params.put("farmId", farmId);
+        }
+        //startDate
+        if(startDate != null){
+            whereSql.append(" AND date_format(add_time,'%Y-%m-%d')>= :startDate ");
+            params.put("startDate", startDate);
+        }
+        //endDate
+        if(endDate != null){
+            whereSql.append(" AND date_format(add_time,'%Y-%m-%d')<= :endDate");
+            params.put("endDate", endDate);
+        }
+        //加入排序顺序
+        whereSql.append(" ORDER BY id DESC");
+        String countSql = new StringBuilder().append(countSelectSql).append(whereSql).toString();
+        Query countQuery = this.entityManager.createNativeQuery(countSql);
+        this.setParameters(countQuery, params);
+//        Long count = (Long) countQuery.getSingleResult();
+        int size = countQuery.getResultList().size();
+//        System.out.println("count>>"+count);
+        System.out.println("size>>"+size);
+
+        String querySql = new StringBuilder().append(selectSql).append(whereSql).toString();
+        Query query = this.entityManager.createNativeQuery(querySql);
+        this.setParameters(query, params);
+        query.setFirstResult((pageNo - 1) * pageSize);
+        query.setMaxResults(pageSize);
+        List<ProdBatchWeightEntity> batchWeightList = query.getResultList();
+//        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, Sort.Direction.DESC, "id");
+//        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
+//        Page<ProdBatchWeightEntity> incomeDailyPage = new PageImpl<ProdBatchWeightEntity>(batchWeightList, pageable);
+
+        return new Result(ResultCode.SUCCESS, batchWeightList);
+    }
+}

+ 289 - 0
huimv-farm-produce/src/main/java/com/huimv/produce/produce/utils/DateUtil.java

@@ -0,0 +1,289 @@
+package com.huimv.produce.produce.utils;
+
+import cn.hutool.core.date.DateTime;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+@Slf4j
+public class DateUtil {
+    /**
+     * 获取今天
+     * @return String
+     * */
+    public String getToday(){
+        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
+    }
+    /**
+     * 获取昨天
+     * @return String
+     * */
+    public String getYestoday(){
+        Calendar cal=Calendar.getInstance();
+        cal.add(Calendar.DATE,-1);
+        Date time=cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+    /**
+     * 获取本月开始日期
+     * @return String
+     * **/
+    public String getMonthStart(){
+        Calendar cal=Calendar.getInstance();
+        cal.add(Calendar.MONTH, 0);
+        cal.set(Calendar.DAY_OF_MONTH, 1);
+        Date time=cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+    /**
+     * 获取本月最后一天
+     * @return String
+     * **/
+    public String getMonthEnd(){
+        Calendar cal=Calendar.getInstance();
+        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
+        Date time=cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+    /**
+     * 获取本周的第一天
+     * @return String
+     * **/
+    public String getWeekStart(){
+        Calendar cal=Calendar.getInstance();
+        cal.add(Calendar.WEEK_OF_MONTH, 0);
+        cal.set(Calendar.DAY_OF_WEEK, 2);
+        Date time=cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+    /**
+     * 获取本周的最后一天
+     * @return String
+     * **/
+    public String getWeekEnd(){
+        Calendar cal=Calendar.getInstance();
+        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
+        cal.add(Calendar.DAY_OF_WEEK, 1);
+        Date time=cal.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(time);
+    }
+    /**
+     * 获取本年的第一天
+     * @return String
+     * **/
+    public String getYearStart(){
+        return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
+    }
+
+    /**
+     * 获取本年的最后一天
+     * @return String
+     * **/
+    public String getYearEnd(){
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
+        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
+        Date currYearLast = calendar.getTime();
+        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast);
+    }
+
+    //Long转换为Date格式
+    public String fromLongToDate(Long time ,String format) {
+        SimpleDateFormat sdf = new SimpleDateFormat(format);
+        Date date = new Date(time);
+        return sdf.format(date);
+    }
+
+    public String formatTimestampToDatetime(Timestamp timestamp){
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return sdf.format(timestamp);
+    }
+
+    public String formatTimestampToDate(Timestamp timestamp){
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(timestamp);
+    }
+
+    //格式化本年
+    public String getThisYear(){
+        Calendar cal = Calendar.getInstance();
+        int year = cal.get(Calendar.YEAR);
+        return String.valueOf(year);
+    }
+
+    //格式化本月
+    public String getThisMonth(){
+        Calendar cal = Calendar.getInstance();
+        int month = cal.get(Calendar.MONTH) + 1;
+        if(String.valueOf(month).length()==1)
+        {
+            return "0"+String.valueOf(month);
+        }else{
+            return String.valueOf(month);
+        }
+    }
+
+    //格式化日期时间
+    public String formatDateTime(String dateText) throws ParseException {
+        if(dateText.indexOf("T") != -1){
+            dateText = dateText.replace("T"," ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Date date = sdf.parse(dateText);
+        return sdf.format(date);
+    }
+
+    public String formatDateText(String dateText) throws ParseException {
+        if(dateText.indexOf("T") != -1){
+            dateText = dateText.replace("T"," ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        Date date = sdf.parse(dateText);
+        return sdf.format(date);
+    }
+
+    public String formatDateText(Date date) throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(date);
+    }
+
+    public Date parseDateTime(String dateText) throws ParseException {
+        if(dateText.indexOf("T") != -1){
+            dateText = dateText.replace("T"," ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return sdf.parse(dateText);
+    }
+
+    public Date parseDate(String dateText) throws ParseException {
+        if(dateText.indexOf("T") != -1){
+            dateText = dateText.replace("T"," ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.parse(dateText);
+    }
+
+    public Date parseDate2(String dateText) throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.parse(dateText);
+    }
+
+    public Long parseDateTimeLong(String dateText) throws ParseException {
+        if(dateText.indexOf("T") != -1){
+            dateText = dateText.replace("T"," ");
+        }
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        Date date = sdf.parse(dateText);
+        return date.getTime();
+    }
+
+
+
+    //
+    public Date getTodayDate() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.parse(sdf.format(new Date()));
+    }
+
+    public String getTodayDateText() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+        return sdf.format(new Date());
+    }
+
+    public String getTodayText() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return sdf.format(new Date());
+    }
+
+    public String getTodayMissionText() throws ParseException {
+        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+        return sdf.format(new Date());
+    }
+
+    public String getStartDateInThisMonth(){
+        DateTime date = cn.hutool.core.date.DateUtil.date();
+        return (cn.hutool.core.date.DateUtil.beginOfMonth(date) + "").substring(0, 10);
+    }
+
+    public String getEndDateInThisMonth(){
+        DateTime date = cn.hutool.core.date.DateUtil.date();
+        return (date + "").substring(0, 10);
+    }
+
+    /**
+     * 获取过去或者未来 任意天内的日期数组
+     * @param intervals      intervals天内
+     * @return              日期数组
+     */
+    public ArrayList<String> test(int intervals ) {
+        ArrayList<String> pastDaysList = new ArrayList<>();
+        ArrayList<String> fetureDaysList = new ArrayList<>();
+        for (int i = 0; i <intervals; i++) {
+            pastDaysList.add(getPastDate(i));
+            fetureDaysList.add(getFetureDate(i));
+        }
+        return pastDaysList;
+    }
+
+    /**
+     * 获取过去第几天的日期
+     *
+     * @param past
+     * @return
+     */
+    public String getPastDate(int past) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
+        Date today = calendar.getTime();
+        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+        String result = format.format(today);
+        return result;
+    }
+
+    /**
+     * 获取未来 第 past 天的日期
+     * @param past
+     * @return
+     */
+    public String getFetureDate(int past) {
+        Calendar calendar = Calendar.getInstance();
+        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
+        Date today = calendar.getTime();
+        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
+        String result = format.format(today);
+        return result;
+    }
+
+    //重新构建日期
+    public String rebuildDateTime(String text){
+        return text.substring(0,4)+"-"+text.substring(4,6)+"-"+text.substring(6,8)+" "+text.substring(8,10)+":"+text.substring(10,12)+":"+text.substring(12,14);
+    }
+
+    public static void main(String[] args){
+        DateUtil du = new DateUtil();
+        //
+        du.test1();
+    }
+
+    private void test1() {
+        String text = "20211201104300";
+//        String text = "1234567890abcd";
+        String date = text.substring(0,4)+"-"+text.substring(4,6)+"-"+text.substring(6,8)+" "+text.substring(8,10)+":"+text.substring(10,12)+":"+text.substring(12,14);
+        System.out.println("date="+date);
+    }
+}