基于Java的厦门旅游电子商务预订系统(Vue.js+SpringBoot)-五、核心代码

5.1 新增景点类型

@RequestMapping(value = "/insert", method = RequestMethod.POST)
@ApiOperation(value = "新增景点类型")
public Result<ScenicSpotType> insert(ScenicSpotType scenicSpotType){
    if(scenicSpotType.getSortOrder() == null || Objects.equals(0,scenicSpotType.getSortOrder().compareTo(BigDecimal.ZERO))) {
        scenicSpotType.setSortOrder(BigDecimal.valueOf(iScenicSpotTypeService.count() + 1));
    }
    iScenicSpotTypeService.saveOrUpdate(scenicSpotType);
    return new ResultUtil<ScenicSpotType>().setData(scenicSpotType);
}

5.2 查询推荐的景点

@RequestMapping(value = "/getRecommendList2OnWeb", method = RequestMethod.GET)
@ApiOperation(value = "查询推荐的景点")
public Result<List<ScenicSpot>> getRecommendList2(){
    List<ScenicSpot> spotList = iScenicSpotService.list();
    int[] arr = new int[spotList.size()];
    for(int i = 1; i < spotList.size(); i ++) {
        arr[i - 1] = i;
    }
    int[] ints = selectM(arr, 10);
    List<ScenicSpot> ans = new ArrayList<>();
    for (int i : ints) {
        ans.add(spotList.get(i));
    }
    return new ResultUtil<List<ScenicSpot>>().setData(ans);
}

public static int[] selectM(int[] arr,int m){
    int len=arr.length;
    if(m>arr.length) {
        throw new RuntimeException("xxxxx");
    }
    int[] res=new int[m];
    for(int i=0;i<m;i++){
        int randomIndex=len-1-new Random().nextInt(len-i);
        res[i]=arr[randomIndex];
        int tmp=arr[randomIndex];
        arr[randomIndex]=arr[i];
        arr[i]=tmp;
    }
    return res;
}

5.3 查询单个景点

@RequestMapping(value = "/getOneOnWeb", method = RequestMethod.GET)
@ApiOperation(value = "查询单个景点")
public Result<ScenicSpot> getOneOnWeb(@RequestParam String id){
    ScenicSpot vo = iScenicSpotService.getById(id);
    if(vo == null) {
        return ResultUtil.error("景点不存在");
    }
    // 评分
    BigDecimal evaluateSum = BigDecimal.ZERO;
    QueryWrapper<Evaluate> evalQw = new QueryWrapper<>();
    evalQw.eq("spot_id",vo.getId());
    List<Evaluate> evaluateList = iEvaluateService.list(evalQw);
    for (Evaluate evaluate : evaluateList) {
        evaluateSum = evaluateSum.add(evaluate.getLevel());
    }
    if(evaluateList.size() > 0) {
        vo.setStar(evaluateSum.divide(BigDecimal.valueOf(evaluateList.size()),2, RoundingMode.DOWN));
    } else {
        vo.setStar(BigDecimal.valueOf(-1));
    }
    // 收藏
    QueryWrapper<Collection> coQw = new QueryWrapper<>();
    coQw.eq("spot_id",vo.getId());
    vo.setCollection(iCollectionService.count(coQw));
    String viewStr = redisTemplate.get("SPOT_VIEW:" + vo.getId());
    if(!ZwzNullUtils.isNull(viewStr)) {
        try {
            long viewNumber = Long.parseLong(viewStr);
            redisTemplate.set("SPOT_VIEW:" + vo.getId(),(viewNumber + 1) + "");
        } catch (Exception e) {}
    } else {
        redisTemplate.set("SPOT_VIEW:" + vo.getId(), "1");
    }
    return new ResultUtil<ScenicSpot>().setData(vo);
}

5.4 查询评价前10的景点

@RequestMapping(value = "/getTopEvaluate", method = RequestMethod.GET)
@ApiOperation(value = "查询评价前10的景点")
public Result<List<ScenicSpot>> getTopEvaluate(){
    List<ScenicSpot> spotList = iScenicSpotService.list();
    for (ScenicSpot vo : spotList) {
        BigDecimal evaluateSum = BigDecimal.ZERO;
        QueryWrapper<Evaluate> evalQw = new QueryWrapper<>();
        evalQw.eq("spot_id",vo.getId());
        List<Evaluate> evaluateList = iEvaluateService.list(evalQw);
        for (Evaluate evaluate : evaluateList) {
            evaluateSum = evaluateSum.add(evaluate.getLevel());
        }
        if(evaluateList.size() > 0) {
            vo.setStar(evaluateSum.divide(BigDecimal.valueOf(evaluateList.size()),2, RoundingMode.DOWN));
        } else {
            vo.setStar(BigDecimal.ZERO);
        }
    }
    Collections.sort(spotList, new Comparator<ScenicSpot>() {
        @Override
        public int compare(ScenicSpot o1, ScenicSpot o2) {
            return o2.getStar().compareTo(o1.getStar());
        }
    });
    if(spotList.size() > 10) {
        spotList = spotList.subList(0,10);
    }
    return new ResultUtil<List<ScenicSpot>>().setData(spotList);
}

5.5 新增景点评价

@RequestMapping(value = "/addEvaluate", method = RequestMethod.GET)
@ApiOperation(value = "新增评价")
public Result<Evaluate> addEvaluate(@RequestParam String id, @RequestParam BigDecimal level, @RequestParam String message){
    ScenicSpot ss = iScenicSpotService.getById(id);
    if(ss == null) {
        return ResultUtil.error("景点不存在");
    }
    User currUser = securityUtil.getCurrUser();
    QueryWrapper<Evaluate> qw = new QueryWrapper<>();
    qw.eq("spot_id",ss.getId());
    qw.eq("user_id",currUser.getId());
    qw.last("limit 1");
    Evaluate evaluate = iEvaluateService.getOne(qw);
    if(evaluate == null) {
        evaluate = new Evaluate();
        evaluate.setSpotId(ss.getId());
        evaluate.setSpotName(ss.getTitle());
        evaluate.setUserId(currUser.getId());
        evaluate.setUserName(currUser.getNickname());
    }
    evaluate.setLevel(level);
    evaluate.setMessage(message);
    evaluate.setTime(DateUtil.now());
    iEvaluateService.saveOrUpdate(evaluate);
    return ResultUtil.success();
}

上一篇:AI系统性学习—LangChain入门


下一篇:基于arm的ubuntu上运行qgc