EvaluationUtil.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package com.huaxu.utils;
  2. import com.huaxu.evaluation.enums.EvaluationCycleEnums;
  3. import com.huaxu.evaluation.vo.EvaluationItemVo;
  4. import com.huaxu.util.DatesUtil;
  5. import org.apache.commons.lang3.StringUtils;
  6. import java.math.BigDecimal;
  7. import java.math.RoundingMode;
  8. import java.time.LocalDate;
  9. import java.time.temporal.TemporalAdjusters;
  10. import java.util.Calendar;
  11. import java.util.Date;
  12. import java.util.List;
  13. import java.util.stream.Collectors;
  14. /**
  15. * @ClassName EvaluationJobUtil
  16. * @Description: TODO
  17. * @Author lihui
  18. * @Date 2021/5/10
  19. * @Version V1.0
  20. **/
  21. public class EvaluationUtil {
  22. /**
  23. * 考评周期格式化
  24. */
  25. private static final String DATE_STRING = "%s年%s";
  26. private static Calendar getCalendar(){
  27. return Calendar.getInstance();
  28. }
  29. public static Calendar getCalendar (String dateTime){
  30. Calendar calendar = getCalendar();
  31. calendar.setTime(DatesUtil.parseDate(dateTime, "yyyy-MM-dd HH:mm:ss"));
  32. return calendar;
  33. }
  34. /***
  35. * 格式化考评周期时间
  36. * @author lihui
  37. * @date 11:00 2021/5/19
  38. * @param cycle :
  39. * @param year :
  40. * @param month :
  41. * @return java.lang.String
  42. **/
  43. public static String formatCycle(int cycle, String year, String month){
  44. String monthString = "";
  45. if (cycle != EvaluationCycleEnums.YEAR.getType()) {
  46. monthString = cycle == 0 ? month + "月" : month + "季度";
  47. }
  48. if ("3".equals(month)) {
  49. month = "1";
  50. } else if ("6".equals(month)){
  51. month = "2";
  52. } else if ("9".equals(month)){
  53. month = "3";
  54. } else if ("12".equals(month)){
  55. month = "4";
  56. }
  57. return String.format(DATE_STRING, year, monthString);
  58. }
  59. public static Integer toInteger(Long lon) {
  60. return lon == null ? null : lon.intValue();
  61. }
  62. public static BigDecimal divide(Integer completeCount, Integer total){
  63. String defaultValue = "0";
  64. BigDecimal bigDecimalComplete = new BigDecimal(completeCount.toString());
  65. BigDecimal bigDecimalTotal = new BigDecimal(total.toString());
  66. if (bigDecimalTotal.compareTo(new BigDecimal(defaultValue)) == 0){
  67. return new BigDecimal(defaultValue);
  68. }
  69. return bigDecimalComplete.divide(bigDecimalTotal,2, RoundingMode.HALF_UP);
  70. }
  71. /**
  72. * @Author lihui
  73. * @Description 转换分钟
  74. * @Date 17:11 2021/5/11
  75. * @Param [dataLimit]
  76. * @return int
  77. **/
  78. public static int minute(String dataLimit){
  79. if (StringUtils.isEmpty(dataLimit)){
  80. return 0;
  81. }
  82. return new BigDecimal(dataLimit).multiply(new BigDecimal("60")).intValue();
  83. }
  84. /**
  85. * @Author lihui
  86. * @Description 完成状态
  87. * @Date 17:08 2021/5/11
  88. * @Param [status]
  89. * @return boolean
  90. **/
  91. public static boolean completed(Integer status){
  92. return status == 2 || status == 3 ;
  93. }
  94. /**
  95. * @Author lihui
  96. * @Description 计算是否延期完成
  97. * @Date 11:06 2021/5/11
  98. * @Param [finishDate :最终完成时间, planFinishDate:预计完成时间, addMinute:可延期的时间]
  99. * @return boolean
  100. **/
  101. public static boolean isDelay(Date finishDate, Date planFinishDate, int addMinute){
  102. if (finishDate == null || planFinishDate == null) {
  103. return false;
  104. }
  105. return finishDate.after(DatesUtil.addMinuteOfDate(planFinishDate,addMinute));
  106. }
  107. public static Long[] toLong (List<Integer> userIds){
  108. Long [] longs = new Long[userIds.size()];
  109. for (int i = 0 ; i < userIds.size(); i++) {
  110. longs[i] =Long.parseLong(userIds.get(i).toString()) ;
  111. }
  112. return longs;
  113. }
  114. /**
  115. * @Author lihui
  116. * @Description 是否是当天
  117. * @Date 17:08 2021/5/11
  118. * @Param [evaluationDay]
  119. * @return boolean
  120. **/
  121. public static boolean isToday(int evaluationDay){
  122. return Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == evaluationDay;
  123. }
  124. /**
  125. * @Author lihui
  126. * @Description 是否是同月
  127. * @Date 18:14 2021/5/11
  128. * @Param [month]
  129. * @return boolean
  130. **/
  131. public static boolean sameMonth(int month){
  132. return Calendar.getInstance().get(Calendar.MONTH) + 1 == month;
  133. }
  134. public static boolean containsType(List<EvaluationItemVo> itemEntityList, Integer type){
  135. List<String> list = itemEntityList.stream().map(EvaluationItemVo::getCycle).collect(Collectors.toList());
  136. for (String str: list) {
  137. if (str.indexOf(type.toString()) != -1) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. private static Integer calendarMonth(Integer calendarMonth){
  144. switch (calendarMonth){
  145. case 1 :
  146. return 4;
  147. case 4 :
  148. return 1;
  149. case 7 :
  150. return 2;
  151. case 10 :
  152. return 3;
  153. default: ;
  154. }
  155. return null;
  156. }
  157. private static String toString (Integer var){
  158. String result = var.toString();
  159. return result.length() == 1 ? "0" + result : result;
  160. }
  161. public static String getStartTime(int cycle, int day) {
  162. LocalDate localDate = LocalDate.now();
  163. if (cycle == EvaluationCycleEnums.MONTH.getType()) {
  164. localDate = localDate.plusMonths(-1);
  165. }
  166. if (cycle == EvaluationCycleEnums.QUARTER.getType()){
  167. Integer type = calendarMonth(localDate.getMonthValue());
  168. if (type == null) {
  169. return null;
  170. }
  171. int month = type == 1 ? 1 :
  172. type == 2 ? 4 :
  173. type == 3 ? 7 :
  174. type == 4 ? 10 : null;
  175. localDate = type == 4 ? localDate.plusYears(-1) : localDate;
  176. localDate = localDate.withMonth(month);
  177. }
  178. if (cycle == EvaluationCycleEnums.YEAR.getType()){
  179. localDate = localDate.plusYears(-1);
  180. localDate = localDate.withMonth(1);
  181. }
  182. localDate = localDate.withDayOfMonth(day);
  183. return localDate.toString() + " 00:00:00";
  184. }
  185. public static String getEndTime(int cycle, int day) {
  186. LocalDate localDate = LocalDate.now();
  187. if (cycle == EvaluationCycleEnums.MONTH.getType()) {
  188. localDate = day == 1 ? localDate.plusMonths(-1) : localDate;
  189. }
  190. if (cycle == EvaluationCycleEnums.QUARTER.getType()){
  191. Integer type = calendarMonth(localDate.getMonthValue());
  192. if (type == null) {
  193. return null;
  194. }
  195. int month = type == 1 ? 4 :
  196. type == 2 ? 7 :
  197. type == 3 ? 10 :
  198. type == 4 ? 1 : null;
  199. // 设置月份
  200. if (type == 4 && day == 1 ){
  201. localDate = localDate.plusMonths(-1);
  202. } else {
  203. localDate = localDate.withMonth(day == 1 ? month -1 : month);
  204. }
  205. }
  206. if (cycle == EvaluationCycleEnums.YEAR.getType()){
  207. localDate = localDate.withMonth(1);
  208. localDate = day == 1 ? localDate.plusMonths(-1) : localDate;
  209. }
  210. localDate = day != 1 ? localDate.withDayOfMonth(day - 1) : localDate.with(TemporalAdjusters.lastDayOfMonth());
  211. return localDate.toString() + " 23:59:59";
  212. }
  213. public static LocalDate getLocalDateByCycle(String startTime, String endTime, int cycle){
  214. // 季度获取后面的时间
  215. String quarterString = "4,7,10,1";
  216. boolean isQuarter = cycle == EvaluationCycleEnums.QUARTER.getType();
  217. String time = isQuarter ? endTime : startTime;
  218. LocalDate localDate = DatesUtil.parseLocalDate(time);
  219. if (isQuarter && quarterString.indexOf(localDate.getMonthValue() + "") != -1) {
  220. localDate = localDate.plusMonths(-1);
  221. }
  222. return localDate;
  223. }
  224. public static void main(String[] args) {
  225. LocalDate localDate = EvaluationUtil.getLocalDateByCycle("2020-12-01 00:00:00","2021-01-30 00:00:00",1);
  226. System.out.println(localDate.getYear() + "-" + localDate.getMonthValue());
  227. }
  228. }