土地项目使用到的注解

该代码段展示了如何在Java中使用枚举定义地块获取方式,如系统赠送和分享获取等。在事务控制下,通过用户ID获取用户信息并进行地块分配,检查免费次数,更新数据库记录,并发送消息通知。同时,包含了一个弃用的随机地块生成方法。

一、枚举

/**
 * 地块获得方式
 */
@AllArgsConstructor
@Getter
public enum LandObtainWayEnum {
    /** 系统赠送 **/
    FREE,
    /** 分享获取 **/
    SHARE,
    /** 购买获取 **/
    BUY,
    /** 替换获取 **/
    REPLACE,
    /** 好友赠送 **/
    GIVE
}

二、事务  锁

 @Transactional(rollbackFor = Exception.class)
    @Override
    public LandMinVo freeLand(String areaCode) {
        Long curUserId = SecurityUtils.getUserId();
        RLock lock = LockUtil.lock(String.valueOf(curUserId));
        LandMinVo freeLand;
        try {
            TbUserExt userExt = userExtMapper.selectById(curUserId);
            if (null == userExt) {
                throw new ServiceException("用户不存在");
            }
            if (userExt.getFreeNum() < 1) {
                throw new ServiceException("免费获取次数已用完");
            }
            CityInfo cityInfo = cityInfoService.getCityInfo(areaCode);
            freeLand = randomLandV2(cityInfo.getCityCode(), curUserId);
            TbLand land = new TbLand();
            land.setAreaCode(freeLand.getAreaCode());
            land.setLandNumber(freeLand.getLandCode());
            land.setLandHash(freeLand.getLandHash());
            land.setLandLat(freeLand.getLandLat());
            land.setLandLng(freeLand.getLandLng());
            land.setUserId(curUserId);
            if (userExt.getFreeNum() - userExt.getShareNum() > 0) {
                land.setObtainWay(LandObtainWayEnum.FREE.ordinal());
            } else {
                land.setObtainWay(LandObtainWayEnum.SHARE.ordinal());
            }
            baseMapper.insert(land);
            //土地数增加
            userExtMapper.incNum(curUserId, "land_num");
            //免费数减少
            userExtMapper.decNum(curUserId, "free_num");
            //粉丝收到消息
            msgService.sendGetLandMsg(curUserId, land.getId(), freeLand.getProvName() + freeLand.getCityName());
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        return freeLand;
    }
​

三、弃用的方法

 /***
     * 获取随机地块(根据城市中心点坐标)
     * @param areaCode
     * @param userId
     * @return
     */
    @Deprecated
    private LandMinVo randomLand(String areaCode, Long userId) {
        List<TbCityInfo> cityInfos = cityInfoService.list(new LambdaQueryWrapper<TbCityInfo>().ne(TbCityInfo::getAreaCode, areaCode).eq(TbCityInfo::getLevel, CityLevelEnum.CITY.ordinal()));
        int index = RandomUtil.getRandom().nextInt(cityInfos.size());
        TbCityInfo cityInfo = cityInfos.get(index);
        TbCityInfo parentInfo = cityInfoService.getById(cityInfo.getParentCode());
        Integer cityMax = Convert.toInt(configService.selectConfigByKey(CacheConstants.CITY_LAND_LIMIT), -1);
        double lat;
        double lng;
        String geoHash;
        do {
            int[] geo = RandomUtil.getRandom().ints(0, 1000).limit(2).toArray();
            String latPre = BigDecimal.valueOf(cityInfo.getCenterLat()).setScale(1, RoundingMode.DOWN).toPlainString();
            String lngPre = BigDecimal.valueOf(cityInfo.getCenterLng()).setScale(1, RoundingMode.DOWN).toPlainString();
            lat = Double.valueOf(latPre + (10 > geo[0] ? "00" + geo[0] : 100 > geo[0] ? "0" + geo[0] : geo[0]));
            lng = Double.valueOf(lngPre + (10 > geo[1] ? "00" + geo[1] : 100 > geo[1] ? "0" + geo[1] : geo[1]));
            geoHash = GeoHashUtils.encode(lat, lng);

        } while (0 < baseMapper.selectCount(new LambdaQueryWrapper<TbLand>().eq(TbLand::getLandNumber, geoHash))
                || (-1 < cityMax && cityMax <= baseMapper.selectCount(new LambdaQueryWrapper<TbLand>().eq(TbLand::getUserId, userId).eq(TbLand::getAreaCode, areaCode))));
        LandMinVo minVo = new LandMinVo();
        minVo.setLandCode(geoHash);
        minVo.setLandLat(lat);
        minVo.setLandLng(lng);
        minVo.setAreaCode(cityInfo.getAreaCode());
        minVo.setCityName(cityInfo.getAreaName());
        minVo.setProvName(parentInfo.getAreaName());
        return minVo;
    }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值