AppHomePageImplService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package com.happy.service.impl;
  2. import com.happy.Model.Booking;
  3. import com.happy.Model.FileInfo;
  4. import com.happy.Model.Hotel;
  5. import com.happy.Model.House;
  6. import com.happy.Model.weixin.Users;
  7. import com.happy.Until.DateUtil;
  8. import com.happy.Until.Enum.OrderEnum;
  9. import com.happy.Until.Enum.PayEnum;
  10. import com.happy.Until.Func;
  11. import com.happy.dao.BookDao;
  12. import com.happy.dao.HouseDao;
  13. import com.happy.dto.IPage;
  14. import com.happy.service.*;
  15. import org.springframework.stereotype.Service;
  16. import javax.annotation.Resource;
  17. import java.util.*;
  18. @Service("AppHomePageService")
  19. public class AppHomePageImplService implements AppHomePageService {
  20. @Resource
  21. public BookDao bookDao;
  22. @Resource
  23. public HouseDao houseDao;
  24. @Resource
  25. public HouseService houseService;
  26. @Resource
  27. public HotelService hotelService;
  28. @Resource
  29. public BookService bookService;
  30. @Resource
  31. public AdminManagerService adminManagerService;
  32. @Resource
  33. public FileService fileService;
  34. @Resource
  35. public UserService userService;
  36. @Override
  37. public IPage<Hotel> getHotelList(String queryValue,String hotel_township, int page, int rows) {
  38. StringBuilder strSql = new StringBuilder(" and hstatus=1 "); // 酒店正在营业中才显示
  39. if (!Func.checkNull(queryValue)){
  40. strSql.append(" and hname like '%").append(queryValue).append("%' ");
  41. }
  42. if (!Func.checkNull(hotel_township)){
  43. strSql.append(" and c.hotel_township = '").append(hotel_township).append("' ");
  44. }
  45. return hotelService.queryPagePrice(strSql.toString(),page,rows);
  46. }
  47. @Override
  48. public IPage<Hotel> getHotelList(String queryValue,String hotel_township,int type, int page, int rows) {
  49. StringBuilder strSql = new StringBuilder(" and hstatus=1 "); // 酒店正在营业中才显示
  50. if (!Func.checkNull(queryValue)){
  51. strSql.append(" and hname like '%").append(queryValue).append("%' ");
  52. }
  53. if (!Func.checkNull(hotel_township)){
  54. strSql.append(" and c.hotel_township = '").append(hotel_township).append("' ");
  55. }
  56. if (!Func.checkNull(String.valueOf(type))){
  57. strSql.append(" and c.type = '").append(type).append("' ");
  58. }
  59. return hotelService.queryPagePrice(strSql.toString(),page,rows);
  60. }
  61. /**
  62. * 优选
  63. * @param queryValue
  64. * @param hotel_township
  65. * @param type
  66. * @param page
  67. * @param rows
  68. * @return
  69. */
  70. @Override
  71. public IPage<Hotel> getPreferredHotelList(String queryValue, String hotel_township, int type, int page, int rows) {
  72. StringBuilder strSql = new StringBuilder(" and hstatus=1 "); // 酒店正在营业中才显示
  73. if (!Func.checkNull(queryValue)){
  74. strSql.append(" and hname like '%").append(queryValue).append("%' ");
  75. }
  76. if (!Func.checkNull(hotel_township)){
  77. strSql.append(" and c.hotel_township = '").append(hotel_township).append("' ");
  78. }
  79. if (!Func.checkNull(String.valueOf(type))){
  80. strSql.append(" and c.type = '").append(type).append("' ");
  81. }
  82. return hotelService.queryPreferredPagePrice(strSql.toString(),page,rows);
  83. }
  84. /**
  85. * 精选
  86. * @param queryValue
  87. * @param hotel_township
  88. * @param type
  89. * @param page
  90. * @param rows
  91. * @return
  92. */
  93. @Override
  94. public IPage<Hotel> getSelectedHotelList(String queryValue, String hotel_township, int type, int page, int rows) {
  95. StringBuilder strSql = new StringBuilder(" and hstatus=1 "); // 酒店正在营业中才显示
  96. if (!Func.checkNull(queryValue)){
  97. strSql.append(" and hname like '%").append(queryValue).append("%' ");
  98. }
  99. if (!Func.checkNull(hotel_township)){
  100. strSql.append(" and c.hotel_township = '").append(hotel_township).append("' ");
  101. }
  102. if (!Func.checkNull(String.valueOf(type))){
  103. strSql.append(" and c.type = '").append(type).append("' ");
  104. }
  105. return hotelService.querySelectedPagePrice(strSql.toString(),page,rows);
  106. }
  107. @Override
  108. public IPage<Hotel> getHotelList(List<String> ids, int page, int rows) {
  109. if (ids == null || ids.size() <= 0 )
  110. return null;
  111. StringBuilder strSql = new StringBuilder(" and hstatus=1 "); // 酒店正在营业中才显示" and hstatus=1 "
  112. strSql.append(" and a.id in( ");
  113. for (String id: ids) {
  114. if (Func.checkNull(id)) {
  115. continue;
  116. }
  117. // 去掉前面的null
  118. id=id.replaceAll("null","");
  119. strSql.append(" "+ id + ",");
  120. }
  121. strSql.delete(strSql.length() - 1, strSql.length());
  122. strSql.append(" )");
  123. return hotelService.queryPagePrice(strSql.toString(),page,rows);
  124. }
  125. @Override
  126. public int getHotelTotal(String queryValue) {
  127. return 0;
  128. }
  129. @Override
  130. public Hotel assignCollect(String userId, Hotel hotel)
  131. {
  132. Users users = userService.queryByUserId(userId);
  133. if (users == null || hotel == null)
  134. throw new RuntimeException("无法查询到当前用户");
  135. hotel.setCollect_hotel(users.getCollect_hotel());
  136. boolean flag = false; // 标识是否收获过当前酒店,Collect_hotel为空时未收藏, Collect_hotel未包含hotelid未收藏
  137. if (!Func.checkNull(users.getCollect_hotel()))
  138. {
  139. String collect_hotel = users.getCollect_hotel();
  140. flag = collect_hotel.contains(hotel.getId()+"");
  141. }
  142. hotel.setIs_collect_hotel(flag);
  143. return hotel;
  144. }
  145. @Override
  146. public Hotel getHotelAndHouseByHotelId(String hotelId, String queryStartTime, String queryEndTime) {
  147. if (Func.checkNull(hotelId))
  148. return new Hotel();
  149. Hotel hotel = hotelService.getById(Func.parseInt(hotelId));
  150. if (hotel == null || Func.checkNull(String.valueOf(hotel.getManagerId())))
  151. return hotel;
  152. // 数据字典写入hotel
  153. Map dicMap = hotelService.queryHotelDicMap();
  154. if (!Func.checkNull(hotel.getHconfig()))
  155. {
  156. List<String> list = Arrays.asList(hotel.getHconfig().split(","));
  157. List hotelDicList = new ArrayList();
  158. for (String hotelDicId:list) {
  159. if (!dicMap.containsKey(Func.parseInt(hotelDicId)))
  160. continue;
  161. hotelDicList.add(dicMap.get(Func.parseInt(hotelDicId)));
  162. }
  163. hotel.setHconfigList(hotelDicList);
  164. }
  165. System.out.println(hotel.getManagerId());// 需求确定使用商户id关联房型
  166. StringBuffer strSql = new StringBuffer();
  167. strSql.append(" and manager_id = '").append(hotel.getManagerId()).append("' ");
  168. List<House> houseList = houseService.queryList(strSql.toString());
  169. if (houseList == null )
  170. return hotel;
  171. // 数据字典写入hotel
  172. List<String> hlist = new ArrayList<>();
  173. List houseDicList = new ArrayList();
  174. Map<String,House> houseMap = new HashMap<>();
  175. for (House house:houseList) {
  176. if (house == null || Func.checkNull(Func.parseStr(house.getNumber())))
  177. continue;
  178. houseMap.put(Func.parseStr(house.getId()),house);
  179. // “,”号分逗开来
  180. hlist = Arrays.asList(house.gethConfig().split(","));
  181. houseDicList = new ArrayList();
  182. for (String houseDicId:hlist) {
  183. if (!dicMap.containsKey(Func.parseInt(houseDicId)))
  184. continue;
  185. houseDicList.add(dicMap.get(Func.parseInt(houseDicId)));
  186. }
  187. house.sethConfigList(houseDicList);
  188. }
  189. // 判断是否有房间,对每个房型进行标识
  190. StringBuffer strSqlBook = new StringBuffer();
  191. strSqlBook.append(" and hotel_manager_id = '").append(hotel.getManagerId()).append(" ' ");
  192. //1待支付,2已支付,3待入住,4已入住,5已消费,6支付超时,7已取消,8已退单,9已退款
  193. strSqlBook.append(" and ( order_status = '").append(PayEnum.待支付.getNum()).append("' ");
  194. strSqlBook.append(" or order_status = '").append(PayEnum.已支付.getNum()).append("' ");
  195. strSqlBook.append(" or order_status = '").append(PayEnum.待入住.getNum()).append("' ");
  196. strSqlBook.append(" or order_status = '").append(PayEnum.已入住.getNum()).append(" ' ) ");
  197. List<Booking> bookList = bookService.queryList(strSqlBook.toString());
  198. Map<String,Integer> houseBookingMap = assignHouseRemainRooms(bookList,queryStartTime,queryEndTime,houseMap); // 每个房间留存房间数量Map<房间id,数量>
  199. for (House house:houseList) {
  200. if (house == null)
  201. continue;
  202. // 将房间的图片,写入房型对象中
  203. StringBuilder fileSqlx = new StringBuilder("");
  204. fileSqlx.append(" and link_id = '").append(house.getId()).append("'");
  205. List<FileInfo> fileInfoList = fileService.queryList(fileSqlx.toString());
  206. house.setFileInfoList(fileInfoList);
  207. // 进行累加数据,计算房型还剩余多少房间
  208. if (!houseBookingMap.containsKey(Func.parseStr(house.getId()))) {
  209. house.setRemainRooms(Func.parseStr(house.getNumber()));
  210. continue;
  211. }
  212. int i = Func.parseInt(house.getNumber()) - Func.parseInt(houseBookingMap.get(Func.parseStr(house.getId())));
  213. house.setRemainRooms(Func.parseStr(i));
  214. };
  215. hotel.setHouseList(houseList);
  216. return hotel;
  217. }
  218. /**
  219. * 在时间区间下进行验证每个房间的剩余数据,取最小数量作为留存房间的数量
  220. * @param bookList 所有的订单列表
  221. * @param startDate 查询开始时间
  222. * @param endDate 查询结束时间
  223. * @param houseMap Map<房间id, House>
  224. * @return Map<房间id,数量>
  225. */
  226. public Map assignHouseRemainRooms(List<Booking> bookList, String startDate, String endDate ,Map<String, House> houseMap) {
  227. if (bookList == null || Func.checkNull(startDate) || Func.checkNull(endDate) || houseMap == null)
  228. return new HashMap();
  229. // 将时间统一转化为 yyyy-MM-dd
  230. String startTime = DateUtil.parseDateToStr((Func.parseDate(startDate)), DateUtil.Time_Formatter_Day);
  231. String endTime = DateUtil.parseDateToStr((Func.parseDate(endDate)), DateUtil.Time_Formatter_Day);
  232. Map<String, List<Booking>> bookMap = new HashMap<>(); // map<houseId, liset<Booking>>
  233. List<Booking> bList = new ArrayList<>();
  234. for (Booking b :bookList) {
  235. if (Func.checkNull(b.getHouseId()))
  236. continue;
  237. if (bookMap.containsKey(b.getHouseId()))
  238. {
  239. bookMap.get(b.getHouseId()).add(b);
  240. continue;
  241. }
  242. bList = new ArrayList<>();
  243. bList.add(b);
  244. bookMap.put(b.getHouseId(),bList);
  245. }
  246. Set<String> dayQuerySet = new HashSet<>();
  247. // 遍历查询的每一天当做一个列表数据
  248. int dayQuerys = DateUtil.getDayDiff(startTime, endTime);
  249. for (int i = 0; i < dayQuerys; i++) {
  250. String time = DateUtil.parseDateToStr(DateUtil.addDate(DateUtil.parseDateOnly(startTime), Calendar.DATE, i), DateUtil.Time_Formatter_Day);
  251. dayQuerySet.add(time);
  252. }
  253. Map<String, Integer> houseBookingMap = new HashMap<>();
  254. List<Booking> bookHouseList = new ArrayList<>(); // 相同房型的订单汇总
  255. Map<String, Integer> bookingTimeMap = new HashMap(); // map<时间,天数>进行累加 订单中的房间数量
  256. String houseId = null; // 房间id
  257. String timeTemp = null; // 日期 例2023-08-23
  258. for (Map.Entry<String, List<Booking>> entry : bookMap.entrySet())
  259. {
  260. houseId = entry.getKey();
  261. bookHouseList = entry.getValue(); // 相同房型的订单汇总
  262. bookingTimeMap = new HashMap(); // map<时间,天数>进行累加 单个房型的订单中房间的数量
  263. for (Booking booking : bookHouseList) {
  264. if (Func.checkNull(booking.getHouseId()))
  265. continue;
  266. int day = DateUtil.getDayDiff(booking.getOrderStartTime(), booking.getOrderEndTime());
  267. for (int i = 0; i < day; i++) {
  268. timeTemp = DateUtil.parseDateToStr(DateUtil.addDate(DateUtil.parseDateOnly(booking.getOrderStartTime()), Calendar.DATE, i), DateUtil.Time_Formatter_Day);
  269. if (!dayQuerySet.contains(timeTemp)) // 不在查询范围内,不作记录
  270. continue;
  271. if (!bookingTimeMap.containsKey(timeTemp)) {
  272. bookingTimeMap.put(timeTemp, booking.getHouseOrderNumber());
  273. continue;
  274. }
  275. bookingTimeMap.put(timeTemp, booking.getHouseOrderNumber() + Func.parseInt(bookingTimeMap.get(timeTemp)));
  276. }
  277. houseBookingMap.put(houseId,getMaxNum(bookingTimeMap));
  278. }
  279. }
  280. return houseBookingMap;
  281. }
  282. /**
  283. * 一个房间的所有日期当中,取单日最大值房间数量,当做留存数
  284. * @param bookingTimeMap
  285. * @return
  286. */
  287. public Integer getMaxNum(Map<String, Integer> bookingTimeMap)
  288. {
  289. if (bookingTimeMap == null || bookingTimeMap.isEmpty())
  290. return 0;
  291. List<Integer> numbersTemp = new ArrayList<Integer>(); // 临时数值
  292. for (Map.Entry<String,Integer> entry : bookingTimeMap.entrySet())
  293. {
  294. numbersTemp.add(entry.getValue());
  295. }
  296. return numbersTemp.stream ().reduce (Integer::max).get ();// 得到最大值
  297. }
  298. }