AppHomePageAction.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. package com.happy.action;
  2. import com.baidubce.model.User;
  3. import com.happy.Model.Hotel;
  4. import com.happy.Model.House;
  5. import com.happy.Model.weixin.Users;
  6. import com.happy.Until.*;
  7. import com.happy.Until.Enum.B;
  8. import com.happy.Until.Enum.TempEnum;
  9. import com.happy.constant.ResultStatusCode;
  10. import com.happy.dao.UserVisitsDao;
  11. import com.happy.dto.IPage;
  12. import com.happy.dto.townshipCountDTO;
  13. import com.happy.service.*;
  14. import com.happy.vo.*;
  15. import com.opensymphony.xwork2.ActionSupport;
  16. import net.sf.json.JSONObject;
  17. import org.apache.commons.lang.StringUtils;
  18. import org.apache.struts2.ServletActionContext;
  19. import org.apache.struts2.interceptor.ServletRequestAware;
  20. import javax.annotation.Resource;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23. import java.math.BigDecimal;
  24. import java.text.DecimalFormat;
  25. import java.text.ParseException;
  26. import java.util.ArrayList;
  27. import java.util.Comparator;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import java.util.stream.Collectors;
  31. /**
  32. * 首页进去Action请求交互
  33. */
  34. public class AppHomePageAction extends ActionSupport implements ServletRequestAware {
  35. private HttpServletRequest request;
  36. public HttpServletResponse response;
  37. @Resource
  38. public UserVisitsDao userVisitsDao;
  39. @Resource
  40. public UserService userService;
  41. @Resource
  42. public AppHomePageService appHomePageService;
  43. @Resource
  44. public HotelService hotelService;
  45. @Resource
  46. public HouseService houseService;
  47. @Resource
  48. public FileService fileService;
  49. @Resource
  50. public AdminManagerService adminManagerService;
  51. @Resource(name = "housePriceService")
  52. private HousePriceService housePriceService;
  53. public int page; // 当前页
  54. public int rows;// 每页显示的行数rows
  55. public Integer id = new Integer(0);
  56. public String status;
  57. public String hotelId; //选择的当前酒店的id
  58. public String queryStartTime; // 查询开始时间
  59. public String queryEndTime; // 查询结束时间
  60. public String userCode; // 第一次进来获取用户的code
  61. public String queryValue; // 首页查寻搜索关键词
  62. private String hotel_township; // 所属乡镇
  63. public String userId; // 用户id
  64. public int type;
  65. public HttpServletRequest getRequest() {
  66. return request;
  67. }
  68. public void setRequest(HttpServletRequest request) {
  69. this.request = request;
  70. }
  71. public void setServletRequest(HttpServletRequest request) {
  72. this.request = request;
  73. }
  74. public HttpServletResponse getResponse() {
  75. return response;
  76. }
  77. public void setResponse(HttpServletResponse response) {
  78. this.response = response;
  79. }
  80. public int getPage() {
  81. return page;
  82. }
  83. public void setPage(int page) {
  84. this.page = page;
  85. }
  86. public int getRows() {
  87. return rows;
  88. }
  89. public void setRows(int rows) {
  90. this.rows = rows;
  91. }
  92. public Integer getId() {
  93. return id;
  94. }
  95. public void setId(Integer id) {
  96. this.id = id;
  97. }
  98. public String getStatus() {
  99. return status;
  100. }
  101. public void setStatus(String status) {
  102. this.status = status;
  103. }
  104. /**
  105. * 进入首页,展示民宿数据列表
  106. * queryValue 查询字段
  107. * hotel_townshipId 所属乡镇
  108. *
  109. * @return
  110. */
  111. public String homePage() {
  112. // 获取民宿列表
  113. IPage<Hotel> iPage = appHomePageService.getHotelList(queryValue, hotel_township, type, page, rows);
  114. List<Integer> managerIds = iPage.getPageList().stream().map(Hotel::getManagerId).collect(Collectors.toList());
  115. //2023-09-22 A-jax 获取最低价
  116. if(managerIds.size() > 0){
  117. List<HotelPriceDataVo> prices = housePriceService.queryHotelPriceDatas(StringUtils.join(managerIds, ","), TimeExchange.getDateStr());
  118. List<HotelPriceOneDataVo> oneDatas = new ArrayList<>();
  119. //获取当天的
  120. for (HotelPriceDataVo hp : prices) {
  121. HotelPriceOneDataVo oneData = new HotelPriceOneDataVo();
  122. oneData.setSetDate(TimeExchange.getDateStr());
  123. oneData.setCreateDate(hp.getCreateDate());
  124. oneData.setPrice(hp.getPrice());
  125. oneData.setManagerId(hp.getManagerId());
  126. oneDatas.add(oneData);
  127. }
  128. /**
  129. * 根据房型ID处理重复的数据
  130. */
  131. List<HotelPriceOneDataVo> newOneDatas = new ArrayList<>();
  132. if(oneDatas.size() > 0){
  133. for (Integer managerId : managerIds) {
  134. Optional<HotelPriceOneDataVo> one = oneDatas.stream().filter(e -> e.getManagerId().equals(managerId)).sorted(Comparator.comparing(HotelPriceOneDataVo::getCreateDate, Comparator.reverseOrder())).findFirst();
  135. if (one != null && one.isPresent()) {
  136. HotelPriceOneDataVo oneData = new HotelPriceOneDataVo();
  137. oneData.setSetDate(one.get().getSetDate());
  138. oneData.setCreateDate(one.get().getCreateDate());
  139. oneData.setPrice(one.get().getPrice());
  140. oneData.setManagerId(one.get().getManagerId());
  141. newOneDatas.add(oneData);
  142. }
  143. }
  144. }
  145. /**
  146. * 获取商家集合中最低房型价格
  147. */
  148. List<PriceHotelDataVo> mins = houseService.gethotelMinPrice(StringUtils.join(managerIds, ","));
  149. DecimalFormat decimalFormat = new DecimalFormat("#####.##");
  150. for (Hotel hotel: iPage.getPageList()) {
  151. Optional<HotelPriceOneDataVo> one = newOneDatas.stream().filter(e -> e.getManagerId().equals(hotel.getManagerId())).findFirst();
  152. if(one != null && one.isPresent()){
  153. hotel.setMin_price(decimalFormat.format(one.get().getPrice()));
  154. }else{
  155. Optional<PriceHotelDataVo> min = mins.stream().filter(e -> e.getManagerId().equals(hotel.getManagerId())).findFirst();
  156. if(min != null && min.isPresent()){
  157. hotel.setMin_price(decimalFormat.format(min.get().getPrice()));
  158. }
  159. }
  160. }
  161. }
  162. // 用户访问量数据
  163. userVisitsDao.add(userId);
  164. ResponseUtil.writeJsonIPage(ServletActionContext.getResponse(), iPage);
  165. return null;
  166. }
  167. /**
  168. * 用户点击收藏民宿
  169. * userId
  170. * hotelId
  171. *
  172. * @return
  173. */
  174. public String collectHotel() {
  175. if (Func.checkNull(userId) || Func.checkNull(hotelId))
  176. return null;
  177. Users users = userService.addhotelAndUsers(hotelId, userId, TempEnum.收藏);
  178. JSONObject jsonObject = new JSONObject();
  179. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  180. jsonObject.put(B.data, users);
  181. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  182. return null;
  183. }
  184. /**
  185. * 用户点击取消 收藏民宿
  186. * userId 用户id
  187. * hotelId 酒店id
  188. *
  189. * @return
  190. */
  191. public String delCollectHotel() {
  192. if (Func.checkNull(userId) || Func.checkNull(hotelId))
  193. return null;
  194. Users users = userService.delhotelAndUsers(hotelId, userId, TempEnum.收藏);
  195. JSONObject jsonObject = new JSONObject();
  196. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  197. jsonObject.put(B.data, users);
  198. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  199. return null;
  200. }
  201. /**
  202. * hotelId 酒店id
  203. * queryStartTime
  204. * queryEndTime
  205. * userId
  206. * 通过酒店的id获取到房间信息
  207. *
  208. * @return
  209. */
  210. public String getHouseByHotelId() throws ParseException {
  211. JSONObject jsonObject = new JSONObject();
  212. // 如果未赋值,则直接跳出
  213. if (Func.checkNull(hotelId) || Func.checkNull(queryStartTime) || Func.checkNull(queryEndTime)) {
  214. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  215. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  216. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  217. return null;
  218. }
  219. // 使用天去查"yyyy/MM/dd"
  220. String startDate = DateUtil.parseDateToStr((Func.parseDate(queryStartTime)), DateUtil.Time_Formatter_Day);
  221. String endDate = DateUtil.parseDateToStr((Func.parseDate(queryEndTime)), DateUtil.Time_Formatter_Day);
  222. Hotel hotel = appHomePageService.getHotelAndHouseByHotelId(hotelId, startDate, endDate);
  223. hotel.setHotelFileInfoList(fileService.queryListByLinkId(hotel.getManagerId()+""));
  224. // 添加是否已收藏的酒店
  225. if (!Func.checkNull(userId))
  226. hotel = appHomePageService.assignCollect(userId, hotel);
  227. //region 2023-09-12 A-jax 添加房型均价:入住的每一天的价格之和 / 入住天数
  228. List<HousePriceDataVo> hprd = housePriceService.queryHousePriceDatas(hotel.getManagerId(), queryStartTime, TimeExchange.TimeDesD(queryEndTime, -1));
  229. List<HousePriceOneDataVo> oneDatas = new ArrayList<>();
  230. //将数据处理成单天的
  231. for (HousePriceDataVo hp : hprd) {
  232. if (hp.getStartTime().equals(hp.getEndTime())) {
  233. HousePriceOneDataVo oneData = new HousePriceOneDataVo();
  234. oneData.setSetDate(hp.getStartTime());
  235. oneData.setCreateDate(hp.getCreateDate());
  236. oneData.setPrice(hp.getPrice());
  237. oneData.setHouseId(hp.getHouseId());
  238. oneDatas.add(oneData);
  239. } else {
  240. int beDateNum = TimeExchange.daysBetween(hp.getStartTime(), hp.getEndTime());
  241. for (int i = 0; i <= beDateNum; i++) {
  242. String date = TimeExchange.TimeDesD(hp.getStartTime(), i);
  243. if (date.equals(queryEndTime)) {
  244. break;
  245. }
  246. HousePriceOneDataVo oneData = new HousePriceOneDataVo();
  247. oneData.setSetDate(TimeExchange.TimeDesD(hp.getStartTime(), i));
  248. oneData.setCreateDate(hp.getCreateDate());
  249. oneData.setPrice(hp.getPrice());
  250. oneData.setHouseId(hp.getHouseId());
  251. oneDatas.add(oneData);
  252. }
  253. }
  254. }
  255. List<String> dateStrs = new ArrayList<>();
  256. int dateNum = TimeExchange.daysBetween(queryStartTime, TimeExchange.TimeDesD(queryEndTime, -1));
  257. for (int i = 0; i <= dateNum; i++) {
  258. String date = TimeExchange.TimeDesD(queryStartTime, i);
  259. dateStrs.add(date);
  260. }
  261. List<House> houseList = hotel.getHouseList();
  262. for (House houseData : houseList) {
  263. BigDecimal todayPrice = BigDecimal.ZERO;
  264. for (String dateStr : dateStrs) {
  265. Optional<HousePriceOneDataVo> oneData = oneDatas.stream().filter(e -> e.getHouseId().toString().equals(houseData.getId()) && e.getSetDate().equals(dateStr)).sorted(Comparator.comparing(HousePriceOneDataVo::getCreateDate, Comparator.reverseOrder())).findFirst();
  266. if (oneData != null && oneData.isPresent()) {
  267. todayPrice = todayPrice.add(BigDecimal.valueOf(oneData.get().getPrice()));
  268. } else {
  269. todayPrice = todayPrice.add(BigDecimal.valueOf(houseData.getPrice()));
  270. }
  271. }
  272. houseData.setPrice(todayPrice.doubleValue());
  273. if (dateStrs.size() > 0) {
  274. houseData.setPrice(houseData.getPrice() == 0.0 ? 0.0 : (new BigDecimal(houseData.getPrice()).divide(new BigDecimal(dateStrs.size())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
  275. }
  276. }
  277. //endregion
  278. // 通过时间区间查询房间信息,并带好是否有房标识给前台
  279. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  280. jsonObject.put(B.data, ResultUtil.ok(hotel));
  281. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  282. return null;
  283. }
  284. /**
  285. * 通过酒店的id获取到酒店详细信息
  286. * hotelId
  287. * userId
  288. *
  289. * @return
  290. */
  291. public String getHotelInfoByHotelId() {
  292. JSONObject jsonObject = new JSONObject();
  293. // 如果未赋值,则直接跳出
  294. if (Func.checkNull(hotelId)) {
  295. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  296. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  297. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  298. return null;
  299. }
  300. Hotel hotel = hotelService.getById(Func.parseInt(hotelId));
  301. if (hotel == null) {
  302. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  303. jsonObject.put(B.message, "无法查询到当前酒店信息");
  304. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  305. return null;
  306. }
  307. hotel.setHotelFileInfoList(fileService.queryListByLinkId(hotel.getManagerId()+""));
  308. // 添加是否已收藏的酒店
  309. if (!Func.checkNull(userId))
  310. appHomePageService.assignCollect(userId, hotel);
  311. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  312. jsonObject.put(B.data, hotel);
  313. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  314. return null;
  315. }
  316. /**
  317. * 描述:获取各乡俗民宿级别数量
  318. *
  319. * @return
  320. */
  321. public String getTownshipCount() {
  322. JSONObject resultjson = new JSONObject();
  323. List<townshipCountDTO> list = adminManagerService.getTownshipCount();
  324. if (list != null) {
  325. resultjson.put("message", "返回成功");
  326. resultjson.put("code", 200);
  327. resultjson.put("data", list);
  328. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  329. return null;
  330. }
  331. resultjson.put("message", "数据为空");
  332. resultjson.put("code", 205);
  333. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  334. return null;
  335. }
  336. /**
  337. * 描述:获取各乡镇剩余房数
  338. *
  339. * @return
  340. */
  341. public String getResidueCount() {
  342. JSONObject resultjson = new JSONObject();
  343. List<townshipCountDTO> list = adminManagerService.getResidueCount();
  344. if (list != null) {
  345. resultjson.put("message", "返回成功");
  346. resultjson.put("code", 200);
  347. resultjson.put("data", list);
  348. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  349. return null;
  350. }
  351. resultjson.put("message", "数据为空");
  352. resultjson.put("code", 205);
  353. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  354. return null;
  355. }
  356. public String getUserCode() {
  357. return userCode;
  358. }
  359. public void setUserCode(String userCode) {
  360. this.userCode = userCode;
  361. }
  362. public String getQueryValue() {
  363. return queryValue;
  364. }
  365. public void setQueryValue(String queryValue) {
  366. this.queryValue = queryValue;
  367. }
  368. public String getHotelId() {
  369. return hotelId;
  370. }
  371. public void setHotelId(String hotelId) {
  372. this.hotelId = hotelId;
  373. }
  374. public void setHotel_township(String hotel_township) {
  375. this.hotel_township = hotel_township;
  376. }
  377. }