|
|
|
@ -0,0 +1,326 @@
|
|
|
|
|
package com.wsk.controller;
|
|
|
|
|
|
|
|
|
|
import com.wsk.bean.ShopContextBean;
|
|
|
|
|
import com.wsk.bean.ShopInformationBean;
|
|
|
|
|
import com.wsk.bean.UserWantBean;
|
|
|
|
|
import com.wsk.pojo.*;
|
|
|
|
|
import com.wsk.service.*;
|
|
|
|
|
import com.wsk.token.TokenProccessor;
|
|
|
|
|
import com.wsk.tool.StringUtils;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by wsk1103 on 2017/5/14.
|
|
|
|
|
* GoodsController类,主要用于处理商品相关的各种业务操作,例如发布商品、查询商品、查看商品详情等功能,是Spring MVC框架中的一个控制器类。
|
|
|
|
|
*/
|
|
|
|
|
@Controller
|
|
|
|
|
// @Controller注解表明这个类是Spring MVC的控制器类,会被Spring框架扫描并用于处理Web请求,可将请求映射到对应的方法上。
|
|
|
|
|
public class GoodsController {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
// @Resource注解用于依赖注入,将Spring容器中管理的ShopInformationService类型的Bean注入到当前类中,方便后续调用其提供的与店铺信息相关的业务方法。
|
|
|
|
|
private ShopInformationService shopInformationService;
|
|
|
|
|
@Resource
|
|
|
|
|
private ShopContextService shopContextService;
|
|
|
|
|
@Resource
|
|
|
|
|
private UserInformationService userInformationService;
|
|
|
|
|
@Resource
|
|
|
|
|
private SpecificeService specificeService;
|
|
|
|
|
@Resource
|
|
|
|
|
private ClassificationService classificationService;
|
|
|
|
|
@Resource
|
|
|
|
|
private AllKindsService allKindsService;
|
|
|
|
|
@Resource
|
|
|
|
|
private UserWantService userWantService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理进入发布商品页面的请求方法。
|
|
|
|
|
* 该方法支持GET请求方式,会先判断用户是否登录,如果未登录则重定向到登录页面;如果已登录,进一步判断用户是否经过认证,未认证则提示先认证信息并重定向到个人信息页面;若已认证则准备相关数据并跳转到发布商品页面。
|
|
|
|
|
*
|
|
|
|
|
* @param request HttpServletRequest对象,用于获取请求相关的信息以及操作会话等。
|
|
|
|
|
* @param model Spring MVC中的Model对象,可用于向视图传递数据,比如将用户信息、页面相关参数等传递给视图。
|
|
|
|
|
* @return 返回一个视图名称,根据不同情况可能是重定向到其他页面或者跳转到"page/publish_product"页面。
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/publish_product.do", method = RequestMethod.GET)
|
|
|
|
|
public String publish(HttpServletRequest request, Model model) {
|
|
|
|
|
// 从会话中获取用户信息,判断用户是否已登录。
|
|
|
|
|
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
|
|
|
|
|
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
|
|
|
|
|
// 如果用户信息为空,表示用户没有登录,重定向到登录页面。
|
|
|
|
|
return "redirect:/login.do";
|
|
|
|
|
} else {
|
|
|
|
|
// 如果用户已登录,将用户信息添加到Model中,以便视图可以获取并展示相关信息。
|
|
|
|
|
model.addAttribute("userInformation", userInformation);
|
|
|
|
|
}
|
|
|
|
|
// 如果登录了,判断该用户有没有经过认证,通过获取用户的真实姓名、学号、宿舍等信息是否为空来判断。
|
|
|
|
|
try {
|
|
|
|
|
String realName = userInformation.getRealname();
|
|
|
|
|
String sno = userInformation.getSno();
|
|
|
|
|
String dormitory = userInformation.getDormitory();
|
|
|
|
|
if (StringUtils.getInstance().isNullOrEmpty(realName) || StringUtils.getInstance().isNullOrEmpty(sno) || StringUtils.getInstance().isNullOrEmpty(dormitory)) {
|
|
|
|
|
// 如果这些关键信息有空值,说明用户未认证,添加提示信息到Model中,并重定向到个人信息页面让用户去认证。
|
|
|
|
|
model.addAttribute("message", "请先认证真实信息");
|
|
|
|
|
return "redirect:personal_info.do";
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return "redirect:/login.do";
|
|
|
|
|
}
|
|
|
|
|
// 使用TokenProccessor生成一个token,用于可能的表单防重复提交等安全验证场景(此处假设)。
|
|
|
|
|
String goodsToken = TokenProccessor.getInstance().makeToken();
|
|
|
|
|
request.getSession().setAttribute("goodsToken", goodsToken);
|
|
|
|
|
// 向Model中添加一个新的ShopInformation对象,可能用于页面上的表单绑定等操作(具体取决于页面需求)。
|
|
|
|
|
model.addAttribute("shopInformation", new ShopInformation());
|
|
|
|
|
model.addAttribute("action", 1);
|
|
|
|
|
model.addAttribute("token", goodsToken);
|
|
|
|
|
// 最终跳转到发布商品的页面(具体页面路径配置可能在视图解析器中定义)。
|
|
|
|
|
return "page/publish_product";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理模糊查询商品的请求方法。
|
|
|
|
|
* 根据传入的商品名称进行模糊查询,获取符合条件的商品信息列表,然后将相关信息进行处理和封装后添加到Model中,并返回展示商品列表的视图页面。
|
|
|
|
|
*
|
|
|
|
|
* @param request HttpServletRequest对象,用于获取请求相关信息以及操作会话等,比如获取用户信息。
|
|
|
|
|
* @param model Spring MVC中的Model对象,用于向视图传递查询到的商品信息列表等数据。
|
|
|
|
|
* @param name 从请求中获取的要模糊查询的商品名称参数。
|
|
|
|
|
* @return 返回一个视图名称,若查询过程出现异常则重定向到商城页面("mall_page.do"),正常则返回"page/mall_page"视图展示查询结果。
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/findShopByName.do")
|
|
|
|
|
public String findByName(HttpServletRequest request, Model model,
|
|
|
|
|
@RequestParam String name) {
|
|
|
|
|
try {
|
|
|
|
|
// 调用ShopInformationService的方法,根据商品名称进行模糊查询,获取符合条件的ShopInformation列表。
|
|
|
|
|
List<ShopInformation> shopInformations = shopInformationService.selectByName(name);
|
|
|
|
|
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
|
|
|
|
|
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
|
|
|
|
|
userInformation = new UserInformation();
|
|
|
|
|
// 如果用户未登录(用户信息为空),创建一个空的UserInformation对象添加到Model中,可能用于页面展示等情况。
|
|
|
|
|
model.addAttribute("userInformation", userInformation);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果用户已登录,将用户信息添加到Model中。
|
|
|
|
|
model.addAttribute("userInformation", userInformation);
|
|
|
|
|
}
|
|
|
|
|
List<ShopInformationBean> shopInformationBeans = new ArrayList<>();
|
|
|
|
|
String sortName;
|
|
|
|
|
// 遍历查询到的每个ShopInformation对象,进行相关信息的提取和封装到ShopInformationBean对象中。
|
|
|
|
|
for (ShopInformation shopInformation : shopInformations) {
|
|
|
|
|
int sort = shopInformation.getSort();
|
|
|
|
|
sortName = getSort(sort);
|
|
|
|
|
ShopInformationBean shopInformationBean = new ShopInformationBean();
|
|
|
|
|
shopInformationBean.setId(shopInformation.getId());
|
|
|
|
|
shopInformationBean.setName(shopInformation.getName());
|
|
|
|
|
shopInformationBean.setLevel(shopInformation.getLevel());
|
|
|
|
|
shopInformationBean.setRemark(shopInformation.getRemark());
|
|
|
|
|
shopInformationBean.setPrice(shopInformation.getPrice().doubleValue());
|
|
|
|
|
shopInformationBean.setQuantity(shopInformation.getQuantity());
|
|
|
|
|
shopInformationBean.setTransaction(shopInformation.getTransaction());
|
|
|
|
|
shopInformationBean.setSort(sortName);
|
|
|
|
|
shopInformationBean.setUid(shopInformation.getUid());
|
|
|
|
|
shopInformationBean.setImage(shopInformation.getImage());
|
|
|
|
|
shopInformationBeans.add(shopInformationBean);
|
|
|
|
|
}
|
|
|
|
|
// 将封装好的商品信息列表添加到Model中,以便视图展示。
|
|
|
|
|
model.addAttribute("shopInformationBean", shopInformationBeans);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return "redirect:mall_page.do";
|
|
|
|
|
}
|
|
|
|
|
return "page/mall_page";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理进入查看商品详情页面的请求方法。
|
|
|
|
|
* 根据传入的商品ID,获取商品详细信息以及相关的评论等上下文信息,进行处理和准备相关数据后,跳转到展示商品详情的页面。
|
|
|
|
|
*
|
|
|
|
|
* @param id 从请求中获取的要查看详情的商品ID参数。
|
|
|
|
|
* @param request HttpServletRequest对象,用于获取请求相关信息以及操作会话等,比如获取用户信息、设置会话属性等。
|
|
|
|
|
* @param model Spring MVC中的Model对象,用于向视图传递商品详情、用户信息、评论信息等数据。
|
|
|
|
|
* @return 返回一个视图名称,若获取商品信息等过程出现异常则重定向到根路径("/"),正常则返回"page/product_info"视图展示商品详情。
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/selectById.do")
|
|
|
|
|
public String selectById(@RequestParam int id,
|
|
|
|
|
HttpServletRequest request, Model model) {
|
|
|
|
|
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
|
|
|
|
|
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
|
|
|
|
|
userInformation = new UserInformation();
|
|
|
|
|
model.addAttribute("userInformation", userInformation);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// 调用ShopInformationService的方法,根据商品ID获取商品的详细信息,并添加到Model中。
|
|
|
|
|
ShopInformation shopInformation = shopInformationService.selectByPrimaryKey(id);
|
|
|
|
|
model.addAttribute("shopInformation", shopInformation);
|
|
|
|
|
// 调用ShopContextService的方法,根据商品ID获取该商品的相关评论等上下文信息列表。
|
|
|
|
|
List<ShopContext> shopContexts = shopContextService.selectById(id);
|
|
|
|
|
List<ShopContextBean> shopContextBeans = new ArrayList<>();
|
|
|
|
|
// 遍历每个评论上下文信息,进行相关信息的提取和封装到ShopContextBean对象中。
|
|
|
|
|
for (ShopContext s : shopContexts) {
|
|
|
|
|
ShopContextBean shopContextBean = new ShopContextBean();
|
|
|
|
|
UserInformation u = userInformationService.selectByPrimaryKey(s.getUid());
|
|
|
|
|
shopContextBean.setContext(s.getContext());
|
|
|
|
|
shopContextBean.setId(s.getId());
|
|
|
|
|
shopContextBean.setModified(s.getModified());
|
|
|
|
|
shopContextBean.setUid(u.getId());
|
|
|
|
|
shopContextBean.setUsername(u.getUsername());
|
|
|
|
|
shopContextBeans.add(shopContextBean);
|
|
|
|
|
}
|
|
|
|
|
String sort = getSort(shopInformation.getSort());
|
|
|
|
|
String goodsToken = TokenProccessor.getInstance().makeToken();
|
|
|
|
|
request.getSession().setAttribute("goodsToken", goodsToken);
|
|
|
|
|
model.addAttribute("token", goodsToken);
|
|
|
|
|
model.addAttribute("sort", sort);
|
|
|
|
|
model.addAttribute("userInformation", userInformation);
|
|
|
|
|
model.addAttribute("shopContextBeans", shopContextBeans);
|
|
|
|
|
return "page/product_info";
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return "redirect:/";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理进入求购商城页面的请求方法。
|
|
|
|
|
* 获取所有的求购商品信息列表,进行相关信息的处理和封装后添加到Model中,最后跳转到展示求购商城的页面。
|
|
|
|
|
*
|
|
|
|
|
* @param request HttpServletRequest对象,用于获取请求相关信息以及操作会话等,比如获取用户信息。
|
|
|
|
|
* @param model Spring MVC中的Model对象,用于向视图传递求购商品信息列表等数据。
|
|
|
|
|
* @return 返回一个视图名称,即"page/require_mall",用于展示求购商城页面。
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/require_mall.do")
|
|
|
|
|
public String requireMall(HttpServletRequest request, Model model) {
|
|
|
|
|
UserInformation userInformation = (UserInformation) request.getSession().getAttribute("userInformation");
|
|
|
|
|
if (StringUtils.getInstance().isNullOrEmpty(userInformation)) {
|
|
|
|
|
userInformation = new UserInformation();
|
|
|
|
|
model.addAttribute("userInformation", userInformation);
|
|
|
|
|
} else {
|
|
|
|
|
model.addAttribute("userInformation", userInformation);
|
|
|
|
|
}
|
|
|
|
|
// 调用UserWantService的方法,获取所有的求购商品信息列表。
|
|
|
|
|
List<UserWant> userWants = userWantService.selectAll();
|
|
|
|
|
List<UserWantBean> list = new ArrayList<>();
|
|
|
|
|
// 遍历每个求购商品信息,进行相关信息的提取和封装到UserWantBean对象中。
|
|
|
|
|
for (UserWant userWant : userWants) {
|
|
|
|
|
UserWantBean u = new UserWantBean();
|
|
|
|
|
u.setSort(getSort(userWant.getSort()));
|
|
|
|
|
u.setRemark(userWant.getRemark());
|
|
|
|
|
u.setQuantity(userWant.getQuantity());
|
|
|
|
|
u.setPrice(userWant.getPrice().doubleValue());
|
|
|
|
|
u.setUid(userWant.getUid());
|
|
|
|
|
u.setId(userWant.getId());
|
|
|
|
|
u.setModified(userWant.getModified());
|
|
|
|
|
u.setName(userWant.getName());
|
|
|
|
|
list.add(u);
|
|
|
|
|
}
|
|
|
|
|
model.addAttribute("list", list);
|
|
|
|
|
return "page/require_mall";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据商品ID查找商品详细信息的方法,该方法会直接返回商品信息对象,并且使用了@ResponseBody注解,意味着返回结果会直接作为响应体(通常是JSON格式等)返回给客户端,常用于提供数据接口。
|
|
|
|
|
*
|
|
|
|
|
* @param id 从请求中获取的要查找详情的商品ID参数。
|
|
|
|
|
* @return 返回ShopInformation对象,即对应商品的详细信息,作为响应体返回给客户端。
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/findShopById.do")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public ShopInformation findShopById(@RequestParam int id) {
|
|
|
|
|
return shopInformationService.selectByPrimaryKey(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据分类选择商品的方法,同样使用了@ResponseBody注解,会将查询到的符合分类条件的商品信息列表作为响应体返回给客户端,用于提供根据分类获取商品数据的接口。
|
|
|
|
|
*
|
|
|
|
|
* @param sort 从请求中获取的分类ID参数,用于筛选对应分类的商品。
|
|
|
|
|
* @return 返回List<ShopInformation>,即符合分类条件的商品信息列表,作为响应体返回给客户端。
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/selectBySort.do")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public List<ShopInformation> selectBySort(@RequestParam int sort) {
|
|
|
|
|
return shopInformationService.selectBySort(sort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 分页查询商品的方法,使用了@ResponseBody注解,根据传入的页码数进行分页查询,获取对应页的商品信息列表并作为响应体返回给客户端,用于提供分页获取商品数据的接口。
|
|
|
|
|
*
|
|
|
|
|
* @param counts 从请求中获取的页码数参数,用于确定要查询的商品数据页。
|
|
|
|
|
* @return 返回List<ShopInformation>,即对应页码的商品信息列表,作为响应体返回给客户端。
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping(value = "/selectByCounts.do")
|
|
|
|
|
@ResponseBody
|
|
|
|
|
public List<ShopInformation> selectByCounts(@RequestParam int counts) {
|
|
|
|
|
Map<String, Integer> map = new HashMap<>();
|
|
|
|
|
map.put("start", (counts - 1) * 12);
|
|
|
|
|
map.put("end", 12);
|
|
|
|
|
return shopInformationService.selectTen(map);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// //通过id查看商品详情
|
|
|
|
|
// @RequestMapping(value = "/showShop")
|
|
|
|
|
// public String showShop(@RequestParam int id, HttpServletRequest request, Model model) {
|
|
|
|
|
// ShopInformation shopInformation =
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据分类ID获取最详细的分类信息(第三层分类)的私有方法,用于内部辅助获取商品分类相关的详细信息,方便其他方法构建完整的分类名称等操作。
|
|
|
|
|
*
|
|
|
|
|
* @param sort 要获取的分类ID参数。
|
|
|
|
|
* @return 返回Specific对象,即对应分类ID的最详细分类信息。
|
|
|
|
|
*/
|
|
|
|
|
private Specific selectSpecificBySort(int sort) {
|
|
|
|
|
return specificeService.selectByPrimaryKey(sort);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据二级分类ID获取第二层分类信息的私有方法,同样用于辅助获取商品分类相关信息,与其他分类获取方法配合使用,构建完整的分类路径等。
|
|
|
|
|
*
|
|
|
|
|
* @param cid 要获取的二级分类ID参数。
|
|
|
|
|
* @return 返回Classification对象,即对应二级分类ID的第二层分类信息。
|
|
|
|
|
*/
|
|
|
|
|
private Classification selectClassificationByCid(int cid) {
|
|
|
|
|
return classificationService.selectByPrimaryKey(cid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据一级分类ID获取第一层分类信息的私有方法,也是用于辅助构建商品分类完整路径,方便获取和展示更详细准确的商品分类情况。
|
|
|
|
|
*
|
|
|
|
|
* @param aid 要获取的一级分类ID参数。
|
|
|
|
|
* @return 返回AllKinds对象,即对应一级分类ID的第一层分类信息。
|
|
|
|
|
*/
|
|
|
|
|
private AllKinds selectAllKindsByAid(int aid) {
|
|
|
|
|
return allKindsService.selectByPrimaryKey(aid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据商品分类ID构建完整分类名称的私有方法,通过依次获取一、二、三级分类信息,将各级分类名称拼接起来,形成一个完整的分类名称字符串,用于在页面展示商品分类情况等用途。
|
|
|
|
|
*
|
|
|
|
|
* @param sort 要构建分类名称的商品分类ID参数。
|
|
|
|
|
* @return 返回一个表示完整分类名称的字符串,格式为"一级分类名-二级分类名-三级分类名"。
|
|
|
|
|
*/
|
|
|
|
|
private String getSort(int sort) {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
Specific specific = selectSpecificBySort(sort);
|
|
|
|
|
int cid = specific.getCid();
|
|
|
|
|
Classification classification = selectClassificationByCid(cid);
|
|
|
|
|
int aid = classification.getAid();
|
|
|
|
|
AllKinds allKinds = selectAllKindsByAid(aid);
|
|
|
|
|
String allName = allKinds.getName();
|
|
|
|
|
sb.append(allName);
|
|
|
|
|
sb.append("-");
|
|
|
|
|
sb.append(classification.getName());
|
|
|
|
|
sb.append("-");
|
|
|
|
|
sb.append(specific.getName());
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|