ClassTimeStatisticsServiceImpl.java 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. package com.xjrsoft.module.classtime.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  5. import com.github.yulichang.base.MPJBaseServiceImpl;
  6. import com.google.gson.Gson;
  7. import com.google.gson.JsonArray;
  8. import com.google.gson.JsonElement;
  9. import com.google.gson.JsonObject;
  10. import com.google.gson.JsonParser;
  11. import com.xjrsoft.common.enums.CourseAdjustTypeEnum;
  12. import com.xjrsoft.common.enums.CourseTimeTypeEnum;
  13. import com.xjrsoft.common.exception.MyException;
  14. import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
  15. import com.xjrsoft.module.classtime.dto.AddClassTimeStatisticsDto;
  16. import com.xjrsoft.module.classtime.entity.ClassTimeDelete;
  17. import com.xjrsoft.module.classtime.entity.ClassTimeStatistics;
  18. import com.xjrsoft.module.classtime.entity.ClassTimeStatisticsRecord;
  19. import com.xjrsoft.module.classtime.entity.ClassTimeStatisticsSet;
  20. import com.xjrsoft.module.classtime.mapper.ClassTimeStatisticsMapper;
  21. import com.xjrsoft.module.classtime.mapper.ClassTimeStatisticsRecordMapper;
  22. import com.xjrsoft.module.classtime.service.IClassTimeDeleteService;
  23. import com.xjrsoft.module.classtime.service.IClassTimeStatisticsService;
  24. import com.xjrsoft.module.classtime.service.IClassTimeStatisticsSetService;
  25. import com.xjrsoft.module.classtime.vo.ClassTimeStatisticsRecordVo;
  26. import com.xjrsoft.module.classtime.vo.CourseListVo;
  27. import com.xjrsoft.module.classtime.vo.TeacherListVo;
  28. import com.xjrsoft.module.classtime.vo.WeekTimeRangeVo;
  29. import com.xjrsoft.module.oa.entity.WfTeacherCourseTime;
  30. import lombok.AllArgsConstructor;
  31. import me.zhyd.oauth.log.Log;
  32. import org.apache.poi.ss.usermodel.Cell;
  33. import org.apache.poi.ss.usermodel.CellStyle;
  34. import org.apache.poi.ss.usermodel.Font;
  35. import org.apache.poi.ss.usermodel.HorizontalAlignment;
  36. import org.apache.poi.ss.usermodel.Row;
  37. import org.apache.poi.ss.usermodel.Sheet;
  38. import org.apache.poi.ss.usermodel.VerticalAlignment;
  39. import org.apache.poi.ss.usermodel.Workbook;
  40. import org.apache.poi.ss.util.CellRangeAddress;
  41. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  42. import org.springframework.stereotype.Service;
  43. import org.springframework.transaction.annotation.Transactional;
  44. import java.io.ByteArrayOutputStream;
  45. import java.math.BigDecimal;
  46. import java.time.DayOfWeek;
  47. import java.time.LocalDate;
  48. import java.time.format.DateTimeFormatter;
  49. import java.time.temporal.ChronoUnit;
  50. import java.util.ArrayList;
  51. import java.util.Arrays;
  52. import java.util.Date;
  53. import java.util.HashMap;
  54. import java.util.HashSet;
  55. import java.util.LinkedHashMap;
  56. import java.util.List;
  57. import java.util.Map;
  58. import java.util.Set;
  59. import java.util.concurrent.CompletableFuture;
  60. import java.util.stream.Collectors;
  61. /**
  62. * @title: 课时统计
  63. * @Author dzx
  64. * @Date: 2024-09-26
  65. * @Version 1.0
  66. */
  67. @Service
  68. @AllArgsConstructor
  69. public class ClassTimeStatisticsServiceImpl extends MPJBaseServiceImpl<ClassTimeStatisticsMapper, ClassTimeStatistics> implements IClassTimeStatisticsService {
  70. private final ClassTimeStatisticsMapper statisticsMapper;
  71. private final ClassTimeStatisticsRecordMapper recordMapper;
  72. private final IClassTimeStatisticsSetService statisticsSetService;
  73. private final IClassTimeDeleteService deleteService;
  74. @Override
  75. @Transactional(rollbackFor = Exception.class)
  76. public Boolean add(ClassTimeStatistics classTimeStatistics) {
  77. statisticsMapper.insert(classTimeStatistics);
  78. return true;
  79. }
  80. @Override
  81. @Transactional(rollbackFor = Exception.class)
  82. public Boolean update(ClassTimeStatistics classTimeStatistics) {
  83. statisticsMapper.updateById(classTimeStatistics);
  84. return true;
  85. }
  86. @Override
  87. @Transactional(rollbackFor = Exception.class)
  88. public Boolean delete(List<Long> ids) {
  89. statisticsMapper.deleteBatchIds(ids);
  90. recordMapper.delete(Wrappers.lambdaQuery(ClassTimeStatisticsRecord.class).in(ClassTimeStatisticsRecord::getClassTimeStatisticsId, ids));
  91. return true;
  92. }
  93. @Override
  94. @Transactional(rollbackFor = Exception.class)
  95. public Boolean add(AddClassTimeStatisticsDto dto) {
  96. ClassTimeStatistics classTimeStatistics = BeanUtil.toBean(dto, ClassTimeStatistics.class);
  97. classTimeStatistics.setStatus(0);
  98. classTimeStatistics.setCreateDate(new Date());
  99. //查询最新权重并保存到统计中
  100. Integer category = 1;
  101. List<ClassTimeStatisticsSet> list = statisticsSetService.list(
  102. new QueryWrapper<ClassTimeStatisticsSet>().lambda()
  103. .eq(ClassTimeStatisticsSet::getCategory, category)
  104. .orderByDesc(ClassTimeStatisticsSet::getCreateDate)
  105. );
  106. if(list.isEmpty()){
  107. throw new MyException("请先设置权重并进行保存");
  108. }
  109. classTimeStatistics.setWeightSetJson(list.get(0).getJsonContent());
  110. //查询最新费用并保存到统计中
  111. category = 2;
  112. list = statisticsSetService.list(
  113. new QueryWrapper<ClassTimeStatisticsSet>().lambda()
  114. .eq(ClassTimeStatisticsSet::getCategory, category)
  115. .orderByDesc(ClassTimeStatisticsSet::getCreateDate)
  116. );
  117. if(list.isEmpty()){
  118. throw new MyException("请先设置费用并进行保存");
  119. }
  120. classTimeStatistics.setCostSetJson(list.get(0).getJsonContent());
  121. this.save(classTimeStatistics);
  122. CompletableFuture.runAsync(() -> {
  123. refreshRecord(classTimeStatistics);
  124. });
  125. return true;
  126. }
  127. /**
  128. * 1、通过xjr_user和base_teacher查询所有教师,查询聘用类型为:正式聘用和外聘的
  129. * 2、根据统计的开始日期和结束日期查询课时补充(wf_teacher_course_time)中的教研会、督导听课、临近三年退休政策、出题、阅卷、周末培优
  130. * 3、根绝统计的开始日期和结束日期查询课程表(course_table)中的所有课程数据
  131. * 4、查询补班日期下的所有课程
  132. * 5、属于节假日的课程不查询
  133. * @param statistics
  134. */
  135. @Override
  136. @Transactional(rollbackFor = Exception.class)
  137. public Boolean refreshRecord(ClassTimeStatistics statistics) {
  138. try {
  139. // 1、查询教师
  140. List<TeacherListVo> teacherList = this.baseMapper.getTeacherList();
  141. // 2、查询补课课时
  142. List<WfTeacherCourseTime> courseTimeList = this.baseMapper.getWfTeacherCourseTimeList(statistics);
  143. //按照课时补充类型分类统计
  144. Map<String, List<WfTeacherCourseTime>> courseTimeMap = courseTimeList.stream().collect(Collectors.groupingBy(WfTeacherCourseTime::getCourseTimeType));
  145. JsonParser parser = new JsonParser();
  146. //费用设置jsonArray
  147. JsonArray costSetArray = parser.parse(statistics.getCostSetJson()).getAsJsonArray();
  148. Map<String, Double> costSetMap = new LinkedHashMap<>();
  149. for (JsonElement jsonElement : costSetArray) {
  150. JsonObject object = jsonElement.getAsJsonObject();
  151. costSetMap.put(object.get("field").getAsString(), object.get("value").getAsDouble());
  152. }
  153. //权重设置jsonArray
  154. JsonArray weightSetArray = parser.parse(statistics.getWeightSetJson()).getAsJsonArray();
  155. Map<String, Double> weightSetMap = new LinkedHashMap<>();
  156. for (JsonElement jsonElement : weightSetArray) {
  157. JsonObject object = jsonElement.getAsJsonObject();
  158. weightSetMap.put(object.get("label").getAsString(), object.get("value").getAsDouble());
  159. }
  160. //计算出这个时间段内一共多少周
  161. List<WeekTimeRangeVo> weekTimeRangeVos = calculateNaturalWeeks(statistics.getStartDate(), statistics.getEndDate());
  162. //取出所有的日期
  163. List<LocalDate> allDateList = getDatesBetween(statistics.getStartDate(), statistics.getEndDate());
  164. //查询课程数据,排除开节假日的数据
  165. List<CourseListVo> allCourseList = this.baseMapper.getCourseList(statistics);
  166. allCourseList.addAll(this.baseMapper.getHolidayReplaceCourseList(statistics));
  167. //查询删除课时的数据,将每个班删除的具体日期统计出来
  168. List<ClassTimeDelete> deleteList = deleteService.list(
  169. new QueryWrapper<ClassTimeDelete>().lambda()
  170. .ne(ClassTimeDelete::getStatus, 2)
  171. );
  172. Map<Long, List<ClassTimeDelete>> deleteDataMap = deleteList.stream().collect(Collectors.groupingBy(ClassTimeDelete::getClassId));
  173. Map<Long, Map<LocalDate, String>> deleteMap = new HashMap<>();//将每个班级计算出来所有被删除的日期存入map
  174. for (Long classId : deleteDataMap.keySet()) {
  175. Map<LocalDate, String> dateMap = new HashMap<>();
  176. for (ClassTimeDelete classTimeDelete : deleteDataMap.get(classId)) {
  177. LocalDate currentDate = classTimeDelete.getStartDate();
  178. while (!currentDate.isAfter(classTimeDelete.getEndDate())) {
  179. dateMap.put(currentDate, classTimeDelete.getTimePeriod());
  180. currentDate = currentDate.plusDays(1); // 增加一天
  181. }
  182. }
  183. //去重并存到map中
  184. deleteMap.put(classId, dateMap);
  185. }
  186. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  187. //查询所有老师发起顶课通过的数量,也要分别计算早自习、晚自习、正课、如果日期出入课时删除中,也需要跳过
  188. List<CourseListVo> allSubstituteList = this.baseMapper.getSubstituteList(statistics);
  189. List<ClassTimeStatisticsRecord> insertList = new ArrayList<>();
  190. //循环教师,准备开始计算
  191. for (TeacherListVo teacher : teacherList) {
  192. ClassTimeStatisticsRecord record = new ClassTimeStatisticsRecord();
  193. record.setUserId(teacher.getId());
  194. record.setClassTimeStatisticsId(statistics.getId());
  195. record.setEmployType(teacher.getEmployType());
  196. JsonArray allClassTimeDataArray = new JsonArray();
  197. Double allClassTime = 0D;
  198. //计算补充课时,算作正课课时,但是计算超出课时时不计算这部分
  199. for (String courseTimeType : courseTimeMap.keySet()) {
  200. List<WfTeacherCourseTime> courseTimes = courseTimeMap.get(courseTimeType);
  201. double sum = courseTimes.stream()
  202. .filter(x -> x.getTeacherIds().contains(teacher.getId().toString()))
  203. .mapToDouble(WfTeacherCourseTime::getCourseTime).sum();
  204. allClassTime = allClassTime + sum;
  205. if(CourseTimeTypeEnum.CTT001.getCode().equals(courseTimeType)){
  206. record.setClassTime1(sum);
  207. for (LocalDate localDate : allDateList) {
  208. double sum1 = courseTimes.stream()
  209. .filter(x -> x.getTeacherIds().contains(teacher.getId().toString()) && localDate.equals(x.getScheduleDate()))
  210. .mapToDouble(WfTeacherCourseTime::getCourseTime).sum();
  211. JsonObject courseJson = new JsonObject();
  212. courseJson.addProperty("type", CourseTimeTypeEnum.CTT001.getValue());
  213. courseJson.addProperty("scheduleDate", localDate.format(formatter));
  214. courseJson.addProperty("content", sum1);
  215. courseJson.addProperty("adjustType", "");
  216. allClassTimeDataArray.add(courseJson);
  217. }
  218. }else if(CourseTimeTypeEnum.CTT002.getCode().equals(courseTimeType)){
  219. record.setClassTime2(sum);
  220. for (LocalDate localDate : allDateList) {
  221. double sum1 = courseTimes.stream()
  222. .filter(x -> x.getTeacherIds().contains(teacher.getId().toString()) && localDate.equals(x.getScheduleDate()))
  223. .mapToDouble(WfTeacherCourseTime::getCourseTime).sum();
  224. JsonObject courseJson = new JsonObject();
  225. courseJson.addProperty("type", CourseTimeTypeEnum.CTT001.getValue());
  226. courseJson.addProperty("scheduleDate", localDate.format(formatter));
  227. courseJson.addProperty("content", sum1);
  228. courseJson.addProperty("adjustType", "");
  229. allClassTimeDataArray.add(courseJson);
  230. }
  231. }else if(CourseTimeTypeEnum.CTT003.getCode().equals(courseTimeType)){
  232. record.setClassTime3(sum);
  233. for (LocalDate localDate : allDateList) {
  234. double sum1 = courseTimes.stream()
  235. .filter(x -> x.getTeacherIds().contains(teacher.getId().toString()) && localDate.equals(x.getScheduleDate()))
  236. .mapToDouble(WfTeacherCourseTime::getCourseTime).sum();
  237. JsonObject courseJson = new JsonObject();
  238. courseJson.addProperty("type", CourseTimeTypeEnum.CTT001.getValue());
  239. courseJson.addProperty("scheduleDate", localDate.format(formatter));
  240. courseJson.addProperty("content", sum1);
  241. courseJson.addProperty("adjustType", "");
  242. allClassTimeDataArray.add(courseJson);
  243. }
  244. }else if(CourseTimeTypeEnum.CTT004.getCode().equals(courseTimeType)){
  245. record.setClassTime4(sum);
  246. for (LocalDate localDate : allDateList) {
  247. double sum1 = courseTimes.stream()
  248. .filter(x -> x.getTeacherIds().contains(teacher.getId().toString()) && localDate.equals(x.getScheduleDate()))
  249. .mapToDouble(WfTeacherCourseTime::getCourseTime).sum();
  250. JsonObject courseJson = new JsonObject();
  251. courseJson.addProperty("type", CourseTimeTypeEnum.CTT001.getValue());
  252. courseJson.addProperty("scheduleDate", localDate.format(formatter));
  253. courseJson.addProperty("content", sum1);
  254. courseJson.addProperty("adjustType", "");
  255. allClassTimeDataArray.add(courseJson);
  256. }
  257. }else if(CourseTimeTypeEnum.CTT005.getCode().equals(courseTimeType)){
  258. record.setClassTime5(sum);
  259. for (LocalDate localDate : allDateList) {
  260. double sum1 = courseTimes.stream()
  261. .filter(x -> x.getTeacherIds().contains(teacher.getId().toString()) && localDate.equals(x.getScheduleDate()))
  262. .mapToDouble(WfTeacherCourseTime::getCourseTime).sum();
  263. JsonObject courseJson = new JsonObject();
  264. courseJson.addProperty("type", CourseTimeTypeEnum.CTT001.getValue());
  265. courseJson.addProperty("scheduleDate", localDate.format(formatter));
  266. courseJson.addProperty("content", sum1);
  267. courseJson.addProperty("adjustType", "");
  268. allClassTimeDataArray.add(courseJson);
  269. }
  270. }else if(CourseTimeTypeEnum.CTT006.getCode().equals(courseTimeType)){
  271. record.setClassTime6(sum);
  272. for (LocalDate localDate : allDateList) {
  273. double sum1 = courseTimes.stream()
  274. .filter(x -> x.getTeacherIds().contains(teacher.getId().toString()) && localDate.equals(x.getScheduleDate()))
  275. .mapToDouble(WfTeacherCourseTime::getCourseTime).sum();
  276. JsonObject courseJson = new JsonObject();
  277. courseJson.addProperty("type", CourseTimeTypeEnum.CTT001.getValue());
  278. courseJson.addProperty("scheduleDate", localDate.format(formatter));
  279. courseJson.addProperty("content", sum1);
  280. courseJson.addProperty("adjustType", "");
  281. allClassTimeDataArray.add(courseJson);
  282. }
  283. }
  284. }
  285. //早自习、正课、晚辅、顶课、调课
  286. Double classTime7 = 0D,classTime8 = 0D,classTime9 = 0D,classTime11 = 0D;
  287. List<String> zkList = Arrays.asList("上1","上2","上3","上4","下1","下2","下3","下4");
  288. List<String> wzxList = Arrays.asList("晚1","晚2","晚3");
  289. //查询出老师的课程
  290. List<CourseListVo> courseList = allCourseList.stream()
  291. .filter(x -> x.getTeacherId().contains(teacher.getId().toString()))
  292. .collect(Collectors.toList());
  293. //循环,统计出各项数据
  294. for (CourseListVo courseListVo : courseList) {
  295. //如果这个课程数据包含在被删除的课时中,跳过不计算
  296. Map<LocalDate, String> deleteDates = deleteMap.get(courseListVo.getClassId());
  297. JsonObject courseJson = new JsonObject();
  298. if(deleteDates != null && deleteDates.containsKey(courseListVo.getScheduleDate()) && deleteDates.get(courseListVo.getScheduleDate()) != null
  299. && deleteDates.get(courseListVo.getScheduleDate()).contains(courseListVo.getTimeNumber()) ){
  300. courseJson.addProperty("type", courseListVo.getShortName());
  301. courseJson.addProperty("scheduleDate", courseListVo.getScheduleDate().format(formatter));
  302. courseJson.addProperty("content", "");
  303. courseJson.addProperty("adjustType", "course_delete");
  304. allClassTimeDataArray.add(courseJson);
  305. continue;
  306. }
  307. if("早自习".equals(courseListVo.getShortName())){
  308. classTime7 += weightSetMap.get(courseListVo.getShortName()) == null ? 0d : weightSetMap.get(courseListVo.getShortName());
  309. }else if(zkList.contains(courseListVo.getShortName())){
  310. classTime8 += weightSetMap.get(courseListVo.getShortName()) == null ? 0d : weightSetMap.get(courseListVo.getShortName());
  311. }else if(wzxList.contains(courseListVo.getShortName())){
  312. classTime9 += weightSetMap.get(courseListVo.getShortName()) == null ? 0d : weightSetMap.get(courseListVo.getShortName());
  313. }
  314. if(courseListVo.getAdjustType() != null && !courseListVo.getAdjustType().isEmpty()){
  315. if(CourseAdjustTypeEnum.courseExchange.getCode().equals(courseListVo.getAdjustType())){
  316. classTime11 += weightSetMap.get(courseListVo.getShortName());
  317. }
  318. }
  319. courseJson.addProperty("type", courseListVo.getShortName());
  320. courseJson.addProperty("scheduleDate", courseListVo.getScheduleDate().format(formatter));
  321. courseJson.addProperty("content", courseListVo.getClassName() + "," + courseListVo.getCourseName());
  322. courseJson.addProperty("adjustType", courseListVo.getAdjustType() == null?"":courseListVo.getAdjustType());
  323. allClassTimeDataArray.add(courseJson);
  324. }
  325. record.setClassTime7(classTime7);
  326. record.setClassTime8(classTime8);
  327. record.setClassTime9(classTime9);
  328. record.setClassTime11(classTime11);
  329. JsonObject weekDataJson = new JsonObject();
  330. Double allTimes = 0d;
  331. Double allCcksTime = 0d;//总的超出课时,从每周的正课课时中计算
  332. BigDecimal ccksTime = BigDecimal.ZERO;//超出课时标准(每周)
  333. if("FB1601".equals(teacher.getEmployType())){
  334. ccksTime = BigDecimal.valueOf(costSetMap.get("cost7"));
  335. }else{//外聘FB1605、合作人员FB1609
  336. ccksTime = BigDecimal.valueOf(costSetMap.get("cost8"));
  337. }
  338. //存每周的数据
  339. for (WeekTimeRangeVo timeRangeVo : weekTimeRangeVos) {
  340. Double zkTimes = 0d;
  341. Double wzxTimes = 0d;
  342. Double tkTimes = 0d;
  343. for (CourseListVo courseListVo : courseList) {
  344. LocalDate scheduleDate = courseListVo.getScheduleDate();
  345. //如果这个课程数据包含在被删除的课时中,跳过不计算
  346. Map<LocalDate, String> deleteDates = deleteMap.get(courseListVo.getClassId());
  347. if(deleteDates != null && deleteDates.containsKey(scheduleDate) && deleteDates.get(courseListVo.getScheduleDate()) != null
  348. && deleteDates.get(courseListVo.getScheduleDate()).contains(courseListVo.getTimeNumber())){
  349. continue;
  350. }
  351. if(!( (scheduleDate.equals(timeRangeVo.getMondayDate()) || scheduleDate.isAfter(timeRangeVo.getMondayDate()))
  352. && (scheduleDate.equals(timeRangeVo.getSundayDate()) || scheduleDate.isBefore(timeRangeVo.getSundayDate())))){
  353. continue;
  354. }
  355. if("早自习".equals(courseListVo.getShortName())){
  356. zkTimes += weightSetMap.get(courseListVo.getShortName()) == null ? 0d : weightSetMap.get(courseListVo.getShortName());
  357. }else if(zkList.contains(courseListVo.getShortName())){
  358. zkTimes += weightSetMap.get(courseListVo.getShortName()) == null ? 0d : weightSetMap.get(courseListVo.getShortName());
  359. }else if(wzxList.contains(courseListVo.getShortName())){
  360. wzxTimes += weightSetMap.get(courseListVo.getShortName()) == null ? 0d : weightSetMap.get(courseListVo.getShortName());
  361. }
  362. if(courseListVo.getAdjustType() != null && !courseListVo.getAdjustType().isEmpty()){
  363. if(CourseAdjustTypeEnum.courseExchange.getCode().equals(courseListVo.getAdjustType())){
  364. tkTimes += weightSetMap.get(courseListVo.getShortName());
  365. }
  366. }
  367. }
  368. //计算该老师发起的事假、病假顶课数据
  369. List<CourseListVo> substituteList = allSubstituteList.stream().filter(x -> x.getExchangeTeacherId().equals(teacher.getId()))
  370. .collect(Collectors.toList());
  371. Double reduceTime = 0d;//顶课课时
  372. for (CourseListVo courseListVo : substituteList) {
  373. Map<LocalDate, String> deleteDates = deleteMap.get(courseListVo.getClassId());
  374. if(deleteDates != null && deleteDates.containsKey(courseListVo.getScheduleDate()) && deleteDates.get(courseListVo.getScheduleDate()).contains(courseListVo.getTimeNumber())){
  375. continue;
  376. }
  377. if(!( (courseListVo.getScheduleDate().equals(timeRangeVo.getMondayDate()) || courseListVo.getScheduleDate().isAfter(timeRangeVo.getMondayDate()))
  378. && (courseListVo.getScheduleDate().equals(timeRangeVo.getSundayDate()) || courseListVo.getScheduleDate().isBefore(timeRangeVo.getSundayDate())))){
  379. continue;
  380. }
  381. reduceTime += weightSetMap.get(courseListVo.getShortName());
  382. }
  383. substituteList = allSubstituteList.stream().filter(x -> x.getTeacherId().equals(teacher.getId().toString()))
  384. .collect(Collectors.toList());
  385. for (CourseListVo courseListVo : substituteList) {
  386. Map<LocalDate, String> deleteDates = deleteMap.get(courseListVo.getClassId());
  387. if(deleteDates != null && deleteDates.containsKey(courseListVo.getScheduleDate()) && deleteDates.get(courseListVo.getScheduleDate()).contains(courseListVo.getTimeNumber())){
  388. continue;
  389. }
  390. if(!( (courseListVo.getScheduleDate().equals(timeRangeVo.getMondayDate()) || courseListVo.getScheduleDate().isAfter(timeRangeVo.getMondayDate()))
  391. && (courseListVo.getScheduleDate().equals(timeRangeVo.getSundayDate()) || courseListVo.getScheduleDate().isBefore(timeRangeVo.getSundayDate())))){
  392. continue;
  393. }
  394. reduceTime = reduceTime - weightSetMap.get(courseListVo.getShortName());
  395. }
  396. timeRangeVo.setZkTimes(zkTimes);
  397. timeRangeVo.setWzxTimes(wzxTimes);
  398. timeRangeVo.setDkTimes(reduceTime);
  399. timeRangeVo.setTkTimes(tkTimes);
  400. allTimes += (zkTimes + wzxTimes);
  401. if(zkTimes > ccksTime.doubleValue()){
  402. allCcksTime += zkTimes - ccksTime.doubleValue();
  403. }
  404. }
  405. weekDataJson.add("weekData", new Gson().toJsonTree(weekTimeRangeVos));
  406. weekDataJson.addProperty("allTimes", allTimes);
  407. record.setWeekData(weekDataJson.toString());
  408. //计算总课时
  409. allClassTime = allClassTime + classTime7 + classTime8 + classTime9;
  410. record.setAllClassTime(allClassTime);
  411. //计算费用,根据聘用类型判断费用问题
  412. Double classTimeAmount = 0d;
  413. Double beyondClassTimeAmount = 0d;
  414. BigDecimal zzxCost = BigDecimal.ZERO;//早自习费用
  415. if("FB1601".equals(teacher.getEmployType())){
  416. zzxCost = BigDecimal.valueOf(costSetMap.get("cost1"));
  417. }else{//外聘FB1605、合作人员FB1609
  418. zzxCost = BigDecimal.valueOf(costSetMap.get("cost2"));
  419. }
  420. classTimeAmount += BigDecimal.valueOf(classTime7).multiply(zzxCost).doubleValue();
  421. BigDecimal zkCost = BigDecimal.ZERO;//正课费用
  422. if("FB1601".equals(teacher.getEmployType())){
  423. zkCost = BigDecimal.valueOf(costSetMap.get("cost3"));
  424. }else{//外聘FB1605、合作人员FB1609
  425. zkCost = BigDecimal.valueOf(costSetMap.get("cost4"));
  426. }
  427. double classTime = classTime8
  428. + (record.getClassTime1() == null ? 0d:record.getClassTime1())
  429. + (record.getClassTime2() == null ? 0d:record.getClassTime2())
  430. + (record.getClassTime3() == null ? 0d:record.getClassTime3())
  431. + (record.getClassTime4() == null ? 0d:record.getClassTime4())
  432. + (record.getClassTime5() == null ? 0d:record.getClassTime5())
  433. + (record.getClassTime6() == null ? 0d:record.getClassTime6());
  434. classTimeAmount += BigDecimal.valueOf(classTime).multiply(zkCost).doubleValue();
  435. BigDecimal wzxCost = BigDecimal.ZERO;//晚自习费用
  436. if("FB1601".equals(teacher.getEmployType())){
  437. wzxCost = BigDecimal.valueOf(costSetMap.get("cost5"));
  438. }else{//外聘FB1605、合作人员FB1609
  439. wzxCost = BigDecimal.valueOf(costSetMap.get("cost6"));
  440. }
  441. classTimeAmount += BigDecimal.valueOf(classTime9).multiply(wzxCost).doubleValue();
  442. /**
  443. * 顶课:
  444. * 1、流程发起人(被顶老师)的课被顶课老师上了,那么被顶老师的课时就会-1,顶课老师课时就会+1
  445. * 2、顶课老师会根据规则额外加一笔费用,根据设置1课时额外加4元
  446. * 3、顶课类型为事假和病假的时候会扣除被顶老师的费用,根据设置为扣除4元
  447. */
  448. BigDecimal reduceTimeAmount = BigDecimal.ZERO;
  449. {
  450. //计算该老师发起的事假、病假顶课数据
  451. List<CourseListVo> substituteList = allSubstituteList.stream().filter(x -> x.getExchangeTeacherId().equals(teacher.getId()))
  452. .collect(Collectors.toList());
  453. Double reduceTime = 0d;//顶课课时
  454. Double dkClassTime = 0d;//去顶课的数量,所有顶课数量
  455. for (CourseListVo courseListVo : substituteList) {
  456. Map<LocalDate, String> deleteDates = deleteMap.get(courseListVo.getClassId());
  457. if(deleteDates != null && deleteDates.containsKey(courseListVo.getScheduleDate()) && deleteDates.get(courseListVo.getScheduleDate()).contains(courseListVo.getTimeNumber())){
  458. continue;
  459. }
  460. reduceTime += weightSetMap.get(courseListVo.getShortName());
  461. dkClassTime += weightSetMap.get(courseListVo.getShortName());
  462. }
  463. Double bdkClassTime = 0d;//发起顶课的数量,只统计事假和病假的数量
  464. substituteList = allSubstituteList.stream().filter(x -> x.getTeacherId().equals(teacher.getId().toString()))
  465. .collect(Collectors.toList());
  466. for (CourseListVo courseListVo : substituteList) {
  467. Map<LocalDate, String> deleteDates = deleteMap.get(courseListVo.getClassId());
  468. if(deleteDates != null && deleteDates.containsKey(courseListVo.getScheduleDate()) && deleteDates.get(courseListVo.getScheduleDate()).contains(courseListVo.getTimeNumber())){
  469. continue;
  470. }
  471. reduceTime = reduceTime - weightSetMap.get(courseListVo.getShortName());
  472. if("sick _leave".equals(courseListVo.getReason()) || "leave_absence".equals(courseListVo.getReason())){
  473. bdkClassTime += weightSetMap.get(courseListVo.getShortName());
  474. }
  475. }
  476. record.setClassTime10(reduceTime);
  477. BigDecimal dkCost = BigDecimal.ZERO;//顶课费用,顶课老师加钱
  478. if("FB1601".equals(teacher.getEmployType())){
  479. dkCost = BigDecimal.valueOf(costSetMap.get("cost11"));
  480. }else{//外聘FB1605、合作人员FB1609
  481. dkCost = BigDecimal.valueOf(costSetMap.get("cost12"));
  482. }
  483. reduceTimeAmount = BigDecimal.valueOf(dkClassTime).multiply(dkCost);
  484. BigDecimal bdkCost = BigDecimal.ZERO;//顶课费用,被顶课老师扣钱
  485. if("FB1601".equals(teacher.getEmployType())){
  486. bdkCost = BigDecimal.valueOf(costSetMap.get("cost13")==null?0:costSetMap.get("cost13"));
  487. }else{//外聘FB1605、合作人员FB1609
  488. bdkCost = BigDecimal.valueOf(costSetMap.get("cost14")==null?0:costSetMap.get("cost14"));
  489. }
  490. //计算被扣除的费用
  491. reduceTimeAmount = reduceTimeAmount.subtract(bdkCost.multiply(BigDecimal.valueOf(bdkClassTime)));
  492. }
  493. record.setClassTimeAmount(classTimeAmount);
  494. //计算超出的课时,需要先计算出每个周的开始结束时间
  495. BigDecimal ccksCost = BigDecimal.ZERO;
  496. if("FB1601".equals(teacher.getEmployType())){
  497. ccksCost = BigDecimal.valueOf(costSetMap.get("cost9"));
  498. }else{//外聘FB1605、合作人员FB1609
  499. ccksCost = BigDecimal.valueOf(costSetMap.get("cost10"));
  500. }
  501. //计算出总的超出课时并计算超课时费
  502. beyondClassTimeAmount = BigDecimal.valueOf(allCcksTime).multiply(ccksCost).doubleValue();
  503. if(!"FB1601".equals(teacher.getEmployType())){
  504. record.setBeyondClassTimeAmount(beyondClassTimeAmount);
  505. }
  506. //计算总金额,课时费+超课时费
  507. Double totalAmount = classTimeAmount + beyondClassTimeAmount + reduceTimeAmount.doubleValue();
  508. record.setTotalAmount(totalAmount);
  509. record.setAllClassTimeData(calculateClassTime(allClassTimeDataArray, weightSetMap, allDateList));
  510. record.setCreateDate(new Date());
  511. insertList.add(record);
  512. }
  513. if(!insertList.isEmpty()){
  514. //先删除
  515. String sql = "delete from class_time_statistics_record where class_time_statistics_id = " + statistics.getId();
  516. SqlRunnerAdapter.db().delete(sql);
  517. for (ClassTimeStatisticsRecord record : insertList) {
  518. recordMapper.insert(record);
  519. }
  520. }
  521. statistics.setAllClassTime(insertList.stream().filter(x -> x.getAllClassTime() != null).mapToDouble(ClassTimeStatisticsRecord::getAllClassTime).sum());
  522. statistics.setTotalAmount(insertList.stream().filter(x -> x.getTotalAmount() != null).mapToDouble(ClassTimeStatisticsRecord::getTotalAmount).sum());
  523. statistics.setBeyondClassTimeAmount(insertList.stream().filter(x -> x.getBeyondClassTimeAmount() != null).mapToDouble(ClassTimeStatisticsRecord::getBeyondClassTimeAmount).sum());
  524. statistics.setClassTimeAmount(insertList.stream().filter(x -> x.getClassTimeAmount() != null).mapToDouble(ClassTimeStatisticsRecord::getClassTimeAmount).sum());
  525. statistics.setStatus(1);
  526. this.updateById(statistics);
  527. return true;
  528. }catch (Exception e){
  529. Log.error(e.getMessage(), e);
  530. throw new MyException("统计出错,请联系管理员");
  531. }
  532. }
  533. @Override
  534. public Boolean lockData(Long id) {
  535. ClassTimeStatistics statistics = this.getById(id);
  536. return null;
  537. }
  538. @Override
  539. public List<ClassTimeStatisticsRecordVo> getRecordList(Long id) {
  540. return this.baseMapper.getRecordList(id);
  541. }
  542. /**
  543. * 导出课时统计明细
  544. * @param id
  545. * @return
  546. */
  547. @Override
  548. public byte[] recordExport(Long id) {
  549. try {
  550. //1、查询数据
  551. List<ClassTimeStatisticsRecordVo> recordList = this.baseMapper.getRecordList(id);
  552. //计算出一共有多少周
  553. ClassTimeStatistics statistics = this.getById(id);
  554. List<WeekTimeRangeVo> weekTimeRangeVos = calculateNaturalWeeks(statistics.getStartDate(), statistics.getEndDate());
  555. /**
  556. * 2、将查询出来的数据按照表头转换成二维数组
  557. * 前端固定表头:序号、工号、姓名、教研会、督导听课、临近三年退休政策、出题、阅卷、周末培优、早自习、正课(含调顶课时)、晚辅(含调顶课时)、顶课、调课
  558. * 中间动态表头:每周,总课时(含调顶课节)、顶课(节)、调课(节)、晚辅(节)......课时晚辅合计
  559. * 后端固定表头:总课时、课时费(元)、外聘教师超课时费(元)、总金额(元)
  560. */
  561. //计算一共多少列
  562. int column = 19 + (4 * weekTimeRangeVos.size());
  563. List<ArrayList<String>> dataList = new ArrayList<>();
  564. Integer sortCode = 1;
  565. JsonParser parser = new JsonParser();
  566. for (ClassTimeStatisticsRecordVo recordVo : recordList) {
  567. ArrayList<String> rowData = new ArrayList<>();
  568. rowData.add(sortCode.toString());//序号
  569. rowData.add(recordVo.getUserName());//工号
  570. rowData.add(recordVo.getName());//姓名
  571. rowData.add(recordVo.getClassTime1()==null?"":recordVo.getClassTime1().toString());//教研会
  572. rowData.add(recordVo.getClassTime2()==null?"":recordVo.getClassTime2().toString());//督导听课
  573. rowData.add(recordVo.getClassTime3()==null?"":recordVo.getClassTime3().toString());//临近三年退休政策
  574. rowData.add(recordVo.getClassTime4()==null?"":recordVo.getClassTime4().toString());//出题
  575. rowData.add(recordVo.getClassTime5()==null?"":recordVo.getClassTime5().toString());//阅卷
  576. rowData.add(recordVo.getClassTime6()==null?"":recordVo.getClassTime6().toString());//周末培优
  577. rowData.add(recordVo.getClassTime7()==null?"":recordVo.getClassTime7().toString());//早自习
  578. rowData.add(recordVo.getClassTime8()==null?"":recordVo.getClassTime8().toString());//正课(含调顶课时)
  579. rowData.add(recordVo.getClassTime9()==null?"":recordVo.getClassTime9().toString());//晚辅(含调顶课时)
  580. rowData.add(recordVo.getClassTime10()==null?"":recordVo.getClassTime10().toString());//顶课
  581. rowData.add(recordVo.getClassTime11()==null?"":recordVo.getClassTime11().toString());//调课
  582. //每周数据
  583. JsonObject weekDataJson = parser.parse(recordVo.getWeekData()).getAsJsonObject();
  584. JsonArray weekDataArray = weekDataJson.getAsJsonArray("weekData");
  585. for (int i = 0; i < weekDataArray.size(); i ++){
  586. JsonObject jsonElement = weekDataArray.get(i).getAsJsonObject();
  587. rowData.add(jsonElement.get("zkTimes").getAsString());//总课时(含调顶课节)
  588. rowData.add(jsonElement.get("dkTimes").getAsString());//顶课(节)
  589. rowData.add(jsonElement.get("tkTimes").getAsString());//调课(节)
  590. rowData.add(jsonElement.get("wzxTimes").getAsString());//晚辅(节)
  591. }
  592. rowData.add(weekDataJson.get("allTimes").getAsString());//课时晚辅合计
  593. rowData.add(recordVo.getAllClassTime().toString());//总课时
  594. rowData.add(recordVo.getClassTimeAmount().toString());//课时费(元)
  595. rowData.add(recordVo.getBeyondClassTimeAmount()==null?"":recordVo.getBeyondClassTimeAmount().toString());//外聘教师超课时费(元)
  596. rowData.add(recordVo.getTotalAmount().toString());//总金额(元)
  597. dataList.add(rowData);
  598. sortCode ++;
  599. }
  600. // 创建一个新的工作簿
  601. Workbook workbook = new XSSFWorkbook();
  602. // 创建一个工作表(sheet)
  603. String sheetName = "数据";
  604. Sheet sheet = workbook.createSheet(sheetName);
  605. // 创建一个字体对象
  606. Font font = workbook.createFont();
  607. font.setBold(true);// 设置为粗体
  608. font.setFontName("宋体");
  609. //font.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
  610. font.setFontHeightInPoints((short)24);
  611. // 创建一个单元格样式对象
  612. CellStyle cellStyle = workbook.createCellStyle();
  613. cellStyle.setFont(font); // 将字体应用到样式
  614. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  615. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  616. // 第一行表头
  617. {
  618. Row row = sheet.createRow(0);
  619. //合并第一行的列
  620. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, column - 1));
  621. //3、处理表头
  622. String title1 = statistics.getYear() + "年" + statistics.getMonth() + "月课时统计";
  623. // 创建单元格并设置值
  624. Cell cell = row.createCell(0);
  625. cell.setCellValue(title1);
  626. cell.setCellStyle(cellStyle);
  627. }
  628. //第二行表头
  629. createSecondTitle(workbook, sheet, statistics, weekTimeRangeVos.size() * 4 + 1);
  630. //第三行表头
  631. createThirdTitle(workbook, sheet, statistics.getMonth(), weekTimeRangeVos, 13);
  632. //第四行表头
  633. createFourthTitle(workbook, sheet, weekTimeRangeVos, 13);
  634. int dataRowNumber = 4;
  635. //设置样式
  636. font = workbook.createFont();
  637. font.setBold(false);// 设置为粗体
  638. font.setFontName("宋体");
  639. //font.setColor(IndexedColors.RED.getIndex()); // 设置字体颜色为红色
  640. font.setFontHeightInPoints((short)12);
  641. // 创建一个单元格样式对象
  642. cellStyle = workbook.createCellStyle();
  643. cellStyle.setFont(font); // 将字体应用到样式
  644. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  645. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  646. for (ArrayList<String> rowData : dataList) {
  647. Row dataRow = sheet.createRow(dataRowNumber);
  648. for (int i = 0; i < rowData.size(); i ++){
  649. Cell row1cell2 = dataRow.createCell(i);
  650. row1cell2.setCellValue(rowData.get(i));
  651. row1cell2.setCellStyle(cellStyle);
  652. }
  653. dataRowNumber ++;
  654. }
  655. //写入文件
  656. ByteArrayOutputStream bot = new ByteArrayOutputStream();
  657. workbook.write(bot);
  658. return bot.toByteArray();
  659. }catch (Exception e){
  660. log.error(e.getMessage());
  661. throw new MyException("导出出错,请联系管理员");
  662. }
  663. }
  664. /**
  665. * 生成第二行表头
  666. * 前端固定表头:序号、工号、姓名、教研会、督导听课、临近三年退休政策、出题、阅卷、周末培优、早自习、正课(含调顶课时)、晚辅(含调顶课时)、顶课、调课
  667. * 中间动态表头:每周,总课时(含调顶课节)、顶课(节)、调课(节)、晚辅(节)......课时晚辅合计
  668. * 后端固定表头:总课时、课时费(元)、外聘教师超课时费(元)、总金额(元)
  669. */
  670. void createSecondTitle(Workbook workbook, Sheet sheet, ClassTimeStatistics statistics, int mergeCoulmn){
  671. Font font = workbook.createFont();
  672. font.setFontName("宋体");
  673. font.setFontHeightInPoints((short)12);
  674. CellStyle cellStyle = workbook.createCellStyle();
  675. cellStyle.setFont(font); // 将字体应用到样式
  676. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  677. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  678. Row row1 = sheet.createRow(1);
  679. Cell row1cell1 = row1.createCell(0);
  680. row1cell1.setCellValue("序号");
  681. row1cell1.setCellStyle(cellStyle);
  682. Cell row1cell2 = row1.createCell(1);
  683. row1cell2.setCellValue("工号");
  684. row1cell2.setCellStyle(cellStyle);
  685. Cell row1cell3 = row1.createCell(2);
  686. row1cell3.setCellValue("姓名");
  687. row1cell3.setCellStyle(cellStyle);
  688. Cell row1cell4 = row1.createCell(3);
  689. row1cell4.setCellValue("教研会");
  690. row1cell4.setCellStyle(cellStyle);
  691. Cell row1cell5 = row1.createCell(4);
  692. row1cell5.setCellValue("督导听课");
  693. row1cell5.setCellStyle(cellStyle);
  694. Cell row1cell6 = row1.createCell(5);
  695. row1cell6.setCellValue("临近三年退休政策");
  696. row1cell6.setCellStyle(cellStyle);
  697. Cell row1cell7 = row1.createCell(6);
  698. row1cell7.setCellValue("出题");
  699. row1cell7.setCellStyle(cellStyle);
  700. Cell row1cell8 = row1.createCell(7);
  701. row1cell8.setCellValue("阅卷");
  702. row1cell8.setCellStyle(cellStyle);
  703. Cell row1cell9 = row1.createCell(8);
  704. row1cell9.setCellValue("周末培优");
  705. row1cell9.setCellStyle(cellStyle);
  706. Cell row1cell10 = row1.createCell(9);
  707. row1cell10.setCellValue("早自习");
  708. row1cell10.setCellStyle(cellStyle);
  709. Cell row1cell11 = row1.createCell(10);
  710. row1cell11.setCellValue("正课(含调顶课时)");
  711. row1cell11.setCellStyle(cellStyle);
  712. Cell row1cell12 = row1.createCell(11);
  713. row1cell12.setCellValue("晚辅(含调顶课时)");
  714. row1cell12.setCellStyle(cellStyle);
  715. Cell row1cell13 = row1.createCell(12);
  716. row1cell13.setCellValue("顶课");
  717. row1cell13.setCellStyle(cellStyle);
  718. Cell row1cell14 = row1.createCell(13);
  719. row1cell14.setCellValue("调课");
  720. row1cell14.setCellStyle(cellStyle);
  721. Cell row1cell15 = row1.createCell(14);
  722. row1cell15.setCellValue("正课课时数(平台导出" + statistics.getMonth() + "月)");
  723. row1cell15.setCellStyle(cellStyle);
  724. int index = 14 + mergeCoulmn;
  725. Cell row1cell16 = row1.createCell(index);
  726. row1cell16.setCellValue("总课时");
  727. row1cell16.setCellStyle(cellStyle);
  728. Cell row1cell17 = row1.createCell(index + 1);
  729. row1cell17.setCellValue("课时费(元)");
  730. row1cell17.setCellStyle(cellStyle);
  731. Cell row1cell18 = row1.createCell(index + 2);
  732. row1cell18.setCellValue("外聘教师超课时费(元)");
  733. row1cell18.setCellStyle(cellStyle);
  734. Cell row1cell19 = row1.createCell(index + 3);
  735. row1cell19.setCellValue("总金额(元)");
  736. row1cell19.setCellStyle(cellStyle);
  737. //合并表头
  738. for(int i = 0; i < 14; i ++){
  739. sheet.addMergedRegion(new CellRangeAddress(1, 3, i, i));
  740. }
  741. //合并中间动态表头
  742. sheet.addMergedRegion(new CellRangeAddress(1, 1, 14, index - 1));
  743. //合并前端表头
  744. for(int i = index; i < index + 4; i ++){
  745. sheet.addMergedRegion(new CellRangeAddress(1, 3, i, i));
  746. }
  747. }
  748. /**
  749. * 生成第三行表头
  750. * 关于周的表头,周一日期到周二日期,第几周,最后拼接一个单独的月份
  751. *
  752. * @param workbook
  753. * @param sheet
  754. * @param month 月份
  755. * @param weekTimeRangeVos 周的数据,包含周一的日期周二的日期
  756. * @param lastIndex 前一个表头的单元格
  757. */
  758. void createThirdTitle(Workbook workbook, Sheet sheet, int month, List<WeekTimeRangeVo> weekTimeRangeVos, int lastIndex){
  759. Font font = workbook.createFont();
  760. font.setFontName("宋体");
  761. font.setFontHeightInPoints((short)12);
  762. CellStyle cellStyle = workbook.createCellStyle();
  763. cellStyle.setFont(font); // 将字体应用到样式
  764. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  765. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  766. //设置内容
  767. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.dd");
  768. int rowNumber = 2;
  769. Row row2 = sheet.createRow(rowNumber);
  770. for(int i = 0; i < weekTimeRangeVos.size(); i ++){
  771. WeekTimeRangeVo rangeVo = weekTimeRangeVos.get(i);
  772. String cellValue = rangeVo.getMondayDate().format(formatter) + "-"
  773. + rangeVo.getSundayDate().format(formatter) + "(第" + rangeVo.getWeeks() + "周)";
  774. Cell row1cell1 = row2.createCell(lastIndex + 1 + (i * 4));
  775. row1cell1.setCellValue(cellValue);
  776. row1cell1.setCellStyle(cellStyle);
  777. }
  778. //合并单元格
  779. int monthIndex = 0;
  780. for(int i = 0; i < weekTimeRangeVos.size(); i ++){
  781. int startCoulmn = lastIndex + 1 + (i * 4);
  782. int endCoulmn = startCoulmn + 3;
  783. sheet.addMergedRegion(new CellRangeAddress(rowNumber, rowNumber, startCoulmn, endCoulmn));
  784. monthIndex = endCoulmn;
  785. }
  786. Cell row1cell2 = row2.createCell(monthIndex + 1);
  787. row1cell2.setCellValue(month + "月");
  788. row1cell2.setCellStyle(cellStyle);
  789. }
  790. /**
  791. * 生成第四行表头
  792. * 表头内容:总课时(含调顶课节)、顶课(节)、调课(节)、晚辅(节)、课时晚辅合计
  793. *
  794. * @param workbook
  795. * @param sheet
  796. * @param weekTimeRangeVos 周的数据,包含周一的日期周二的日期
  797. * @param lastIndex 前一个表头的单元格
  798. */
  799. void createFourthTitle(Workbook workbook, Sheet sheet, List<WeekTimeRangeVo> weekTimeRangeVos, int lastIndex){
  800. Font font = workbook.createFont();
  801. font.setFontName("宋体");
  802. font.setFontHeightInPoints((short)12);
  803. CellStyle cellStyle = workbook.createCellStyle();
  804. cellStyle.setFont(font); // 将字体应用到样式
  805. cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
  806. cellStyle.setAlignment(HorizontalAlignment.CENTER);
  807. int rowNumber = 3;
  808. Row row2 = sheet.createRow(rowNumber);
  809. int index = lastIndex + 1;
  810. for(int i = 0; i < weekTimeRangeVos.size(); i ++){
  811. Cell row1cell1 = row2.createCell(index);
  812. row1cell1.setCellValue("总课时(含调顶课节)");
  813. row1cell1.setCellStyle(cellStyle);
  814. index ++;
  815. Cell row1cell2 = row2.createCell(index);
  816. row1cell2.setCellValue("顶课(节)");
  817. row1cell2.setCellStyle(cellStyle);
  818. index ++;
  819. Cell row1cell3 = row2.createCell(index);
  820. row1cell3.setCellValue("调课(节)");
  821. row1cell3.setCellStyle(cellStyle);
  822. index ++;
  823. Cell row1cell4 = row2.createCell(index);
  824. row1cell4.setCellValue("晚辅(节)");
  825. row1cell4.setCellStyle(cellStyle);
  826. index ++;
  827. }
  828. Cell row1cell5 = row2.createCell(index);
  829. row1cell5.setCellValue("课时晚辅合计");
  830. row1cell5.setCellStyle(cellStyle);
  831. }
  832. //计算日期内一共几个周,并返回每周的周一日期和周日日期
  833. private static List<WeekTimeRangeVo> calculateNaturalWeeks(LocalDate startDate, LocalDate endDate) {
  834. List<WeekTimeRangeVo> result = new ArrayList<>();
  835. // 获取第一个周一
  836. LocalDate currentMonday = startDate.with(DayOfWeek.MONDAY);
  837. // 如果 startDate 不是周一,则用下一个周一
  838. if (!startDate.isEqual(currentMonday) && startDate.isAfter(currentMonday)) {
  839. currentMonday = currentMonday.plusWeeks(1);
  840. }
  841. int weeks = 1;
  842. // 遍历每个周
  843. while (!currentMonday.isAfter(endDate)) {
  844. // 计算当前周的周日
  845. LocalDate currentSunday = currentMonday.plusDays(6);
  846. // 限制周日的范围
  847. if (currentSunday.isAfter(endDate)) {
  848. currentSunday = endDate;
  849. }
  850. // 添加结果
  851. WeekTimeRangeVo timeRangeVo = new WeekTimeRangeVo();
  852. timeRangeVo.setMondayDate(currentMonday);
  853. timeRangeVo.setSundayDate(currentSunday);
  854. timeRangeVo .setWeeks(weeks);
  855. result.add(timeRangeVo);
  856. // 移动到下一个周的周一
  857. currentMonday = currentMonday.plusWeeks(1);
  858. weeks ++;
  859. }
  860. return result;
  861. }
  862. /**
  863. * 取出所有日期
  864. * @param startDate 开始时间
  865. * @param endDate 结束时间
  866. * @return 返回所有日期的集合
  867. */
  868. private static List<LocalDate> getDatesBetween(LocalDate startDate, LocalDate endDate) {
  869. List<LocalDate> dates = new ArrayList<>();
  870. long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate) + 1; // +1 包含结束日期
  871. for (long i = 0; i < numOfDaysBetween; i++) {
  872. dates.add(startDate.plusDays(i));
  873. }
  874. return dates;
  875. }
  876. String calculateClassTime(JsonArray allClassTimeDataArray, Map<String, Double> weightSetMap, List<LocalDate> allDateList){
  877. JsonObject result = new JsonObject();
  878. //计算纵向的合计
  879. Double allClassTime = 0d;
  880. DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM.dd");
  881. JsonArray rowTitle = new JsonArray();
  882. for (LocalDate localDate : allDateList) {
  883. JsonObject rowTitleJson = new JsonObject();
  884. rowTitleJson.addProperty("scheduleDate", localDate.format(formatter));
  885. Double sum = 0d;
  886. for (JsonElement jsonElement : allClassTimeDataArray) {
  887. JsonObject object = jsonElement.getAsJsonObject();
  888. LocalDate scheduleDate = LocalDate.parse(object.get("scheduleDate").getAsString());
  889. if(!scheduleDate.equals(localDate)){
  890. continue;
  891. }
  892. String adjustType = object.get("adjustType").getAsString();
  893. if("course_delete".equals(adjustType)){
  894. continue;
  895. }
  896. if(weightSetMap.get(object.get("type").getAsString()) == null){
  897. sum += object.get("content").getAsDouble();
  898. allClassTime += object.get("content").getAsDouble();
  899. }else{
  900. sum += weightSetMap.get(object.get("type").getAsString());
  901. allClassTime += weightSetMap.get(object.get("type").getAsString());
  902. }
  903. }
  904. rowTitleJson.addProperty("content", sum);
  905. rowTitleJson.addProperty("type", "");
  906. rowTitleJson.addProperty("adjustType", "");
  907. rowTitle.add(rowTitleJson);
  908. }
  909. result.add("rowTitle", rowTitle);
  910. //计算横向的合计
  911. JsonArray columnTitle = new JsonArray();
  912. for (String type : weightSetMap.keySet()) {
  913. Double sum = 0d;
  914. JsonObject columnTitleJson = new JsonObject();
  915. for (JsonElement jsonElement : allClassTimeDataArray) {
  916. JsonObject object = jsonElement.getAsJsonObject();
  917. String typeStr = object.get("type").getAsString();
  918. if(!type.equals(typeStr)){
  919. continue;
  920. }
  921. String adjustType = object.get("adjustType").getAsString();
  922. if("course_delete".equals(adjustType)){
  923. continue;
  924. }
  925. sum += weightSetMap.get(typeStr);
  926. }
  927. columnTitleJson.addProperty("type", type);
  928. columnTitleJson.addProperty("content", sum);
  929. columnTitleJson.addProperty("scheduleDate", "");
  930. columnTitleJson.addProperty("adjustType", "");
  931. columnTitle.add(columnTitleJson);
  932. }
  933. String[] courseTimeTypes = CourseTimeTypeEnum.getValues();
  934. for (String courseTimeType : courseTimeTypes) {
  935. double sum = 0d;
  936. JsonObject columnTitleJson = new JsonObject();
  937. for (JsonElement jsonElement : allClassTimeDataArray) {
  938. JsonObject object = jsonElement.getAsJsonObject();
  939. String typeStr = object.get("type").getAsString();
  940. if (!courseTimeType.equals(typeStr)) {
  941. continue;
  942. }
  943. String adjustType = object.get("adjustType").getAsString();
  944. if("course_delete".equals(adjustType)){
  945. continue;
  946. }
  947. sum += object.get("content").getAsDouble();
  948. }
  949. columnTitleJson.addProperty("type", courseTimeType);
  950. columnTitleJson.addProperty("content", sum);
  951. columnTitleJson.addProperty("scheduleDate", "");
  952. columnTitleJson.addProperty("adjustType", "");
  953. columnTitle.add(columnTitleJson);
  954. }
  955. result.add("columnTitle", columnTitle);
  956. result.add("data", allClassTimeDataArray);
  957. result.addProperty("allClassTime", allClassTime);
  958. return result.toString();
  959. }
  960. }