Compare commits
	
		
			9 Commits 
		
	
	
	| Author | SHA1 | Date | 
|---|---|---|
|  | 6c3b9ce354 | 1 year ago | 
|  | 7ccbfdd628 | 1 year ago | 
|  | 3be168b3b2 | 1 year ago | 
|  | 798482c9ec | 1 year ago | 
|  | 58f24ecc8d | 1 year ago | 
|  | d4b2f99610 | 1 year ago | 
|  | a38870de9b | 1 year ago | 
|  | 5b2d7ecd3d | 1 year ago | 
|  | 9019d17f1f | 1 year ago | 
| @ -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,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,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); | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue