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.format.TextStyle; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.List; import java.util.Locale; /** *

* 服务实现类 *

* * @author ceshi * @since 2023-11-21 */ @Service public class HouseOrderServziceImpl extends ServiceImpl implements HouseOrderService { @Autowired HouseService houseService; @Autowired HousePriceService housePriceService; @Autowired UsersService usersService; @Autowired ClassScheduleService classScheduleService; @Autowired HouseOrderMapper houseOrderMapper; @Autowired ApplicationProcedureTemporaryService apts; @Autowired ApplicationProcedureTemporaryService applicationProcedureTemporaryService; @Override public BigDecimal getHouseOrderPrice(int houseOrderNumber, String userId, String houseId, String liveTime, String leaveTime,Integer source) { 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); // 判断你是老师还是临时人员 String department = users.getDepartment(); // 先判断免费来源是否是对的 BigDecimal totalPrice = new BigDecimal(0); if (ObjectUtils.isNotEmpty(source)) { if (source==1) { // 获取预定时间的开始星期和结束星期 String chineseDayOfWeek = state.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA); Integer week = 0; if ("星期一".equals(chineseDayOfWeek)) { week = 1; } else if ("星期二".equals(chineseDayOfWeek)) { week = 2; } else if ("星期三".equals(chineseDayOfWeek)) { week = 3; } else if ("星期四".equals(chineseDayOfWeek)) { week = 4; } else if ("星期五".equals(chineseDayOfWeek)) { week = 5; } else if ("星期六".equals(chineseDayOfWeek)) { week = 6; } else { week = 7; } LocalDate localDate = state.minusDays(week - 1); String stateTimeXq = localDate.format(dateTimeFormatter2); LocalDate localEnd = localDate.plusDays(6); String endTimeXq = localEnd.format(dateTimeFormatter2); // 查询该星期是否满足有2天的订单 LambdaQueryWrapper wrapperHO = new LambdaQueryWrapper<>(); wrapperHO.between(HouseOrder::getReserveLiveTime, stateTimeXq + " 00:00:00", endTimeXq + " 23:59:59") .eq(HouseOrder::getReserveUserId, userId) .eq(HouseOrder::getSource, 1) .eq(HouseOrder::getPayPrice, 0) .in(HouseOrder::getOrderStatus, 1, 2, 3, 4, 8); List orderList = this.list(wrapperHO); long dayDiff = 0; for (HouseOrder houseOrder : orderList) { String reserveLiveTime = houseOrder.getReserveLiveTime(); String reserveLeaveTime = houseOrder.getReserveLeaveTime(); LocalDateTime stateTime2 = LocalDateTime.parse(reserveLiveTime, dateTimeFormatter1); LocalDateTime endTime2 = LocalDateTime.parse(reserveLeaveTime, dateTimeFormatter1); LocalDate state1 = stateTime2.toLocalDate(); LocalDate end2 = endTime2.toLocalDate(); // 计算2个时间差 long until2 = state1.until(end2, ChronoUnit.DAYS); dayDiff = dayDiff + until2; } // 计算2个时间差 long until2 = state.until(end, ChronoUnit.DAYS); Integer count = 1; if (dayDiff + until2 > 2) { count = 0; } else { for (int i = 0; i < until; i++) { LocalDateTime dateTime1 = stateTime.plusDays(i); LocalDateTime startS = dateTime1.withHour(0).withMinute(0).withSecond(0); LocalDateTime endS = dateTime1.withHour(23).withMinute(59).withSecond(59); LambdaQueryWrapper wrapperCS = new LambdaQueryWrapper<>(); wrapperCS.eq(ClassSchedule::getJsgh, users.getCardNumber()) .between(ClassSchedule::getDateTime, startS, endS); List scheduleList = classScheduleService.list(wrapperCS); // 只要有一天是空的就不是免费 if (ObjectUtils.isEmpty(scheduleList)) { count = 0; break; } else { // 有课表就查询是否已经有该订单了 List list = this.getDate(dateTime1, userId, 1); if (ObjectUtils.isNotEmpty(list)) { count = 0; break; } } } } if (count > 0) { return totalPrice; } }else if (source==2){ // 获取流程 List aptList = applicationProcedureTemporaryService.getApt(state, end, userId); Integer count = 1; if (ObjectUtils.isEmpty(aptList)) { count=1; } else { ApplicationProcedureTemporary applicationProcedureTemporary = aptList.get(0); String houseCount = applicationProcedureTemporary.getHouseCount(); String startTime = applicationProcedureTemporary.getStartTime(); String endTime1 = applicationProcedureTemporary.getEndTime(); // 计算开始到结束时间有多少订单 LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); wrapper.eq(HouseOrder::getReserveUserId, userId) .eq(HouseOrder::getSource, 2) .between(HouseOrder::getReserveLiveTime, startTime, endTime1) .eq(HouseOrder::getPayPrice, 0) .in(HouseOrder::getOrderStatus, 1, 2, 3, 4, 8); List list = this.list(wrapper); Integer count2 = Integer.valueOf(houseCount); count = count2 - list.size(); } if (count > 0) { return totalPrice; } } } for (int i = 0; i < until; i++) { LocalDate localDate1 = state.plusDays(i); LocalDateTime dateTime1 = stateTime.plusDays(i); List housePrices1 = housePriceService.getDatePrice(localDate1, houseId); // if ("4".equals(department)) { // // LocalDateTime startS = dateTime1.withHour(0).withMinute(0).withSecond(0); // LocalDateTime endS = dateTime1.withHour(23).withMinute(59).withSecond(59); // LambdaQueryWrapper wrapperCS = new LambdaQueryWrapper<>(); // wrapperCS.eq(ClassSchedule::getJsgh, users.getCardNumber()) // .between(ClassSchedule::getDateTime, startS, endS); // List scheduleList = classScheduleService.list(wrapperCS); // // if (ObjectUtils.isEmpty(scheduleList)) { // if (ObjectUtils.isNotEmpty(housePrices1)) { // HousePrice housePrice = housePrices1.get(housePrices1.size() - 1); // BigDecimal price = housePrice.getPrice(); // totalPrice = totalPrice.add(price); // } else { // totalPrice = totalPrice.add(roomPrice); // } // }else { //// 需要先判断当前时间是否已经有过订单,有的话则不免费 //// dateTime1 // List list = houseOrderMapper.getDate(dateTime1, userId,1); // if (list.size() > 0) { // if (ObjectUtils.isNotEmpty(housePrices1)) { // HousePrice housePrice = housePrices1.get(housePrices1.size() - 1); // BigDecimal price = housePrice.getPrice(); // totalPrice = totalPrice.add(price); // } else { // totalPrice = totalPrice.add(roomPrice); // } // } // } // // // // }else if ("7".equals(department)) { // List list = apts.getDate(dateTime1, userId); // if (ObjectUtils.isNotEmpty(list)) { //// 当有临时表时需要判断是否已经有订单,有的话则原价 // List list2 = houseOrderMapper.getDate(dateTime1, userId,2); // if (ObjectUtils.isNotEmpty(list2)) { // if (ObjectUtils.isNotEmpty(housePrices1)) { // HousePrice housePrice = housePrices1.get(housePrices1.size() - 1); // 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); // } 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); } } 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, String houseId, Integer keyType, Integer giveBack, Integer buildingId,Integer orgId) { Page pageVo = new Page<>(page, size); IPage result = houseOrderMapper.pageList(pageVo, keyWord, houseType, orderStatus, payPriceStartTime, payPriceEndTime, refundStartTime, refundEndTime, cancelStartTime, cancelEndTime, liveStartTime, liveEndTime, leaveStartTime, leaveEndTime, houseId, keyType, giveBack, buildingId,orgId); 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, String houseId, Integer keyType, Integer giveBack, Integer buildingId,Integer orgId) { return houseOrderMapper.queryExport(keyWord, houseType, orderStatus, payPriceStartTime, payPriceEndTime, refundStartTime, refundEndTime, cancelStartTime, cancelEndTime, liveStartTime, liveEndTime, leaveStartTime, leaveEndTime, houseId, keyType, giveBack, buildingId,orgId); } @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); } @Override public List getDate(LocalDateTime dateTime1, String userId,Integer source) { return houseOrderMapper.getDate(dateTime1, userId,source); } @Override public List getWater(String houseOrderSource) { return houseOrderMapper.getWater(houseOrderSource); } @Override public List getElectric(String houseOrderSource) { return houseOrderMapper.getElectric(houseOrderSource); } @Override public List getWaterAndElectric(String houseOrderSource) { return houseOrderMapper.getWaterAndElectric(houseOrderSource); } }