基于javaweb+mysql的springboot景区旅游管理系统(java+springboot+html+layui+bootstrap+jquery+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SpringBoot景区旅游管理系统(java+springboot+html+layui+bootstrap+jquery+mysql)
该项目分为前台网站展示与后台管理。
前台网站主要功能有:用户登录、注册;首页有热门酒店、热门景点、景区游玩攻略、热门路线等模块;其次导航栏有景区游玩路线、我关注的路线;景区游玩攻略、我发布的攻略、我关注的攻略;在线酒店预订、景点预订、我的预订等; 后台管理主要功能有:用户管理、酒店管理、景点管理、攻略管理、路线管理等;
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.是否Maven项目: 是;查看源码目录中是否包含pom.xml;若包含,则为maven项目,否则为非maven项目 6.数据库:MySql 5.7版本;
技术栈
-
后端:SpringBoot
-
前端:html+layui+bootstrap+jQuery
使用说明
-
使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,下载所需jar包;
-
使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 3. 将项目中application.yml配置文件中的数据库配置改为自己的配置 4. 配置tomcat,然后运行项目,输入localhost:8087/travel 登录 5. 后台管理员账户:admin 密码:admin
前台普通用户账户:user 密码:123456
}
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);
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);
}
@Controller
@RequestMapping("/user")
public class UserCenterController {
@Autowired
private UserCenterService userCenterService;
@RequestMapping("/centerUI")
public String centerUI(Model model, HttpServletRequest request) {
User user = userCenterService.getUser(request);
model.addAttribute("user", user);
return "center/user-center";
}
@RequestMapping("/centerEditUI")
public String centerEditUI(Model model, HttpServletRequest request) {
User user = userCenterService.getUser(request);
model.addAttribute("user", user);
return "center/user-center-edit";
}
@RequestMapping("/centerEdit")
@ResponseBody
public Result centerEdit(Model model, User user) {
return userCenterService.centerEdit(user);
}
@RequestMapping("/centerEditPWUI")
public String centerEditPWUI(Model model, HttpServletRequest request) {
User user = userCenterService.getUser(request);
model.addAttribute("id", user.getId());
return "center/user-center-editpw";
}
@RequestMapping("/centerEditPW")
@ResponseBody
public Result centerEditPW(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam("id") String id, @RequestParam("oldPassword") String oldPassword, @RequestParam("newPassword") String newPassword) {
Boolean flag = strategyService.isStrategy(request, id);
List<TravelStrategy> top10Strategy = strategyService.findTop10Strategy();
model.addAttribute("top10Strategy", top10Strategy);
model.addAttribute("travelStrategy", travelStrategy);
model.addAttribute("flag", flag);
return "strategy/travelStrategy-details";
}
@RequestMapping("/cancelTravelStrategyReserve")
@ResponseBody
public Result cancelTravelStrategyReserve(HttpServletRequest request, String id) {
return strategyService.cancelTravelStrategyReserve(request, id);
}
@RequestMapping("/strategyManageUI")
public String strategyManageUI(Model model, HttpServletRequest request) {
List<UserStrategy> userStrategyList = strategyService.getTravelStrategyByUser(request);
List<TravelStrategy> top10Strategy = strategyService.findTop10Strategy();
model.addAttribute("top10Strategy", top10Strategy);
model.addAttribute("userStrategyList", userStrategyList);
return "strategy/strategy-manage";
}
@RequestMapping("/saveTravelStrategy")
@ResponseBody
public Result saveTravelStrategy(HttpServletRequest request, TravelStrategy travelStrategy) {
return strategyService.saveTravelStrategy(request, travelStrategy);
}
@RequestMapping("/pushStrategyListUI")
public String pushStrategyListUI(HttpServletRequest request, Model model, @ModelAttribute("searchName") String searchName, @PageableDefault(size = 10) Pageable pageable) {
Page<TravelStrategy> page = strategyService.PushStrategyListUI(request,searchName, pageable);
List<TravelStrategy> top10Strategy = strategyService.findTop10Strategy();
model.addAttribute("top10Strategy", top10Strategy);
model.addAttribute("page", page);
return "strategy/pushStrategy";
}
}
package hue.edu.xiong.volunteer_travel.service;
}
package hue.edu.xiong.volunteer_travel.service;
@Service
public class LoginService {
@Autowired
private UserRepository userRepository;
public Result login(User user, HttpServletResponse response) {
User userByUsername = userRepository.findUserByUsername(user.getUsername());
if (userByUsername == null) {
return ResultGenerator.genFailResult("用户名错误!");
} else {
if (user.getPassword().equals(userByUsername.getPassword())) {
Cookie cookie = new Cookie("username", user.getUsername());
cookie.setPath("/");
cookie.setMaxAge(3600);
response.addCookie(cookie);
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult("密码错误!");
}
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")));
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);
return true;
}
}
return false;
}
@Transactional(rollbackFor = Exception.class)
public Result cancelTravelStrategyReserve(HttpServletRequest request, String id) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("用户没有登录!");
}
TravelStrategy travelStrategy = findTravelStrategyById(id);
User user = userRepository.findUserByUsername(cookie.getValue());
UserStrategy userStrategy = userStrategyRepository.findUserStrategyByTravelStrategyAndUser(travelStrategy, user);
//存在值就是取消预约.不存在值就是预约
if (userStrategy != null) {
userStrategyRepository.delete(userStrategy);
} else {
UserStrategy newUserStrategy = new UserStrategy();
newUserStrategy.setId(IdGenerator.id());
newUserStrategy.setCreateDate(new Date());
newUserStrategy.setUser(user);
newUserStrategy.setTravelStrategy(travelStrategy);
userStrategyRepository.saveAndFlush(newUserStrategy);
}
return ResultGenerator.genSuccessResult();
}
public List<UserStrategy> getTravelStrategyByUser(HttpServletRequest request) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("未能获得正确的用户名");
}
User user = userRepository.findUserByUsername(cookie.getValue());
return userStrategyRepository.findUserStrategyByUser(user);
}
public TravelStrategy getTravelStrategyById(String id) {
TravelStrategy travelStrategy = travelStrategyRepository.findById(id).orElseThrow(() -> new ServiceException("攻略ID错误"));
return travelStrategy;
}
@Transactional(rollbackFor = Exception.class)
public Result saveTravelStrategy(HttpServletRequest request, TravelStrategy travelStrategy) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("未能获得正确的用户名");
}
User user = userRepository.findUserByUsername(cookie.getValue());
return null;
}, pageable);
return attractionsPage;
}
public Attractions getAttractionsById(String id) {
Attractions attractions = attractionsRepository.findById(id).orElseThrow(() -> new ServiceException("景点ID错误"));
return attractions;
}
public Result updateAttractionsStatus(String id) {
Attractions attractions = getAttractionsById(id);
if (attractions.getStatus().equals(StatusEnum.DOWM_STATUS.getCode())) {
//改变状态
attractions.setStatus(StatusEnum.UP_STATUS.getCode());
} else {
attractions.setStatus(StatusEnum.DOWM_STATUS.getCode());
}
attractionsRepository.saveAndFlush(attractions);
return ResultGenerator.genSuccessResult();
}
@Transactional(rollbackFor = Exception.class)
public Result saveAttractions(Attractions attractions) {
if (StringUtils.isEmpty(attractions.getId())) {//没有id的情况
attractions.setId(IdGenerator.id());
attractions.setStatus(StatusEnum.DOWM_STATUS.getCode());
attractions.setCreateDate(new Date());
int i = random.nextInt(100);
attractions.setImage("MY_jingdian_0" + (i % 8 + 1));
} else {
//有id的情况
Attractions oldAttractions = getAttractionsById(attractions.getId());
attractions.setStatus(oldAttractions.getStatus());
attractions.setCreateDate(oldAttractions.getCreateDate());
attractions.setImage(oldAttractions.getImage());
}
attractionsRepository.saveAndFlush(attractions);
return ResultGenerator.genSuccessResult();
}
public Page<TravelRoute> getTravelRoutePage(Pageable pageable) {
Page<TravelRoute> travelRoutePage = travelRouteRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
return null;
}, pageable);
return travelRoutePage;
}
@Transactional(rollbackFor = Exception.class)
public Result saveTravelRoute(TravelRoute travelRoute) {
private AttractionsRepository attractionsRepository;
@Autowired
private TravelRouteRepository travelRouteRepository;
@Autowired
private TravelStrategyRepository travelStrategyRepository;
private Random random = new Random(100);
public Result login(SysUser sysUser, HttpServletResponse response) {
SysUser sysUserByUsrname = sysUserRepository.findSysUserByUsername(sysUser.getUsername());
if (sysUserByUsrname == null) {
return ResultGenerator.genFailResult("用户名错误!");
} else {
if (sysUser.getPassword().equals(sysUserByUsrname.getPassword())) {
CookieUitl.set(response, "sysUsername", sysUser.getUsername(), 3600);
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult("密码错误");
}
}
}
public void logout(HttpServletRequest request, HttpServletResponse response) {
Cookie cookie = CookieUitl.get(request, "sysUsername");
if (cookie != null) {
CookieUitl.set(response, "sysUsername", null, 0);
}
}
public Page<User> getUserPage(Pageable pageable) {
Page<User> userPage = userRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("id")));
return null;
}, pageable);
return userPage;
}
@Transactional(rollbackFor = Exception.class)
public Result saveUser(User user) {
System.out.println(user.getId());
if (StringUtils.isEmpty(user.getId())) {//没有id的情况
user.setId(IdGenerator.id());
} else {
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);
}
}
package hue.edu.xiong.volunteer_travel.controller;
public Result updateStatus(String id) {
Hotel hotel = getHotelById(id);
if (hotel.getStatus().equals(StatusEnum.DOWM_STATUS.getCode())) {
//改变状态
hotel.setStatus(StatusEnum.UP_STATUS.getCode());
} else {
hotel.setStatus(StatusEnum.DOWM_STATUS.getCode());
}
hotelRepository.saveAndFlush(hotel);
return ResultGenerator.genSuccessResult();
}
public Page<Attractions> getAttractionsPage(Pageable pageable) {
Page<Attractions> attractionsPage = attractionsRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
return null;
}, pageable);
return attractionsPage;
}
public Attractions getAttractionsById(String id) {
Attractions attractions = attractionsRepository.findById(id).orElseThrow(() -> new ServiceException("景点ID错误"));
return attractions;
}
public Result updateAttractionsStatus(String id) {
Attractions attractions = getAttractionsById(id);
if (attractions.getStatus().equals(StatusEnum.DOWM_STATUS.getCode())) {
//改变状态
attractions.setStatus(StatusEnum.UP_STATUS.getCode());
} else {
attractions.setStatus(StatusEnum.DOWM_STATUS.getCode());
}
attractionsRepository.saveAndFlush(attractions);
return ResultGenerator.genSuccessResult();
}
@Transactional(rollbackFor = Exception.class)
public Result saveAttractions(Attractions attractions) {
if (StringUtils.isEmpty(attractions.getId())) {//没有id的情况
attractions.setId(IdGenerator.id());
attractions.setStatus(StatusEnum.DOWM_STATUS.getCode());
attractions.setCreateDate(new Date());
return null;
}, pageable);
return travelStrategyPage;
}
public Page<TravelStrategy> PushStrategyListUI(HttpServletRequest request, String searchName, Pageable pageable) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("用户未登录");
}
User user = userRepository.findUserByUsername(cookie.getValue());
//查询通过后台审核的攻略列表
Page<TravelStrategy> travelStrategyPage = travelStrategyRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
//攻略name模糊查询
predicates.add((cb.equal(root.get("user"), user)));
//攻略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 travelStrategyPage;
}
public TravelStrategy findTravelStrategyById(String id) {
return travelStrategyRepository.findById(id).orElseThrow(() -> new ServiceException("攻略id错误!"));
}
public Boolean isStrategy(HttpServletRequest request, String id) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie != null) {
User user = userRepository.findUserByUsername(cookie.getValue());
TravelStrategy travelStrategy1 = findTravelStrategyById(id);
UserStrategy userStrategy = userStrategyRepository.findUserStrategyByTravelStrategyAndUser(travelStrategy1, user);
//每个路线只能关注一次
if (userStrategy != null) {
return true;
}
}
return false;
}
hotel.setCreateDate(new Date());
int i = random.nextInt(100);
hotel.setImage("MY_kezhan_0" + (i % 8 + 1));
} else {
//有id的情况
Hotel oldHotel = getHotelById(hotel.getId());
hotel.setStatus(oldHotel.getStatus());
hotel.setCreateDate(oldHotel.getCreateDate());
hotel.setImage(oldHotel.getImage());
}
hotelRepository.saveAndFlush(hotel);
return ResultGenerator.genSuccessResult();
}
public Hotel getHotelById(String id) {
Hotel hotel = hotelRepository.findById(id).orElseThrow(() -> new ServiceException("酒店ID错误!"));
return hotel;
}
public Result updateStatus(String id) {
Hotel hotel = getHotelById(id);
if (hotel.getStatus().equals(StatusEnum.DOWM_STATUS.getCode())) {
//改变状态
hotel.setStatus(StatusEnum.UP_STATUS.getCode());
} else {
hotel.setStatus(StatusEnum.DOWM_STATUS.getCode());
}
hotelRepository.saveAndFlush(hotel);
return ResultGenerator.genSuccessResult();
}
public Page<Attractions> getAttractionsPage(Pageable pageable) {
Page<Attractions> attractionsPage = attractionsRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
return null;
}, pageable);
return attractionsPage;
}
public Attractions getAttractionsById(String id) {
Attractions attractions = attractionsRepository.findById(id).orElseThrow(() -> new ServiceException("景点ID错误"));
return attractions;
}
public Result updateAttractionsStatus(String id) {
Attractions attractions = getAttractionsById(id);
if (attractions.getStatus().equals(StatusEnum.DOWM_STATUS.getCode())) {
//改变状态
attractions.setStatus(StatusEnum.UP_STATUS.getCode());
} else {
public List<UserStrategy> getTravelStrategyByUser(HttpServletRequest request) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("未能获得正确的用户名");
}
User user = userRepository.findUserByUsername(cookie.getValue());
return userStrategyRepository.findUserStrategyByUser(user);
}
public TravelStrategy getTravelStrategyById(String id) {
TravelStrategy travelStrategy = travelStrategyRepository.findById(id).orElseThrow(() -> new ServiceException("攻略ID错误"));
return travelStrategy;
}
@Transactional(rollbackFor = Exception.class)
public Result saveTravelStrategy(HttpServletRequest request, TravelStrategy travelStrategy) {
Cookie cookie = CookieUitl.get(request, "username");
if (cookie == null) {
throw new ServiceException("未能获得正确的用户名");
}
User user = userRepository.findUserByUsername(cookie.getValue());
if (StringUtils.isEmpty(travelStrategy.getId())) {//没有id的情况
travelStrategy.setId(IdGenerator.id());
if (travelStrategy.getStatus() == null) {
//默认为停用
travelStrategy.setStatus(StatusEnum.DOWM_STATUS.getCode());
travelStrategy.setCreateDate(new Date());
travelStrategy.setUser(user);
}
} else {
//有id的情况
TravelStrategy oldTravelStrategy = getTravelStrategyById(travelStrategy.getId());
travelStrategy.setStatus(oldTravelStrategy.getStatus());
travelStrategy.setCreateDate(oldTravelStrategy.getCreateDate());
}
travelStrategyRepository.saveAndFlush(travelStrategy);
return ResultGenerator.genSuccessResult();
}
public List<TravelStrategy> findTop10Strategy() {
PageRequest pageable = PageRequest.of(0, 10);
//查询启用的景区游玩路线列表
Page<TravelStrategy> travelStrategyPage = travelStrategyRepository.findAll((root, query, cb) -> {
SysUser sysUserByUsrname = sysUserRepository.findSysUserByUsername(sysUser.getUsername());
if (sysUserByUsrname == null) {
return ResultGenerator.genFailResult("用户名错误!");
} else {
if (sysUser.getPassword().equals(sysUserByUsrname.getPassword())) {
CookieUitl.set(response, "sysUsername", sysUser.getUsername(), 3600);
return ResultGenerator.genSuccessResult();
} else {
return ResultGenerator.genFailResult("密码错误");
}
}
}
public void logout(HttpServletRequest request, HttpServletResponse response) {
Cookie cookie = CookieUitl.get(request, "sysUsername");
if (cookie != null) {
CookieUitl.set(response, "sysUsername", null, 0);
}
}
public Page<User> getUserPage(Pageable pageable) {
Page<User> userPage = userRepository.findAll((root, query, cb) -> {
List<Predicate> predicates = new ArrayList<>();
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("id")));
return null;
}, pageable);
return userPage;
}
@Transactional(rollbackFor = Exception.class)
public Result saveUser(User user) {
System.out.println(user.getId());
if (StringUtils.isEmpty(user.getId())) {//没有id的情况
user.setId(IdGenerator.id());
} else {
User oldUser = getUserById(user.getId());
user.setUsername(oldUser.getUsername());
user.setName(oldUser.getName());
oldUser.setPassword(user.getPassword());
}
userRepository.saveAndFlush(user);
return ResultGenerator.genSuccessResult();
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");