String params = serviceItemDao.getById(c.getServiceTeamId()).getParams();
Map<String, String> paramsMap = JSONObject.parseObject(params.toString(), Map.class);
//获取前一个月第一天
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MONTH, -1);
calendar1.set(Calendar.DAY_OF_MONTH,1);
String firstDay = sdf.format(calendar1.getTime());
//获取前一个月最后一天
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, 0);
String lastDay = sdf.format(calendar2.getTime());
if (monthValue >= 1 && monthValue <= 3){
LocalDate lastYear = now.minusYears(1); // 当前年份减1
LocalDate firstDay = LocalDate.of(lastYear.getYear(), 10,1); // 获取前一年最后季度的第一天
LocalDate lastDay = LocalDate.of(lastYear.getYear(), 12,31); // 获取前一年最后季度的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if(monthValue >= 4 && 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 >= 7 && monthValue <= 9){
LocalDate firstDay = LocalDate.of(now.getYear(), 4,1); // 获取前一年最后季度的第一天
LocalDate lastDay = LocalDate.of(now.getYear(), 6,30); // 获取前一年最后季度的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if(monthValue >= 10 && monthValue <= 12){
LocalDate firstDay = LocalDate.of(now.getYear(), 7,1); // 获取前一年最后季度的第一天
LocalDate lastDay = LocalDate.of(now.getYear(), 9,30); // 获取前一年最后季度的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
}
} else if (cycle.equals("year")) {
healthRecordEntity.setPushCycle(3);
LocalDate now = LocalDate.now();
LocalDate lastYear = now.minusYears(1); // 当前年份减1
LocalDate firstDay = lastYear.with(TemporalAdjusters.firstDayOfYear()); // 获取前一年的第一天
LocalDate lastDay = lastYear.with(TemporalAdjusters.lastDayOfYear()); // 获取前一年的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
}
// 查询所有客户
List<CustomerEntity> customersAll = customerDao.list();
List<CustomerPackageEntity> customerPackageEntities = customerPackageDao.list().stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCustomerId()))), ArrayList::new));
List<CustomerEntity> customers = new ArrayList<>();
for (CustomerPackageEntity cus : customerPackageEntities){
// 绑定了套餐的客户 - 按服务套餐生成
customers.add(customerDao.getById(cus.getCustomerId()));
}
// 未绑定套餐的客户
customersAll.removeAll(customers);
List<CustomerEntity> customersB = new ArrayList<>();
// 查询检测次数大于等于15(times:15)次的客户
List<CustomerBodyMetricsEntity> customerBodyMatterList = customerBodyMetricsDao.listCount15();
for (CustomerBodyMetricsEntity cbm : customerBodyMatterList){
CustomerEntity customer = customerDao.getById(cbm.getCustomerId());
customersB.add(customer);
}
// 未绑定套餐的客户 且 次数大于15的客户 - 默认生成
customersB.removeAll(customersAll);
// 1. 默认生成
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); // 当前月份减1
LocalDate firstDay = lastMonth.with(TemporalAdjusters.firstDayOfMonth()); // 获取前月的第一天
LocalDate lastDay = lastMonth.with(TemporalAdjusters.lastDayOfMonth()); // 获取前月的最后一天
hre.setRecordTime(firstDay+" - "+lastDay);
hre.setHrType(0);
// 生成之后,八点推送
hre.setPushStatus(0);
// 推送时间为生成当天的8:00
// hre.setPushTime("");
// 生成
healthRecordDao.save(hre);
}
// 2. 按服务套餐生成
for(CustomerEntity c : customers){
List<CustomerPackageDetailEntity> customerPackageDetailEntityList = new ArrayList<>();
// 查询此用户绑定的套餐列表
LambdaQueryWrapper<CustomerPackageEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(CustomerPackageEntity::getCustomerId, c.getId());
List<CustomerPackageEntity> list = customerPackageDao.list(wrapper);
for (CustomerPackageEntity cp : list){
// 根据套餐列表查询服务项目列表
LambdaQueryWrapper<CustomerPackageDetailEntity> 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(o -> o.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(c.getServiceTeamId()).getParams();
Map<String, String> paramsMap = JSONObject.parseObject(params.toString(), Map.class);
String cycle = paramsMap.get("cycle");
if (cycle.equals("week")){
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 (cycle.equals("month")) {
healthRecordEntity.setPushCycle(1);
LocalDate now = LocalDate.now();
LocalDate lastMonth = now.minusMonths(1); // 当前月份减1
LocalDate firstDay = lastMonth.with(TemporalAdjusters.firstDayOfMonth()); // 获取前月的第一天
LocalDate lastDay = lastMonth.with(TemporalAdjusters.lastDayOfMonth()); // 获取前月的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if (cycle.equals("quarter")) {
healthRecordEntity.setPushCycle(2);
LocalDate now = LocalDate.now();
int monthValue = now.getMonthValue();
int quarter = 0;
if (monthValue >= 1 && monthValue <= 3){
LocalDate lastYear = now.minusYears(1); // 当前年份减1
LocalDate firstDay = LocalDate.of(lastYear.getYear(), 10,1); // 获取前一年最后季度的第一天
LocalDate lastDay = LocalDate.of(lastYear.getYear(), 12,31); // 获取前一年最后季度的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if(monthValue >= 4 && 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 >= 7 && monthValue <= 9){
LocalDate firstDay = LocalDate.of(now.getYear(), 4,1); // 获取前一年最后季度的第一天
LocalDate lastDay = LocalDate.of(now.getYear(), 6,30); // 获取前一年最后季度的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
} else if(monthValue >= 10 && monthValue <= 12){
LocalDate firstDay = LocalDate.of(now.getYear(), 7,1); // 获取前一年最后季度的第一天
LocalDate lastDay = LocalDate.of(now.getYear(), 9,30); // 获取前一年最后季度的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
}
} else if (cycle.equals("year")) {
healthRecordEntity.setPushCycle(3);
LocalDate now = LocalDate.now();
LocalDate lastYear = now.minusYears(1); // 当前年份减1
LocalDate firstDay = lastYear.with(TemporalAdjusters.firstDayOfYear()); // 获取前一年的第一天
LocalDate lastDay = lastYear.with(TemporalAdjusters.lastDayOfYear()); // 获取前一年的最后一天
healthRecordEntity.setRecordTime(firstDay+" - "+lastDay);
}
healthRecordEntity.setHrType(0);
// 生成之后,八点推送
healthRecordEntity.setPushStatus(0);
// 推送时间为生成时间的八点
// hre.setPushTime("");
// 生成
healthRecordDao.save(healthRecordEntity);
}
}
根据参数获取周期
存放到mq中