package cc.mrbird.febs.mall.service.impl; import cc.mrbird.febs.common.entity.FebsResponse; import cc.mrbird.febs.common.enumerates.DataDictionaryEnum; import cc.mrbird.febs.common.enumerates.FlowTypeEnum; import cc.mrbird.febs.common.enumerates.MoneyFlowTypeNewEnum; import cc.mrbird.febs.common.exception.FebsException; import cc.mrbird.febs.common.utils.*; import cc.mrbird.febs.mall.conversion.CjInfoConversion; import cc.mrbird.febs.mall.conversion.TzInfoConversion; import cc.mrbird.febs.mall.dto.ApiCreateDto; import cc.mrbird.febs.mall.dto.ApiCreateItemDto; import cc.mrbird.febs.mall.dto.KjRecordDto; import cc.mrbird.febs.mall.dto.OpenPrice; import cc.mrbird.febs.mall.entity.*; import cc.mrbird.febs.mall.mapper.*; import cc.mrbird.febs.mall.service.AsyncCjService; import cc.mrbird.febs.mall.service.CJService; import cc.mrbird.febs.mall.service.IApiMallMemberService; import cc.mrbird.febs.mall.vo.ApiKjInfoVo; import cc.mrbird.febs.mall.vo.ApiKjRecordVo; import cc.mrbird.febs.mall.vo.ApiTzInfoVo; import cc.mrbird.febs.mall.vo.ApiTzListVo; import cc.mrbird.febs.rabbit.producter.AgentProducer; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSON; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.text.ParseException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @Slf4j @Service @RequiredArgsConstructor public class CJServiceImpl extends ServiceImpl implements CJService { private final KjRecordMapper kjRecordMapper; private final RedisUtils redisUtils; private final DataDictionaryCustomMapper dataDictionaryCustomMapper; private final AgentProducer agentProducer; private final TzInfoMapper tzInfoMapper; private final MallMemberWalletMapper mallMemberWalletMapper; private final CjInfoMapper cjInfoMapper; private final CjItemMapper cjItemMapper; private final IApiMallMemberService mallMemberService; private final AsyncCjService asyncCjService; @Override public FebsResponse kjRecord(KjRecordDto kjRecordDto) { IPage page = new Page<>(kjRecordDto.getPageNum(), kjRecordDto.getPageSize()); IPage pages = kjRecordMapper.selectListInPage(page, kjRecordDto); List records = pages.getRecords(); if(CollUtil.isNotEmpty(records)){ for(ApiKjRecordVo apiKjRecordVo : records){ apiKjRecordVo.setKjNum(this.getKjNo(apiKjRecordVo.getKjNum())); } } return new FebsResponse().success().data(pages); } @Override public void createKjRecord() { KjRecord kjRecord = new KjRecord(); String kjNo = MallUtils.getOrderNum(); kjRecord.setKjNo(kjNo); kjRecordMapper.insert(kjRecord); /** * redis中设置下一期的开奖编号和开奖时间 */ //开奖编号 String recordNoExit = redisUtils.getString(AppContants.KJ_RECORD_NO); if (StrUtil.isNotBlank(recordNoExit)) { redisUtils.del(AppContants.KJ_RECORD_NO); } redisUtils.set(AppContants.KJ_RECORD_NO, kjNo, AppContants.KJ_RECORD_TIME_INTERVAL*60); //开奖时间 if (StrUtil.isNotBlank(redisUtils.getString(AppContants.KJ_RECORD_TIME))) { redisUtils.del(AppContants.KJ_RECORD_TIME); } redisUtils.set(AppContants.KJ_RECORD_TIME, kjRecord.getCreatedTime(), AppContants.KJ_RECORD_TIME_INTERVAL*60); agentProducer.sendChouJiangDelayMsg(kjNo, AppContants.KJ_RECORD_TIME_INTERVAL * 2 * 30 * 1000L); agentProducer.sendNoTimeDelayMsg(kjNo, ((AppContants.KJ_RECORD_TIME_INTERVAL * 2)-1) * 30 * 1000L);//提一分钟停止下注 //todo 测试下注 ApiCreateDto apiCreateDto = new ApiCreateDto(); List items = new ArrayList<>(); for(int i = 1;i<= 43;i++){ ApiCreateItemDto apiCreateItemDto6 = new ApiCreateItemDto(); apiCreateItemDto6.setId(Long.parseLong(String.valueOf(i))); apiCreateItemDto6.setTzAmount(new BigDecimal("10")); items.add(apiCreateItemDto6); } apiCreateDto.setItems(items); asyncCjService.create(apiCreateDto,1L); } @Override public FebsResponse kjInfo() { ApiKjInfoVo apiKjInfoVo = new ApiKjInfoVo(); String kjNoOld = redisUtils.getString(AppContants.KJ_RECORD_NO_OLD); String kjNumOld = this.getKjNo(redisUtils.getString(AppContants.KJ_RECORD_NUM_OLD)); if(StrUtil.isBlank(kjNoOld)){ KjRecord kjRecordOld = kjRecordMapper.selectOneByLimitZero(1,1); if(ObjectUtil.isNotEmpty(kjRecordOld)){ apiKjInfoVo.setKjNoOld(kjRecordOld.getKjNo()); apiKjInfoVo.setKjNumOld(this.getKjNo(kjRecordOld.getKjNum())); } }else{ apiKjInfoVo.setKjNoOld(kjNoOld); apiKjInfoVo.setKjNumOld(kjNumOld); } String kjNo = redisUtils.getString(AppContants.KJ_RECORD_NO); String createdTime = redisUtils.getString(AppContants.KJ_RECORD_TIME); if(StrUtil.isBlank(kjNo)){ KjRecord kjRecord = kjRecordMapper.selectOneByLimitZero(0,1); if(ObjectUtil.isNotEmpty(kjRecord)){ apiKjInfoVo.setKjNo(kjRecord.getKjNo()); apiKjInfoVo.setCreatedTime(kjRecord.getCreatedTime().toString()); } }else{ apiKjInfoVo.setKjNo(kjNo); try { apiKjInfoVo.setCreatedTime(DateUtil.formatCstTime(createdTime, DateUtil.FULL_TIME_SPLIT_PATTERN)); } catch (ParseException e) { e.printStackTrace(); } } DataDictionaryCustom timeIntervalDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.TIME_INTERVAL.getType(), DataDictionaryEnum.TIME_INTERVAL.getCode() ); apiKjInfoVo.setDjs(Integer.parseInt(timeIntervalDic.getValue())); // 获取当前时间 LocalDateTime currentTime = LocalDateTime.now(); // 定义时间格式 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); apiKjInfoVo.setSystemTime(currentTime.format(formatter)); return new FebsResponse().success().data(apiKjInfoVo); } @Override public void getChouJiangDelayMsg(String kjNo) { //开启下注 DataDictionaryCustom noTimeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.NO_TIME.getType(), DataDictionaryEnum.NO_TIME.getCode() ); noTimeDic.setValue("1"); dataDictionaryCustomMapper.updateById(noTimeDic); //开奖 KjRecord kjRecord = kjRecordMapper.selectKjNo(kjNo); if(ObjectUtil.isEmpty(kjRecord)){ return; } DataDictionaryCustom openPriceDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.OPEN_PRICE.getType(), DataDictionaryEnum.OPEN_PRICE.getCode() ); int randomInt = RandomUtil.randomInt(1, 27); if(StrUtil.isNotEmpty(openPriceDic.getValue())){ randomInt = Integer.parseInt(openPriceDic.getValue()); openPriceDic.setValue(""); dataDictionaryCustomMapper.updateById(openPriceDic); } kjRecord.setKjNum( String.valueOf(randomInt)); kjRecordMapper.updateById(kjRecord); //开奖编号 String recordNoExit = redisUtils.getString(AppContants.KJ_RECORD_NO_OLD); if (StrUtil.isNotBlank(recordNoExit)) { redisUtils.del(AppContants.KJ_RECORD_NO_OLD); } redisUtils.set(AppContants.KJ_RECORD_NO_OLD, kjNo, AppContants.KJ_RECORD_TIME_INTERVAL * 60); //开奖号码 if (StrUtil.isNotBlank(redisUtils.getString(AppContants.KJ_RECORD_NUM_OLD))) { redisUtils.del(AppContants.KJ_RECORD_NUM_OLD); } redisUtils.set(AppContants.KJ_RECORD_NUM_OLD, randomInt, AppContants.KJ_RECORD_TIME_INTERVAL * 60); OpenPrice openPrice = new OpenPrice(); openPrice.setKjNo(kjNo); openPrice.setKjNum(String.valueOf(randomInt)); agentProducer.sendOpenPriceMsg(openPrice); } @Override public FebsResponse tzInfo() { LinkedHashMap tzInfo1 = new LinkedHashMap<>(); Object tzInfoMap = redisUtils.get(AppContants.TZ_INFO); if(ObjectUtil.isEmpty(tzInfoMap)){ tzInfo1 = this.getTzInfo(); }else{ tzInfo1 = JSONUtil.toBean(JSONUtil.parseObj(tzInfoMap), LinkedHashMap.class); } return new FebsResponse().success().data(tzInfo1); } @Override @Transactional public FebsResponse create(ApiCreateDto apiCreateDto) { Long memberId = LoginUserUtil.getLoginUser().getId(); //开启下注 DataDictionaryCustom noTimeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.NO_TIME.getType(), DataDictionaryEnum.NO_TIME.getCode() ); if("2".equals(noTimeDic.getValue())){ throw new FebsException("已停止投注"); } List items = apiCreateDto.getItems(); if(CollUtil.isEmpty(items)){ throw new FebsException("参数有误,请刷新页面"); } MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId); BigDecimal totalAmount = BigDecimal.ZERO; for(ApiCreateItemDto apiCreateItemDto : items){ BigDecimal tzAmount = apiCreateItemDto.getTzAmount().setScale(2,BigDecimal.ROUND_DOWN);//投注金额 if(BigDecimal.ZERO.compareTo(tzAmount) >= 0){ throw new FebsException("金额必须大于0"); } Long id = apiCreateItemDto.getId();//投注ID TzInfo tzInfo = tzInfoMapper.selectById(id); if(ObjectUtil.isEmpty(tzInfo)){ throw new FebsException("参数有误,请刷新页面"); } apiCreateItemDto.setTzInfo(tzInfo); totalAmount = totalAmount.add(tzAmount); } if(mallMemberWallet.getBalance().compareTo(totalAmount) < 0){ throw new FebsException("余额不足"); } //更新用户余额 mallMemberWallet.setBalance(mallMemberWallet.getBalance().subtract(totalAmount)); mallMemberWalletMapper.updateBalanceWithVersion(mallMemberWallet); //新增一条投注主表记录 CjInfo cjInfo = new CjInfo(); cjInfo.setMemberId(memberId); String kjNo = redisUtils.getString(AppContants.KJ_RECORD_NO); cjInfo.setKjNo(kjNo); cjInfo.setKjState(0); cjInfo.setPerkAmount(BigDecimal.ZERO); cjInfo.setTotalAmount(totalAmount); cjInfoMapper.insert(cjInfo); for(ApiCreateItemDto apiCreateItemDto : items){ Long id = apiCreateItemDto.getId();//投注ID TzInfo tzInfo = apiCreateItemDto.getTzInfo(); BigDecimal tzAmount = apiCreateItemDto.getTzAmount().setScale(2,BigDecimal.ROUND_DOWN);//投注金额 CjItem cjItem = new CjItem(); cjItem.setInfoId(cjInfo.getId()); cjItem.setMemberId(memberId); cjItem.setTzId(id); cjItem.setTzCode(tzInfo.getTzCode()); cjItem.setTzPercent(tzInfo.getTzPercent()); cjItem.setType(tzInfo.getType()); cjItem.setAmount(tzAmount); cjItem.setItemNum(tzInfo.getTzCode()); cjItem.setPerkAmount(BigDecimal.ZERO); cjItemMapper.insert(cjItem); } String orderNo = MallUtils.getOrderNum("T"); mallMemberService.addMoneyFlow( memberId, totalAmount.negate(), MoneyFlowTypeNewEnum.STATIC_BONUS.getValue(), orderNo, MoneyFlowTypeNewEnum.STATIC_BONUS.getDescription(), null, cjInfo.getId(), 2, FlowTypeEnum.BALANCE.getValue()); return new FebsResponse().success().message("操作成功"); } @Override public FebsResponse tzInfoList(KjRecordDto kjRecordDto) { Long memberId = LoginUserUtil.getLoginUser().getId(); kjRecordDto.setMemberId(memberId); IPage page = new Page<>(kjRecordDto.getPageNum(), kjRecordDto.getPageSize()); IPage pages = cjInfoMapper.selectTzListByMemberId(page, kjRecordDto); List records = pages.getRecords(); if(CollUtil.isNotEmpty(records)){ for(CjInfo item : records){ Long id = item.getId(); item.setKjNum(this.getKjNo(item.getKjNum())); List cjItems = cjItemMapper.selectListByInfoId(id); if(CollUtil.isNotEmpty(cjItems)){ for(CjItem cjItem : cjItems){ cjItem.setKjNum(this.getKjNo(cjItem.getKjNum())); } } item.setItems(cjItems); } } List apiTzListVos = CjInfoConversion.INSTANCE.entitysToVos(records); return new FebsResponse().success().data(apiTzListVos); } @Override public void getNoTimeDelayMsg(String kjNo) { DataDictionaryCustom noTimeDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.NO_TIME.getType(), DataDictionaryEnum.NO_TIME.getCode() ); noTimeDic.setValue("2"); dataDictionaryCustomMapper.updateById(noTimeDic); } @Override public void getOpenPriceMsg(String kjNo, String kjNum) { QueryWrapper objectQueryWrapper = new QueryWrapper<>(); objectQueryWrapper.eq("kj_no",kjNo); List cjInfos = cjInfoMapper.selectList(objectQueryWrapper); if(ObjectUtil.isEmpty(cjInfos)){ return; } String danshuang = judgeNumberOne(Integer.parseInt(kjNum)); String daXiao = judgeNumberTwo(Integer.parseInt(kjNum)); DataDictionaryCustom redBallDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.RED_BALL.getType(), DataDictionaryEnum.RED_BALL.getCode() ); String hong = judgeNumberHong(kjNum, redBallDic); DataDictionaryCustom blueBallDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.BLUE_BALL.getType(), DataDictionaryEnum.BLUE_BALL.getCode() ); String lan = judgeNumberLan(kjNum, blueBallDic); DataDictionaryCustom colorBallDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.COLOR_BALL.getType(), DataDictionaryEnum.COLOR_BALL.getCode() ); String caiqiu = judgeNumberColor(kjNum, colorBallDic); String liugehao = getGroupNumberSix(Integer.parseInt(kjNum)); String sangehao = getGroupNumberThree(Integer.parseInt(kjNum)); for(CjInfo cjInfo : cjInfos){ asyncCjService.openPriceAsync(cjInfo,kjNum,danshuang,daXiao,hong,lan,caiqiu,liugehao,sangehao); } } @Override public LinkedHashMap getTzInfo() { LinkedHashMap linkedHashMap = new LinkedHashMap<>(); List tzInfos = tzInfoMapper.selectList(null); linkedHashMap.put("daXiao", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 1).collect(Collectors.toList())))); linkedHashMap.put("danShuang", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 2).collect(Collectors.toList())))); linkedHashMap.put("shuZi", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 3).collect(Collectors.toList())))); linkedHashMap.put("hong", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 4).collect(Collectors.toList())))); linkedHashMap.put("caiqiu", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 5).collect(Collectors.toList())))); linkedHashMap.put("liuHao", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 6).collect(Collectors.toList())))); linkedHashMap.put("sanHao", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 7).collect(Collectors.toList())))); linkedHashMap.put("lan", JSONUtil.parse( TzInfoConversion.INSTANCE.entitysToVo( tzInfos.stream().filter(tzInfo -> tzInfo.getType() == 8).collect(Collectors.toList())))); //投注信息 String tzInfoNoExit = redisUtils.getString(AppContants.TZ_INFO); if (StrUtil.isNotBlank(tzInfoNoExit)) { redisUtils.del(AppContants.TZ_INFO); } JSON jsonStr = JSONUtil.parse(linkedHashMap); redisUtils.set(AppContants.TZ_INFO, jsonStr); return linkedHashMap; } @Override public String getKjNo(String num) { DataDictionaryCustom redBallDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.RED_BALL.getType(), DataDictionaryEnum.RED_BALL.getCode() ); DataDictionaryCustom blueBallDic = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( DataDictionaryEnum.BLUE_BALL.getType(), DataDictionaryEnum.BLUE_BALL.getCode() ); if(redBallDic.getValue().equals(num)){ return "红"; } if(blueBallDic.getValue().equals(num)){ return "蓝"; } return num; } /** * 取模运算符%来判断该数字是否为偶数。如果余数为0,则该数字为偶数,返回"双数";否则,该数字为奇数,返回"单数" * @param num * @return 1-单数 2-双数 */ public static String judgeNumberOne(int num) { if(num > 24){ return String.valueOf(num); } if (num % 2 == 0) { return "双"; } else { return "单"; } } /** * 小于等于12为小 大于12为大 * @param num * @return 1-小数 2-大数 */ public static String judgeNumberTwo(int num) { if(num > 24){ return String.valueOf(num); } if (num <= 12) { return "龙1-12"; } else { return "虎13-24"; } } /** * 红 * @param num */ public static String judgeNumberHong(String num,DataDictionaryCustom redBallDic) { String redBall = redBallDic.getValue(); if (redBall.equals(num)) { return "红球"; } else { return num; } } /** * 蓝 * @param num */ public static String judgeNumberLan(String num,DataDictionaryCustom blueBallDic) { String blueBall = blueBallDic.getValue(); if (blueBall.equals(num)) { return "蓝球"; } else { return num; } } /** * 彩球 * @param num */ public static String judgeNumberColor(String num,DataDictionaryCustom colorBallDic) { String colorBall = colorBallDic.getValue(); List colorBallList = Arrays.asList(StrUtil.split(colorBall, ",")); if (colorBallList.contains(num)) { return "彩球"; } else { return num; } } /** * 六个号 * 判断数字属于哪一组并返回组号 * @param number 1-24之间的数字 * @return 组号(1, 2, 3, 4) */ /** * 判断一个数字属于哪个组并返回组号 * * @param num 输入的数字(1-24之间) * @return 组号(1-4之间) */ public static String getGroupNumberSix(int num) { if(num > 24){ return String.valueOf(num); } // 使用整除来确定组号 int i = (num - 1) / 6 + 1; if(1 == i){ return "1-6"; }else if(2 == i){ return "7-12"; }else if(3 == i){ return "13-18"; }else{ return "19-24"; } } /** * 六个号 * 判断数字属于哪一组并返回组号 * @param number 1-24之间的数字 * @return 组号(1, 2, 3, 4) */ /** * 判断一个数字属于哪个组并返回组号 * * @param num 输入的数字(1-24之间) * @return 组号(1-4之间) */ public static String getGroupNumberThree(int num) { if(num > 24){ return String.valueOf(num); } // 使用整除来确定组号 int i = (num - 1) / 3 + 1; if(1 == i){ return "1-3"; }else if(2 == i){ return "4-6"; }else if(3 == i){ return "7-9"; }else if(4 == i){ return "10-12"; }else if(5 == i){ return "13-15"; }else if(6 == i){ return "16-18"; }else if(7 == i){ return "19-21"; }else{ return "22-24"; } } public static void main(String[] args) { ArrayList CjItemUpdateList = new ArrayList<>(); for(int i =1 ;i<=10000;i++){ CjItemUpdateList.add(i); } int listSize= CjItemUpdateList.size(); int toIndex = 500; for(int i = 0; i listSize){ //作用为toIndex最后没有1000条数据则剩余几条newList中就装几条 toIndex = listSize - i; } List newList = new ArrayList<>(CjItemUpdateList.subList(i, i + toIndex)); System.out.println(newList.toString()); } } }