Compare commits
7 Commits
main
...
liuxiaomei
Author | SHA1 | Date |
---|---|---|
pfor58hiw | 0ede829ce9 | 5 months ago |
pfor58hiw | 447f8996f0 | 5 months ago |
pfor58hiw | 2b07af0346 | 5 months ago |
pfor58hiw | 4499663128 | 5 months ago |
pfor58hiw | dff8680515 | 5 months ago |
pfor58hiw | 6cd4d68e5e | 5 months ago |
pfor58hiw | 62c57cbc23 | 5 months ago |
@ -0,0 +1,84 @@
|
||||
package com.power.travel.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Table(name = "hotel")
|
||||
public class Hotel {
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private String id;
|
||||
@Column(name = "image")
|
||||
private String image;
|
||||
@Column(name = "hotelName")
|
||||
private String name;
|
||||
@Column(name = "hotelAddress")
|
||||
private String address;
|
||||
@Column(name = "hotelDescribe")
|
||||
private String describe;
|
||||
@Column(name = "HotelStatus")
|
||||
private Integer status;
|
||||
@Column(name = "createDate")
|
||||
private Date createDate;
|
||||
|
||||
public Date getCreateDate() {
|
||||
return createDate;
|
||||
}
|
||||
|
||||
public void setCreateDate(Date createDate) {
|
||||
this.createDate = createDate;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Integer status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescribe() {
|
||||
return describe;
|
||||
}
|
||||
|
||||
public void setDescribe(String describe) {
|
||||
this.describe = describe;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.power.travel.controller;
|
||||
|
||||
import com.power.travel.core.Result;
|
||||
import com.power.travel.model.User;
|
||||
import com.power.travel.service.LoginService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
||||
@Autowired
|
||||
private LoginService loginService;
|
||||
|
||||
@RequestMapping("/loginUI")
|
||||
public String loginUI() {
|
||||
return "login/index-login";
|
||||
}
|
||||
|
||||
@RequestMapping("/login")
|
||||
@ResponseBody
|
||||
public Result login(Model model, User user, HttpServletResponse response) {
|
||||
return loginService.login(user, response);
|
||||
}
|
||||
|
||||
@RequestMapping("/logout")
|
||||
public String logout(HttpServletRequest request, HttpServletResponse response) {
|
||||
loginService.logout(request, response);
|
||||
//SpringMVC重定向
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
@RequestMapping("/registerUI")
|
||||
public String registerUI() {
|
||||
return "login/index-register";
|
||||
}
|
||||
|
||||
@RequestMapping("/register")
|
||||
@ResponseBody
|
||||
public Result register(Model model, User user) {
|
||||
return loginService.register(user);
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package com.power.travel.service;
|
||||
|
||||
import com.power.travel.core.Result;
|
||||
import com.power.travel.core.ResultGenerator;
|
||||
import com.power.travel.model.User;
|
||||
import com.power.travel.util.CookieUitl;
|
||||
import com.power.travel.util.IdGenerator;
|
||||
import com.power.travel.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
//业务逻辑层
|
||||
@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("密码错误!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void logout(HttpServletRequest request, HttpServletResponse response) {
|
||||
Cookie cookie = CookieUitl.get(request, "username");
|
||||
if(cookie != null){
|
||||
CookieUitl.set(response,"username",null,0);
|
||||
}
|
||||
|
||||
|
||||
// String value = null;
|
||||
//
|
||||
// Cookie[] cookies = request.getCookies();
|
||||
// if (cookies == null){
|
||||
// System.out.println("error");
|
||||
// }else{
|
||||
// for (int i = 0;i<cookies.length;i++){
|
||||
// if(cookies[i].getName().equals("root")){
|
||||
// value = cookies[i].getValue();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// Cookie cookie = new Cookie("username",value);
|
||||
// cookie.setMaxAge(-1);
|
||||
}
|
||||
|
||||
public Result register(User user) {
|
||||
User userByUsername = userRepository.findUserByUsername(user.getUsername());
|
||||
if(userByUsername != null){
|
||||
return ResultGenerator.genFailResult("用户名重复!");
|
||||
}
|
||||
//Todo 这里有一个事务操作
|
||||
user.setId(IdGenerator.id());
|
||||
userRepository.save(user);
|
||||
return ResultGenerator.genSuccessResult();
|
||||
}
|
||||
}
|
@ -0,0 +1,207 @@
|
||||
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,55 @@
|
||||
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.User;
|
||||
import com.power.travel.util.CookieUitl;
|
||||
import com.power.travel.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@Service
|
||||
public class UserCenterService {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
private LoginService loginService;
|
||||
|
||||
public User getUser(HttpServletRequest request) {
|
||||
Cookie cookie = CookieUitl.get(request, "username");
|
||||
User user = null;
|
||||
if (cookie != null) {
|
||||
user = userRepository.findUserByUsername(cookie.getValue());
|
||||
} else {
|
||||
throw new ServiceException("不存在该用户!");
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public Result centerEdit(User user) {
|
||||
User oldUser = userRepository.findById(user.getId()).orElseThrow(() -> new ServiceException("用户ID错误!"));
|
||||
oldUser.setName(user.getName());
|
||||
userRepository.save(oldUser);
|
||||
return ResultGenerator.genSuccessResult();
|
||||
}
|
||||
|
||||
public Result centerEditPW(HttpServletRequest request, HttpServletResponse response, String id, String oldPassword, String newPassword) {
|
||||
User oldUser = userRepository.findById(id).orElseThrow(() -> new ServiceException("用户ID错误!"));
|
||||
if (!oldUser.getPassword().equals(oldPassword)) {
|
||||
return ResultGenerator.genFailResult("原始密码错误!");
|
||||
}
|
||||
oldUser.setPassword(newPassword);
|
||||
userRepository.save(oldUser);
|
||||
//重新登录
|
||||
loginService.logout(request, response);
|
||||
return ResultGenerator.genSuccessResult();
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package com.power.travel.model;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@Table(name = "user_hotel")
|
||||
public class UserHotel {
|
||||
|
||||
@Id
|
||||
@Column(name = "id")
|
||||
private String id;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "user_id")
|
||||
private User user;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "hotel_id")
|
||||
private Hotel hotel;
|
||||
|
||||
@Column(name = "userHotelDescribe")
|
||||
private String describe;
|
||||
|
||||
@Column(name = "createDate")
|
||||
private Date createDate;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
public Hotel getHotel() {
|
||||
return hotel;
|
||||
}
|
||||
|
||||
public void setHotel(Hotel hotel) {
|
||||
this.hotel = hotel;
|
||||
}
|
||||
|
||||
public String getDescribe() {
|
||||
return describe;
|
||||
}
|
||||
|
||||
public void setDescribe(String describe) {
|
||||
this.describe = describe;
|
||||
}
|
||||
|
||||
public Date getCreateDate() {
|
||||
return createDate;
|
||||
}
|
||||
|
||||
public void setCreateDate(Date createDate) {
|
||||
this.createDate = createDate;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue