|
@@ -25,6 +25,7 @@ import java.math.RoundingMode;
|
|
|
import java.time.Duration;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.time.YearMonth;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
@@ -370,4 +371,118 @@ public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog
|
|
|
}
|
|
|
return httpRespMsg;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HttpRespMsg exportDeviceTimeCostByMonth(HttpServletRequest request, String date, Integer projectId) {
|
|
|
+ HttpRespMsg httpRespMsg = new HttpRespMsg();
|
|
|
+ String[] strings = date.split("-");
|
|
|
+ int year = Integer.parseInt(strings[0]);
|
|
|
+ int month = Integer.parseInt(strings[1]);
|
|
|
+ YearMonth yearMonth = YearMonth.of((year), month);
|
|
|
+ int daysInMonth = yearMonth.lengthOfMonth();
|
|
|
+
|
|
|
+ // 获取该月份的第一天
|
|
|
+ String startDate = String.format("%04d-%02d-01", year, month);
|
|
|
+ // 获取下个月的第一天
|
|
|
+ LocalDate lastDate = LocalDate.of(year, month, 1).plusDays(LocalDate.of(year, month, 1).lengthOfMonth() - 1);
|
|
|
+ String endDate = String.format("%04d-%02d-%02d", year, month, lastDate.getDayOfMonth());
|
|
|
+
|
|
|
+ User targetUser = userMapper.selectById(request.getHeader("Token"));
|
|
|
+ Integer companyId = targetUser.getCompanyId();
|
|
|
+ List<Map<String, Object>> sumProjectList =deviceLogMapper.selectListGroupByProject(companyId,startDate,endDate,projectId);
|
|
|
+ List<Device> deviceList = deviceMapper.selectList(new QueryWrapper<Device>().eq("company_id", companyId));
|
|
|
+ List<DeviceLog> deviceLogs = deviceLogMapper.selectList(new QueryWrapper<DeviceLog>().between("create_date", startDate, endDate).isNotNull("end_time"));
|
|
|
+
|
|
|
+ ArrayList<List<String>> allList = new ArrayList<>();
|
|
|
+ List<String> headList = new ArrayList<String>();
|
|
|
+ headList.add("项目编号");
|
|
|
+ headList.add("项目名称");
|
|
|
+ headList.add("设备编号");
|
|
|
+ headList.add("设备名称");
|
|
|
+ headList.add("成本(元)");
|
|
|
+ headList.add("月度总工时(h)");
|
|
|
+ for (int i = 1; i <= daysInMonth; i++) {
|
|
|
+ headList.add(i+"日");
|
|
|
+ }
|
|
|
+ allList.add(headList);
|
|
|
+
|
|
|
+ if (!sumProjectList.isEmpty()){
|
|
|
+ for (Map<String, Object> sum : sumProjectList) {
|
|
|
+ List<String> rowSum = new ArrayList<String>();
|
|
|
+
|
|
|
+ rowSum.add(sum.get("projectCode")==null?"":sum.get("projectCode").toString());
|
|
|
+ rowSum.add(sum.get("projectName")==null?"":sum.get("projectName").toString());
|
|
|
+ rowSum.add("");
|
|
|
+ rowSum.add("");
|
|
|
+ rowSum.add(sum.get("costMoney")==null?"":sum.get("costMoney").toString());
|
|
|
+ rowSum.add(sum.get("cost")==null?"":sum.get("cost").toString());
|
|
|
+ for (int i = 1; i <= daysInMonth; i++) {
|
|
|
+ int finalI = i;
|
|
|
+ double useTime = deviceLogs.stream()
|
|
|
+ .filter(dl -> dl.getProjectId().equals(sum.get("id"))
|
|
|
+ && dl.getCreateDate().getYear() == year
|
|
|
+ && dl.getCreateDate().getMonthValue() == month
|
|
|
+ && dl.getCreateDate().getDayOfMonth() == finalI)
|
|
|
+ .mapToDouble(DeviceLog::getUseTime).sum();
|
|
|
+ rowSum.add(useTime==0?"":BigDecimal.valueOf(useTime).setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
+
|
|
|
+ }
|
|
|
+ allList.add(rowSum);
|
|
|
+
|
|
|
+
|
|
|
+ String idProject = sum.get("id").toString();
|
|
|
+ List<Integer> deviceIds = deviceLogs.stream().filter(dl -> dl.getProjectId().toString().equals(idProject)).map(DeviceLog::getDeviceId).distinct().collect(Collectors.toList());
|
|
|
+ if (!deviceIds.isEmpty()){
|
|
|
+ for (Integer deviceId : deviceIds) {
|
|
|
+ List<String> row = new ArrayList<String>();
|
|
|
+ row.add("");
|
|
|
+ row.add("");
|
|
|
+ List<Device> deviceList1 = deviceList.stream().filter(d -> d.getId().equals(deviceId)).collect(Collectors.toList());
|
|
|
+ if (!deviceList1.isEmpty()) {
|
|
|
+ Device device = deviceList1.get(0);
|
|
|
+ row.add(device.getDeviceCode());
|
|
|
+ row.add(device.getDeviceName());
|
|
|
+ }else {
|
|
|
+ row.add("");
|
|
|
+ row.add("");
|
|
|
+ }
|
|
|
+ BigDecimal usageCost = deviceLogs.stream()
|
|
|
+ .filter(dl -> dl.getProjectId().toString().equals(idProject) && dl.getDeviceId().equals(deviceId)&&dl.getUsageCost()!=null)
|
|
|
+ .map(DeviceLog::getUsageCost)
|
|
|
+ .reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
|
+ double useTime = deviceLogs.stream()
|
|
|
+ .filter(dl -> dl.getProjectId().toString().equals(idProject) && dl.getDeviceId().equals(deviceId))
|
|
|
+ .mapToDouble(DeviceLog::getUseTime).sum();
|
|
|
+ row.add(usageCost.toString());
|
|
|
+ row.add(useTime==0?"":BigDecimal.valueOf(useTime).setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
+
|
|
|
+ for (int i = 1; i <= daysInMonth; i++) {
|
|
|
+ int finalI = i;
|
|
|
+ Optional<DeviceLog> first = deviceLogs.stream()
|
|
|
+ .filter(dl -> dl.getProjectId().toString().equals(idProject)
|
|
|
+ && dl.getDeviceId().equals(deviceId)
|
|
|
+ && dl.getCreateDate().getYear() == year
|
|
|
+ && dl.getCreateDate().getMonthValue() == month
|
|
|
+ && dl.getCreateDate().getDayOfMonth() == finalI).findFirst();
|
|
|
+ if (first.isPresent()) {
|
|
|
+ Double useTime1 = first.get().getUseTime();
|
|
|
+ row.add(useTime1==null?"":BigDecimal.valueOf(useTime1).setScale(2, RoundingMode.HALF_UP).toString());
|
|
|
+ }else {
|
|
|
+ row.add("");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ allList.add(row);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String fileName = "设备成本导出"+System.currentTimeMillis();
|
|
|
+ try {
|
|
|
+ return excelExportService.exportGeneralExcelByTitleAndList(null,null,fileName,allList, path);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return httpRespMsg;
|
|
|
+ }
|
|
|
}
|