HouseOrderServiceImpl.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. package com.template.services.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.baomidou.mybatisplus.core.metadata.IPage;
  4. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.template.common.utils.DateUtil;
  7. import com.template.mapper.HouseOrderMapper;
  8. import com.template.model.vo.HouseOrderPageListVo;
  9. import com.template.model.pojo.*;
  10. import com.template.model.result.PageUtils;
  11. import com.template.model.vo.PriceVo;
  12. import com.template.model.vo.QueryExportVo;
  13. import com.template.services.*;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.math.BigDecimal;
  18. import java.time.Duration;
  19. import java.time.LocalDate;
  20. import java.time.LocalDateTime;
  21. import java.time.ZoneId;
  22. import java.time.format.DateTimeFormatter;
  23. import java.time.temporal.ChronoUnit;
  24. import java.util.Date;
  25. import java.util.List;
  26. /**
  27. * <p>
  28. * 服务实现类
  29. * </p>
  30. *
  31. * @author ceshi
  32. * @since 2023-11-21
  33. */
  34. @Service
  35. public class HouseOrderServiceImpl extends ServiceImpl<HouseOrderMapper, HouseOrder> implements HouseOrderService {
  36. @Autowired
  37. HouseService houseService;
  38. @Autowired
  39. HousePriceService housePriceService;
  40. @Autowired
  41. UsersService usersService;
  42. @Autowired
  43. ClassScheduleService classScheduleService;
  44. @Autowired
  45. HouseOrderMapper houseOrderMapper;
  46. @Override
  47. public BigDecimal getHouseOrderPrice(int houseOrderNumber, String userId, String houseId, String liveTime, String leaveTime) {
  48. House house = houseService.getById(houseId);
  49. // 原价
  50. BigDecimal roomPrice = house.getRoomPrice();
  51. DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  52. DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  53. LocalDate state = LocalDateTime.parse(liveTime, dateTimeFormatter1).toLocalDate();
  54. LocalDate end = LocalDateTime.parse(leaveTime, dateTimeFormatter1).toLocalDate();
  55. LocalDateTime stateTime = LocalDateTime.parse(liveTime, dateTimeFormatter1);
  56. LocalDateTime endTime = LocalDateTime.parse(leaveTime, dateTimeFormatter1);
  57. // 获取这段时间内的所有改价记录
  58. List<HousePrice> housePrices = housePriceService.housePrice(stateTime, endTime, houseId);
  59. // 是否是钟点房
  60. Integer roomType = house.getRoomType();
  61. if (roomType == 2) {
  62. // 计算小时差
  63. Duration duration = Duration.between(stateTime, endTime);
  64. // 小时差
  65. long hours = duration.toHours();
  66. if (ObjectUtils.isNotEmpty(housePrices)) {
  67. for (HousePrice housePrice : housePrices) {
  68. String setDate = housePrice.getSetDate();
  69. String[] split = setDate.split(",");
  70. Date startDate = DateUtil.parseDateOnly(split[0]);
  71. Date endDate = DateUtil.parseDateOnly(split[1]);
  72. Date date1 = Date.from(stateTime.atZone(ZoneId.systemDefault()).toInstant());
  73. // 判断当前时间是否在[startTime, endTime]区间
  74. assert startDate != null;
  75. boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
  76. if (effectiveDate) {
  77. BigDecimal price = housePrice.getPrice();
  78. BigDecimal totalPrice = price.multiply(new BigDecimal(houseOrderNumber));
  79. return totalPrice;
  80. }
  81. }
  82. }
  83. BigDecimal totalPrice = roomPrice.multiply(new BigDecimal(houseOrderNumber));
  84. return totalPrice;
  85. }
  86. // 计算2个时间差
  87. long until = state.until(end, ChronoUnit.DAYS);
  88. // 获取这断时间内的上 8 9节课的老师
  89. Users users = usersService.getById(userId);
  90. LambdaQueryWrapper<ClassSchedule> wrapperCS = new LambdaQueryWrapper<>();
  91. wrapperCS.eq(ClassSchedule::getJsgh, users.getCardNumber())
  92. .between(ClassSchedule::getDateTime, state, end);
  93. List<ClassSchedule> scheduleList = classScheduleService.list(wrapperCS);
  94. BigDecimal totalPrice = new BigDecimal(0);
  95. for (int i = 0; i < until; i++) {
  96. LocalDate localDate1 = state.plusDays(i);
  97. if (ObjectUtils.isNotEmpty(scheduleList)) {
  98. for (ClassSchedule classSchedule : scheduleList) {
  99. String dateTime = classSchedule.getDateTime();
  100. LocalDate parse = LocalDate.parse(dateTime, dateTimeFormatter2);
  101. if (!localDate1.equals(parse)) {
  102. if (ObjectUtils.isNotEmpty(housePrices)) {
  103. for (HousePrice housePrice : housePrices) {
  104. String setDate = housePrice.getSetDate();
  105. String[] split = setDate.split(",");
  106. Date startDate = DateUtil.parseDateOnly(split[0]);
  107. Date endDate = DateUtil.parseDateOnly(split[1]);
  108. Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
  109. // 判断当前时间是否在[startTime, endTime]区间
  110. assert startDate != null;
  111. boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
  112. if (effectiveDate) {
  113. BigDecimal price = housePrice.getPrice();
  114. totalPrice = totalPrice.add(price);
  115. } else {
  116. totalPrice = totalPrice.add(roomPrice);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. } else {
  123. if (ObjectUtils.isNotEmpty(housePrices)) {
  124. for (HousePrice housePrice : housePrices) {
  125. String setDate = housePrice.getSetDate();
  126. String[] split = setDate.split(",");
  127. Date startDate = DateUtil.parseDateOnly(split[0]);
  128. Date endDate = DateUtil.parseDateOnly(split[1]);
  129. Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
  130. // 判断当前时间是否在[startTime, endTime]区间
  131. assert startDate != null;
  132. boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
  133. if (effectiveDate) {
  134. BigDecimal price = housePrice.getPrice();
  135. totalPrice = totalPrice.add(price);
  136. } else {
  137. totalPrice = totalPrice.add(roomPrice);
  138. }
  139. }
  140. } else {
  141. totalPrice = totalPrice.add(roomPrice);
  142. }
  143. }
  144. }
  145. return totalPrice.multiply(new BigDecimal(houseOrderNumber));
  146. }
  147. @Override
  148. public PriceVo reservePrice(String houseId, String cardNumber, String liveTime, String leaveTime) {
  149. PriceVo priceVo = new PriceVo();
  150. House house = houseService.getById(houseId);
  151. // 原价
  152. BigDecimal roomPrice = house.getRoomPrice();
  153. DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  154. DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  155. LocalDateTime stateTime = LocalDateTime.parse(liveTime, dateTimeFormatter1);
  156. LocalDateTime endTime = LocalDateTime.parse(leaveTime, dateTimeFormatter1);
  157. // 获取这段时间内的所有改价记录
  158. List<HousePrice> housePrices = housePriceService.housePrice(stateTime, endTime, houseId);
  159. // 是否是钟点房 是的话则不需要判断是否有免费的
  160. Integer roomType = house.getRoomType();
  161. if (roomType == 2) {
  162. // 计算小时差
  163. Duration duration = Duration.between(stateTime, endTime);
  164. // 小时差
  165. long hours = duration.toHours();
  166. if (ObjectUtils.isNotEmpty(housePrices)) {
  167. for (HousePrice housePrice : housePrices) {
  168. String setDate = housePrice.getSetDate();
  169. String[] split = setDate.split(",");
  170. Date startDate = DateUtil.parseDateOnly(split[0]);
  171. Date endDate = DateUtil.parseDateOnly(split[1]);
  172. Date date1 = Date.from(stateTime.atZone(ZoneId.systemDefault()).toInstant());
  173. // 判断当前时间是否在[startTime, endTime]区间
  174. assert startDate != null;
  175. boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
  176. if (effectiveDate) {
  177. BigDecimal price = housePrice.getPrice();
  178. BigDecimal totalPrice = price;
  179. priceVo.setTotalPrice(totalPrice);
  180. priceVo.setPrice(price);
  181. return priceVo;
  182. }
  183. }
  184. }
  185. BigDecimal totalPrice = roomPrice;
  186. priceVo.setTotalPrice(totalPrice);
  187. priceVo.setPrice(roomPrice);
  188. return priceVo;
  189. }
  190. LocalDate state = LocalDateTime.parse(liveTime, dateTimeFormatter1).toLocalDate();
  191. LocalDate end = LocalDateTime.parse(leaveTime, dateTimeFormatter1).toLocalDate();
  192. // 计算2个时间差
  193. long until = state.until(end, ChronoUnit.DAYS);
  194. // 获取这断时间内的上 8 9节课的老师
  195. LambdaQueryWrapper<ClassSchedule> wrapperCS = new LambdaQueryWrapper<>();
  196. wrapperCS.eq(ClassSchedule::getJsgh,cardNumber)
  197. .between(ClassSchedule::getDateTime, state, end);
  198. List<ClassSchedule> scheduleList = classScheduleService.list(wrapperCS);
  199. BigDecimal totalPrice = new BigDecimal(0);
  200. for (int i = 0; i < until; i++) {
  201. LocalDate localDate1 = state.plusDays(i);
  202. if (ObjectUtils.isNotEmpty(scheduleList)) {
  203. for (ClassSchedule classSchedule : scheduleList) {
  204. String dateTime = classSchedule.getDateTime();
  205. LocalDate parse = LocalDate.parse(dateTime, dateTimeFormatter2);
  206. if (!localDate1.equals(parse)) {
  207. if (ObjectUtils.isNotEmpty(housePrices)) {
  208. for (HousePrice housePrice : housePrices) {
  209. String setDate = housePrice.getSetDate();
  210. String[] split = setDate.split(",");
  211. Date startDate = DateUtil.parseDateOnly(split[0]);
  212. Date endDate = DateUtil.parseDateOnly(split[1]);
  213. Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
  214. // 判断当前时间是否在[startTime, endTime]区间
  215. assert startDate != null;
  216. boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
  217. if (effectiveDate) {
  218. BigDecimal price = housePrice.getPrice();
  219. totalPrice = totalPrice.add(price);
  220. } else {
  221. totalPrice = totalPrice.add(roomPrice);
  222. }
  223. }
  224. }
  225. }
  226. }
  227. } else {
  228. if (ObjectUtils.isNotEmpty(housePrices)) {
  229. for (HousePrice housePrice : housePrices) {
  230. String setDate = housePrice.getSetDate();
  231. String[] split = setDate.split(",");
  232. Date startDate = DateUtil.parseDateOnly(split[0]);
  233. Date endDate = DateUtil.parseDateOnly(split[1]);
  234. Date date1 = Date.from(localDate1.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
  235. // 判断当前时间是否在[startTime, endTime]区间
  236. assert startDate != null;
  237. boolean effectiveDate = DateUtil.isEffectiveDate(date1, startDate, endDate);
  238. if (effectiveDate) {
  239. BigDecimal price = housePrice.getPrice();
  240. totalPrice = totalPrice.add(price);
  241. } else {
  242. totalPrice = totalPrice.add(roomPrice);
  243. }
  244. }
  245. } else {
  246. totalPrice = totalPrice.add(roomPrice);
  247. }
  248. }
  249. }
  250. priceVo.setTotalPrice(totalPrice);
  251. BigDecimal price = totalPrice.divide(new BigDecimal(until), 2, BigDecimal.ROUND_HALF_UP);
  252. priceVo.setPrice(price);
  253. return priceVo;
  254. }
  255. @Override
  256. public PageUtils<HouseOrderPageListVo> 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) {
  257. Page<HouseOrderPageListVo> pageVo = new Page<>(page,size);
  258. IPage<HouseOrderPageListVo> result=houseOrderMapper.pageList(pageVo,keyWord,houseType,orderStatus,payPriceStartTime,payPriceEndTime,refundStartTime,refundEndTime,cancelStartTime,cancelEndTime,liveStartTime,liveEndTime,leaveStartTime,leaveEndTime);
  259. return new PageUtils(result);
  260. }
  261. @Override
  262. public List<QueryExportVo> 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) {
  263. return houseOrderMapper.queryExport(keyWord,houseType,orderStatus,payPriceStartTime,payPriceEndTime,refundStartTime,refundEndTime,cancelStartTime,cancelEndTime,liveStartTime,liveEndTime,leaveStartTime,leaveEndTime);
  264. }
  265. }