HuimvSmartApiserviceApplicationTests.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package com.huimv.apiservice;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.huimv.apiservice.dao.IndoorEnvironmentDao;
  5. import com.huimv.apiservice.dao.OutdoorEnvironmentDao;
  6. import com.huimv.apiservice.dao.PigDao;
  7. import com.huimv.apiservice.dao.SowChildbirthDao;
  8. import com.huimv.apiservice.entity.IndoorEnvironmentEntity;
  9. import com.huimv.apiservice.entity.OutdoorEnvironmentEntity;
  10. import com.huimv.apiservice.entity.SowChildbirthEntity;
  11. import com.huimv.apiservice.entity.YearPigBaseEntity;
  12. import org.junit.jupiter.api.Test;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.boot.test.context.SpringBootTest;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import java.math.BigDecimal;
  17. import java.math.RoundingMode;
  18. import java.util.*;
  19. @SpringBootTest
  20. class HuimvSmartApiserviceApplicationTests {
  21. @Autowired
  22. private IndoorEnvironmentDao indoorEnvironmentDao;
  23. @Autowired
  24. private OutdoorEnvironmentDao outdoorEnvironmentDao;
  25. @Autowired
  26. private PigDao pigDao;
  27. @Autowired
  28. private SowChildbirthDao sowChildbirthDao;
  29. @Test
  30. void contextLoads() {
  31. }
  32. @Test
  33. void createIndoorEnv() {
  34. DateTime today = DateUtil.beginOfDay(new Date());
  35. System.out.println(today);
  36. // BigDecimal bigDecimal = new BigDecimal(Math.random() * 8 + 25).setScale(1, BigDecimal.ROUND_HALF_UP);
  37. // double temperature = bigDecimal.doubleValue();
  38. // BigDecimal bigDecimal2 = new BigDecimal(Math.random() * 0.3 + 0.3).setScale(2, BigDecimal.ROUND_HALF_UP);
  39. // double humidity = bigDecimal2.doubleValue();
  40. // System.out.println(temperature + " " + humidity);
  41. Date collectTime = today;
  42. List<String> gas = new ArrayList<>();
  43. gas.add("优");
  44. gas.add("良");
  45. gas.add("轻度");
  46. gas.add("中度");
  47. gas.add("重度");
  48. gas.add("严重");
  49. Random random = new Random();
  50. List<IndoorEnvironmentEntity> list = new ArrayList<>();
  51. for (int i = 0; i < 288; i++) {
  52. IndoorEnvironmentEntity entity = new IndoorEnvironmentEntity();
  53. entity.setDate(today);
  54. entity.setPigstyId(1);
  55. BigDecimal bigDecimal = new BigDecimal(Math.random() * 3 + 23).setScale(1, BigDecimal.ROUND_HALF_UP);
  56. double temperature = bigDecimal.doubleValue();
  57. entity.setTemperature(temperature);
  58. BigDecimal bigDecimal2 = new BigDecimal(Math.random() * 0.3 + 0.3).setScale(2, BigDecimal.ROUND_HALF_UP);
  59. double humidity = bigDecimal2.doubleValue();
  60. entity.setHumidity(humidity);
  61. entity.setGas(gas.get(random.nextInt(5)));
  62. entity.setCollectTime(collectTime);
  63. collectTime = DateUtil.offsetMinute(collectTime,5);
  64. System.out.println(entity);
  65. list.add(entity);
  66. }
  67. indoorEnvironmentDao.batchInsertData(list);
  68. }
  69. @Test
  70. void createOutdoorEnv() {
  71. DateTime today = DateUtil.beginOfDay(new Date());
  72. System.out.println(today);
  73. // BigDecimal bigDecimal = new BigDecimal(Math.random() * 8 + 25).setScale(1, BigDecimal.ROUND_HALF_UP);
  74. // double temperature = bigDecimal.doubleValue();
  75. // BigDecimal bigDecimal2 = new BigDecimal(Math.random() * 0.3 + 0.3).setScale(2, BigDecimal.ROUND_HALF_UP);
  76. // double humidity = bigDecimal2.doubleValue();
  77. // System.out.println(temperature + " " + humidity);
  78. Date collectTime = today;
  79. List<String> gas = new ArrayList<>();
  80. gas.add("优");
  81. gas.add("良");
  82. gas.add("轻度");
  83. gas.add("中度");
  84. gas.add("重度");
  85. gas.add("严重");
  86. Random random = new Random();
  87. List<OutdoorEnvironmentEntity> list = new ArrayList<>();
  88. for (int i = 0; i < 288; i++) {
  89. OutdoorEnvironmentEntity entity = new OutdoorEnvironmentEntity();
  90. entity.setDate(today);
  91. BigDecimal bigDecimal = new BigDecimal(Math.random() * 3 + 23).setScale(1, BigDecimal.ROUND_HALF_UP);
  92. double temperature = bigDecimal.doubleValue();
  93. entity.setTemperature(temperature);
  94. BigDecimal bigDecimal2 = new BigDecimal(Math.random() * 0.3 + 0.3).setScale(2, BigDecimal.ROUND_HALF_UP);
  95. double humidity = bigDecimal2.doubleValue();
  96. entity.setHumidity(humidity);
  97. entity.setGas(gas.get(random.nextInt(4)));
  98. entity.setCollectTime(collectTime);
  99. collectTime = DateUtil.offsetMinute(collectTime,5);
  100. list.add(entity);
  101. }
  102. outdoorEnvironmentDao.batchInsertData(list);
  103. }
  104. @Test
  105. void createPigData() {
  106. String[] breeds = {"黑猪","金华两头乌","杜巴嘉","巴嘉","杜洛克","嘉兴黑猪","大白猪"};
  107. String[] originPlace = {"金华","杭州"};
  108. List<YearPigBaseEntity> all = new ArrayList<>();
  109. List<YearPigBaseEntity> mothers = new ArrayList<>();
  110. List<YearPigBaseEntity> fathers = new ArrayList<>();
  111. Random random = new Random();
  112. for (int i = 5; i < 2001; i++) {
  113. YearPigBaseEntity entity = new YearPigBaseEntity();
  114. String number = String.valueOf(i);
  115. String earTag = "N00000".substring(0, 6 - number.length()) + number;
  116. entity.setEartag(earTag);
  117. int dayAge = random.nextInt(200) + 50;
  118. entity.setDayAge(dayAge);
  119. entity.setUnitId(random.nextInt(4) + 1);
  120. BigDecimal bigDecimal = new BigDecimal(dayAge);
  121. BigDecimal weight = bigDecimal.multiply(new BigDecimal(0.8)).setScale(2, BigDecimal.ROUND_HALF_UP);
  122. entity.setWeight(weight.doubleValue());
  123. entity.setBreed(breeds[random.nextInt(breeds.length)]);
  124. String sex = i % 2 == 0 ? "公" : "母";
  125. entity.setSex(sex);
  126. if (dayAge >= 150 && "母".equals(sex)) {
  127. mothers.add(entity);
  128. }
  129. if (dayAge >= 150 && "公".equals(sex)) {
  130. fathers.add(entity);
  131. }
  132. entity.setBirthday(DateUtil.offsetDay(DateUtil.date(),i % 200));
  133. entity.setOriginPlace(originPlace[random.nextInt(2)]);
  134. //System.out.println(entity.getDayAge() + " " + " " + entity.getUnitId() + " " + entity.getWeight());
  135. entity.setPigstyId(random.nextInt(4) + 1);
  136. entity.setPeriodId(1);
  137. entity.setInFenceTime(DateUtil.offsetDay(DateUtil.parseDate("2021-01-01"),random.nextInt(91) - 30));
  138. all.add(entity);
  139. }
  140. for (YearPigBaseEntity yearPigBaseEntity : all) {
  141. if (yearPigBaseEntity.getDayAge() < 150) {
  142. yearPigBaseEntity.setMotherEartag(mothers.get(random.nextInt(mothers.size())).getEartag());
  143. yearPigBaseEntity.setFatherEartag(fathers.get(random.nextInt(fathers.size())).getEartag());
  144. }
  145. }
  146. pigDao.batchInsertData(all);
  147. List<SowChildbirthEntity> sowChildbirthEntityList = new ArrayList<>();
  148. for (YearPigBaseEntity mother : mothers) {
  149. SowChildbirthEntity entity = new SowChildbirthEntity();
  150. entity.setEartag(mother.getEartag());
  151. entity.setChildbirthWeight(mother.getWeight());
  152. entity.setPregnancyDate(DateUtil.offsetMonth(mother.getInFenceTime(),3));
  153. entity.setChildbirthCount(random.nextInt(10) + 5);
  154. sowChildbirthEntityList.add(entity);
  155. }
  156. sowChildbirthDao.batchInsertData(sowChildbirthEntityList);
  157. }
  158. }