|  | @@ -0,0 +1,177 @@
 | 
	
		
			
				|  |  | +package cn.iocoder.yudao.module.pms.job.dailyreport;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import cn.hutool.core.collection.CollUtil;
 | 
	
		
			
				|  |  | +import cn.hutool.core.date.LocalDateTimeUtil;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.controller.admin.iotprojecttask.vo.IotProjectTaskPageReqVO;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.controller.admin.iotrddailyreport.vo.IotRdDailyReportPageReqVO;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojecttask.IotProjectTaskDO;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.dal.dataobject.iotrddailyreport.IotRdDailyReportDO;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.message.PmsMessage;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.service.iotdeviceperson.IotDevicePersonService;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.service.iotmaintenancebom.IotMaintenanceBomService;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.service.iotmainworkorderbom.IotMainWorkOrderBomService;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.service.iotprojecttask.IotProjectTaskService;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.pms.service.iotrddailyreport.IotRdDailyReportService;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
 | 
	
		
			
				|  |  | +import cn.iocoder.yudao.module.system.service.dept.DeptService;
 | 
	
		
			
				|  |  | +import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import javax.annotation.Resource;
 | 
	
		
			
				|  |  | +import java.time.LocalDateTime;
 | 
	
		
			
				|  |  | +import java.time.LocalTime;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import static cn.iocoder.yudao.module.pms.framework.config.MultiThreadConfiguration.PMS_THREAD_POOL_TASK_EXECUTOR;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +@Component
 | 
	
		
			
				|  |  | +@Slf4j
 | 
	
		
			
				|  |  | +public class CreateRdDailyReportOrderJob implements JobHandler {
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private IotDevicePersonService iotDevicePersonService;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private IotMaintenanceBomService iotMaintenanceBomService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private IotRdDailyReportService iotRdDailyReportService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private IotProjectTaskService iotProjectTaskService;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private AdminUserApi adminUserApi;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private IotMainWorkOrderBomService iotMainWorkOrderBomService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Resource(name = PMS_THREAD_POOL_TASK_EXECUTOR)
 | 
	
		
			
				|  |  | +    private ThreadPoolTaskExecutor pmsThreadPoolTaskExecutor;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private PmsMessage pmsMessage;
 | 
	
		
			
				|  |  | +    @Resource
 | 
	
		
			
				|  |  | +    private DeptService deptService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    @TenantIgnore
 | 
	
		
			
				|  |  | +    public String execute(String param) throws Exception {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 查询 SAP同步领料单 开始时间 结束时间 数据字典
 | 
	
		
			
				|  |  | +        String createTime = DictFrameworkUtils.parseDictDataValue("rq_iot_rd_daily_report_createtime", "生成时间");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 查询所有瑞都的项目任务 以‘井号-时间’ 为唯一键 当天创建时间 内没有生成过日报 就自动生成
 | 
	
		
			
				|  |  | +        IotRdDailyReportPageReqVO pageReqVO = new IotRdDailyReportPageReqVO();
 | 
	
		
			
				|  |  | +        List<IotRdDailyReportDO> dailyReports = iotRdDailyReportService.dailyReports(pageReqVO);
 | 
	
		
			
				|  |  | +        LocalDateTime currentDate = LocalDateTime.now();
 | 
	
		
			
				|  |  | +        String currentFormatDateStr = LocalDateTimeUtil.format(currentDate, "yyyy-MM-dd");
 | 
	
		
			
				|  |  | +        // 前一天的日期
 | 
	
		
			
				|  |  | +        LocalDateTime yesterday = currentDate.minusDays(1);
 | 
	
		
			
				|  |  | +        // 需要生成日报的 任务集合
 | 
	
		
			
				|  |  | +        Set<Long> taskIds = new HashSet<>();
 | 
	
		
			
				|  |  | +        if (CollUtil.isNotEmpty(dailyReports)) {
 | 
	
		
			
				|  |  | +            dailyReports.forEach(report -> {
 | 
	
		
			
				|  |  | +                // 查询 创建时间 不包含当天的日报
 | 
	
		
			
				|  |  | +                LocalDateTime createDate = report.getCreateTime();
 | 
	
		
			
				|  |  | +                String formatDateStr = LocalDateTimeUtil.format(createDate, "yyyy-MM-dd");
 | 
	
		
			
				|  |  | +                if (!formatDateStr.equals(currentFormatDateStr)) {
 | 
	
		
			
				|  |  | +                    taskIds.add(report.getTaskId());
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 查询瑞都相关的项目任务 根据每个任务的填报人进行工单生成和推送
 | 
	
		
			
				|  |  | +        IotProjectTaskPageReqVO taskReqVO = new IotProjectTaskPageReqVO();
 | 
	
		
			
				|  |  | +        taskReqVO.setCompanyId(163l);
 | 
	
		
			
				|  |  | +        List<IotProjectTaskDO> tasks = iotProjectTaskService.projectTasks(taskReqVO);
 | 
	
		
			
				|  |  | +        Set<Long> userIds = new HashSet<>();
 | 
	
		
			
				|  |  | +        List<IotRdDailyReportDO> reports = new ArrayList<>();
 | 
	
		
			
				|  |  | +        Map<Long, Long> taskSubmitterPair = new HashMap<>();
 | 
	
		
			
				|  |  | +        if (CollUtil.isNotEmpty(tasks)) {
 | 
	
		
			
				|  |  | +            tasks.forEach(task -> {
 | 
	
		
			
				|  |  | +                // 查询当前任务的 工单填报人
 | 
	
		
			
				|  |  | +                Set<Long> submitterIds = task.getSubmitter();
 | 
	
		
			
				|  |  | +                if (CollUtil.isNotEmpty(submitterIds)) {
 | 
	
		
			
				|  |  | +                    // 查询工单填报人所在部门
 | 
	
		
			
				|  |  | +                    userIds.addAll(submitterIds);
 | 
	
		
			
				|  |  | +                    taskSubmitterPair.put(task.getId(), new ArrayList<>(submitterIds).get(0));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            // 查询所有任务配置的工单填报人的信息
 | 
	
		
			
				|  |  | +            Map<Long, AdminUserRespDTO> users = adminUserApi.getUserMap(userIds);
 | 
	
		
			
				|  |  | +            Set<Long> deptIds = new HashSet<>();
 | 
	
		
			
				|  |  | +            if (CollUtil.isNotEmpty(users)) {
 | 
	
		
			
				|  |  | +                // 统一查询所有用户所属的部门信息
 | 
	
		
			
				|  |  | +                users.forEach((userId, user) -> {
 | 
	
		
			
				|  |  | +                    deptIds.add(user.getDeptId());
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            Map<Long, DeptDO> deptPair = deptService.getDeptMap(deptIds);
 | 
	
		
			
				|  |  | +            // 以任务为维度 生成瑞都日报 部门为任务配置的工单填报人对应的部门
 | 
	
		
			
				|  |  | +            tasks.forEach(task -> {
 | 
	
		
			
				|  |  | +                if (taskIds.contains(task.getId()) && CollUtil.isNotEmpty(task.getSubmitter())) {
 | 
	
		
			
				|  |  | +                    // 生成当天的日报
 | 
	
		
			
				|  |  | +                    IotRdDailyReportDO report = new IotRdDailyReportDO();
 | 
	
		
			
				|  |  | +                    if (taskSubmitterPair.containsKey(task.getId())) {
 | 
	
		
			
				|  |  | +                        Long userId = taskSubmitterPair.get(task.getId());
 | 
	
		
			
				|  |  | +                        if (users.containsKey(userId)) {
 | 
	
		
			
				|  |  | +                            AdminUserRespDTO user = users.get(userId);
 | 
	
		
			
				|  |  | +                            report.setDeptId(user.getDeptId());
 | 
	
		
			
				|  |  | +                            // 根据用户部门名称 生成日报标题
 | 
	
		
			
				|  |  | +                            if (deptPair.containsKey(user.getDeptId())) {
 | 
	
		
			
				|  |  | +                                DeptDO dept = deptPair.get(user.getDeptId());
 | 
	
		
			
				|  |  | +                                String deptName = dept.getName();
 | 
	
		
			
				|  |  | +                                String[] multiLangDeptNames = deptName.split("~~");
 | 
	
		
			
				|  |  | +                                report.setReportName(multiLangDeptNames[0] + "/" + currentFormatDateStr + "日报填报" );
 | 
	
		
			
				|  |  | +                            }
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    report.setProjectId(task.getProjectId());
 | 
	
		
			
				|  |  | +                    report.setTaskId(task.getId());
 | 
	
		
			
				|  |  | +                    report.setDeviceIds(task.getDeviceIds());
 | 
	
		
			
				|  |  | +                    report.setStartTime(LocalTime.of(8, 0, 0));
 | 
	
		
			
				|  |  | +                    report.setEndTime(LocalTime.of(8, 0, 0));
 | 
	
		
			
				|  |  | +                    report.setConstructionStartDate(yesterday);
 | 
	
		
			
				|  |  | +                    report.setConstructionEndDate(currentDate);
 | 
	
		
			
				|  |  | +                    report.setCreateTime(LocalDateTime.now());
 | 
	
		
			
				|  |  | +                    reports.add(report);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (CollUtil.isNotEmpty(reports)) {
 | 
	
		
			
				|  |  | +            iotRdDailyReportService.batchAddDailyReports(reports);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 向工单责任人发送 站内信及 钉钉消息
 | 
	
		
			
				|  |  | +        /* if (CollUtil.isNotEmpty(workOrders)) {
 | 
	
		
			
				|  |  | +            // 先查询所有工单责任人的手机号
 | 
	
		
			
				|  |  | +            Set<Long> personIds = new HashSet<>(convertList(workOrders, order -> Long.valueOf(order.getResponsiblePerson())));
 | 
	
		
			
				|  |  | +            Map<Long, AdminUserRespDTO> users = adminUserApi.getUserMap(personIds);
 | 
	
		
			
				|  |  | +            // 建立保养工单与 用户手机号的 对应关系
 | 
	
		
			
				|  |  | +            Map<Long, String> orderMobilePair = new HashMap<>();
 | 
	
		
			
				|  |  | +            workOrders.forEach(order -> {
 | 
	
		
			
				|  |  | +                if (users.containsKey(Long.valueOf(order.getResponsiblePerson()))) {
 | 
	
		
			
				|  |  | +                    AdminUserRespDTO user = users.get(Long.valueOf(order.getResponsiblePerson()));
 | 
	
		
			
				|  |  | +                    orderMobilePair.put(order.getId(), user.getMobile());
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            // 异步多线程发送 站内信 钉钉 消息
 | 
	
		
			
				|  |  | +            workOrders.forEach(order -> {
 | 
	
		
			
				|  |  | +                CountDownLatch latch = new CountDownLatch(workOrders.size());
 | 
	
		
			
				|  |  | +                pmsThreadPoolTaskExecutor.execute(() -> {
 | 
	
		
			
				|  |  | +                    try {
 | 
	
		
			
				|  |  | +                        if (orderMobilePair.containsKey(order.getId())) {
 | 
	
		
			
				|  |  | +                            String mobile = orderMobilePair.get(order.getId());
 | 
	
		
			
				|  |  | +                            pmsMessage.sendMessage(order.getId(), order.getName(), PmsConstants.GENERATE_MAINTENANCE,
 | 
	
		
			
				|  |  | +                                    Long.valueOf(order.getResponsiblePerson()), mobile);
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    } finally {
 | 
	
		
			
				|  |  | +                        latch.countDown();
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                });
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +        } */
 | 
	
		
			
				|  |  | +        return "创建成功";
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +}
 |