package com.ruoyi.inspect.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ruoyi.common.core.domain.entity.User; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.inspect.dto.CostStatisticsDto; import com.ruoyi.inspect.mapper.InsOrderMapper; import com.ruoyi.inspect.mapper.InsProductMapper; import com.ruoyi.inspect.mapper.InsProductUserMapper; import com.ruoyi.inspect.mapper.ScheduleMapper; import com.ruoyi.inspect.pojo.InsOrder; import com.ruoyi.inspect.pojo.InsProduct; import com.ruoyi.inspect.pojo.InsProductUser; import com.ruoyi.inspect.pojo.Schedule; import com.ruoyi.inspect.service.ReportService; import com.ruoyi.system.mapper.UserMapper; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @Service @AllArgsConstructor public class ReportServiceImpl implements ReportService { private InsOrderMapper insOrderMapper; private InsProductUserMapper insProductUserMapper; private InsProductMapper insProductMapper; private ScheduleMapper scheduleMapper; private UserMapper userMapper; //每日业务统计 @Override public Map businessStatisticsByDay() { Map map = new HashMap<>(); /*任务接收*/ //今日任务接收 Long receive = insOrderMapper.selectCount(Wrappers.lambdaQuery().eq(InsOrder::getState, 1).apply("DATE(create_time) = CURDATE()")); map.put("RECEIVE", receive); //昨日任务接收 Long received = insOrderMapper.selectCount(Wrappers.lambdaQuery().eq(InsOrder::getState, 1).apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY")); //比例=(今天-昨天)/昨天 BigDecimal ratio = new BigDecimal(receive - received).divide(new BigDecimal(received == 0 ? 1 : received), 2, BigDecimal.ROUND_HALF_UP); map.put("RECEIVE_RATIO", ratio); /*任务已完成*/ //今日任务完成 Long finishe = insOrderMapper.selectCount(Wrappers.lambdaQuery().eq(InsOrder::getState, 4).apply("DATE(create_time) = CURDATE()")); map.put("FINISHE", finishe); //昨日任务完成 Long finished = insOrderMapper.selectCount(Wrappers.lambdaQuery().eq(InsOrder::getState, 4).apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY")); //任务完成比例=(今天-昨天)/昨天 BigDecimal finishedRatio = new BigDecimal(finishe - finished).divide(new BigDecimal(finished == 0 ? 1 : finished), 2, BigDecimal.ROUND_HALF_UP); map.put("FINISHE_RATIO", finishedRatio); /*任务剩余*/ //今日任务剩余 long surplus = receive - finishe; map.put("SURPLUS", surplus); //昨日任务剩余 long surplused = received - finished; //剩余比例=(今天-昨天)/昨天 BigDecimal surplusRatio = new BigDecimal(surplus - surplused).divide(new BigDecimal(surplused == 0 ? 1 : surplused), 2, BigDecimal.ROUND_HALF_UP); map.put("SURPLUS_RATIO", surplusRatio); LocalDate today = LocalDate.now(); LocalTime end = LocalTime.of(23, 59, 59); // 获取当前人所在实验室 Integer userId = Integer.parseInt(SecurityUtils.getUserId().toString()); String departLimsId = userMapper.selectById(userId).getDepartLimsId(); String laboratory = ""; if(StringUtils.isNotBlank(departLimsId)) { String[] split = departLimsId.split(","); for (String s : split) { if (StringUtils.isNotBlank(s) && (!Arrays.asList("1","22").contains(s))) { laboratory = insOrderMapper.getDepartment(Integer.parseInt(s)); } } } /*检测费用*/ //今日检测费用 QueryWrapper costStatisticsDtoQueryWrappers = new QueryWrapper<>(); costStatisticsDtoQueryWrappers.eq("create_time", LocalDateTime.now()); IPage page = new Page<>(); page.setSize(-1); page.setCurrent(-1); IPage iPage = insOrderMapper.selectCostStatistics(page, costStatisticsDtoQueryWrappers,LocalDate.now().atStartOfDay().toString(),today.atTime(end).toString(),laboratory); BigDecimal price = BigDecimal.ZERO; for (CostStatisticsDto record : iPage.getRecords()) { price.add(record.getPrice()); } map.put("PRICE", price); //昨日检测费用 QueryWrapper costWrappers = new QueryWrapper<>(); costWrappers.eq("create_time", LocalDateTime.now().minusDays(1)); IPage dtoIPage = insOrderMapper.selectCostStatistics(page, costWrappers,LocalDate.now().atStartOfDay().toString(),today.atTime(end).toString(),laboratory); BigDecimal priced = BigDecimal.ZERO; for (CostStatisticsDto record : dtoIPage.getRecords()) { priced.add(record.getPrice()); } //比例=(今日-昨日)/昨日 BigDecimal priceRatio = (price.subtract(priced)).divide(priced.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ONE : priced, 2, BigDecimal.ROUND_HALF_UP); map.put("PRICE_RATIO", priceRatio); /*检测工时*/ //今日检测工时 Double cost = 0.0; for (CostStatisticsDto record : iPage.getRecords()) { cost += record.getCost(); } map.put("COST", cost); //昨日检测工时 Double costed = 0.0; for (CostStatisticsDto record : dtoIPage.getRecords()) { costed += record.getCost(); } //比例=(今日-昨日)/昨日 BigDecimal costRatio = new BigDecimal(cost - costed).divide(new BigDecimal(costed == 0 ? 1 : costed), 2, BigDecimal.ROUND_HALF_UP); map.put("COST_RATIO", costRatio); /*检测人员*/ //今日检测人员 List insProductUsers = insProductUserMapper.selectList(Wrappers.lambdaQuery().apply("DATE(create_time) = CURDATE()")); long person = insProductUsers.stream().map(InsProductUser::getCreateUser).distinct().count(); map.put("PERSON", person); //昨日检测人员 List insProductUserss = insProductUserMapper.selectList(Wrappers.lambdaQuery().apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY")); long persons = insProductUserss.stream().map(InsProductUser::getCreateUser).distinct().count(); //比例=(今天-昨天)/昨天 BigDecimal personRatio = new BigDecimal(person - persons).divide(new BigDecimal(persons == 0 ? 1 : persons), 2, BigDecimal.ROUND_HALF_UP); map.put("PERSON_RATIO", personRatio); /*近十日任务接收量与完成量*/ //获取近十日的横坐标 LocalDate currentDate = LocalDate.now(); List lastTenDays = new ArrayList<>(); List receTenDays = new ArrayList<>(); List finTenDays = new ArrayList<>(); for (int i = 9; i > -1; i--) { lastTenDays.add(currentDate.minusDays(i)); receTenDays.add(insOrderMapper.selectCount(Wrappers.lambdaQuery().eq(InsOrder::getState, 1).apply("DATE(create_time) = CURDATE() - INTERVAL " + i + " DAY"))); finTenDays.add(insOrderMapper.selectCount(Wrappers.lambdaQuery().eq(InsOrder::getState, 4).apply("DATE(create_time) = CURDATE() - INTERVAL " + i + " DAY"))); } map.put("DAYS", lastTenDays); map.put("RECETENDAYS", receTenDays); map.put("FINISHTENDAYS", finTenDays); return map; } //检测项目统计 @Override public Map testProductByDay() { Map map = new HashMap<>(); /*项目接收*/ //今日项目接收量 Long receive = insProductMapper.selectCount(Wrappers.lambdaQuery().eq(InsProduct::getState, 1).apply("DATE(create_time) = CURDATE()")); map.put("RECEVICE", receive); //昨日项目接收量 Long received = insProductMapper.selectCount(Wrappers.lambdaQuery().eq(InsProduct::getState, 1).apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY")); //比例=(今天-昨天)/昨天 BigDecimal ratio = new BigDecimal(receive - received).divide(new BigDecimal(received == 0 ? 1 : received), 2, BigDecimal.ROUND_HALF_UP); map.put("RECEIVE_RATIO", ratio); /*项目完成*/ //今日项目完成量 Long finishe = insProductMapper.selectCount(Wrappers.lambdaQuery().eq(InsProduct::getState, 1).isNotNull(InsProduct::getInsResult).apply("DATE(create_time) = CURDATE()")); map.put("FINISHE", finishe); //昨日项目完成量 Long finished = insProductMapper.selectCount(Wrappers.lambdaQuery().eq(InsProduct::getState, 1).isNotNull(InsProduct::getInsResult).apply("DATE(create_time) = CURDATE() - INTERVAL 1 DAY")); //任务完成比例=(今天-昨天)/昨天 BigDecimal finishedRatio = new BigDecimal(finishe - finished).divide(new BigDecimal(finished == 0 ? 1 : finished), 2, BigDecimal.ROUND_HALF_UP); map.put("FINISHE_RATIO", finishedRatio); /*项目剩余*/ //今日项目剩余量 long surplus = receive - finishe; map.put("SURPLUS", surplus); //昨日项目剩余 long surplused = received - finished; //剩余比例=(今天-昨天)/昨天 BigDecimal surplusRatio = new BigDecimal(surplus - surplused).divide(new BigDecimal(surplused == 0 ? 1 : surplused), 2, BigDecimal.ROUND_HALF_UP); map.put("SURPLUS_RATIO", surplusRatio); /*今日项目合格率*/ //今日完成量中的合格量/今日完成量 Long accept = insProductMapper.selectCount(Wrappers.lambdaQuery().eq(InsProduct::getState, 1).eq(InsProduct::getInsResult,1).apply("DATE(create_time) = CURDATE()")); map.put("ACCEPT", accept); BigDecimal acceptRate = new BigDecimal(accept).divide(new BigDecimal(finishe == 0 ? 1 : finishe), 2, BigDecimal.ROUND_HALF_UP); map.put("ACCEPT_RATE_TODAY", acceptRate); /*今日项目完成率*/ //今日完成量/今日接收量 BigDecimal finishRate = new BigDecimal(finishe).divide(new BigDecimal(receive == 0 ? 1 : receive), 2, BigDecimal.ROUND_HALF_UP); map.put("FINISH_RATE_TODAY", finishRate); /*今日项目延期率*/ //今日项目剩余/今日项目接收量 BigDecimal delayRate = new BigDecimal(surplus).divide(new BigDecimal(received == 0 ? 1 : received), 2, BigDecimal.ROUND_HALF_UP); map.put("DELAY_RATE_TODAY", delayRate); /*近十日的项目接收量与完成量*/ //获取近十日的横坐标 LocalDate currentDate = LocalDate.now(); List lastTenDays = new ArrayList<>(); List receTenDays = new ArrayList<>(); List finTenDays = new ArrayList<>(); for (int i = 9; i > -1; i--) { lastTenDays.add(currentDate.minusDays(i)); receTenDays.add(insProductMapper.selectCount(Wrappers.lambdaQuery().eq(InsProduct::getState, 1).apply("DATE(create_time) = CURDATE() - INTERVAL " + i + " DAY"))); finTenDays.add(insProductMapper.selectCount(Wrappers.lambdaQuery().eq(InsProduct::getState, 1).isNotNull(InsProduct::getInsResult).apply("DATE(create_time) = CURDATE() - INTERVAL " + i + " DAY"))); } map.put("DAYS", lastTenDays); map.put("RECETENDAYS", receTenDays); map.put("FINISHTENDAYS", finTenDays); return map; } //首页-->日历任务图 @Override public Map calendarWorkByWeek() { Map map = new HashMap<>(); List insState = new ArrayList<>(); insState.add(0); insState.add(1); /*获取后一周日期*/ LocalDate currentDate = LocalDate.now(); List weekDays = new ArrayList<>(); for (int i = 6, j = 0; i >= 0; i--, j++) { weekDays.add(currentDate.minusDays(i)); //查询当天需要检测的委托订单 List insOrders = insOrderMapper.selectList(Wrappers.lambdaQuery() .eq(InsOrder::getState, 1) .in(InsOrder::getInsState, insState) .apply("DATE(create_time) = CURDATE() - INTERVAL " + j + " DAY")); List> works = insOrders.stream().map(insOrder -> { HashMap hashMap = new HashMap<>(); hashMap.put("text", insOrder.getEntrustCode()); hashMap.put("type", insOrder.getType()); User user = userMapper.selectById(insOrder.getCreateUser()); hashMap.put("name", user.getName()); return hashMap; }).collect(Collectors.toList()); map.put("work" + i, works); } map.put("weekDays", weekDays); return map; } //首页-->添加日程 @Override public int addSchedule(String time, String text) { //获取当前用户id Integer userId = Integer.parseInt(SecurityUtils.getUserId().toString()); Schedule schedule = new Schedule(); schedule.setUserId(userId); schedule.setScheduleTime(LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); schedule.setText(text); return scheduleMapper.insert(schedule); } //首页-->我的日程 @Override public List ScheduleByMe(String date) { //获取当前用户id Integer userId = Integer.parseInt(SecurityUtils.getUserId().toString()); LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd")); LocalDateTime startTime = localDate.atStartOfDay(); LocalDateTime endTime = localDate.plusDays(1).atStartOfDay().minusSeconds(1); return scheduleMapper.selectList(Wrappers.lambdaQuery().eq(Schedule::getUserId, userId).between(Schedule::getScheduleTime, startTime, endTime)); } }