package com.template.services.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.template.common.utils.DateUtil;
import com.template.mapper.HouseOrderMapper;
import com.template.model.pojo.*;
import com.template.model.result.PageUtils;
import com.template.model.vo.*;
import com.template.services.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
/**
*
* 服务实现类
*
*
* @author ceshi
* @since 2023-11-21
*/
@Service
public class HouseOrderServiceImpl extends ServiceImpl implements HouseOrderService {
@Autowired
HouseService houseService;
@Autowired
HousePriceService housePriceService;
@Autowired
UsersService usersService;
@Autowired
ClassScheduleService classScheduleService;
@Autowired
HouseOrderMapper houseOrderMapper;
@Override
public BigDecimal getHouseOrderPrice(int houseOrderNumber, String userId, String houseId, String liveTime, String leaveTime) {
House house = houseService.getById(houseId);
// 原价
BigDecimal roomPrice = house.getRoomPrice();
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime stateTime = LocalDateTime.parse(liveTime, dateTimeFormatter1);
LocalDateTime endTime = LocalDateTime.parse(leaveTime, dateTimeFormatter1);
LocalDate state = stateTime.toLocalDate();
LocalDate end = endTime.toLocalDate();
// 获取这段时间内的所有改价记录
List housePrices = housePriceService.housePrice(stateTime, endTime, houseId);
// 是否是钟点房
Integer roomType = house.getRoomType();
if (roomType == 2) {
// 计算小时差
Duration duration = Duration.between(stateTime, endTime);
// 小时差
long hours = duration.toHours();
if (ObjectUtils.isNotEmpty(housePrices)) {
for (HousePrice housePrice : housePrices) {
String setDate = housePrice.getSetDate();
String[] split = setDate.split(",");
Date startDate = DateUtil.parseDateOnly(split[0]);
Date endDate = DateUtil.parseDateOnly(split[1]);
Date date1 = Date.from(stateTime.atZone(ZoneId.systemDefault()).toInstant());
// 判断当前时间是否在[startTime, endTime]区间
assert startDate != null;
boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
if (effectiveDate) {
BigDecimal price = housePrice.getPrice();
BigDecimal totalPrice = price.multiply(new BigDecimal(houseOrderNumber));
return totalPrice;
}
}
}
BigDecimal totalPrice = roomPrice.multiply(new BigDecimal(houseOrderNumber));
return totalPrice;
}
// 计算2个时间差
long until = state.until(end, ChronoUnit.DAYS);
// 获取这断时间内的上 8 9节课的老师
Users users = usersService.getById(userId);
LambdaQueryWrapper wrapperCS = new LambdaQueryWrapper<>();
wrapperCS.eq(ClassSchedule::getJsgh, users.getCardNumber())
.between(ClassSchedule::getDateTime, state, end);
List scheduleList = classScheduleService.list(wrapperCS);
BigDecimal totalPrice = new BigDecimal(0);
for (int i = 0; i < until; i++) {
LocalDate localDate1 = state.plusDays(i);
List housePrices1 = housePriceService.getDatePrice(localDate1, houseId);
// LocalDate localDate2 = state.plusDays(i+1);
// List housePrices = housePriceService.housePrice(localDate1, localDate2, houseId);
if (ObjectUtils.isNotEmpty(scheduleList)) {
for (ClassSchedule classSchedule : scheduleList) {
String dateTime = classSchedule.getDateTime();
String[] s = dateTime.split(" ");
dateTime=s[0];
LocalDate parse = LocalDate.parse(dateTime, dateTimeFormatter2);
if (!localDate1.equals(parse)) {
if (ObjectUtils.isNotEmpty(housePrices1)) {
HousePrice housePrice = housePrices1.get(housePrices1.size() - 1);
BigDecimal price = housePrice.getPrice();
totalPrice = totalPrice.add(price);
} else {
totalPrice = totalPrice.add(roomPrice);
}
// for (HousePrice housePrice : housePrices1) {
// String setDate = housePrice.getSetDate();
// String[] split = setDate.split(",");
// Date startDate = DateUtil.parseDateOnly(split[0]);
// Date endDate = DateUtil.parseDateOnly(split[1]);
// Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
// // 判断当前时间是否在[startTime, endTime]区间
// assert startDate != null;
// boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
// if (effectiveDate) {
// BigDecimal price = housePrice.getPrice();
// totalPrice = totalPrice.add(price);
// } else {
// totalPrice = totalPrice.add(roomPrice);
// }
// }
}
}
} else {
if (ObjectUtils.isNotEmpty(housePrices1)) {
if (ObjectUtils.isNotEmpty(housePrices1)) {
HousePrice housePrice = housePrices1.get(housePrices1.size() - 1);
BigDecimal price = housePrice.getPrice();
totalPrice = totalPrice.add(price);
}
// for (HousePrice housePrice : housePrices1) {
// String setDate = housePrice.getSetDate();
// String[] split = setDate.split(",");
// Date startDate = DateUtil.parseDateOnly(split[0]);
// Date endDate = DateUtil.parseDateOnly(split[1]);
// Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
// // 判断当前时间是否在[startTime, endTime]区间
// assert startDate != null;
// boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
// if (effectiveDate) {
// BigDecimal price = housePrice.getPrice();
// totalPrice = totalPrice.add(price);
// } else {
// totalPrice = totalPrice.add(roomPrice);
// }
// }
} else {
totalPrice = totalPrice.add(roomPrice);
}
}
}
return totalPrice.multiply(new BigDecimal(houseOrderNumber));
}
@Override
public PriceVo reservePrice(String houseId, String cardNumber, String liveTime, String leaveTime) {
PriceVo priceVo = new PriceVo();
House house = houseService.getById(houseId);
// 原价
BigDecimal roomPrice = house.getRoomPrice();
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime stateTime = LocalDateTime.parse(liveTime, dateTimeFormatter1);
LocalDateTime endTime = LocalDateTime.parse(leaveTime, dateTimeFormatter1);
// 获取这段时间内的所有改价记录
List housePrices = housePriceService.housePrice(stateTime, endTime, houseId);
// 是否是钟点房 是的话则不需要判断是否有免费的
Integer roomType = house.getRoomType();
if (roomType == 2) {
// 计算小时差
Duration duration = Duration.between(stateTime, endTime);
// 小时差
long hours = duration.toHours();
if (ObjectUtils.isNotEmpty(housePrices)) {
HousePrice housePrice = housePrices.get(housePrices.size() - 1);
// for (HousePrice housePrice : housePrices) {
// String setDate = housePrice.getSetDate();
// String[] split = setDate.split(",");
// Date startDate = DateUtil.parseDateOnly(split[0]);
// Date endDate = DateUtil.parseDateOnly(split[1]);
// Date date1 = Date.from(stateTime.atZone(ZoneId.systemDefault()).toInstant());
// // 判断当前时间是否在[startTime, endTime]区间
// assert startDate != null;
// boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
// if (effectiveDate) {
BigDecimal price = housePrice.getPrice();
BigDecimal totalPrice = price;
priceVo.setTotalPrice(totalPrice);
priceVo.setPrice(price);
return priceVo;
// }
// }
}
BigDecimal totalPrice = roomPrice;
priceVo.setTotalPrice(totalPrice);
priceVo.setPrice(roomPrice);
return priceVo;
}
LocalDate state = LocalDateTime.parse(liveTime, dateTimeFormatter1).toLocalDate();
LocalDate end = LocalDateTime.parse(leaveTime, dateTimeFormatter1).toLocalDate();
// 计算2个时间差
long until = state.until(end, ChronoUnit.DAYS);
// 获取这断时间内的上 8 9节课的老师
LambdaQueryWrapper wrapperCS = new LambdaQueryWrapper<>();
wrapperCS.eq(ClassSchedule::getJsgh, cardNumber)
.between(ClassSchedule::getDateTime, state, end);
List scheduleList = classScheduleService.list(wrapperCS);
BigDecimal totalPrice = new BigDecimal(0);
for (int i = 0; i < until; i++) {
LocalDate localDate1 = state.plusDays(i);
List housePrices1 = housePriceService.getDatePrice(localDate1, houseId);
if (ObjectUtils.isNotEmpty(scheduleList)) {
for (ClassSchedule classSchedule : scheduleList) {
String dateTime = classSchedule.getDateTime();
String[] s = dateTime.split(" ");
dateTime=s[0];
LocalDate parse = LocalDate.parse(dateTime, dateTimeFormatter2);
if (!localDate1.equals(parse)) {
if (ObjectUtils.isNotEmpty(housePrices1)) {
HousePrice housePrice = housePrices1.get(housePrices1.size() - 1);
BigDecimal price = housePrice.getPrice();
totalPrice = totalPrice.add(price);
} else {
totalPrice = totalPrice.add(roomPrice);
}
// for (HousePrice housePrice : housePrices) {
// String setDate = housePrice.getSetDate();
// String[] split = setDate.split(",");
// Date startDate = DateUtil.parseDateOnly(split[0]);
// Date endDate = DateUtil.parseDateOnly(split[1]);
// Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
// // 判断当前时间是否在[startTime, endTime]区间
// assert startDate != null;
// boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
// if (effectiveDate) {
// BigDecimal price = housePrice.getPrice();
// totalPrice = totalPrice.add(price);
// } else {
// totalPrice = totalPrice.add(roomPrice);
// }
// }
}
}
} else {
if (ObjectUtils.isNotEmpty(housePrices1)) {
HousePrice housePrice = housePrices1.get(housePrices1.size() - 1);
BigDecimal price = housePrice.getPrice();
totalPrice = totalPrice.add(price);
// for (HousePrice housePrice : housePrices) {
// String setDate = housePrice.getSetDate();
// String[] split = setDate.split(",");
// Date startDate = DateUtil.parseDateOnly(split[0]);
// Date endDate = DateUtil.parseDateOnly(split[1]);
// Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
// // 判断当前时间是否在[startTime, endTime]区间
// assert startDate != null;
// boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
// if (effectiveDate) {
// BigDecimal price = housePrice.getPrice();
// totalPrice = totalPrice.add(price);
// } else {
// totalPrice = totalPrice.add(roomPrice);
// }
// }
} else {
totalPrice = totalPrice.add(roomPrice);
}
}
}
priceVo.setTotalPrice(totalPrice);
BigDecimal price = new BigDecimal(0);
if (totalPrice.doubleValue() > 0) {
price = totalPrice.divide(new BigDecimal(until), 2, BigDecimal.ROUND_HALF_UP);
}
priceVo.setPrice(price);
return priceVo;
}
@Override
public PageUtils pageList(int page, int size, String keyWord, String houseType, String orderStatus, String payPriceStartTime, String payPriceEndTime, String refundStartTime, String refundEndTime, String cancelStartTime, String cancelEndTime, String liveStartTime, String liveEndTime, String leaveStartTime, String leaveEndTime) {
Page pageVo = new Page<>(page, size);
IPage result = houseOrderMapper.pageList(pageVo, keyWord, houseType, orderStatus, payPriceStartTime, payPriceEndTime, refundStartTime, refundEndTime, cancelStartTime, cancelEndTime, liveStartTime, liveEndTime, leaveStartTime, leaveEndTime);
return new PageUtils(result);
}
@Override
public List queryExport(String keyWord, String houseType, String orderStatus, String payPriceStartTime, String payPriceEndTime, String refundStartTime, String refundEndTime, String cancelStartTime, String cancelEndTime, String liveStartTime, String liveEndTime, String leaveStartTime, String leaveEndTime) {
return houseOrderMapper.queryExport(keyWord, houseType, orderStatus, payPriceStartTime, payPriceEndTime, refundStartTime, refundEndTime, cancelStartTime, cancelEndTime, liveStartTime, liveEndTime, leaveStartTime, leaveEndTime);
}
@Override
public HouseOrderCheckInShowVo getOrderNumbre(String orderNumber) {
return houseOrderMapper.getOrderNumber(orderNumber);
}
@Override
public PageUtils reportStatisticsPage(String s, String keyWord, String startTime, String endTime, int page, int size) {
Page pageVo = new Page<>(page, size);
IPage result = houseOrderMapper.reportStatisticsPage(pageVo, s, keyWord, startTime, endTime);
return new PageUtils(result);
}
@Override
public List reportStatisticsExport(String s, String keyWord, String startTime, String endTime) {
return houseOrderMapper.reportStatisticsExport(s, keyWord, startTime, endTime);
}
@Override
public List payHouseOrderUser(LocalDateTime start, LocalDateTime end) {
return houseOrderMapper.payHouseOrderUser(start, end);
}
}