springboot 定时任务(每天凌晨0点更新用户的称号)

在android项目中要使用到定时调用接口来更新数据库中的战力称号...(类似王者荣耀的称号)

/**
 * @author sun weikai
 */
@Component
@Configuration      //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SetTitleScheduled {
    @Resource
    UserMapper userMapper;
    @Resource
    RegionMapper regionMapper;
    @Resource
    IRegionService regionService;
    //3.添加定时任务,每天凌晨0点更新一次
    @Scheduled(cron = "0 0 0 * * ?")
    //或直接指定时间间隔,例如:5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() throws Exception {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
        //刷新每个用户的战区,对数据库里的每个战区排名
        //将所有用户的称号初始化为“无”
        List userList = userMapper.selectList(
                Wrappers.lambdaQuery(User.class)
                        .isNotNull(User::getUid)
        );
        //初始化称号为无
        for (User user:userList){
            //初始化更新构造器
            UpdateWrapper updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid",user.getUid());
            user.setTitle("无");
            //进行更新操作
            userMapper.update(user,updateWrapper);
        }
        //然后从县级开始对排行榜列表的前一百名分发称号(当前返回的是所有,待改进)
        List regions = regionMapper.selectList(
                Wrappers.lambdaQuery(Region.class)
                        .isNotNull(Region::getRid)
        );
        for (Region region:regions){
            ViewRegionListRequest viewRegionListRequest = new ViewRegionListRequest();
            viewRegionListRequest.setProvince(region.getProvince());
            viewRegionListRequest.setCity(region.getCity());
            viewRegionListRequest.setCounty(region.getCounty());
            viewRegionListRequest.setPhone("15031806111");
            viewRegionListRequest.setFlag(1);
            //查询县级排行榜
            List userListInfos =regionService.viewRegionList(viewRegionListRequest);
            for (UserListInfo userListInfo:userListInfos){
                User user0 = userMapper.selectOne(
                        Wrappers.lambdaQuery(User.class)
                                .eq(User::getPhone,userListInfo.getPhone())
                );
                //找到这个用户的排名No
                int No = userListInfos.indexOf(userListInfo)+1;
                //将数字转换为中文数字
                String ChineseNo = Int2chineseNum.int2chineseNum(No);
                //找到这个用户,然后修改其的称号
                UpdateWrapper updateWrapper0 = new UpdateWrapper<>();
                updateWrapper0.eq("uid",user0.getUid());
                //如果省 和 市的名字相同,则省略掉一个    天津市北辰区第一
                if (viewRegionListRequest.getProvince().equals(viewRegionListRequest.getCity())){
                    String title1 = viewRegionListRequest.getProvince()
                            +viewRegionListRequest.getCounty()
                            +"第"+ChineseNo;
                    user0.setTitle(title1);
                    //在这里编写一个判断那个称号更强的比较工具函数
                    if (CompareTitle.CompareTitle(title1,user0.getHistoricalTitle())){
                        //更新历史称号
                        user0.setHistoricalTitle(title1);
                    }

                }else {
                    String title2 = viewRegionListRequest.getProvince()
                            +viewRegionListRequest.getCity()
                            +viewRegionListRequest.getCounty()
                            +"第"+ChineseNo;
                    user0.setTitle(title2);
                    //更新下历史最高,前一个称号高则返回true
                    if (CompareTitle.CompareTitle(title2,user0.getHistoricalTitle())){
                        //更新历史称号
                        user0.setHistoricalTitle(title2);
                    }
                }
                //进行更新操作
                userMapper.update(user0,updateWrapper0);
            }
        }
        //市级排行榜
        for (Region region:regions){
            ViewRegionListRequest viewRegionListRequest = new ViewRegionListRequest();
            viewRegionListRequest.setProvince(region.getProvince());
            viewRegionListRequest.setCity(region.getCity());
            viewRegionListRequest.setCounty(region.getCounty());
            viewRegionListRequest.setPhone("15031806111");
            viewRegionListRequest.setFlag(2);
            //查询县级排行榜
            List userListInfos =regionService.viewRegionList(viewRegionListRequest);
            for (UserListInfo userListInfo:userListInfos){
                User user0 = userMapper.selectOne(
                        Wrappers.lambdaQuery(User.class)
                                .eq(User::getPhone,userListInfo.getPhone())
                );
                //找到这个用户的排名No
                int No = userListInfos.indexOf(userListInfo)+1;
                //将数字转换为中文数字
                String ChineseNo = Int2chineseNum.int2chineseNum(No);
                //找到这个用户,然后修改其的称号
                UpdateWrapper updateWrapper0 = new UpdateWrapper<>();
                updateWrapper0.eq("uid",user0.getUid());
                //如果省 和 市的名字相同,则省略掉一个    天津市北辰区第一
                if (viewRegionListRequest.getProvince().equals(viewRegionListRequest.getCity())){
                    String title1 = viewRegionListRequest.getProvince()
                            +"第"+ChineseNo;
                    user0.setTitle(title1);
                    if (CompareTitle.CompareTitle(title1,user0.getHistoricalTitle())){
                        //更新历史称号
                        user0.setHistoricalTitle(title1);
                    }
                }else {
                    String title2 = viewRegionListRequest.getProvince()
                            +viewRegionListRequest.getCity()
                            +"第"+ChineseNo;
                    user0.setTitle(title2);
                    if (CompareTitle.CompareTitle(title2,user0.getHistoricalTitle())){
                        //更新历史称号
                        user0.setHistoricalTitle(title2);
                    }
                }
                //进行更新操作
                userMapper.update(user0,updateWrapper0);
            }
        }
        //省级
        for (Region region:regions){
            ViewRegionListRequest viewRegionListRequest = new ViewRegionListRequest();
            viewRegionListRequest.setProvince(region.getProvince());
            viewRegionListRequest.setCity(region.getCity());
            viewRegionListRequest.setCounty(region.getCounty());
            viewRegionListRequest.setPhone("15031806111");
            viewRegionListRequest.setFlag(3);
            //查询县级排行榜
            List userListInfos =regionService.viewRegionList(viewRegionListRequest);
            for (UserListInfo userListInfo:userListInfos){
                User user0 = userMapper.selectOne(
                        Wrappers.lambdaQuery(User.class)
                                .eq(User::getPhone,userListInfo.getPhone())
                );
                //找到这个用户的排名No
                int No = userListInfos.indexOf(userListInfo)+1;
                //将数字转换为中文数字
                String ChineseNo = Int2chineseNum.int2chineseNum(No);
                //找到这个用户,然后修改其的称号
                UpdateWrapper updateWrapper0 = new UpdateWrapper<>();
                updateWrapper0.eq("uid",user0.getUid());
                //如果省 和 市的名字相同,则省略掉一个    天津市北辰区第一
                String title1 = viewRegionListRequest.getProvince()
                        +"第"+ChineseNo;
                user0.setTitle(title1);
                if (CompareTitle.CompareTitle(title1,user0.getHistoricalTitle())){
                    //更新历史称号
                    user0.setHistoricalTitle(title1);
                }
                //进行更新操作
                userMapper.update(user0,updateWrapper0);
            }
        }

    }
}

 

你可能感兴趣的:(SpringBoot)