AppHomePageAction.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. if(iPage.getPageList() != null && iPage.getPageList().size() > 0){
  115. List<Integer> managerIds = iPage.getPageList().stream().map(Hotel::getManagerId).collect(Collectors.toList());
  116. //2023-09-22 A-jax 获取最低价
  117. if(managerIds.size() > 0){
  118. List<HotelPriceDataVo> prices = housePriceService.queryHotelPriceDatas(StringUtils.join(managerIds, ","), TimeExchange.getDateStr());
  119. List<HotelPriceOneDataVo> oneDatas = new ArrayList<>();
  120. //获取当天的
  121. for (HotelPriceDataVo hp : prices) {
  122. HotelPriceOneDataVo oneData = new HotelPriceOneDataVo();
  123. oneData.setSetDate(TimeExchange.getDateStr());
  124. oneData.setCreateDate(hp.getCreateDate());
  125. oneData.setPrice(hp.getPrice());
  126. oneData.setManagerId(hp.getManagerId());
  127. oneDatas.add(oneData);
  128. }
  129. /**
  130. * 根据房型ID处理重复的数据
  131. */
  132. List<HotelPriceOneDataVo> newOneDatas = new ArrayList<>();
  133. if(oneDatas.size() > 0){
  134. for (Integer managerId : managerIds) {
  135. Optional<HotelPriceOneDataVo> one = oneDatas.stream().filter(e -> e.getManagerId().equals(managerId)).sorted(Comparator.comparing(HotelPriceOneDataVo::getCreateDate, Comparator.reverseOrder())).findFirst();
  136. if (one != null && one.isPresent()) {
  137. HotelPriceOneDataVo oneData = new HotelPriceOneDataVo();
  138. oneData.setSetDate(one.get().getSetDate());
  139. oneData.setCreateDate(one.get().getCreateDate());
  140. oneData.setPrice(one.get().getPrice());
  141. oneData.setManagerId(one.get().getManagerId());
  142. newOneDatas.add(oneData);
  143. }
  144. }
  145. }
  146. /**
  147. * 获取商家集合中最低房型价格
  148. */
  149. List<PriceHotelDataVo> mins = houseService.gethotelMinPrice(StringUtils.join(managerIds, ","));
  150. DecimalFormat decimalFormat = new DecimalFormat("#####.##");
  151. for (Hotel hotel: iPage.getPageList()) {
  152. Optional<HotelPriceOneDataVo> one = newOneDatas.stream().filter(e -> e.getManagerId().equals(hotel.getManagerId())).findFirst();
  153. if(one != null && one.isPresent()){
  154. hotel.setMin_price(decimalFormat.format(one.get().getPrice()));
  155. }else{
  156. Optional<PriceHotelDataVo> min = mins.stream().filter(e -> e.getManagerId().equals(hotel.getManagerId())).findFirst();
  157. if(min != null && min.isPresent()){
  158. hotel.setMin_price(decimalFormat.format(min.get().getPrice()));
  159. }
  160. }
  161. }
  162. }
  163. }
  164. // 用户访问量数据
  165. userVisitsDao.add(userId);
  166. ResponseUtil.writeJsonIPage(ServletActionContext.getResponse(), iPage);
  167. return null;
  168. }
  169. /**
  170. * 用户点击收藏民宿
  171. * userId
  172. * hotelId
  173. *
  174. * @return
  175. */
  176. public String collectHotel() {
  177. if (Func.checkNull(userId) || Func.checkNull(hotelId))
  178. return null;
  179. Users users = userService.addhotelAndUsers(hotelId, userId, TempEnum.收藏);
  180. JSONObject jsonObject = new JSONObject();
  181. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  182. jsonObject.put(B.data, users);
  183. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  184. return null;
  185. }
  186. /**
  187. * 用户点击取消 收藏民宿
  188. * userId 用户id
  189. * hotelId 酒店id
  190. *
  191. * @return
  192. */
  193. public String delCollectHotel() {
  194. if (Func.checkNull(userId) || Func.checkNull(hotelId))
  195. return null;
  196. Users users = userService.delhotelAndUsers(hotelId, userId, TempEnum.收藏);
  197. JSONObject jsonObject = new JSONObject();
  198. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  199. jsonObject.put(B.data, users);
  200. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  201. return null;
  202. }
  203. /**
  204. * hotelId 酒店id
  205. * queryStartTime
  206. * queryEndTime
  207. * userId
  208. * 通过酒店的id获取到房间信息
  209. *
  210. * @return
  211. */
  212. public String getHouseByHotelId() throws ParseException {
  213. JSONObject jsonObject = new JSONObject();
  214. // 如果未赋值,则直接跳出
  215. if (Func.checkNull(hotelId) || Func.checkNull(queryStartTime) || Func.checkNull(queryEndTime)) {
  216. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  217. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  218. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  219. return null;
  220. }
  221. // 使用天去查"yyyy/MM/dd"
  222. String startDate = DateUtil.parseDateToStr((Func.parseDate(queryStartTime)), DateUtil.Time_Formatter_Day);
  223. String endDate = DateUtil.parseDateToStr((Func.parseDate(queryEndTime)), DateUtil.Time_Formatter_Day);
  224. Hotel hotel = appHomePageService.getHotelAndHouseByHotelId(hotelId, startDate, endDate);
  225. hotel.setHotelFileInfoList(fileService.queryListByLinkId(hotel.getManagerId()+""));
  226. // 添加是否已收藏的酒店
  227. if (!Func.checkNull(userId))
  228. hotel = appHomePageService.assignCollect(userId, hotel);
  229. //region 2023-09-12 A-jax 添加房型均价:入住的每一天的价格之和 / 入住天数
  230. List<HousePriceDataVo> hprd = housePriceService.queryHousePriceDatas(hotel.getManagerId(), queryStartTime, TimeExchange.TimeDesD(queryEndTime, -1));
  231. List<HousePriceOneDataVo> oneDatas = new ArrayList<>();
  232. //将数据处理成单天的
  233. for (HousePriceDataVo hp : hprd) {
  234. if (hp.getStartTime().equals(hp.getEndTime())) {
  235. HousePriceOneDataVo oneData = new HousePriceOneDataVo();
  236. oneData.setSetDate(hp.getStartTime());
  237. oneData.setCreateDate(hp.getCreateDate());
  238. oneData.setPrice(hp.getPrice());
  239. oneData.setHouseId(hp.getHouseId());
  240. oneDatas.add(oneData);
  241. } else {
  242. int beDateNum = TimeExchange.daysBetween(hp.getStartTime(), hp.getEndTime());
  243. for (int i = 0; i <= beDateNum; i++) {
  244. String date = TimeExchange.TimeDesD(hp.getStartTime(), i);
  245. if (date.equals(queryEndTime)) {
  246. break;
  247. }
  248. HousePriceOneDataVo oneData = new HousePriceOneDataVo();
  249. oneData.setSetDate(TimeExchange.TimeDesD(hp.getStartTime(), i));
  250. oneData.setCreateDate(hp.getCreateDate());
  251. oneData.setPrice(hp.getPrice());
  252. oneData.setHouseId(hp.getHouseId());
  253. oneDatas.add(oneData);
  254. }
  255. }
  256. }
  257. List<String> dateStrs = new ArrayList<>();
  258. int dateNum = TimeExchange.daysBetween(queryStartTime, TimeExchange.TimeDesD(queryEndTime, -1));
  259. for (int i = 0; i <= dateNum; i++) {
  260. String date = TimeExchange.TimeDesD(queryStartTime, i);
  261. dateStrs.add(date);
  262. }
  263. List<House> houseList = hotel.getHouseList();
  264. for (House houseData : houseList) {
  265. BigDecimal todayPrice = BigDecimal.ZERO;
  266. for (String dateStr : dateStrs) {
  267. 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();
  268. if (oneData != null && oneData.isPresent()) {
  269. todayPrice = todayPrice.add(BigDecimal.valueOf(oneData.get().getPrice()));
  270. } else {
  271. todayPrice = todayPrice.add(BigDecimal.valueOf(houseData.getPrice()));
  272. }
  273. }
  274. houseData.setPrice(todayPrice.doubleValue());
  275. if (dateStrs.size() > 0) {
  276. // houseData.setPrice(houseData.getPrice() == 0.0 ? 0.0 : (new BigDecimal(houseData.getPrice()).divide(new BigDecimal(dateStrs.size())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
  277. houseData.setPrice(houseData.getPrice() == 0.0 ? 0.0 : (new BigDecimal(houseData.getPrice()).divide(new BigDecimal(dateStrs.size()),2, BigDecimal.ROUND_HALF_UP).doubleValue()));
  278. }
  279. }
  280. //endregion
  281. // 通过时间区间查询房间信息,并带好是否有房标识给前台
  282. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  283. jsonObject.put(B.data, ResultUtil.ok(hotel));
  284. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  285. return null;
  286. }
  287. /**
  288. * 通过酒店的id获取到酒店详细信息
  289. * hotelId
  290. * userId
  291. *
  292. * @return
  293. */
  294. public String getHotelInfoByHotelId() {
  295. JSONObject jsonObject = new JSONObject();
  296. // 如果未赋值,则直接跳出
  297. if (Func.checkNull(hotelId)) {
  298. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  299. jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
  300. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  301. return null;
  302. }
  303. Hotel hotel = hotelService.getById(Func.parseInt(hotelId));
  304. if (hotel == null) {
  305. jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
  306. jsonObject.put(B.message, "无法查询到当前酒店信息");
  307. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  308. return null;
  309. }
  310. hotel.setHotelFileInfoList(fileService.queryListByLinkId(hotel.getManagerId()+""));
  311. // 添加是否已收藏的酒店
  312. if (!Func.checkNull(userId))
  313. appHomePageService.assignCollect(userId, hotel);
  314. jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
  315. jsonObject.put(B.data, hotel);
  316. ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
  317. return null;
  318. }
  319. /**
  320. * 描述:获取各乡俗民宿级别数量
  321. *
  322. * @return
  323. */
  324. public String getTownshipCount() {
  325. JSONObject resultjson = new JSONObject();
  326. List<townshipCountDTO> list = adminManagerService.getTownshipCount();
  327. if (list != null) {
  328. resultjson.put("message", "返回成功");
  329. resultjson.put("code", 200);
  330. resultjson.put("data", list);
  331. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  332. return null;
  333. }
  334. resultjson.put("message", "数据为空");
  335. resultjson.put("code", 205);
  336. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  337. return null;
  338. }
  339. /**
  340. * 描述:获取各乡镇剩余房数
  341. *
  342. * @return
  343. */
  344. public String getResidueCount() {
  345. JSONObject resultjson = new JSONObject();
  346. List<townshipCountDTO> list = adminManagerService.getResidueCount();
  347. if (list != null) {
  348. resultjson.put("message", "返回成功");
  349. resultjson.put("code", 200);
  350. resultjson.put("data", list);
  351. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  352. return null;
  353. }
  354. resultjson.put("message", "数据为空");
  355. resultjson.put("code", 205);
  356. ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
  357. return null;
  358. }
  359. public String getUserCode() {
  360. return userCode;
  361. }
  362. public void setUserCode(String userCode) {
  363. this.userCode = userCode;
  364. }
  365. public String getQueryValue() {
  366. return queryValue;
  367. }
  368. public void setQueryValue(String queryValue) {
  369. this.queryValue = queryValue;
  370. }
  371. public String getHotelId() {
  372. return hotelId;
  373. }
  374. public void setHotelId(String hotelId) {
  375. this.hotelId = hotelId;
  376. }
  377. public void setHotel_township(String hotel_township) {
  378. this.hotel_township = hotel_township;
  379. }
  380. }