|
@@ -1,22 +1,31 @@
|
|
|
package com.huimv.produce.produce.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.huimv.common.utils.Result;
|
|
|
import com.huimv.common.utils.ResultCode;
|
|
|
+import com.huimv.produce.produce.entity.ProdWeightDayEntity;
|
|
|
+import com.huimv.produce.produce.entity.ProdWeightDetailsEntity;
|
|
|
import com.huimv.produce.produce.repo.*;
|
|
|
import com.huimv.produce.produce.service.IWeight;
|
|
|
import com.huimv.produce.produce.utils.DateUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.domain.*;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
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.sql.Timestamp;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.*;
|
|
|
|
|
@@ -29,6 +38,7 @@ import java.util.*;
|
|
|
* @Create : 2020-12-25
|
|
|
**/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class WeightImpl implements IWeight {
|
|
|
@Autowired
|
|
|
private ProdDayWeightRepo dayWeightRepo;
|
|
@@ -38,6 +48,211 @@ public class WeightImpl implements IWeight {
|
|
|
private ProdWeightRepo weightRepo;
|
|
|
@Autowired
|
|
|
private DateUtil dateUtil;
|
|
|
+ @Resource
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+ @Autowired
|
|
|
+ private ProdWeightDetailsRepo prodWeightDetailsRepo;
|
|
|
+ @Autowired
|
|
|
+ private ProdWeightDayRepo prodWeightDayRepo;
|
|
|
+ @Value("${weight.http}")
|
|
|
+ private String weightHttp;
|
|
|
+ @Value("${weight.path}")
|
|
|
+ private String weightPath;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result getEggWeightDetails() throws ParseException {
|
|
|
+// String url = "http://39.171.45.196:9200/egg/getWeightDetails?date=2022-06-10";
|
|
|
+// String url = "http://39.171.45.196:9200/egg/getWeightDetails";
|
|
|
+ // 获取全部数据;
|
|
|
+// String url = initUrl();
|
|
|
+ // 获取今天数据
|
|
|
+// String todayDate = new DateUtil().getTodayDateText();
|
|
|
+ // test
|
|
|
+ String todayDate = "2022-06-10";
|
|
|
+ String url = weightHttp + weightPath;
|
|
|
+ url += "?date=" + todayDate;
|
|
|
+// url += "?date=2022-06-10";
|
|
|
+// System.out.println("url=" + url);
|
|
|
+ String result = restTemplate.getForObject(url, String.class);
|
|
|
+ JSONArray resultJa = JSONArray.parseArray(result);
|
|
|
+ log.info("接口返回数量=" + resultJa.size());
|
|
|
+
|
|
|
+ // 保存每天明细
|
|
|
+ saveWeightDetails(resultJa, todayDate);
|
|
|
+
|
|
|
+ // 保存汇总每天总重
|
|
|
+ saveDayWeight(resultJa, todayDate);
|
|
|
+
|
|
|
+ return new Result(ResultCode.SUCCESS);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : saveDayWeight
|
|
|
+ * @Description :
|
|
|
+ * @Params : [resultJa, todayDate]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/7/2
|
|
|
+ * @Time : 4:08
|
|
|
+ */
|
|
|
+ private void saveDayWeight(JSONArray resultJa, String todayDate) throws ParseException {
|
|
|
+ JSONArray purchaseJa = new JSONArray();
|
|
|
+ JSONArray salesJa = new JSONArray();
|
|
|
+ JSONArray otherJa = new JSONArray();
|
|
|
+ for (int a = 0; a < resultJa.size(); a++) {
|
|
|
+ JSONObject resultJo = resultJa.getJSONObject(a);
|
|
|
+ if (resultJo.getString("poundType").equalsIgnoreCase("Purchase")) {
|
|
|
+ purchaseJa.add(resultJo);
|
|
|
+ } else if (resultJo.getString("poundType").equalsIgnoreCase("Sales")) {
|
|
|
+ salesJa.add(resultJo);
|
|
|
+ } else {
|
|
|
+ otherJa.add(resultJo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //
|
|
|
+ List<ProdWeightDayEntity> ProdWeightDayEntityList = getDayWeightList(todayDate);
|
|
|
+ prodWeightDayRepo.deleteAll(ProdWeightDayEntityList);
|
|
|
+
|
|
|
+ BigDecimal grossTotalPurchaseBg = new BigDecimal("0");
|
|
|
+ BigDecimal tareTotalPurchaseBg = new BigDecimal("0");
|
|
|
+ BigDecimal netTotalPurchaseBg = new BigDecimal("0");
|
|
|
+ for (int a = 0; a < purchaseJa.size(); a++) {
|
|
|
+ JSONObject purchaseJo = purchaseJa.getJSONObject(a);
|
|
|
+// System.out.println("" + purchaseJo.getBigDecimal("grossWeight") + " " + purchaseJo.getBigDecimal("tareWeight") + " " + purchaseJo.getBigDecimal("netWeight"));
|
|
|
+ BigDecimal gw = purchaseJo.getBigDecimal("grossWeight");
|
|
|
+ if (gw == null) {
|
|
|
+ gw = new BigDecimal("0");
|
|
|
+ }
|
|
|
+ grossTotalPurchaseBg = grossTotalPurchaseBg.add(gw);
|
|
|
+ BigDecimal tw = purchaseJo.getBigDecimal("tareWeight");
|
|
|
+ if (tw == null) {
|
|
|
+ tw = new BigDecimal("0");
|
|
|
+ }
|
|
|
+ tareTotalPurchaseBg = tareTotalPurchaseBg.add(tw);
|
|
|
+ BigDecimal nw = purchaseJo.getBigDecimal("netWeight");
|
|
|
+ if (nw == null) {
|
|
|
+ nw = new BigDecimal("0");
|
|
|
+ }
|
|
|
+ netTotalPurchaseBg = netTotalPurchaseBg.add(nw);
|
|
|
+ }
|
|
|
+// System.out.println("grossTotalPurchaseBg=" + grossTotalPurchaseBg.toString());
|
|
|
+// System.out.println("tareTotalPurchaseBg=" + tareTotalPurchaseBg.toString());
|
|
|
+// System.out.println("netTotalPurchaseBg=" + netTotalPurchaseBg.toString());
|
|
|
+
|
|
|
+ ProdWeightDayEntity purchaseWeightDayEntity = new ProdWeightDayEntity();
|
|
|
+ purchaseWeightDayEntity.setAddDate(new java.sql.Date(dateUtil.parseDate(todayDate).getTime()));
|
|
|
+ purchaseWeightDayEntity.setGrossWeight(grossTotalPurchaseBg.floatValue());
|
|
|
+ purchaseWeightDayEntity.setTareWeight(tareTotalPurchaseBg.floatValue());
|
|
|
+ purchaseWeightDayEntity.setNetWeight(netTotalPurchaseBg.floatValue());
|
|
|
+ purchaseWeightDayEntity.setPoundType("Purchase");
|
|
|
+ prodWeightDayRepo.save(purchaseWeightDayEntity);
|
|
|
+
|
|
|
+ BigDecimal grossTotalSalesBg = new BigDecimal("0");
|
|
|
+ BigDecimal tareTotalSalesBg = new BigDecimal("0");
|
|
|
+ BigDecimal netTotalSalesBg = new BigDecimal("0");
|
|
|
+ for (int a = 0; a < salesJa.size(); a++) {
|
|
|
+ JSONObject salesJo = salesJa.getJSONObject(a);
|
|
|
+// System.out.println("" + salesJo.getBigDecimal("grossWeight") + " " + salesJo.getBigDecimal("tareWeight") + " " + salesJo.getBigDecimal("netWeight"));
|
|
|
+ BigDecimal gw = salesJo.getBigDecimal("grossWeight");
|
|
|
+ if (gw == null) {
|
|
|
+ gw = new BigDecimal("0");
|
|
|
+ }
|
|
|
+ grossTotalSalesBg = grossTotalSalesBg.add(gw);
|
|
|
+ BigDecimal tw = salesJo.getBigDecimal("tareWeight");
|
|
|
+ if (tw == null) {
|
|
|
+ tw = new BigDecimal("0");
|
|
|
+ }
|
|
|
+ tareTotalSalesBg = tareTotalSalesBg.add(tw);
|
|
|
+ BigDecimal nw = salesJo.getBigDecimal("netWeight");
|
|
|
+ if (nw == null) {
|
|
|
+ nw = new BigDecimal("0");
|
|
|
+ }
|
|
|
+ netTotalSalesBg = netTotalSalesBg.add(nw);
|
|
|
+ }
|
|
|
+// System.out.println("grossTotalSalesBg=" + grossTotalSalesBg.toString());
|
|
|
+// System.out.println("tareTotalSalesBg=" + tareTotalSalesBg.toString());
|
|
|
+// System.out.println("netTotalSalesBg=" + netTotalSalesBg.toString());
|
|
|
+
|
|
|
+ ProdWeightDayEntity salesWeightDayEntity = new ProdWeightDayEntity();
|
|
|
+ salesWeightDayEntity.setAddDate(new java.sql.Date(dateUtil.parseDate(todayDate).getTime()));
|
|
|
+ salesWeightDayEntity.setGrossWeight(grossTotalSalesBg.floatValue());
|
|
|
+ salesWeightDayEntity.setTareWeight(tareTotalSalesBg.floatValue());
|
|
|
+ salesWeightDayEntity.setNetWeight(netTotalSalesBg.floatValue());
|
|
|
+ salesWeightDayEntity.setPoundType("Sales");
|
|
|
+ prodWeightDayRepo.save(salesWeightDayEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ private List<ProdWeightDayEntity> getDayWeightList(String todayDate) {
|
|
|
+ return prodWeightDayRepo.getDayWeightList(todayDate);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : saveWeightDetails
|
|
|
+ * @Description :
|
|
|
+ * @Params : [resultJa, todayDate]
|
|
|
+ * @Return : void
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/7/2
|
|
|
+ * @Time : 4:03
|
|
|
+ */
|
|
|
+ private void saveWeightDetails(JSONArray resultJa, String todayDate) throws ParseException {
|
|
|
+ // 读取每天的一个称重数据
|
|
|
+ List<ProdWeightDetailsEntity> existEntityList = getTodayWeightDetails(todayDate);
|
|
|
+ System.out.println("今天已经取回数量=" + existEntityList.size());
|
|
|
+ JSONArray dataJa = new JSONArray();
|
|
|
+ for (int a = 0; a < resultJa.size(); a++) {
|
|
|
+ JSONObject resultJo = resultJa.getJSONObject(a);
|
|
|
+ boolean isExist = false;
|
|
|
+ for (ProdWeightDetailsEntity weightDetailsEntity : existEntityList) {
|
|
|
+ if (weightDetailsEntity.getSerialNumber().equalsIgnoreCase(resultJo.getString("serialNumber"))) {
|
|
|
+ isExist = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!isExist) {
|
|
|
+ dataJa.add(resultJo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("dataJa.size=" + dataJa.size());
|
|
|
+ for (int a = 0; a < dataJa.size(); a++) {
|
|
|
+ System.out.println("" + (a + 1));
|
|
|
+ JSONObject dataJo = dataJa.getJSONObject(a);
|
|
|
+ ProdWeightDetailsEntity prodWeightDetailsEntity = new ProdWeightDetailsEntity();
|
|
|
+ prodWeightDetailsEntity.setSerialNumber(dataJo.getString("serialNumber"));
|
|
|
+ prodWeightDetailsEntity.setCarNumber(dataJo.getString("carNumber"));
|
|
|
+ prodWeightDetailsEntity.setPoundType(dataJo.getString("poundType"));
|
|
|
+ prodWeightDetailsEntity.setConsigner(dataJo.getString("consigner"));
|
|
|
+ prodWeightDetailsEntity.setConsignee(dataJo.getString("consignee"));
|
|
|
+ prodWeightDetailsEntity.setVariety(dataJo.getString("variety"));
|
|
|
+ prodWeightDetailsEntity.setSpecification(dataJo.getString("specification"));
|
|
|
+ prodWeightDetailsEntity.setGrossWeight(dataJo.getBigDecimal("grossWeight"));
|
|
|
+ prodWeightDetailsEntity.setTareWeight(dataJo.getBigDecimal("tareWeight"));
|
|
|
+ prodWeightDetailsEntity.setNetWeight(dataJo.getBigDecimal("netWeight"));
|
|
|
+ prodWeightDetailsEntity.setDeductionWeight(dataJo.getBigDecimal("deductionWeight"));
|
|
|
+ prodWeightDetailsEntity.setActualWeight(dataJo.getBigDecimal("actualWeight"));
|
|
|
+ prodWeightDetailsEntity.setPrice(dataJo.getBigDecimal("price"));
|
|
|
+ prodWeightDetailsEntity.setMoney(dataJo.getBigDecimal("money"));
|
|
|
+ prodWeightDetailsEntity.setWeighingTime(new java.sql.Date(new DateUtil().parseDateTime(dataJo.getString("weighingTime")).getTime()));
|
|
|
+ prodWeightDetailsEntity.setWeighingType(dataJo.getString("weighingType"));
|
|
|
+ prodWeightDetailsEntity.setAddDate(new Timestamp(new Date().getTime()));
|
|
|
+ prodWeightDetailsRepo.save(prodWeightDetailsEntity);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ private List<ProdWeightDetailsEntity> getTodayWeightDetails(String todayDate) {
|
|
|
+ List<ProdWeightDetailsEntity> list = prodWeightDetailsRepo.getWeightDetails(todayDate);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String todayUrl() throws ParseException {
|
|
|
+ return weightHttp + weightPath + "?date=" + new DateUtil().getTodayDateText();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String initUrl() {
|
|
|
+ return weightHttp + weightPath;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* @Method : getDayWeight
|
|
@@ -226,14 +441,13 @@ public class WeightImpl implements IWeight {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @Method : getEveryTimeWeightOnPage
|
|
|
- * @Description :
|
|
|
- * @Params : [farmId, startDate, endDate, pageNo, pageSize]
|
|
|
- * @Return : com.huimv.common.utils.Result
|
|
|
- *
|
|
|
- * @Author : ZhuoNing
|
|
|
- * @Date : 2021/12/22
|
|
|
- * @Time : 19:41
|
|
|
+ * @Method : getEveryTimeWeightOnPage
|
|
|
+ * @Description :
|
|
|
+ * @Params : [farmId, startDate, endDate, pageNo, pageSize]
|
|
|
+ * @Return : com.huimv.common.utils.Result
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2021/12/22
|
|
|
+ * @Time : 19:41
|
|
|
*/
|
|
|
@Override
|
|
|
public Result getEveryTimeWeightOnPage(Integer farmId, Date startDate, Date endDate, Integer pageNo, Integer pageSize) throws ParseException {
|
|
@@ -261,12 +475,12 @@ public class WeightImpl implements IWeight {
|
|
|
params.put("farmId", farmId);
|
|
|
}
|
|
|
//startDate
|
|
|
- if(startDate != null){
|
|
|
+ if (startDate != null) {
|
|
|
whereSql.append(" and addTime>=:startDate ");
|
|
|
params.put("startDate", startDate);
|
|
|
}
|
|
|
//endDate
|
|
|
- if(endDate != null){
|
|
|
+ if (endDate != null) {
|
|
|
whereSql.append(" and addTime<=:endDate ");
|
|
|
params.put("endDate", endDate);
|
|
|
}
|
|
@@ -313,12 +527,12 @@ public class WeightImpl implements IWeight {
|
|
|
params.put("farmId", farmId);
|
|
|
}
|
|
|
//startDate
|
|
|
- if(startDate != null){
|
|
|
+ if (startDate != null) {
|
|
|
whereSql.append(" AND date_format(add_time,'%Y-%m-%d')>= :startDate ");
|
|
|
params.put("startDate", startDate);
|
|
|
}
|
|
|
//endDate
|
|
|
- if(endDate != null){
|
|
|
+ if (endDate != null) {
|
|
|
whereSql.append(" AND date_format(add_time,'%Y-%m-%d')<= :endDate");
|
|
|
params.put("endDate", endDate);
|
|
|
}
|
|
@@ -330,7 +544,7 @@ public class WeightImpl implements IWeight {
|
|
|
// Long count = (Long) countQuery.getSingleResult();
|
|
|
int size = countQuery.getResultList().size();
|
|
|
// System.out.println("count>>"+count);
|
|
|
- System.out.println("size>>"+size);
|
|
|
+ System.out.println("size>>" + size);
|
|
|
|
|
|
String querySql = new StringBuilder().append(selectSql).append(whereSql).toString();
|
|
|
Query query = this.entityManager.createNativeQuery(querySql);
|
|
@@ -344,4 +558,85 @@ public class WeightImpl implements IWeight {
|
|
|
|
|
|
return new Result(ResultCode.SUCCESS, batchWeightList);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : getManyDayWeight
|
|
|
+ * @Description :
|
|
|
+ * @Params : [days, poundType]
|
|
|
+ * @Return : com.huimv.common.utils.Result
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/7/2
|
|
|
+ * @Time : 5:57
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Result getManyDayWeight(Integer days, String poundType) {
|
|
|
+ if(StringUtils.isBlank(poundType)){
|
|
|
+ poundType = "Sales";
|
|
|
+ }
|
|
|
+ //
|
|
|
+ List<ProdWeightDayEntity> dayWeightList = prodWeightDayRepo.findDayWeight(days,poundType);
|
|
|
+ List newList = new ArrayList();
|
|
|
+ for(int a=dayWeightList.size()-1 ; a >= 0 ; a--){
|
|
|
+ newList.add(dayWeightList.get(a));
|
|
|
+ }
|
|
|
+ return new Result(ResultCode.SUCCESS,newList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : getWeightDetails
|
|
|
+ * @Description :
|
|
|
+ * @Params : [poundType, carNumber, consigner, consignee, variety, startDate, endDate, pageNo, pageSize]
|
|
|
+ * @Return : com.huimv.common.utils.Result
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/7/2
|
|
|
+ * @Time : 6:03
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Result getWeightDetails(String poundType, String carNumber, String consigner, String consignee, String variety, Date startDate,Date endDate,Integer pageNo,Integer pageSize) {
|
|
|
+// .
|
|
|
+ Specification<ProdWeightDetailsEntity> sf = (Specification<ProdWeightDetailsEntity>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
+ //
|
|
|
+ List<Predicate> predList = new ArrayList<>();
|
|
|
+ if (null != poundType) {
|
|
|
+ predList.add(criteriaBuilder.equal(root.get("poundType").as(String.class), poundType));
|
|
|
+ }
|
|
|
+ if (null != carNumber) {
|
|
|
+ predList.add(criteriaBuilder.like(root.get("poundType").as(String.class), carNumber));
|
|
|
+ }
|
|
|
+ if (null != consigner) {
|
|
|
+ predList.add(criteriaBuilder.like(root.get("consigner").as(String.class), consigner));
|
|
|
+ }
|
|
|
+ if (null != consignee) {
|
|
|
+ predList.add(criteriaBuilder.like(root.get("consignee").as(String.class), consignee));
|
|
|
+ }
|
|
|
+ if (null != variety) {
|
|
|
+ predList.add(criteriaBuilder.like(root.get("variety").as(String.class), variety));
|
|
|
+ }
|
|
|
+ if (null != startDate) {
|
|
|
+ predList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("startDate").as(Date.class), startDate));
|
|
|
+ }
|
|
|
+ if (null != endDate) {
|
|
|
+ predList.add(criteriaBuilder.lessThanOrEqualTo(root.get("endDate").as(Date.class), endDate));
|
|
|
+ }
|
|
|
+// if (null != returnState) {
|
|
|
+// predList.add(criteriaBuilder.equal(root.get("returnState").as(Integer.class), returnState));
|
|
|
+// }
|
|
|
+// if (null != deliverState) {
|
|
|
+// predList.add(criteriaBuilder.equal(root.get("deliverState").as(Integer.class), deliverState));
|
|
|
+// }
|
|
|
+ //
|
|
|
+ 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, prodWeightDetailsRepo.findAll(sf, pageable));
|
|
|
+ }
|
|
|
}
|