目录
- 一、将健康报告存入MQ延时队列
- 二、任务调度PowerJob
- 三、健康报告生成及推送
- 四、构建过慢
- 五、idea的core项目maven想deploy到本地失败
- 六、本地分支的代码报错,其他分支不报错?
- 七、代码
一、将健康报告存入MQ延时队列
二、任务调度PowerJob
三、健康报告生成及推送
四、构建过慢
五、idea的core项目maven想deploy到本地失败
六、本地分支的代码报错,其他分支不报错?
七、代码
package com.lanxi.hm.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lanxi.core.model.CustomerBodyMetrics;
import com.lanxi.core.model.MessageInfo;
import com.lanxi.core.model.MessageInfoWrapper;
import com.lanxi.core.model.enums.MessageBizEnum;
import com.lanxi.core.service.CustomerBodyMetricService;
import com.lanxi.core.service.MessagePushService;
import com.lanxi.hm.dao.hm.*;
import com.lanxi.hm.model.entity.hm.*;
import com.lanxi.hm.scheduler.powerjob.processor.HealthRecordProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import tech.powerjob.common.enums.ExecuteType;
import tech.powerjob.common.enums.ProcessorType;
import tech.powerjob.common.enums.TimeExpressionType;
import tech.powerjob.common.request.http.SaveJobInfoRequest;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
@Slf4j
@Component
public class HealthRecordPushUtil {
private final String MSG_TITLE = "HEALTH_RECORD_PUSH_MSG_CENTER_TITLE";
@Autowired
private CustomerBodyMetricService customerBodyMetricService;
@Autowired
private CustomerPackageDao customerPackageDao;
@Autowired
private CustomerPackageDetailDao customerPackageDetailDao;
@Autowired
private HealthRecordDao healthRecordDao;
@Autowired
private CustomerDao customerDao;
@Autowired
private ServiceItemDao serviceItemDao;
@Autowired
private Environment environment;
@Autowired
private MessagePushService messagePushService;
public void createHealthRecords(){
// 查询所有客户
List customersAll = customerDao.list();
List customerPackageEntities = customerPackageDao.list().stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCustomerId()))), ArrayList::new));
List customers = new ArrayList<>();
for (CustomerPackageEntity cus : customerPackageEntities){
// 绑定了套餐的客户 - 按服务套餐生成
customers.add(customerDao.getById(cus.getCustomerId()));
}
// 未绑定套餐的客户
customersAll.removeAll(customers);
List customersB = new ArrayList<>();
// todo 查询检测次数大于等于15(times:15)次的客户
List customerBodyMatterList = customerBodyMetricService.listCount15();
for (CustomerBodyMetrics cbm : customerBodyMatterList){
CustomerEntity customer = customerDao.getById(cbm.getCustomerId());
customersB.add(customer);
}
// 未绑定套餐的客户 且 次数大于15的客户 - 默认生成
customersB.removeAll(customersAll);
// 1. 默认生成
// 健康报告列表
List createDefaultList = new ArrayList<>();
for(CustomerEntity ce : customersB){
HealthRecordEntity hre = new HealthRecordEntity();
hre.setCustomerId(ce.getId());
hre.setTeamId(ce.getServiceTeamId());
hre.setPushCycle(1);
LocalDate now = LocalDate.now();
LocalDate lastMonth = now.minusMonths(1);
LocalDate firstDay = lastMonth.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDay = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
hre.setRecordTime(firstDay+" - "+lastDay);
hre.setHrType(0);
hre.setPushStatus(0);
hre.setTenantId(String.valueOf(ce.getTenantId()));
healthRecordDao.save(hre);
createDefaultList.add(hre);
}
this.createDefault(createDefaultList);
// 2. 按服务套餐生成
// 健康报告列表
List createPackageList = new ArrayList<>();
for(CustomerEntity c : customers){
List customerPackageDetailEntityList = new ArrayList<>();
// 查询此用户绑定的套餐列表
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CustomerPackageEntity::getCustomerId, c.getId());
List list = customerPackageDao.list(wrapper);
for (CustomerPackageEntity cp : list){
// 根据套餐列表查询服务项目列表
LambdaQueryWrapper wrapper1 = new LambdaQueryWrapper<>();
// 根据 客户套餐ID 进行查询
wrapper1.eq(CustomerPackageDetailEntity::getCustomerPackageId, cp.getId());
// 根据 服务项目ID 进行去重
// customerPackageDetailEntityList是去重后的list
customerPackageDetailEntityList = customerPackageDetailDao.list(wrapper1).stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CustomerPackageDetailEntity::getServiceItemId))), ArrayList::new));
}
for (CustomerPackageDetailEntity cpd : customerPackageDetailEntityList){
HealthRecordEntity healthRecordEntity = new HealthRecordEntity();
healthRecordEntity.setCustomerId(c.getId());
healthRecordEntity.setTeamId(c.getServiceTeamId());
// 上传周期(0周、1月、2季、3年)
String params = serviceItemDao.getById(cpd.getServiceItemId()).getParams();
Map paramsMap = JSON.parseObject(params, Map.class);
String cycle = (String) paramsMap.get("cycle");
if ("week".equals(cycle)){
healthRecordEntity.setPushCycle(0);
LocalDate now = LocalDate.now();
LocalDate todayOfLastWeek = now.minusDays(7);
LocalDate monday = todayOfLastWeek.with(TemporalAdjusters.previous(DayOfWeek.SUNDAY)).plusDays(1);
LocalDate sunday = todayOfLastWeek.with(TemporalAdjusters.next(DayOfWeek.MONDAY)).minusDays(1);
healthRecordEntity.setRecordTime(monday+" - "+sunday);
} else if ("month".equals(cycle)) {
healthRecordEntity.setPushCycle(1);
LocalDate now = LocalDate.now();
LocalDate lastMonth = now.minusMonths(1);
LocalDate firstDay = lastMonth.with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDay = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if ("quarter".equals(cycle)) {
healthRecordEntity.setPushCycle(2);
LocalDate now = LocalDate.now();
int monthValue = now.getMonthValue();
if (monthValue <= 3){
LocalDate lastYear = now.minusYears(1);
LocalDate firstDay = LocalDate.of(lastYear.getYear(), 10,1);
LocalDate lastDay = LocalDate.of(lastYear.getYear(), 12,31);
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if(monthValue <= 6){
LocalDate firstDay = LocalDate.of(now.getYear(), 1,1);
LocalDate lastDay = LocalDate.of(now.getYear(), 3,31);
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if(monthValue <= 9){
LocalDate firstDay = LocalDate.of(now.getYear(), 4,1);
LocalDate lastDay = LocalDate.of(now.getYear(), 6,30);
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else {
LocalDate firstDay = LocalDate.of(now.getYear(), 7,1);
LocalDate lastDay = LocalDate.of(now.getYear(), 9,30);
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
}
} else if ("year".equals(cycle)) {
healthRecordEntity.setPushCycle(3);
LocalDate now = LocalDate.now();
LocalDate lastYear = now.minusYears(1);
LocalDate firstDay = lastYear.with(TemporalAdjusters.firstDayOfYear());
LocalDate lastDay = lastYear.with(TemporalAdjusters.lastDayOfYear());
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
}
healthRecordEntity.setHrType(0);
healthRecordEntity.setPushStatus(0);
createPackageList.add(healthRecordEntity);
}
this.createPackage(createPackageList);
}
}
public SaveJobInfoRequest getRequest(String jobName, String cron) {
SaveJobInfoRequest request = new SaveJobInfoRequest();
request.setJobName(jobName);
request.setProcessorType(ProcessorType.BUILT_IN);
request.setExecuteType(ExecuteType.STANDALONE);
request.setProcessorInfo(HealthRecordProcessor.class.getName());
request.setTimeExpressionType(TimeExpressionType.CRON);
request.setTimeExpression(cron);
request.setEnable(true);
return request;
}
public String getCron(String params) {
Map paramsMap = JSON.parseObject(params, Map.class);
String cycle = (String) paramsMap.get("cycle");
String type = (String) paramsMap.get("type");
if ("default".equals(type)){
return "0 0 0 1 * ?";
} else if ("package".equals(type)) {
if ("week".equals(cycle)){
return "0 0 0 ? * MON";
} else if ("month".equals(cycle)) {
return "0 0 0 1 * ?";
} else if ("quarter".equals(cycle)) {
return "0 0 0 1 4,7,10,1 ?";
} else if ("year".equals(cycle)) {
return "0 0 0 1 1 ? *";
}
}
return "0 0 8 * * ?";
}
private void createPackage(List createPackageList) {
for (HealthRecordEntity healthRecordEntity : createPackageList){
healthRecordDao.save(healthRecordEntity);
String type = "package";
this.sendToMsgCenter(healthRecordEntity.getTenantId(), MessageBizEnum.HEALTH_RECORD_PUSH, healthRecordEntity.getId(), healthRecordEntity.getCustomerId(), type);
}
}
private void createDefault(List createDefaultList) {
for (HealthRecordEntity healthRecordEntity : createDefaultList){
healthRecordDao.save(healthRecordEntity);
String type = "default";
this.sendToMsgCenter(healthRecordEntity.getTenantId(), MessageBizEnum.HEALTH_RECORD_PUSH, healthRecordEntity.getId(), healthRecordEntity.getCustomerId(), type);
}
}
private void sendToMsgCenter(String tenantId, MessageBizEnum messageBizEnum, Integer healthRecordId, String customerId, String type) {
String title = "";
String content = "";
JSONObject relateData = new JSONObject();
relateData.put("healthRecordId", healthRecordId);
MessageInfoWrapper messageInfoWrapper = new MessageInfoWrapper();
MessageInfo messageInfo = new MessageInfo();
title = "健康报告";
messageInfo.setTitle(title);
messageInfo.setContent(content);
messageInfo.setTargetPerson(customerId);
messageInfo.setRelateData(relateData);
messageInfo.setCreater("system");
messageInfoWrapper.setMessageInfo(messageInfo);
LocalDateTime dateTime = LocalDateTime.now();
if ("default".equals(type)){
dateTime = LocalDateTime.of(LocalDate.now(), LocalTime.of(8,0,0));
}
messagePushService.sendToMessageCenter(tenantId,messageBizEnum,messageInfoWrapper, dateTime);
}
}
//
// public void createPackage(List createPackageList) {
// for (HealthRecordEntity healthRecordEntity : createPackageList){
// healthRecordDao.save(healthRecordEntity);
// String type = "package";
// this.sendToMsgCenter(healthRecordEntity.getTenantId(), MessageBizEnum.HEALTH_RECORD_PUSH, healthRecordEntity.getId(), healthRecordEntity.getCustomerId(), type);
// }
// }
//
//
// public void createDefault(List createDefaultList) {
// for (HealthRecordEntity healthRecordEntity : createDefaultList){
// healthRecordDao.save(healthRecordEntity);
// String type = "default";
// this.sendToMsgCenter(healthRecordEntity.getTenantId(), MessageBizEnum.HEALTH_RECORD_PUSH, healthRecordEntity.getId(), healthRecordEntity.getCustomerId(), type);
// }
// }