Compare commits

...

9 Commits

@ -0,0 +1,104 @@
//在线预约
package com.power.travel.controller;
import com.power.travel.core.Result;
import com.power.travel.service.ReserveService;
import com.power.travel.model.Attractions;
import com.power.travel.model.Hotel;
import com.power.travel.model.UserAttractions;
import com.power.travel.model.UserHotel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("/reserve")
public class ReserveController {
@Autowired
private ReserveService reserveService;
//这是一个Spring MVC的注解它表示当用户访问/travelRouteListUI这个URL时下面的方法会被调用。
@RequestMapping("/reserveHotelListUI")
public String reserveHotelListUI(Model model, @ModelAttribute("searchName") String searchName, @PageableDefault(size = 10) Pageable pageable) {
Page<Hotel> page = reserveService.reserveHotelListUI(searchName, pageable);
List<Hotel> top10Hotel = reserveService.getTop10Hotel();
List<Attractions> top10Attractions = reserveService.getTop10Attractions();
model.addAttribute("top10Hotel", top10Hotel);
model.addAttribute("top10Attractions", top10Attractions);
model.addAttribute("page", page);
return "reserve/reserve-hotel";
}
@RequestMapping("/hotelDetailsUI")
public String hotelDetailsUI(Model model, HttpServletRequest request, @RequestParam(name = "id") String id) {
Hotel hotel = reserveService.findHotelById(id);
//如果用户显示已经预约,就是查看预约列表
Boolean flag = reserveService.isReserveHotel(request, id);
List<Hotel> top10Hotel = reserveService.getTop10Hotel();
List<Attractions> top10Attractions = reserveService.getTop10Attractions();
model.addAttribute("top10Hotel", top10Hotel);
model.addAttribute("top10Attractions", top10Attractions);
model.addAttribute("hotel", hotel);
model.addAttribute("flag", flag);
return "reserve/reserve-hotel-details";
}
@RequestMapping("/reserveManageUI")
public String reserveManageUI(Model model, HttpServletRequest request) {
List<UserHotel> userHotelList = reserveService.getReserveHotelByUser(request);
List<UserAttractions> userAttractionsList = reserveService.getReserveAttractionsByUser(request);
model.addAttribute("userHotelList", userHotelList);
model.addAttribute("userAttractionsList",userAttractionsList);
return "reserve/reserve-user-manage";
}
@RequestMapping("/cancelReserve")
@ResponseBody
public Result cancelReserve(HttpServletRequest request, String id) {
return reserveService.cancelReserve(request,id);
}
@RequestMapping("/reserveAttractionsListUI")
public String reserveAttractionsListUI(Model model, @ModelAttribute("searchName") String searchName, @PageableDefault(size = 10) Pageable pageable) {
Page<Attractions> page = reserveService.reserveAttractionsListUI(searchName,pageable);
List<Hotel> top10Hotel = reserveService.getTop10Hotel();
List<Attractions> top10Attractions = reserveService.getTop10Attractions();
model.addAttribute("top10Hotel", top10Hotel);
model.addAttribute("top10Attractions", top10Attractions);
model.addAttribute("page", page);
return "reserve/reserve-attractions";
}
@RequestMapping("/attractionsDetailsUI")
public String attractionsDetailsUI(Model model, HttpServletRequest request, @RequestParam(name = "id") String id) {
Attractions attractions = reserveService.findAttractionsById(id);
//如果用户显示已经预约,就是查看预约列表
Boolean flag = reserveService.isReserveAttractions(request, id);
List<Hotel> top10Hotel = reserveService.getTop10Hotel();
List<Attractions> top10Attractions = reserveService.getTop10Attractions();
model.addAttribute("top10Hotel", top10Hotel);
model.addAttribute("top10Attractions", top10Attractions);
model.addAttribute("attractions", attractions);
model.addAttribute("flag", flag);
return "reserve/reserve-attractions-details";
}
@RequestMapping("/cancelAttractionsReserve")
@ResponseBody
public Result cancelAttractionsReserve(HttpServletRequest request,String id) {
return reserveService.cancelAttractionsReserve(request,id);
}
}

@ -0,0 +1,208 @@
//在线预定业务处理
package com.power.travel.service;
import com.power.travel.core.Result;
import com.power.travel.core.ResultGenerator;
import com.power.travel.core.ServiceException;
import com.power.travel.model.*;
import com.power.travel.repository.*;
import com.power.travel.util.CookieUitl;
import com.power.travel.util.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class ReserveService {
@Autowired
private HotelRepository hotelRepository;
@Autowired
private AttractionsRepository attractionsRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private UserHotelRepository userHotelRepository;
@Autowired
private UserAttractionsRepository userAttractionsRepository;
public Page<Hotel> reserveHotelListUI(String searchName, Pageable pageable) {
//查询启用的酒店列表
Page<Hotel> hotelPage = hotelRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
//status状态,查询状态为0,启动的酒店
predicates.add((cb.equal(root.get("status"), 0)));
//酒店name模糊查询
if (!StringUtils.isEmpty(searchName)) {
predicates.add((cb.like(root.get("name"), "%" + searchName + "%")));
}
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
return null;
}, pageable);
return hotelPage;
}
public Hotel findHotelById(String id) {
return hotelRepository.findById(id).orElseThrow(() -> new ServiceException("酒店id错误!"));
}
public Page<Attractions> reserveAttractionsListUI(String searchName, Pageable pageable) {
//查询启用的景点列表
Page<Attractions> attractionsPage = attractionsRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
//status状态,查询状态为0,启动的景点
predicates.add((cb.equal(root.get("status"), 0)));
//景点name模糊查询
if (!StringUtils.isEmpty(searchName)) {
predicates.add((cb.like(root.get("name"), "%" + searchName + "%")));
}
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
return null;
}, pageable);
return attractionsPage;
}
public Attractions findAttractionsById(String id) {
return attractionsRepository.findById(id).orElseThrow(() -> new ServiceException("景点id错误!"));
}
public List<UserHotel> getReserveHotelByUser(HttpServletRequest request) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("未能获得正确的用户名");
}
User user = userRepository.findUserByUsername(cookie.getValue());
return userHotelRepository.findUserHotelsByUser(user);
}
@Transactional(rollbackFor = Exception.class)
public Result cancelReserve(HttpServletRequest request, String id) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("用户没有登录!");
}
Hotel hotel = findHotelById(id);
User user = userRepository.findUserByUsername(cookie.getValue());
UserHotel userHotel = userHotelRepository.findUserHotelByHotelAndUser(hotel, user);
//存在值就是取消预约.不存在值就是预约
if (userHotel != null) {
userHotelRepository.delete(userHotel);
} else {
UserHotel newUserHotel = new UserHotel();
newUserHotel.setId(IdGenerator.id());
newUserHotel.setCreateDate(new Date());
newUserHotel.setUser(user);
newUserHotel.setHotel(hotel);
userHotelRepository.saveAndFlush(newUserHotel);
}
return ResultGenerator.genSuccessResult();
}
public Boolean isReserveHotel(HttpServletRequest request, String id) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie != null) {
User user = userRepository.findUserByUsername(cookie.getValue());
Hotel hotel = findHotelById(id);
UserHotel userHotel = userHotelRepository.findUserHotelByHotelAndUser(hotel, user);
//每个酒店只能预约一次
if (userHotel != null) {
return true;
}
}
return false;
}
public List<UserAttractions> getReserveAttractionsByUser(HttpServletRequest request) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("未能获得正确的用户名");
}
User user = userRepository.findUserByUsername(cookie.getValue());
return userAttractionsRepository.findUserAttractionsByUser(user);
}
@Transactional(rollbackFor = Exception.class)
public Result cancelAttractionsReserve(HttpServletRequest request, String id) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("用户没有登录!");
}
Attractions attractions = findAttractionsById(id);
User user = userRepository.findUserByUsername(cookie.getValue());
UserAttractions userAttractions = userAttractionsRepository.findUserAttractionsByAttractionsAndUser(attractions, user);
//存在值就是取消预约.不存在值就是预约
if (userAttractions != null) {
userAttractionsRepository.delete(userAttractions);
} else {
UserAttractions newUserAttractions = new UserAttractions();
newUserAttractions.setId(IdGenerator.id());
newUserAttractions.setCreateDate(new Date());
newUserAttractions.setUser(user);
newUserAttractions.setAttractions(attractions);
userAttractionsRepository.saveAndFlush(newUserAttractions);
}
return ResultGenerator.genSuccessResult();
}
public Boolean isReserveAttractions(HttpServletRequest request, String id) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie != null) {
User user = userRepository.findUserByUsername(cookie.getValue());
Attractions attractions = findAttractionsById(id);
UserAttractions userAttractions = userAttractionsRepository.
findUserAttractionsByAttractionsAndUser(attractions, user);
//每个景点只能预约一次
if (userAttractions != null) {
return true;
}
}
return false;
}
public List<Hotel> getTop10Hotel() {
PageRequest pageable = PageRequest.of(0, 10);
//查询启用的酒店列表
Page<Hotel> hotelPage = hotelRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
//status状态,查询状态为0,启动的酒店
predicates.add((cb.equal(root.get("status"), 0)));
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
return null;
}, pageable);
return hotelPage.getContent();
}
public List<Attractions> getTop10Attractions() {
PageRequest pageable = PageRequest.of(0, 10);
Page<Attractions> attractionsPage = attractionsRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
//status状态,查询状态为0,启动的景点
predicates.add((cb.equal(root.get("status"), 0)));
//景点name模糊查询
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
return null;
}, pageable);
return attractionsPage.getContent();
}
}

@ -0,0 +1,54 @@
//数据封装
package com.power.travel.core;
import com.alibaba.fastjson.JSON;
/**
* API
*/
public class Result {
private int code;
private String message;
private Object data;
public Result setCode(ResultCode resultCode) {
this.code = resultCode.getCode();
return this;
}
public int getCode() {
return code;
}
public Result setCode(int code) {
this.code = code;
return this;
}
public String getMessage() {
return message;
}
public Result setMessage(String message) {
this.message = message;
return this;
}
public Object getData() {
return data;
}
public Result setData(Object data) {
this.data = data;
return this;
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
}

@ -0,0 +1,43 @@
package com.power.travel.core;
/**
* HTTP
* 200 5000
* ajax
*/
public enum ResultCode {
/**
*
*/
SUCCESS(200),
/**
*
*/
FAIL(400),
/**
*
*/
BIND_FAIL(417),
/**
*
*/
NOT_FOUND(404),
/**
*
*/
INTERNAL_SERVER_ERROR(500),
/**
*
*/
DATA_ERROR(5000);
private int code;
ResultCode(int code) {
this.code = code;
}
public int getCode() {
return code;
}
}

@ -0,0 +1,42 @@
package com.power.travel.core;
/**
*
*/
public class ResultGenerator {
private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
public static Result genSuccessResult() {
return new Result()
.setCode(ResultCode.SUCCESS)
.setMessage(DEFAULT_SUCCESS_MESSAGE);
}
public static Result genSuccessResult(Object data) {
return new Result()
.setCode(ResultCode.SUCCESS)
.setMessage(DEFAULT_SUCCESS_MESSAGE)
.setData(data);
}
public static Result genSuccessMsgResult(String msg) {
return new Result()
.setCode(ResultCode.SUCCESS)
.setMessage(msg);
}
public static Result genFailResult(String message) {
return new Result()
.setCode(ResultCode.FAIL)
.setMessage(message);
}
public static Result genResult(String message) {
return new Result()
.setCode(ResultCode.DATA_ERROR)
.setMessage(message);
}
}

@ -0,0 +1,19 @@
package com.power.travel.core;
/**
* INFO @see WebMvcConfiguration
*/
public class ServiceException extends RuntimeException {
private static final long serialVersionUID = 2987069302761521237L;
public ServiceException() {
}
public ServiceException(String message) {
super(message);
}
public ServiceException(String message, Throwable cause) {
super(message, cause);
}
}

@ -0,0 +1,45 @@
//用户管理Java类
package com.power.travel.model;
import javax.persistence.*;
@Entity
@Table(name = "sys_user")
public class SysUser {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

@ -0,0 +1,12 @@
//SysUser.java的接口
package com.power.travel.repository;
import com.power.travel.model.SysUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
@Repository
public interface SysUserRepository extends JpaRepository<SysUser, Integer>, JpaSpecificationExecutor<SysUser> {
SysUser findSysUserByUsername(String username);
}

@ -0,0 +1,112 @@
//在线预定的页面
<!DOCTYPE html>
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{base/layout}">
<div layout:fragment="content">
<!--header wrapper end-->
<!--med_tittle_section-->
<div class="med_tittle_section">
<div class="med_img_overlay"></div>
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="med_tittle_cont_wrapper">
<div class="med_tittle_cont">
<ol class="breadcrumb">
<li><a th:href="@{/}">系统首页</a></li>
<li>景点列表</li>
<li>预订</li>
</ol>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- med_tittle_section End -->
<!--service section start-->
<div class="page-container">
<div class="container">
<div class="row med_toppadder50 med_bottompadder50">
<div class="col-lg-9 col-md-9 col-sm-8 col-xs-12">
<div class="news-detail-box">
<h3 th:text="${attractions.name}"></h3>
<div class="author-info">
<span th:text="${#calendars.format(attractions.createDate,'yyyy-MM-dd')}"></span>
<span>所在地:<span th:text="${attractions.address}"></span></span>
<span>浏览次数13</span>
</div>
<div class="news-detail-content">
<p th:text="${attractions.describe}">
</p>
<div align="right" style="float:right">
<button th:if="${flag == false}" th:onclick="cancelAttractionsReserve([[${attractions.id}]],'预订')"
type="button" class="btn btn-default btn-info">预订
</button>
<button th:if="${flag == true }" th:onclick="cancelAttractionsReserve([[${attractions.id}]],'取消预订')"
type="button" class="btn btn-default btn-info">
取消预订
</button>
</div>
</div>
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-4 col-xs-12">
<div class="sider-card">
<div class="sider-card-header">
<h3>热门景点</h3>
</div>
<ul class="sider-list-model-a">
<li th:each="attractions:${top10Attractions}">
<a th:href="@{/reserve/attractionsDetailsUI(id=${attractions.id})}" th:text="${attractions.name}"></a>
</li>
</ul>
</div>
<div class="sider-card">
<div class="sider-card-header">
<h3>热门酒店</h3>
</div>
<ul class="sider-list-model-b">
<li th:each="hotel:${top10Hotel}">
<a th:href="@{/reserve/hotelDetailsUI(id=${hotel.id})}" th:text="${hotel.name}"></a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!--service section end-->
<script th:inline="javascript">
function cancelAttractionsReserve(id, str) {
$.ajax({
url: '/travel/reserve/cancelAttractionsReserve',
data: {"id": id},
type: 'post',
dataType: 'json',
success: function (result) {
if (result.code === 200) {
if(str == '预订'){
confirm(str + "成功!"); //在页面上弹出对话框
window.location.href = "/travel/reserve/reserveManageUI";
}else{
confirm(str + "成功!"); //在页面上弹出对话框
window.location.href = "/travel/reserve/reserveAttractionsListUI";
}
} else {
layer.open({
title: '错误提示',
content: result.message
});
}
}
})
}
</script>
</div>
</html>
Loading…
Cancel
Save