小程序端接口实现

This commit is contained in:
HMY 2024-07-15 11:59:06 +08:00
parent 5490e69863
commit 8f3c781183
36 changed files with 790 additions and 31 deletions

View File

@ -45,6 +45,12 @@
<dependencies>
<!-- rabbitmq -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- excel表导入-->
<dependency>
<groupId>com.alibaba</groupId>

View File

@ -0,0 +1,36 @@
package com.ruoyi.project.hit.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* @TableName card_game_user
*/
@TableName(value ="card_game_user")
@Data
public class CardGameUser implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer userid;
private Integer gameid;
private Date createtime;
private static final long serialVersionUID = 1L;
public CardGameUser(Integer userid, Integer gameid, Date createtime) {
this.userid = userid;
this.gameid = gameid;
this.createtime = createtime;
}
}

View File

@ -1,13 +1,14 @@
package com.ruoyi.project.hit.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.*;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* @TableName card_user
*/
@ -17,27 +18,34 @@ public class CardUser implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
@NotEmpty(message = "账号不能为空")
private String userName;
@NotEmpty(message = "密码不能为空")
private String userPass;
private String pic;
@NotEmpty(message = "真实姓名不能为空")
private String realname;
private String idCard;
@NotEmpty(message = "手机号不能为空")
private String phone;
private Integer level;
//自动填充时间
@TableField(fill = FieldFill.INSERT)
private Date createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
private Integer delState;
private String remark;
// private String remark;
private static final long serialVersionUID = 1L;
}

View File

@ -14,6 +14,7 @@ import lombok.Data;
@TableName(value ="card_user_hit")
@Data
public class CardUserHit implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer gameid;
@ -25,4 +26,11 @@ public class CardUserHit implements Serializable {
private Date hittime;
private static final long serialVersionUID = 1L;
public CardUserHit(Integer gameid, Integer userid, Integer productid, Date hittime) {
this.gameid = gameid;
this.userid = userid;
this.productid = productid;
this.hittime = hittime;
}
}

View File

@ -6,6 +6,7 @@ import com.ruoyi.project.hit.controller.vo.TotalInfoVo;
import com.ruoyi.project.hit.domain.CardGame;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -22,6 +23,12 @@ public interface CardGameMapper extends BaseMapper<CardGame> {
List<HashMap> selectCardGameList(ListCardGameVo listCardGameVo);
HashMap getCardGameByGameId(Integer id);
List<HashMap> showAllCardGame();
List<CardGame> getCardByDate(Date now);
HashMap getCardGameBygameid(Integer gameid);
}

View File

@ -2,6 +2,7 @@ package com.ruoyi.project.hit.mapper;
import com.ruoyi.project.hit.domain.CardGameProduct;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.project.uniapp.controller.vo.CardProductVo;
import java.util.HashMap;
import java.util.List;
@ -17,6 +18,8 @@ public interface CardGameProductMapper extends BaseMapper<CardGameProduct> {
List<HashMap> getGameProductByGameId(Integer id);
int getSumAmountByGameId(Integer gameid);
List<CardProductVo> getGameProductList(Integer id);
}

View File

@ -0,0 +1,18 @@
package com.ruoyi.project.hit.mapper;
import com.ruoyi.project.hit.domain.CardGameUser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @author HP
* @description 针对表card_game_user的数据库操作Mapper
* @createDate 2024-07-12 16:48:34
* @Entity com.ruoyi.project.hit.domain.CardGameUser
*/
public interface CardGameUserMapper extends BaseMapper<CardGameUser> {
}

View File

@ -18,12 +18,13 @@ import java.util.Map;
public interface CardUserHitMapper extends BaseMapper<CardUserHit> {
List<HashMap> selectCardUserHitInfo(TotalInfoVo totalInfoVo);
Map getCardGameBygameid(Integer gameid);
// Map getCardGameBygameid(Integer gameid);
List<HashMap> getProductsBygameid(Integer gameid);
List<HashMap> selectCardUserHitList(ListInfoVo listInfoVo);
List<HashMap> winnerRecord(Integer userId);
}

View File

@ -2,6 +2,7 @@ package com.ruoyi.project.hit.service;
import com.ruoyi.project.hit.domain.CardGameProduct;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.project.uniapp.controller.vo.CardProductVo;
import java.util.HashMap;
import java.util.List;
@ -49,4 +50,6 @@ public interface CardGameProductService extends IService<CardGameProduct> {
int getSumAmountByGameId(Integer gameid);
int deleteGameProduct(Integer id);
List<CardProductVo> getGameProductList(Integer id);
}

View File

@ -44,4 +44,6 @@ public interface CardGameRuleService extends IService<CardGameRule> {
int getHitCountSumByGameId(Integer gameid);
int deleteGameRule(Integer id);
List<CardGameRule> getGameRuleList(Integer id);
}

View File

@ -3,7 +3,9 @@ package com.ruoyi.project.hit.service;
import com.ruoyi.project.hit.controller.vo.ListCardGameVo;
import com.ruoyi.project.hit.domain.CardGame;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.project.uniapp.controller.vo.CardUserGameVo;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -44,4 +46,14 @@ public interface CardGameService extends IService<CardGame> {
Map<String, Object> getCardGameDetailById(Integer id);
CardGame getCardGameByGameId(Integer gameid);
List<CardGame> carousel();
List<HashMap> showAllCardGame();
Map<String, Object> getGameDetail(Integer gameId);
void preHot(Date now);
HashMap getCardGameBygameid(Integer gameid);
}

View File

@ -0,0 +1,14 @@
package com.ruoyi.project.hit.service;
import com.ruoyi.project.hit.domain.CardGameUser;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author HP
* @description 针对表card_game_user的数据库操作Service
* @createDate 2024-07-12 16:48:34
*/
public interface CardGameUserService extends IService<CardGameUser> {
int addCardGameUser(CardGameUser cardGameUser);
}

View File

@ -4,6 +4,8 @@ import com.ruoyi.project.hit.controller.vo.ListInfoVo;
import com.ruoyi.project.hit.controller.vo.TotalInfoVo;
import com.ruoyi.project.hit.domain.CardUserHit;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.project.uniapp.controller.vo.CardProductVo;
import com.ruoyi.project.uniapp.controller.vo.CardUserGameVo;
import java.util.HashMap;
import java.util.List;
@ -36,4 +38,10 @@ public interface CardUserHitService extends IService<CardUserHit> {
* @return 中奖列表
*/
List<HashMap> selectCardUserHitList(ListInfoVo listInfoVo);
CardProductVo getPrizeDraw(CardUserGameVo cardUserGameVo);
int addCardUserHit(CardUserHit cardUserHit);
List<HashMap> winnerRecord(Integer userId);
}

View File

@ -1,5 +1,6 @@
package com.ruoyi.project.hit.service;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.hit.controller.vo.ListCardUserVo;
import com.ruoyi.project.hit.domain.CardUser;
import com.baomidou.mybatisplus.extension.service.IService;
@ -27,4 +28,10 @@ public interface CardUserService extends IService<CardUser> {
* @return
*/
List<HashMap> selectCardUserList(ListCardUserVo listCardUserVo);
CardUser login(String userName, String userPass);
boolean weatherLogin(Integer userId);
int addCardUser(CardUser cardUser);
}

View File

@ -5,11 +5,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.project.hit.domain.CardGame;
import com.ruoyi.project.hit.domain.CardGameProduct;
import com.ruoyi.project.hit.domain.CardGameRule;
import com.ruoyi.project.hit.service.CardGameProductService;
import com.ruoyi.project.hit.mapper.CardGameProductMapper;
import com.ruoyi.project.hit.service.CardGameService;
import com.ruoyi.project.hit.service.CardUserHitService;
import com.ruoyi.project.uniapp.controller.vo.CardProductVo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -83,6 +83,11 @@ public class CardGameProductServiceImpl extends ServiceImpl<CardGameProductMappe
//活动已开始时不允许删除该活动的奖品
return cardGameProductMapper.deleteById(id);
}
@Override
public List<CardProductVo> getGameProductList(Integer id) {
return cardGameProductMapper.getGameProductList(id);
}
}

View File

@ -83,6 +83,13 @@ public class CardGameRuleServiceImpl extends ServiceImpl<CardGameRuleMapper, Car
return cardGameRuleMapper.deleteById(id);
}
@Override
public List<CardGameRule> getGameRuleList(Integer id) {
LambdaQueryWrapper<CardGameRule> wrapper=new LambdaQueryWrapper<>();
wrapper.eq(CardGameRule::getGameid,id);
return cardGameRuleMapper.selectList(wrapper);
}
}

View File

@ -1,24 +1,29 @@
package com.ruoyi.project.hit.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.hit.controller.vo.ListCardGameVo;
import com.ruoyi.project.hit.domain.CardGame;
import com.ruoyi.project.hit.domain.CardGameProduct;
import com.ruoyi.project.hit.domain.CardGameRule;
import com.ruoyi.project.hit.service.CardGameProductService;
import com.ruoyi.project.hit.service.CardGameRuleService;
import com.ruoyi.project.hit.service.CardGameService;
import com.ruoyi.project.hit.mapper.CardGameMapper;
import com.ruoyi.project.hit.service.CardUserHitService;
import com.ruoyi.project.uniapp.controller.vo.CardProductVo;
import com.ruoyi.project.uniapp.controller.vo.CardUserGameVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* @author HP
@ -29,6 +34,8 @@ import java.util.Map;
@Service
public class CardGameServiceImpl extends ServiceImpl<CardGameMapper, CardGame>
implements CardGameService{
@Autowired
private RedisTemplate redisTemplate;
@Resource
private CardGameMapper cardGameMapper;
@ -36,6 +43,8 @@ public class CardGameServiceImpl extends ServiceImpl<CardGameMapper, CardGame>
private CardGameRuleService cardGameRuleService;
@Resource
private CardGameProductService cardGameProductService;
@Resource
private CardUserHitService cardUserHitService;
@Override
public List<HashMap> selectCardGameList(ListCardGameVo listCardGameVo) {
@ -115,6 +124,94 @@ public class CardGameServiceImpl extends ServiceImpl<CardGameMapper, CardGame>
public CardGame getCardGameByGameId(Integer gameid) {
return cardGameMapper.selectById(gameid);
}
@Override
public List<CardGame> carousel() {
LambdaQueryWrapper<CardGame> wrapper=new LambdaQueryWrapper<>();
wrapper.select(CardGame::getPic);
wrapper.select(CardGame::getTitle);
return cardGameMapper.selectList(wrapper);
}
@Override
public List<HashMap> showAllCardGame() {
return cardGameMapper.showAllCardGame();
}
@Override
public Map<String, Object> getGameDetail(Integer gameId) {
Map map=new HashMap();
List<HashMap> gameRuleList = cardGameRuleService.getGameRuleListByGameId(gameId);
map.put("gameRuleList",gameRuleList);
List<HashMap> gameProductList = cardGameProductService.getGameProductByGameId(gameId);
map.put("gameProductList",gameProductList);
Map<String, Object> map1 = cardUserHitService.selectCardUserHitInfoByGameId(gameId);
map.putAll(map1);
return map;
}
@Override
public void preHot(Date now) {
List<CardGame> cardGameList = cardGameMapper.getCardByDate(now);
if(cardGameList.size()==0){
return;
}
//活动信息缓存redisjson字符串存储
for (CardGame cardGame : cardGameList) {
String jsonCardGame = JSONArray.toJSONString(cardGame);
//失效时间
long l=cardGame.getEndTime().getTime()-now.getTime();
redisTemplate.opsForValue().set("cardGame:"+cardGame.getId(),jsonCardGame,l,TimeUnit.MILLISECONDS);
List<CardGameRule> gameRuleList = cardGameRuleService.getGameRuleList(cardGame.getId());
for (CardGameRule cardGameRule : gameRuleList) {
//最大中奖数缓存redishash存储
redisTemplate.opsForHash().put("cardGame_maxHitCount:"+cardGame.getId(),cardGameRule.getLevelid(),cardGameRule.getHitCount());
//最大抽奖数缓存redishash存储
redisTemplate.opsForHash().put("cardGame_maxEnterCount:"+cardGame.getId(),cardGameRule.getLevelid(),cardGameRule.getEnterCount());
}
//设置两个hash失效时间
redisTemplate.expire("cardGame_maxHitCount:"+cardGame.getId(),l, TimeUnit.MILLISECONDS);
redisTemplate.expire("cardGame_maxEnterCount:"+cardGame.getId(),l, TimeUnit.MILLISECONDS);
List<CardProductVo> gameProductList = cardGameProductService.getGameProductList(cardGame.getId());
//抽奖令牌桶
List<Long> tokenList=new ArrayList<>();
for (CardProductVo cardProductVo : gameProductList) {
for (int i = 0; i < cardProductVo.getAmount(); i++) {
//给每个奖品都配置一个时间戳
Long start=cardGame.getStartTime().getTime();
Long end=cardGame.getEndTime().getTime();
//时间差
int time= (int) (end-start);
Long token=start+new Random().nextInt(time);
//避免重复
token=token*10000+new Random().nextInt(9999);
tokenList.add(token);
//奖品-令牌缓存redis(string存储
redisTemplate.opsForValue().set("gameId_token:"+cardGame.getId()+"_"+token,cardProductVo,l,TimeUnit.MILLISECONDS);
}
}
//由小到大排
Collections.sort(tokenList);
//令牌集合redis缓存list存储
redisTemplate.opsForList().rightPushAll("game_token:"+cardGame.getId(),tokenList);
redisTemplate.expire("game_token:"+cardGame.getId(),l,TimeUnit.MILLISECONDS);
//活动状态置1
cardGame.setStatus(1);
cardGameMapper.updateById(cardGame);
}
}
@Override
public HashMap getCardGameBygameid(Integer gameid) {
return cardGameMapper.getCardGameBygameid(gameid);
}
}

View File

@ -0,0 +1,30 @@
package com.ruoyi.project.hit.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.project.hit.domain.CardGameUser;
import com.ruoyi.project.hit.service.CardGameUserService;
import com.ruoyi.project.hit.mapper.CardGameUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author HP
* @description 针对表card_game_user的数据库操作Service实现
* @createDate 2024-07-12 16:48:34
*/
@Service
public class CardGameUserServiceImpl extends ServiceImpl<CardGameUserMapper, CardGameUser>
implements CardGameUserService{
@Resource
private CardGameUserMapper cardGameUserMapper;
@Override
public int addCardGameUser(CardGameUser cardGameUser) {
return cardGameUserMapper.insert(cardGameUser);
}
}

View File

@ -1,19 +1,32 @@
package com.ruoyi.project.hit.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.project.hit.controller.vo.ListInfoVo;
import com.ruoyi.project.hit.controller.vo.TotalInfoVo;
import com.ruoyi.project.hit.domain.CardUserHit;
import com.ruoyi.project.hit.mapper.CardGameMapper;
import com.ruoyi.project.hit.domain.*;
import com.ruoyi.project.hit.service.CardGameService;
import com.ruoyi.project.hit.service.CardUserHitService;
import com.ruoyi.project.hit.mapper.CardUserHitMapper;
import com.ruoyi.project.hit.service.CardUserService;
import com.ruoyi.project.uniapp.controller.vo.CardProductVo;
import com.ruoyi.project.uniapp.controller.vo.CardUserGameVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author HP
@ -27,6 +40,16 @@ public class CardUserHitServiceImpl extends ServiceImpl<CardUserHitMapper, CardU
@Resource
private CardUserHitMapper cardUserHitMapper;
@Autowired
private CardGameService cardGameService;
@Autowired
private CardUserService cardUserService;
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public List<HashMap> selectCardUserHitInfo(TotalInfoVo totalInfoVo) {
return cardUserHitMapper.selectCardUserHitInfo(totalInfoVo);
@ -36,13 +59,15 @@ public class CardUserHitServiceImpl extends ServiceImpl<CardUserHitMapper, CardU
public Map<String,Object> selectCardUserHitInfoByGameId(Integer gameid) {
Map<String,Object> map=new HashMap<>();
//根据活动id拿到活动详情以及活动类型2表
Map map1 = cardUserHitMapper.getCardGameBygameid(gameid);
log.debug("cardGame",map1);
Map map1 = cardGameService.getCardGameBygameid(gameid);
// log.debug("cardGame",map1);
map.put("cardGame",map1);
//根据活动id拿到活动中奖明细3表
List<HashMap> hashMapList=cardUserHitMapper.getProductsBygameid(gameid);
map.put("products",hashMapList);
log.debug("products",hashMapList);
CardGame cardGame = cardGameService.getCardGameByGameId(gameid);
if(StringUtils.isNotNull(cardGame)&&cardGame.getEndTime().before(new Date())){
//根据活动id拿到活动中奖明细3表
List<HashMap> hashMapList=cardUserHitMapper.getProductsBygameid(gameid);
map.put("products",hashMapList);
}
return map;
}
@ -51,6 +76,79 @@ public class CardUserHitServiceImpl extends ServiceImpl<CardUserHitMapper, CardU
return cardUserHitMapper.selectCardUserHitList(listInfoVo);
}
@Override
public CardProductVo getPrizeDraw(CardUserGameVo cardUserGameVo) {
Date now=new Date();
boolean b1 = cardUserService.weatherLogin(cardUserGameVo.getUserId());
if(!b1){
throw new CustomException("用户未登录");
}
CardGame cardGame = JSON.parseObject((String) redisTemplate.opsForValue().get("cardGame:" + cardUserGameVo.getGameId()),CardGame.class);
CardUser cardUser= (CardUser) redisTemplate.opsForValue().get("user_login:"+cardUserGameVo.getUserId());
if(StringUtils.isNull(cardGame)||now.before(cardGame.getStartTime())){
throw new CustomException("活动未开始");
}
if(now.after(cardGame.getEndTime())){
throw new CustomException("活动已结束");
}
//判断该用户是否参与过抽奖
Boolean b = redisTemplate.hasKey("cardGame_user:" + cardUserGameVo.getGameId() + "_" + cardUserGameVo.getUserId());
if(!b){
//value抽奖次数
redisTemplate.opsForValue().set("cardGame_user:" + cardUserGameVo.getGameId() + "_" + cardUserGameVo.getUserId(),0,(cardGame.getEndTime().getTime()-now.getTime()), TimeUnit.MILLISECONDS);
//value中奖次数
redisTemplate.opsForValue().set("cardGame_user_hit:" + cardUserGameVo.getGameId() + "_" + cardUserGameVo.getUserId(),0,(cardGame.getEndTime().getTime()-now.getTime()), TimeUnit.MILLISECONDS);
}
int maxEnterCount = (int) redisTemplate.opsForHash().get("cardGame_maxEnterCount:" + cardUserGameVo.getGameId(), cardUser.getLevel());
int EnterCount=(int) redisTemplate.opsForValue().get("cardGame_user:" + cardUserGameVo.getGameId() + "_" + cardUserGameVo.getUserId());
if(EnterCount>=maxEnterCount){
throw new CustomException("抽奖次数已达上限");
}
int maxHitCount = (int) redisTemplate.opsForHash().get("cardGame_maxHitCount:" + cardUserGameVo.getGameId(), cardUser.getLevel());
int HitCount=(int) redisTemplate.opsForValue().get("cardGame_user_hit:" + cardUserGameVo.getGameId() + "_" + cardUserGameVo.getUserId());
//抽奖记录次数加一
redisTemplate.opsForValue().increment("cardGame_user:" + cardUserGameVo.getGameId() + "_" + cardUserGameVo.getUserId(),1);
//mq写入抽奖记录到数据库
CardGameUser cardGameUser=new CardGameUser(cardUserGameVo.getUserId(),cardUserGameVo.getGameId(),now);
String cardGameUserStr= JSONObject.toJSONString(cardGameUser);
rabbitTemplate.convertAndSend("drawRecord",cardGameUserStr);
//该用户已经达到最大中奖次数
if(HitCount>=maxHitCount){
return null;
}
//抽奖流程
Long token= (Long) redisTemplate.opsForList().leftPop("game_token:"+cardUserGameVo.getGameId());
if(StringUtils.isNull(token)){
throw new CustomException("奖品已抽完");
}
if(now.getTime()<token/10000){
//没中奖,token放回令牌桶
redisTemplate.opsForList().leftPush("game_token:"+cardUserGameVo.getGameId(),token);
return null;
}else {
//中奖,中奖记录次数加一
redisTemplate.opsForValue().increment("cardGame_user_hit:" + cardUserGameVo.getGameId() + "_" + cardUserGameVo.getUserId(),1);
CardProductVo cardProductVo= (CardProductVo) redisTemplate.opsForValue().get("gameId_token:"+cardGame.getId()+"_"+token);
//mq写入中奖信息到数据库
CardUserHit cardUserHit=new CardUserHit(cardUserGameVo.getGameId(),cardUserGameVo.getUserId(),cardProductVo.getId(),now);
String cardUserHitStr=JSONObject.toJSONString(cardUserHit);
rabbitTemplate.convertAndSend("winnerRecord",cardUserHitStr);
return cardProductVo;
}
}
@Override
public int addCardUserHit(CardUserHit cardUserHit) {
return cardUserHitMapper.insert(cardUserHit);
}
@Override
public List<HashMap> winnerRecord(Integer userId) {
return cardUserHitMapper.winnerRecord(userId);
}
}

View File

@ -2,16 +2,22 @@ package com.ruoyi.project.hit.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.hit.controller.vo.ListCardUserVo;
import com.ruoyi.project.hit.domain.CardUser;
import com.ruoyi.project.hit.domain.SystemDict;
import com.ruoyi.project.hit.service.CardUserService;
import com.ruoyi.project.hit.mapper.CardUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* @author HP
@ -21,6 +27,8 @@ import java.util.List;
@Service
public class CardUserServiceImpl extends ServiceImpl<CardUserMapper, CardUser>
implements CardUserService{
@Autowired
private RedisTemplate redisTemplate;
@Resource
private CardUserMapper cardUserMapper;
@ -36,6 +44,30 @@ public class CardUserServiceImpl extends ServiceImpl<CardUserMapper, CardUser>
public List<HashMap> selectCardUserList(ListCardUserVo listCardUserVo) {
return cardUserMapper.selectCardUserList(listCardUserVo);
}
@Override
public CardUser login(String userName, String userPass) {
LambdaQueryWrapper<CardUser> wrapper=new LambdaQueryWrapper<>();
wrapper.eq(CardUser::getUserName,userName);
wrapper.eq(CardUser::getUserPass,userPass);
CardUser cardUser = cardUserMapper.selectOne(wrapper);
if(StringUtils.isNotNull(cardUser)){
redisTemplate.opsForValue().set("user_login:"+cardUser.getId(),cardUser, 30, TimeUnit.MINUTES);
return cardUser;
}
throw new CustomException("登录失败");
}
@Override
public boolean weatherLogin(Integer userId) {
return redisTemplate.hasKey("user_login:"+userId);
}
@Override
public int addCardUser(CardUser cardUser) {
return cardUserMapper.insert(cardUser);
}
}

View File

@ -3,6 +3,7 @@ package com.ruoyi.project.system.controller;
import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -39,6 +40,8 @@ public class SysLoginController
@Autowired
private TokenService tokenService;
private RedisTemplate redisTemplate;
/**
* 登录方法
*
@ -56,6 +59,8 @@ public class SysLoginController
return ajax;
}
/**
* 获取用户信息
*

View File

@ -0,0 +1,73 @@
package com.ruoyi.project.uniapp.controller;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hit.domain.CardGame;
import com.ruoyi.project.hit.domain.CardUser;
import com.ruoyi.project.hit.service.CardGameService;
import com.ruoyi.project.hit.service.CardUserHitService;
import com.ruoyi.project.hit.service.CardUserService;
import com.ruoyi.project.uniapp.controller.vo.CardUserGameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@EnableScheduling
@RestController
@RequestMapping("/uniapp/bonus")
public class BonusController extends BaseController {
@Autowired
private CardUserService cardUserService;
@Autowired
private CardGameService cardGameService;
@Autowired
private CardUserHitService cardUserHitService;
@GetMapping("/showAllCardGame")
public TableDataInfo showAllCardGame(){
startPage();
List<HashMap> list = cardGameService.showAllCardGame();
return getDataTable(list);
}
/**
* 活动详情
* @param gameId
* @return
*/
@PostMapping("/getGameDetail/{gameId}")
public AjaxResult getGameDetail(@PathVariable(value = "gameId")Integer gameId){
return AjaxResult.success(cardGameService.getGameDetail(gameId));
}
/**
* 抽奖业务
* @param cardUserGameVo
* @return
*/
@PostMapping("/getPrizeDraw")
public AjaxResult getPrizeDraw(@RequestBody CardUserGameVo cardUserGameVo){
return AjaxResult.success(cardUserHitService.getPrizeDraw(cardUserGameVo));
}
/**
* 活动预热
*/
@Scheduled(fixedRate = 60000)
public void preHot(){
Date now=new Date();
cardGameService.preHot(now);
}
}

View File

@ -0,0 +1,58 @@
package com.ruoyi.project.uniapp.controller;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.hit.domain.CardGame;
import com.ruoyi.project.hit.service.CardGameService;
import com.ruoyi.project.hit.service.CardSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/uniapp/homePage")
public class HomePageController extends BaseController {
@Autowired
private CardGameService cardGameService;
@Autowired
private CardSaleService cardSaleService;
/**
* 主页轮播图
* @return
*/
@GetMapping("/carousel")
public AjaxResult carousel(){
List<CardGame> carousel = cardGameService.carousel();
return AjaxResult.success(carousel);
}
/**
* 热度榜单
* @return
*/
@GetMapping("/getHotSale")
public AjaxResult getHotSale(){
return AjaxResult.success(cardSaleService.getHotSale());
}
@GetMapping("/getAllHotSale")
public AjaxResult getAllHotSale(){
return AjaxResult.success(cardSaleService.getAllHotSale());
}
/**
* 点击(热度增加)
* @param id
* @return
*/
@PostMapping("/addHotSaleNum/{saleId}")
public AjaxResult addHotSaleNum(@PathVariable(value = "saleId")Integer id){
cardSaleService.addHotSaleNum(id);
return AjaxResult.success();
}
}

View File

@ -0,0 +1,39 @@
package com.ruoyi.project.uniapp.controller;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.hit.domain.CardUser;
import com.ruoyi.project.hit.service.CardUserHitService;
import com.ruoyi.project.hit.service.CardUserService;
import com.ruoyi.project.uniapp.controller.vo.CardUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
@RestController
@RequestMapping("/uniapp/personalCenter")
public class PersonalCenterController extends BaseController {
@Autowired
private CardUserService cardUserService;
@Autowired
private CardUserHitService cardUserHitService;
@PostMapping("/login")
public AjaxResult login(@RequestBody @Valid CardUserVo cardUser){
CardUser user = cardUserService.login(cardUser.getUserName(), cardUser.getUserPass());
return AjaxResult.success("登录成功",user);
}
@PostMapping("/addCardUser")
public AjaxResult addCardUser(@Valid @RequestBody CardUser cardUser){
return toAjax(cardUserService.addCardUser(cardUser));
}
@GetMapping("/winnerRecord/{userId}")
public AjaxResult getMyWinnerRecord(@PathVariable(value = "userId")Integer userId){
return AjaxResult.success(cardUserHitService.winnerRecord(userId));
}
}

View File

@ -0,0 +1,29 @@
package com.ruoyi.project.uniapp.controller.vo;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import java.math.BigDecimal;
@Data
public class CardProductVo {
@TableId(type = IdType.AUTO)
private Integer id;
private String pname;
private String pic;
private String content;
private BigDecimal price;
private Integer delState;
private Integer amount;
}

View File

@ -0,0 +1,13 @@
package com.ruoyi.project.uniapp.controller.vo;
import lombok.Data;
import javax.validation.constraints.NotNull;
@Data
public class CardUserGameVo {
@NotNull(message = "用户id不能为空")
private Integer userId;
@NotNull(message = "活动id不能为空")
private Integer gameId;
}

View File

@ -0,0 +1,14 @@
package com.ruoyi.project.uniapp.controller.vo;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
@Data
public class CardUserVo {
@NotEmpty(message = "账号不能为空")
private String userName;
@NotEmpty(message = "密码不能为空")
private String userPass;
}

View File

@ -0,0 +1,23 @@
package com.ruoyi.project.uniapp.mq;
import com.alibaba.fastjson.JSON;
import com.ruoyi.project.hit.domain.CardGameUser;
import com.ruoyi.project.hit.service.CardGameUserService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@RabbitListener(queues = "drawRecord")
@Component
public class Consumer01 {
@Resource
private CardGameUserService cardGameUserService;
@RabbitHandler
public void addCardGameUser(String cardGameUserStr){
CardGameUser cardGameUser= JSON.parseObject(cardGameUserStr,CardGameUser.class);
cardGameUserService.addCardGameUser(cardGameUser);
}
}

View File

@ -0,0 +1,21 @@
package com.ruoyi.project.uniapp.mq;
import com.alibaba.fastjson.JSON;
import com.ruoyi.project.hit.domain.CardUserHit;
import com.ruoyi.project.hit.service.CardUserHitService;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@RabbitListener(queues = "winnerRecord")
@Component
public class Consumer02 {
@Autowired
private CardUserHitService cardUserHitService;
@RabbitHandler
public void addCardUserHit(String cardUserHitStr){
CardUserHit cardUserHit= JSON.parseObject(cardUserHitStr,CardUserHit.class);
cardUserHitService.addCardUserHit(cardUserHit);
}
}

View File

@ -78,6 +78,11 @@ spring:
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
rabbitmq:
host: 192.168.157.129
password: guest
username: guest
port: 5672
# token配置
token:
@ -85,8 +90,8 @@ token:
header: Authorization
# 令牌密钥
secret: abcdefghijklmnopqrstuvwxyz
# 令牌有效期(默认30分钟
expireTime: 30
# 令牌有效期(默认1200分钟
expireTime: 1200
# MyBatis配置
#mybatis:

View File

@ -63,5 +63,32 @@
INNER JOIN system_dict ON card_game.type = system_dict.dict_key and system_dict.dict_type='gametype'
where card_game.id=#{id}
</select>
<select id="showAllCardGame" resultType="java.util.HashMap">
SELECT
id,
title,
start_time,
end_time,
CASE
WHEN NOW() &lt;`start_time` THEN '未开始'
WHEN NOW() BETWEEN `start_time` AND `end_time` THEN '进行中'
ELSE '已结束'
END AS status
FROM
`card_game`;
</select>
<select id="getCardByDate" resultType="com.ruoyi.project.hit.domain.CardGame">
SELECT * FROM card_game
WHERE status = 0
AND start_time &gt; #{now}
AND start_time &lt;DATE_ADD(#{now}, INTERVAL 5 MINUTE)
</select>
<select id="getCardGameBygameid" resultType="java.util.HashMap">
SELECT cg.id,cg.title,cg.pic,cg.content,cg.start_time startTime,cg.end_time endTime,sd.dict_value dictValue
from card_game cg
INNER JOIN system_dict sd
on cg.type=sd.dict_key
where cg.id=#{gameid} and sd.dict_type='gametype'
</select>
</mapper>

View File

@ -28,4 +28,22 @@
<select id="getSumAmountByGameId" resultType="java.lang.Integer">
SELECT IFNULL(SUM(amount),0) FROM `card_game_product` WHERE gameid=#{gameid}
</select>
<select id="getGameProductList" resultType="com.ruoyi.project.uniapp.controller.vo.CardProductVo">
SELECT
card_game_product.amount,
card_product.id,
card_product.pname,
card_product.pic,
card_product.content,
card_product.price,
card_product.del_state AS delState
FROM
card_game_product
INNER JOIN
card_product
ON
card_game_product.productid = card_product.id
WHERE card_game_product.gameid=#{id}
</select>
</mapper>

View File

@ -27,6 +27,7 @@
INNER JOIN system_dict ON card_game_rule.levelid = system_dict.dict_key AND system_dict.dict_type='userlevel'
WHERE card_game_rule.gameid=#{id}
</select>
<select id="getHitCountSumByGameId" resultType="java.lang.Integer">
SELECT IFNULL(SUM(hit_count),0) FROM `card_game_rule` WHERE gameid=#{gameid}
</select>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.project.hit.mapper.CardGameUserMapper">
<resultMap id="BaseResultMap" type="com.ruoyi.project.hit.domain.CardGameUser">
<id property="id" column="id" jdbcType="INTEGER"/>
<result property="userid" column="userid" jdbcType="INTEGER"/>
<result property="gameid" column="gameid" jdbcType="INTEGER"/>
<result property="createtime" column="createtime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id,userid,gameid,
createtime
</sql>
</mapper>

View File

@ -39,13 +39,7 @@
</if>
</where>
</select>
<select id="getCardGameBygameid" resultType="java.util.Map">
SELECT cg.id,cg.title,cg.pic,cg.content,cg.start_time startTime,cg.end_time endTime,sd.dict_value dictValue
from card_game cg
INNER JOIN system_dict sd
on cg.type=sd.dict_key
where cg.id=#{gameid} and sd.dict_type='gametype'
</select>
<select id="getProductsBygameid" resultType="java.util.HashMap">
SELECT cp.id,cp.pname,cgp.amount,IFNULL(cn,0) hitCount
FROM card_game_product cgp
@ -97,4 +91,22 @@
</if>
</where>
</select>
<select id="winnerRecord" resultType="java.util.HashMap">
SELECT
card_product.pname,
card_user_hit.hittime,
card_game.title
FROM
card_user_hit
INNER JOIN
card_game
ON
card_user_hit.gameid = card_game.id
INNER JOIN
card_product
ON
card_user_hit.productid = card_product.id
WHERE
card_user_hit.userid = #{userId}
</select>
</mapper>

View File

@ -16,7 +16,7 @@
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
<result property="delState" column="del_state" jdbcType="INTEGER"/>
<result property="remark" column="remark" jdbcType="VARCHAR"/>
<!-- <result property="remark" column="remark" jdbcType="VARCHAR"/>-->
</resultMap>
<sql id="Base_Column_List">
@ -26,6 +26,7 @@
phone,level,create_time,
update_time,del_state,remark
</sql>
<select id="selectCardUserList" resultType="java.util.HashMap">
SELECT u.id,u.user_name userName,u.pic,u.realname,u.id_card idCard,u.phone,u.create_time createTime,u.update_time updateTime,d.dict_value dictValue
FROM `card_user` u,`system_dict` d