| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- package com.happy.action;
- import com.baidubce.model.User;
- import com.happy.Model.Hotel;
- import com.happy.Model.House;
- import com.happy.Model.weixin.Users;
- import com.happy.Until.*;
- import com.happy.Until.Enum.B;
- import com.happy.Until.Enum.TempEnum;
- import com.happy.constant.ResultStatusCode;
- import com.happy.dao.UserVisitsDao;
- import com.happy.dto.IPage;
- import com.happy.dto.townshipCountDTO;
- import com.happy.service.*;
- import com.happy.vo.*;
- import com.opensymphony.xwork2.ActionSupport;
- import net.sf.json.JSONObject;
- import org.apache.commons.lang.StringUtils;
- import org.apache.struts2.ServletActionContext;
- import org.apache.struts2.interceptor.ServletRequestAware;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.math.BigDecimal;
- import java.text.DecimalFormat;
- import java.text.ParseException;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Optional;
- import java.util.stream.Collectors;
- /**
- * 首页进去Action请求交互
- */
- public class AppHomePageAction extends ActionSupport implements ServletRequestAware {
- private HttpServletRequest request;
- public HttpServletResponse response;
- @Resource
- public UserVisitsDao userVisitsDao;
- @Resource
- public UserService userService;
- @Resource
- public AppHomePageService appHomePageService;
- @Resource
- public HotelService hotelService;
- @Resource
- public HouseService houseService;
- @Resource
- public FileService fileService;
- @Resource
- public AdminManagerService adminManagerService;
- @Resource(name = "housePriceService")
- private HousePriceService housePriceService;
- public int page; // 当前页
- public int rows;// 每页显示的行数rows
- public Integer id = new Integer(0);
- public String status;
- public String hotelId; //选择的当前酒店的id
- public String queryStartTime; // 查询开始时间
- public String queryEndTime; // 查询结束时间
- public String userCode; // 第一次进来获取用户的code
- public String queryValue; // 首页查寻搜索关键词
- private String hotel_township; // 所属乡镇
- public String userId; // 用户id
- public int type;
- public HttpServletRequest getRequest() {
- return request;
- }
- public void setRequest(HttpServletRequest request) {
- this.request = request;
- }
- public void setServletRequest(HttpServletRequest request) {
- this.request = request;
- }
- public HttpServletResponse getResponse() {
- return response;
- }
- public void setResponse(HttpServletResponse response) {
- this.response = response;
- }
- public int getPage() {
- return page;
- }
- public void setPage(int page) {
- this.page = page;
- }
- public int getRows() {
- return rows;
- }
- public void setRows(int rows) {
- this.rows = rows;
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getStatus() {
- return status;
- }
- public void setStatus(String status) {
- this.status = status;
- }
- /**
- * 进入首页,展示民宿数据列表
- * queryValue 查询字段
- * hotel_townshipId 所属乡镇
- *
- * @return
- */
- public String homePage() {
- // 获取民宿列表
- IPage<Hotel> iPage = appHomePageService.getHotelList(queryValue, hotel_township, type, page, rows);
- List<Integer> managerIds = iPage.getPageList().stream().map(Hotel::getManagerId).collect(Collectors.toList());
- //2023-09-22 A-jax 获取最低价
- if(managerIds.size() > 0){
- List<HotelPriceDataVo> prices = housePriceService.queryHotelPriceDatas(StringUtils.join(managerIds, ","), TimeExchange.getDateStr());
- List<HotelPriceOneDataVo> oneDatas = new ArrayList<>();
- //获取当天的
- for (HotelPriceDataVo hp : prices) {
- HotelPriceOneDataVo oneData = new HotelPriceOneDataVo();
- oneData.setSetDate(TimeExchange.getDateStr());
- oneData.setCreateDate(hp.getCreateDate());
- oneData.setPrice(hp.getPrice());
- oneData.setManagerId(hp.getManagerId());
- oneDatas.add(oneData);
- }
- /**
- * 根据房型ID处理重复的数据
- */
- List<HotelPriceOneDataVo> newOneDatas = new ArrayList<>();
- if(oneDatas.size() > 0){
- for (Integer managerId : managerIds) {
- Optional<HotelPriceOneDataVo> one = oneDatas.stream().filter(e -> e.getManagerId().equals(managerId)).sorted(Comparator.comparing(HotelPriceOneDataVo::getCreateDate, Comparator.reverseOrder())).findFirst();
- if (one != null && one.isPresent()) {
- HotelPriceOneDataVo oneData = new HotelPriceOneDataVo();
- oneData.setSetDate(one.get().getSetDate());
- oneData.setCreateDate(one.get().getCreateDate());
- oneData.setPrice(one.get().getPrice());
- oneData.setManagerId(one.get().getManagerId());
- newOneDatas.add(oneData);
- }
- }
- }
- /**
- * 获取商家集合中最低房型价格
- */
- List<PriceHotelDataVo> mins = houseService.gethotelMinPrice(StringUtils.join(managerIds, ","));
- DecimalFormat decimalFormat = new DecimalFormat("#####.##");
- for (Hotel hotel: iPage.getPageList()) {
- Optional<HotelPriceOneDataVo> one = newOneDatas.stream().filter(e -> e.getManagerId().equals(hotel.getManagerId())).findFirst();
- if(one != null && one.isPresent()){
- hotel.setMin_price(decimalFormat.format(one.get().getPrice()));
- }else{
- Optional<PriceHotelDataVo> min = mins.stream().filter(e -> e.getManagerId().equals(hotel.getManagerId())).findFirst();
- if(min != null && min.isPresent()){
- hotel.setMin_price(decimalFormat.format(min.get().getPrice()));
- }
- }
- }
- }
- // 用户访问量数据
- userVisitsDao.add(userId);
- ResponseUtil.writeJsonIPage(ServletActionContext.getResponse(), iPage);
- return null;
- }
- /**
- * 用户点击收藏民宿
- * userId
- * hotelId
- *
- * @return
- */
- public String collectHotel() {
- if (Func.checkNull(userId) || Func.checkNull(hotelId))
- return null;
- Users users = userService.addhotelAndUsers(hotelId, userId, TempEnum.收藏);
- JSONObject jsonObject = new JSONObject();
- jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
- jsonObject.put(B.data, users);
- ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
- return null;
- }
- /**
- * 用户点击取消 收藏民宿
- * userId 用户id
- * hotelId 酒店id
- *
- * @return
- */
- public String delCollectHotel() {
- if (Func.checkNull(userId) || Func.checkNull(hotelId))
- return null;
- Users users = userService.delhotelAndUsers(hotelId, userId, TempEnum.收藏);
- JSONObject jsonObject = new JSONObject();
- jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
- jsonObject.put(B.data, users);
- ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
- return null;
- }
- /**
- * hotelId 酒店id
- * queryStartTime
- * queryEndTime
- * userId
- * 通过酒店的id获取到房间信息
- *
- * @return
- */
- public String getHouseByHotelId() throws ParseException {
- JSONObject jsonObject = new JSONObject();
- // 如果未赋值,则直接跳出
- if (Func.checkNull(hotelId) || Func.checkNull(queryStartTime) || Func.checkNull(queryEndTime)) {
- jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
- jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
- ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
- return null;
- }
- // 使用天去查"yyyy/MM/dd"
- String startDate = DateUtil.parseDateToStr((Func.parseDate(queryStartTime)), DateUtil.Time_Formatter_Day);
- String endDate = DateUtil.parseDateToStr((Func.parseDate(queryEndTime)), DateUtil.Time_Formatter_Day);
- Hotel hotel = appHomePageService.getHotelAndHouseByHotelId(hotelId, startDate, endDate);
- hotel.setHotelFileInfoList(fileService.queryListByLinkId(hotel.getManagerId()+""));
- // 添加是否已收藏的酒店
- if (!Func.checkNull(userId))
- hotel = appHomePageService.assignCollect(userId, hotel);
- //region 2023-09-12 A-jax 添加房型均价:入住的每一天的价格之和 / 入住天数
- List<HousePriceDataVo> hprd = housePriceService.queryHousePriceDatas(hotel.getManagerId(), queryStartTime, TimeExchange.TimeDesD(queryEndTime, -1));
- List<HousePriceOneDataVo> oneDatas = new ArrayList<>();
- //将数据处理成单天的
- for (HousePriceDataVo hp : hprd) {
- if (hp.getStartTime().equals(hp.getEndTime())) {
- HousePriceOneDataVo oneData = new HousePriceOneDataVo();
- oneData.setSetDate(hp.getStartTime());
- oneData.setCreateDate(hp.getCreateDate());
- oneData.setPrice(hp.getPrice());
- oneData.setHouseId(hp.getHouseId());
- oneDatas.add(oneData);
- } else {
- int beDateNum = TimeExchange.daysBetween(hp.getStartTime(), hp.getEndTime());
- for (int i = 0; i <= beDateNum; i++) {
- String date = TimeExchange.TimeDesD(hp.getStartTime(), i);
- if (date.equals(queryEndTime)) {
- break;
- }
- HousePriceOneDataVo oneData = new HousePriceOneDataVo();
- oneData.setSetDate(TimeExchange.TimeDesD(hp.getStartTime(), i));
- oneData.setCreateDate(hp.getCreateDate());
- oneData.setPrice(hp.getPrice());
- oneData.setHouseId(hp.getHouseId());
- oneDatas.add(oneData);
- }
- }
- }
- List<String> dateStrs = new ArrayList<>();
- int dateNum = TimeExchange.daysBetween(queryStartTime, TimeExchange.TimeDesD(queryEndTime, -1));
- for (int i = 0; i <= dateNum; i++) {
- String date = TimeExchange.TimeDesD(queryStartTime, i);
- dateStrs.add(date);
- }
- List<House> houseList = hotel.getHouseList();
- for (House houseData : houseList) {
- BigDecimal todayPrice = BigDecimal.ZERO;
- for (String dateStr : dateStrs) {
- 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();
- if (oneData != null && oneData.isPresent()) {
- todayPrice = todayPrice.add(BigDecimal.valueOf(oneData.get().getPrice()));
- } else {
- todayPrice = todayPrice.add(BigDecimal.valueOf(houseData.getPrice()));
- }
- }
- houseData.setPrice(todayPrice.doubleValue());
- if (dateStrs.size() > 0) {
- houseData.setPrice(houseData.getPrice() == 0.0 ? 0.0 : (new BigDecimal(houseData.getPrice()).divide(new BigDecimal(dateStrs.size())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue()));
- }
- }
- //endregion
- // 通过时间区间查询房间信息,并带好是否有房标识给前台
- jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
- jsonObject.put(B.data, ResultUtil.ok(hotel));
- ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
- return null;
- }
- /**
- * 通过酒店的id获取到酒店详细信息
- * hotelId
- * userId
- *
- * @return
- */
- public String getHotelInfoByHotelId() {
- JSONObject jsonObject = new JSONObject();
- // 如果未赋值,则直接跳出
- if (Func.checkNull(hotelId)) {
- jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
- jsonObject.put(B.message, ResultStatusCode.BAD_REQUEST.getMsg());
- ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
- return null;
- }
- Hotel hotel = hotelService.getById(Func.parseInt(hotelId));
- if (hotel == null) {
- jsonObject.put(B.code, ResultStatusCode.BAD_REQUEST.getStatus());
- jsonObject.put(B.message, "无法查询到当前酒店信息");
- ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
- return null;
- }
- hotel.setHotelFileInfoList(fileService.queryListByLinkId(hotel.getManagerId()+""));
- // 添加是否已收藏的酒店
- if (!Func.checkNull(userId))
- appHomePageService.assignCollect(userId, hotel);
- jsonObject.put(B.code, ResultStatusCode.OK.getStatus());
- jsonObject.put(B.data, hotel);
- ResponseUtil.writeJson(ServletActionContext.getResponse(), jsonObject.toString());
- return null;
- }
- /**
- * 描述:获取各乡俗民宿级别数量
- *
- * @return
- */
- public String getTownshipCount() {
- JSONObject resultjson = new JSONObject();
- List<townshipCountDTO> list = adminManagerService.getTownshipCount();
- if (list != null) {
- resultjson.put("message", "返回成功");
- resultjson.put("code", 200);
- resultjson.put("data", list);
- ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
- return null;
- }
- resultjson.put("message", "数据为空");
- resultjson.put("code", 205);
- ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
- return null;
- }
- /**
- * 描述:获取各乡镇剩余房数
- *
- * @return
- */
- public String getResidueCount() {
- JSONObject resultjson = new JSONObject();
- List<townshipCountDTO> list = adminManagerService.getResidueCount();
- if (list != null) {
- resultjson.put("message", "返回成功");
- resultjson.put("code", 200);
- resultjson.put("data", list);
- ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
- return null;
- }
- resultjson.put("message", "数据为空");
- resultjson.put("code", 205);
- ResUtil.writeJson(ServletActionContext.getResponse(), resultjson.toString());
- return null;
- }
- public String getUserCode() {
- return userCode;
- }
- public void setUserCode(String userCode) {
- this.userCode = userCode;
- }
- public String getQueryValue() {
- return queryValue;
- }
- public void setQueryValue(String queryValue) {
- this.queryValue = queryValue;
- }
- public String getHotelId() {
- return hotelId;
- }
- public void setHotelId(String hotelId) {
- this.hotelId = hotelId;
- }
- public void setHotel_township(String hotel_township) {
- this.hotel_township = hotel_township;
- }
- }
|