项目二06

1.攻略排行

1.1.数据结构



1.2.分时统计表


1.3.实现

前端攻略统计请求

//海外攻略推荐
        ajaxGet("/strategies/rank", {type:1}, function (data) {
            _this.abroadCds = data.data;
        })


        //国内攻略推荐
        ajaxGet("/strategies/rank", {type:2}, function (data) {
            _this.chinaCds = data.data;
        })


        //热门攻略推荐
        ajaxGet("/strategies/rank", {type:3}, function (data) {
            _this.hotCds = data.data;
        })


前端攻略接口

@GetMapping("/rank")
    public Object rank(int type) {
        List<StrategyRank> strategies = strategyRankService.queryByType(type);
        return JsonResult.success(strategies);
    }


SQL

前端攻略实现

@Override
    public List<StrategyRank> queryByType(int type) {
        QueryWrapper<StrategyRank> wrapper = new QueryWrapper<>();
        //select * from strategy_rank
        //where statis_time in(select max(statis_time) from strategy_rank where type =3)
        //and type = 3
        //ORDER BY statisnum desc limit 10

        wrapper.select("dest_id", "dest_name", "strategy_id", "strategy_title");
        wrapper.inSql("statis_time", "select max(statis_time) from strategy_rank where type =" + type + "");
        wrapper.eq("type", type);
        wrapper.orderByDesc("statisnum");
        wrapper.last("limit 10");

        return super.list(wrapper);
    }

1.4.维护

定时器

@Component
public class StrategyRankJob {

    //启动类需要贴这个注解@EnableScheduling
    @Autowired
    private IStrategyRankService strategyRankService;

    @Scheduled(cron = "0/5 * * * * *")
    public void insert(){
        strategyRankService.dataInsert(StrategyRank.TYPE_ABROAD);
        strategyRankService.dataInsert(StrategyRank.TYPE_CHINA);
        strategyRankService.dataInsert(StrategyRank.TYPE_HOT);
    }
}
//-----------------dataInsert方法
@Override
    public void dataInsert(int type) {
        if (type == StrategyRank.TYPE_HOT) {
            QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
            List<StrategyRank> strategyRanks = new ArrayList<>();
            wrapper.orderByDesc("viewnum + replynum");
            wrapper.last("limit 10");

            List<Strategy> strategies = strategyService.list(wrapper);

            Date date = new Date();

            for (Strategy strategy : strategies) {
                StrategyRank strategyRank = new StrategyRank();
                strategyRank.setDestId(strategy.getDestId());
                strategyRank.setDestName(strategy.getDestName());

                strategyRank.setStrategyId(strategy.getId());
                strategyRank.setStrategyTitle(strategy.getTitle());
                strategyRank.setStatisTime(date);

                //设置类型
                strategyRank.setType(type);
                strategyRank.setStatisnum(strategy.getViewnum() + strategy.getReplynum() + 0L);

                strategyRanks.add(strategyRank);
            }

            saveBatch(strategyRanks);
        }
        else if (type == StrategyRank.TYPE_ABROAD) {
            QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
            List<StrategyRank> strategyRanks = new ArrayList<>();
            wrapper.orderByDesc("thumbsupnum + favornum");
            wrapper.eq("isabroad", 1);
            wrapper.last("limit 10");

            List<Strategy> strategies = strategyService.list(wrapper);

            Date date = new Date();

            for (Strategy strategy : strategies) {
                StrategyRank strategyRank = new StrategyRank();
                strategyRank.setDestId(strategy.getDestId());
                strategyRank.setDestName(strategy.getDestName());

                strategyRank.setStrategyId(strategy.getId());
                strategyRank.setStrategyTitle(strategy.getTitle());
                strategyRank.setStatisTime(date);

                //设置类型
                strategyRank.setType(type);
                strategyRank.setStatisnum(strategy.getThumbsupnum() + strategy.getFavornum() + 0L);

                strategyRanks.add(strategyRank);
            }
            saveBatch(strategyRanks);
        }
        else if(type == StrategyRank.TYPE_CHINA){
            QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
            List<StrategyRank> strategyRanks = new ArrayList<>();
            wrapper.orderByDesc("thumbsupnum + favornum");
            wrapper.eq("isabroad", 0);
            wrapper.last("limit 10");

            List<Strategy> strategies = strategyService.list(wrapper);

            Date date = new Date();

            for (Strategy strategy : strategies) {
                StrategyRank strategyRank = new StrategyRank();
                strategyRank.setDestId(strategy.getDestId());
                strategyRank.setDestName(strategy.getDestName());

                strategyRank.setStrategyId(strategy.getId());
                strategyRank.setStrategyTitle(strategy.getTitle());
                strategyRank.setStatisTime(date);

                //设置类型
                strategyRank.setType(type);
                strategyRank.setStatisnum(strategy.getThumbsupnum() + strategy.getFavornum() + 0L);

                strategyRanks.add(strategyRank);
            }
            saveBatch(strategyRanks);
        }
    }

或者这个

2.攻略统计

2.1.数据结构

2.2.实现

攻略统计请求

ajaxGet("/strategies/condition", {type:1}, function (data) {
            _this.abroads = data.data;
        })

        ajaxGet("/strategies/condition", {type:2}, function (data) {
            _this.chinas = data.data;
        })
        ajaxGet("/strategies/condition", {type:3}, function (data) {
            _this.themes = data.data;
        })

前端接口

    @GetMapping("/condition")
    public Object condition(int type) {
        List<StrategyCondition> strategies = strategyConditionService.queryByType(type);
        return JsonResult.success(strategies);
    }

接口实现

    @Override
    public List<StrategyCondition> queryByType(int type) {
        QueryWrapper<StrategyCondition> wrapper = new QueryWrapper<>();

        wrapper.inSql("statis_time", "select max(statis_time) from strategy_condition where type =" + type + "");
        wrapper.eq("type", type);
        wrapper.orderByDesc("count");

        return super.list(wrapper);
    }

2.3.维护

定时器

@Component
public class StrategyConditionJob {

    @Autowired
    private IStrategyConditionService strategyConditionService;

    @Scheduled(cron = "0/5 * * * * *")
    public void insert(){
        strategyConditionService.dataInsert(StrategyCondition.TYPE_ABROAD);
        strategyConditionService.dataInsert(StrategyCondition.TYPE_CHINA);
        strategyConditionService.dataInsert(StrategyCondition.TYPE_THEME);

    }
}


//dataInsert方法
@Override
    public void dataInsert(int type) {
        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        if(type == StrategyCondition.TYPE_THEME){
            wrapper.select("theme_id refid,theme_name name,count(id) count");
            wrapper.groupBy("theme_id,theme_name");
        }else{
            wrapper.select("dest_id refid,dest_name name,count(id) count");
            wrapper.eq("isabroad",StrategyCondition.TYPE_ABROAD==type?1:0);
            wrapper.groupBy("dest_id,dest_name");
        }
        wrapper.orderByDesc("count");

        List<Map<String, Object>> maps = strategyService.listMaps(wrapper);

        List<StrategyCondition> conditions = new ArrayList<>();

        Date date = new Date();

        for (Map<String, Object> map : maps) {
            StrategyCondition strategyCondition = new StrategyCondition();

            strategyCondition.setCount(Integer.parseInt(map.get("count").toString()));
            strategyCondition.setName(map.get("name").toString());
            strategyCondition.setRefid(Long.parseLong(map.get("refid").toString()));
            strategyCondition.setStatisTime(date);
            strategyCondition.setType(type);

            conditions.add(strategyCondition);

        }

        saveBatch(conditions);
    }

3.攻略变化

实体类

@Setter
@Getter
public class StrategyQuery extends  QueryObject{
    private Long destId;
    private Long themeId;

    private Integer type;
    private Long refid;
    private String orderBy;
}

前端接口实现

//-------------攻略分页实现
@Override
    public IPage<Strategy> queryPage(StrategyQuery qo) {
        IPage<Strategy> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        QueryWrapper<Strategy> wrapper = Wrappers.<Strategy>query();
        wrapper.eq(qo.getDestId()!=null,"dest_id",qo.getDestId());
        wrapper.eq(qo.getThemeId()!=null,"theme_id",qo.getThemeId());

        if(qo.getType() != null){
            if(qo.getType() == StrategyCondition.TYPE_ABROAD || qo.getType() == StrategyCondition.TYPE_CHINA){
                wrapper.eq("dest_id",qo.getRefid());
            }else if(qo.getType() == StrategyCondition.TYPE_THEME){
                wrapper.eq("theme_id",qo.getRefid());
            }
        }

        //if(qo.getOrderBy()!=null){
        //    wrapper.orderByDesc("or")
        //
        //}
        wrapper.orderByDesc(StringUtils.hasText(qo.getOrderBy()),qo.getOrderBy());

        return super.page(page, wrapper);
    }


返回课程体系

posted @ 2021-07-09 08:52  LinkYup  阅读(35)  评论(0编辑  收藏  举报