package com.smallbell.business.rest;
import com.smallbell.business.model.domain.Product;
import com.smallbell.business.model.recom.Recommendation;
import com.smallbell.business.model.request.*;
import com.smallbell.business.service.*;
import com.smallbell.business.model.domain.User;
import com.smallbell.business.model.request.*;
import com.smallbell.business.service.ProductService;
import com.smallbell.business.service.RatingService;
import com.smallbell.business.service.RecommenderService;
import com.smallbell.business.service.UserService;
import com.smallbell.business.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.apache.log4j.Logger;
import java.util.List;
@RequestMapping("/rest/product")
@Controller
public class ProductRestApi {
private static Logger logger = Logger.getLogger(ProductRestApi.class.getName());
@Autowired
private RecommenderService recommenderService;
@Autowired
private ProductService productService;
@Autowired
private UserService userService;
@Autowired
private RatingService ratingService;
/**
* 获取热门推荐
* @param model
* @return
*/
@RequestMapping(value = "/hot", produces = "application/json", method = RequestMethod.GET )
@ResponseBody
public Model getHotProducts(@RequestParam("num")int num, Model model) {
List<Recommendation> recommendations = recommenderService.getHotRecommendations(new HotRecommendationRequest(num));
model.addAttribute("success",true);
model.addAttribute("products", productService.getRecommendProducts(recommendations));
return model;
}
/**
* 获取打分最多的商品
* @param model
* @return
*/
@RequestMapping(value = "/rate", produces = "application/json", method = RequestMethod.GET )
@ResponseBody
public Model getRateMoreProducts(@RequestParam("num")int num, Model model) {
List<Recommendation> recommendations = recommenderService.getRateMoreRecommendations(new RateMoreRecommendationRequest(num));
model.addAttribute("success",true);
model.addAttribute("products", productService.getRecommendProducts(recommendations));
return model;
}
// 基于物品的协同过滤
@RequestMapping(value = "/itemcf/{id}", produces = "application/json", method = RequestMethod.GET)
@ResponseBody
public Model getItemCFProducts(@PathVariable("id")int id, Model model) {
List<Recommendation> recommendations = recommenderService.getItemCFRecommendations(new ItemCFRecommendationRequest(id));
model.addAttribute("success", true);
model.addAttribute("products", productService.getRecommendProducts(recommendations));
return model;
}
// 基于内容的推荐
@RequestMapping(value = "/contentbased/{id}", produces = "application/json", method = RequestMethod.GET)
@ResponseBody
public Model getContentBasedProducts(@PathVariable("id")int id, Model model) {
List<Recommendation> recommendations = recommenderService.getContentBasedRecommendations(new ContentBasedRecommendationRequest(id));
model.addAttribute("success", true);
model.addAttribute("products", productService.getRecommendProducts(recommendations));
return model;
}
/**
* 获取单个商品的信息
* @param id
* @param model
* @return
*/
@RequestMapping(value = "/info/{id}", produces = "application/json", method = RequestMethod.GET )
@ResponseBody
public Model getProductInfo(@PathVariable("id")int id, Model model) {
model.addAttribute("success",true);
model.addAttribute("product", productService.findByProductId(id));
return model;
}
/**
* 模糊查询商品
* @param query
* @param model
* @return
*/
@RequestMapping(value = "/search", produces = "application/json", method = RequestMethod.GET )
@ResponseBody
public Model getSearchProducts(@RequestParam("query")String query, Model model) {
try {
query = new String(query.getBytes("ISO-8859-1"),"UTF-8");
} catch(java.io.UnsupportedEncodingException e) {
e.printStackTrace();
}
List<Product> products = productService.findByProductName(query);
model.addAttribute("success",true);
model.addAttribute("products", products);
return model;
}
@RequestMapping(value = "/rate/{id}", produces = "application/json", method = RequestMethod.GET )
@ResponseBody
public Model rateToProduct(@PathVariable("id")int id, @RequestParam("score")Double score, @RequestParam("username")String username, Model model) {
User user = userService.findByUsername(username);
ProductRatingRequest request = new ProductRatingRequest(user.getUserId(), id, score);
boolean complete = ratingService.productRating(request);
//埋点日志
if(complete) {
System.out.print("=========埋点=========");
logger.info(Constant.PRODUCT_RATING_PREFIX + ":" + user.getUserId() +"|"+ id +"|"+ request.getScore() +"|"+ System.currentTimeMillis()/1000);
}
model.addAttribute("success",true);
model.addAttribute("message"," 已完成评分!");
return model;
}
// 离线推荐
@RequestMapping(value = "/offline", produces = "application/json", method = RequestMethod.GET )
@ResponseBody
public Model getOfflineProducts(@RequestParam("username")String username,@RequestParam("num")int num, Model model) {
User user = userService.findByUsername(username);
List<Recommendation> recommendations = recommenderService.getCollaborativeFilteringRecommendations(new UserRecommendationRequest(user.getUserId(), num));
model.addAttribute("success",true);
model.addAttribute("products", productService.getRecommendProducts(recommendations));
return model;
}
// 实时推荐
@RequestMapping(value = "/stream", produces = "application/json", method = RequestMethod.GET )
@ResponseBody
public Model getStreamProducts(@RequestParam("username")String username,@RequestParam("num")int num, Model model) {
User user = userService.findByUsername(username);
List<Recommendation> recommendations = recommenderService.getStreamRecommendations(new UserRecommendationRequest(user.getUserId(), num));
model.addAttribute("success",true);
model.addAttribute("products", productService.getRecommendProducts(recommendations));
return model;
}
}
土豆片片
- 粉丝: 1856
- 资源: 5869
最新资源
- LDPC编码解码matlab代码和Verilog代码及资料.zip
- LSTM的matlab实现.zip
- LSTMMATLAB是MATLAB中的长短期记忆LSTM,这意味着简洁的说明性,仅用于研究目的。它附有一篇论文供参考,.zip
- Lloyd N Trefethen和David Bau III 1997年自己的数值线性代数练习和MATLAB示例代码.zip
- LTspice数据导入到MATLAB中.zip
- LSTM预测时间序列数据jean销售数据集使用matlab.zip
- MATBOX是一个开源的MATLAB工具箱,专门用于多孔非均质材料的微观结构分析.zip
- MATLAB Live Task for Python使您能够直接在MATLAB Live Script中编写和执行P.zip
- MATLAB LMPC实现的双积分器系统.zip
- matlab mex函数的集合和开发工具包,用于OpenCV库.zip
- Matlab Octave的SOFA工具箱API.zip
- Matlab_3D图像查看器与ROI工具的Matlab NIFTI查看器手动分割.zip
- Matlab Octave中的轻量级多变量模式分析MVPA工具箱.zip
- Matlab_3GPP第15版新无线电LDPC码编码器和解码器的Matlab仿真.zip
- Matlab_3GPP第15版新无线电极码的编码器和SCL解码器的Matlab仿真.zip
- Matlab_ADMM部分的MatlabPython代码,我的论文交替优化约束问题,对抗网络和鲁棒模型.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈