Compare commits

...

8 Commits

@ -0,0 +1,57 @@
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;
//这段代码是一个Java实体类用于表示旅游景点Attractions的信息。
@Entity
@Table(name = "attractions")
public class Attractions {
@Id
@Column(name = "id")
private String id;
@Column(name = "image")
private String image;
@Column(name = "attractionsName")
private String name;
@Column(name = "attractionsAddress")
private String address;
@Column(name = "attractionsDescribe")
private String describe;
@Column(name = "attractionsStatus")
private Integer status;
@Column(name = "createDate")
private Date createDate;
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 getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public String getDescribe() { return describe; }
public void setDescribe(String describe) { this.describe = describe; }
public Integer getStatus() { return status; }
public void setStatus(Integer status) { this.status = status; }
public Date getCreateDate() { return createDate; }
public void setCreateDate(Date createDate) { this.createDate = createDate; }
}

@ -0,0 +1,11 @@
package com.power.travel.repository;
import com.power.travel.model.Attractions;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
//这段代码定义了一个名为AttractionsRepository的Java接口
@Repository
public interface AttractionsRepository extends JpaRepository<Attractions, String>, JpaSpecificationExecutor<Attractions> {
}

@ -0,0 +1,81 @@
package com.power.travel.controller;
import com.power.travel.core.Result;
import com.power.travel.service.StrategyService;
import com.power.travel.model.TravelStrategy;
import com.power.travel.model.UserStrategy;
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("/strategy")
public class StrategyController {
@Autowired
private StrategyService strategyService;
@RequestMapping("/travelStrategyListUI")
public String travelStrategyListUI(Model model, @ModelAttribute("searchName") String searchName,
@PageableDefault(size = 10) Pageable pageable) {
Page<TravelStrategy> page = strategyService.TravelStrategyListUI(searchName, pageable);
List<TravelStrategy> top10Strategy = strategyService.findTop10Strategy();
model.addAttribute("top10Strategy", top10Strategy);
model.addAttribute("page", page);
return "strategy/travelStrategy";
}
@RequestMapping("/travelStrategyDetailsUI")
public String travelStrategyDetailsUI(Model model, HttpServletRequest request, @RequestParam(name = "id") String id) {
TravelStrategy travelStrategy = strategyService.findTravelStrategyById(id);
//如果用户显示已经关注,就是查看关注列表
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";
}
}

@ -0,0 +1,175 @@
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.enums.StatusEnum;
import com.power.travel.model.TravelStrategy;
import com.power.travel.model.User;
import com.power.travel.model.UserStrategy;
import com.power.travel.repository.TravelStrategyRepository;
import com.power.travel.repository.UserRepository;
import com.power.travel.repository.UserStrategyRepository;
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 StrategyService {
@Autowired
private TravelStrategyRepository travelStrategyRepository;
@Autowired
private UserRepository userRepository;
@Autowired
private UserStrategyRepository userStrategyRepository;
public Page<TravelStrategy> TravelStrategyListUI(String searchName, Pageable pageable) {
//查询通过后台审核的攻略列表
Page<TravelStrategy> travelStrategyPage = travelStrategyRepository.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("title"), "%" + searchName + "%")));
}
query.where(predicates.toArray(new Predicate[]{}));
query.orderBy(cb.desc(root.get("createDate")));
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;
}
@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());
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) -> {
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 travelStrategyPage.getContent();
}
}

@ -0,0 +1,24 @@
@RequestMapping("/getAttractionsById")
@ResponseBody
public Result getAttractionsById(String id) {
return ResultGenerator.genSuccessResult(systemService.getAttractionsById(id));
}
@RequestMapping("/updateAttractionsStatus")
@ResponseBody
public Result updateAttractionsStatus(String id) {
return systemService.updateAttractionsStatus(id);
}
@RequestMapping("/saveAttractions")
@ResponseBody
public Result saveAttractions(Attractions attractions) {
return systemService.saveAttractions(attractions);
}
@RequestMapping("/travelRouteListUI")
public String travelRouteListUI(Model model, @PageableDefault(size = 10) Pageable pageable) {
Page<TravelRoute> page = systemService.getTravelRoutePage(pageable);
model.addAttribute("page", page);
return "system/route/list";
}

@ -0,0 +1,47 @@
package com.power.travel.model;
import javax.persistence.*;
import java.util.Date;
//定义了一个名为UserAttractions的Java实体类用于表示用户对旅游景点的评价或信息
@Entity //标记该类为一个JPA实体
@Table(name = "user_attractions")
public class UserAttractions {
@Id
@Column(name = "id")
private String id;
@OneToOne
@JoinColumn(name = "user_id")
private User user;
@OneToOne
@JoinColumn(name = "attractions_id")
private Attractions attractions;
@Column(name = "userAttractionsDescribe")
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 Attractions getAttractions() { return attractions; }
public void setAttractions(Attractions attractions) { this.attractions = attractions; }
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;}
}

@ -0,0 +1,16 @@
package com.power.travel.repository;
import com.power.travel.model.Attractions;
import com.power.travel.model.User;
import com.power.travel.model.UserAttractions;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
public interface UserAttractionsRepository extends JpaRepository<UserAttractions, String>, JpaSpecificationExecutor<UserAttractions> {
List<UserAttractions> findUserAttractionsByUser(User user);
UserAttractions findUserAttractionsByAttractionsAndUser(Attractions attractions, User user);
}

@ -0,0 +1,48 @@
package com.power.travel.model;
import javax.persistence.*;
import java.util.Date;
@Entity
@Table(name = "user_strategy")
public class UserStrategy {
@Id
@Column(name = "id")
private String id;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
@OneToOne
@JoinColumn(name = "strategy_id")
private TravelStrategy travelStrategy;
@Column(name = "create_date")
private Date createDate;
@Column(name = "update_date")
private Date updateDate;
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 TravelStrategy getTravelStrategy() { return travelStrategy; }
public void setTravelStrategy(TravelStrategy travelStrategy) { this.travelStrategy = travelStrategy; }
public Date getCreateDate() { return createDate; }
public void setCreateDate(Date createDate) { this.createDate = createDate; }
public Date getUpdateDate() { return updateDate; }
public void setUpdateDate(Date updateDate) { this.updateDate = updateDate; }
}
Loading…
Cancel
Save