Authored by mingdan.ge

cps4期达人排行

... ... @@ -14,7 +14,9 @@ public enum ShareOrdersKeyEnum {
SETTLEMENT_LIST("yh:union:share:settlementList:","key:{}:page:{}:limit:{}",600,"提现列表"),
UNION_TYPE("yh:union:share:unionType:","key:{}",600,"用户登录时获取对应的渠道号"),
RECENTLY_ORDER_LIMIT_TEN("yh:union:share:recentlyOrderLimitTen:","key:{}",600,"用户前台获取个人推广近期订单(10个)"),
ORDER_STATISTICS_INFO("yh:union:share:statisticsInfo:","uid:{}:queryTimeType:{}",600,"统计数据");
ORDER_STATISTICS_INFO("yh:union:share:statisticsInfo:","uid:{}:queryTimeType:{}",600,"统计数据"),
RANK_LIST("yh:union:share:ranklist:","type:{}:start:{}:limit:{}",600,"达人排行榜"),
RANK_INFO("yh:union:share:rank:","uid:{}type:{}:",600,"个人排行");
private String preKey;
... ...
... ... @@ -118,6 +118,33 @@ public class RedisHashCache {
}
return null;
}
/**
* 根据key获取hash数据size
*
* @param cacheKey key前缀
* @param obj key后缀
* @return size
*/
public int size(String cacheKey,Object obj) {
try {
if (StringUtils.isEmpty(cacheKey)) {
return 0;
}
logger.info("RedisHashCache get size,cacheKey is: {},obj is: {}", cacheKey,obj);
RedisKeyBuilder redisKeyBuilder = RedisKeyBuilder.newInstance().appendFixed(cacheKey).appendVar(obj);
Long size = this.hashOperations.size(redisKeyBuilder);
if (size == null||size<0) {
return 0;
}
return size.intValue();
} catch (Exception e) {
logger.warn("RedisHashCache get size failed!!! cacheKey is: {},obj is: {},e is {}", cacheKey,obj,e);
}
return 0;
}
//
// /**
// * 根据key和hashkey获取数据
... ...
... ... @@ -3,6 +3,7 @@ package com.yoho.unions.dal;
import java.math.BigDecimal;
import java.util.List;
import com.yoho.service.model.union.bo.UninoShareIncomeBo;
import com.yoho.service.model.union.request.UnionShareOrdersMonthReqBO;
import com.yoho.unions.dal.model.UnionShareOrdersMonth;
import org.apache.ibatis.annotations.Param;
... ... @@ -17,6 +18,8 @@ public interface UnionShareOrdersMonthMapper {
UnionShareOrdersMonth selectByPrimaryKey(Integer id);
List<UnionShareOrdersMonth> selectMonthData(@Param("date") int date, @Param("promoteUid") Integer promoteUid, @Param("type") Integer type);
List<UninoShareIncomeBo> selectMonthRankList(@Param("date") int date, @Param("start") Integer start, @Param("size") Integer size);
List<UninoShareIncomeBo> selectTotalRankList(@Param("start") Integer start, @Param("size") Integer size);
BigDecimal selectAmountByUid(@Param("date") Integer date, @Param("promoteUid") Integer promoteUid, @Param("type") Integer type);
... ...
... ... @@ -32,6 +32,20 @@
where uid = #{promoteUid,jdbcType=INTEGER} and date=#{date,jdbcType=INTEGER} and type=#{type,jdbcType=INTEGER}
order by id desc
</select>
<select id="selectMonthRankList" resultType="com.yoho.service.model.union.bo.UninoShareIncomeBo" >
select amount ,uid,nickname,image,order_num as orderNum
from union_share_orders_month
where date=#{date,jdbcType=INTEGER}
order by amount desc
limit #{start},#{size}
</select>
<select id="selectTotalRankList" resultType="com.yoho.service.model.union.bo.UninoShareIncomeBo" >
select sum(amount) as amount ,uid,nickname,image,order_num as orderNum
from union_share_orders_month
group by uid
order by sum(amount) desc
limit #{start},#{size}
</select>
<select id="selectAmountByUid" resultType="java.math.BigDecimal" >
select sum(amount)
from union_share_orders_month
... ...
... ... @@ -4,6 +4,8 @@ import com.yoho.core.common.utils.DateUtil;
import com.yoho.core.common.utils.JsonUtil;
import com.yoho.core.rabbitmq.YhConsumer;
import com.yoho.service.model.union.bo.ShareOrderBo;
import com.yoho.unions.common.enums.ShareOrdersKeyEnum;
import com.yoho.unions.common.redis.RedisHashCache;
import com.yoho.unions.server.service.IUnionShareService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -23,6 +25,9 @@ public class UnionShareOrderConsumer implements YhConsumer {
@Autowired
private IUnionShareService unionShareService;
@Autowired
private RedisHashCache redisHashCache;
@Override
public void handleMessage(Object o) throws Exception{
logger.info("UnionShareOrderConsumer,handleMessage {}", o);
... ... @@ -59,6 +64,10 @@ public class UnionShareOrderConsumer implements YhConsumer {
}
});
logger.info("UnionShareOrderConsumer,updateMonthData end,uids is {},o is {}",uids, o);
//清达人相关redis
int date = Integer.valueOf(com.yoho.unions.common.utils.DateUtil.getToday("yyyyMMdd"));
redisHashCache.delete(ShareOrdersKeyEnum.RANK_LIST.getPreKey(),date);
redisHashCache.delete(ShareOrdersKeyEnum.RANK_INFO.getPreKey(),date);
} catch (Exception e) {
logger.warn("UnionShareOrderConsumer,handleMessage fail! obj is {}, e {}",o,e.getMessage());
}
... ...
... ... @@ -4,14 +4,7 @@ import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
... ... @@ -172,6 +165,10 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
private String UNION_SHAREID = "union.shareId";
private String UNION_CPS_IMAGEURL = "union.cps.ImageUrl";
private String UNION_CPS_APPLY_IMAGEURL = "union.cps.applyImageUrl";
private String UNION_CPS_RANK_LIST_SWITCH = "union.cps.rankList.switch";
private String UNION_CPS_RANK_INFO_SWITCH = "union.cps.rankInfo.switch";
private String UNION_CPS_RANK_MONTHDAY = "union.cps.rank.monthday";
private String UNION_CPS_RANK_HOUR = "union.cps.rank.hour";
//USER_SETTLEMENT下hashkey
private String USER_SETTLEMENT_SETTLEAMOUNT = "settleAmount";
... ... @@ -185,6 +182,8 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
private static final byte STATISTICS_QUERY_TIME_LAST_MONTH = 3;
private static final int RANK_LIST_MAX_NUM = 100;
/**
* 获取用户可提现金额、已提现金额、是否可以提现
* */
... ... @@ -1105,39 +1104,130 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
@Override
public UninoShareIncomeRankBo queryRank(UninoShareIncomeRankReqBo req){
UninoShareIncomeRankBo result = new UninoShareIncomeRankBo();
//todo 查询排行榜
PageResponseBO<UninoShareIncomeBo> rankList = getRankList(req);
result.setRankList(rankList);
//每月一号0-8点排行榜关闭
Calendar now = Calendar.getInstance();
// int monthday=configReader.getInt(UNION_CPS_RANK_MONTHDAY,1);
// int hour=configReader.getInt(UNION_CPS_RANK_HOUR,8);
if (now.get(Calendar.DAY_OF_MONTH) == 1 && now.get(Calendar.HOUR_OF_DAY) < 8) {
return result;
}
//查询排行榜
int date = Integer.valueOf(DateUtil.getToday("yyyyMMdd"));
if (req.getUid() != null) {
//todo 需要查询用户的数据
if (req.getDate() != null) {
BigDecimal amount = unionShareOrdersMonthMapper.selectAmountByUid(Integer.valueOf(req.getDate()), req.getUid(), 1);
if (amount == null) {
amount = new BigDecimal(0);
}
result.setAmount(amount);
DecimalFormat df = new DecimalFormat("0.00");
result.setAmountStr("¥"+df.format(result.getAmount()));
//todo result.setRankNum();
UserInfoRspBO userInfoRspBO = userServiceHelper.getUserInfoFromUic(req.getUid());
if (userInfoRspBO != null) {
result.setImage(userInfoRspBO.getHeadIco());
result.setNickname(userInfoRspBO.getNickname());
}
//查询用户的数据
UninoShareIncomeBo rankInfo=getRankInfo(req.getUid(), date, req.getType());
if (rankInfo == null) {
//可能是zk开关关闭
return result;
}
BeanUtils.copyProperties(rankInfo,result);
}
UninoShareIncomeRankBo rankList = queryRankList(date, req.getType(), req.getPage(), req.getSize());
if (rankList != null && rankList.getRankList() != null && CollectionUtils.isNotEmpty(rankList.getRankList().getList())) {
result.setRankList(rankList.getRankList());
}
return result;
}
//todo 获取排行榜数据
private PageResponseBO<UninoShareIncomeBo> getRankList(UninoShareIncomeRankReqBo req) {
List<UninoShareIncomeBo> all = new ArrayList<>();
PageResponseBO<UninoShareIncomeBo> result = new PageResponseBO<>();
result.setPage(req.getPage());
result.setTotal(all.size());
result.setSize(req.getSize());
result.setList(result.getPage()>1?all.subList((result.getPage()-1)*result.getSize()+1,result.getPage()*result.getSize()):null);
return result;
// 获取排行榜数据
private UninoShareIncomeRankBo queryRankList(int date,Integer type,int page,int size){
//查询zk中cps达人排行开关
if (!configReader.getBoolean(UNION_CPS_RANK_LIST_SWITCH, true)) {
return null;
}
//date 具体到日:20180814
int start = page > 1 ? (page - 1) * size : 0;
UninoShareIncomeRankBo cacheRank=getFromRedis(ShareOrdersKeyEnum.RANK_LIST, date, UninoShareIncomeRankBo.class, type.toString(), "" + start, "" + size);
if (cacheRank != null && CollectionUtils.isNotEmpty(cacheRank.getRankList().getList())) {
return cacheRank;
}
PageResponseBO<UninoShareIncomeBo> pagelist = new PageResponseBO<>();
if(start>0||size<RANK_LIST_MAX_NUM){
cacheRank = getFromRedis(ShareOrdersKeyEnum.RANK_LIST, date, UninoShareIncomeRankBo.class, "" + type, "" + 0, "" + RANK_LIST_MAX_NUM);
}
PageResponseBO<UninoShareIncomeBo> ranklist = new PageResponseBO<>();
if (cacheRank == null||CollectionUtils.isEmpty(cacheRank.getRankList().getList())) {
ranklist = queryRankListFromSql(date, type, RANK_LIST_MAX_NUM);
}
if (ranklist == null||CollectionUtils.isEmpty(ranklist.getList())) {
return null;
}
pagelist = new PageResponseBO<>();
int toIndex = start + size - 1;
if (toIndex > ranklist.getList().size()) {
toIndex = ranklist.getList().size();
}
pagelist.setList(ranklist.getList().subList(start, toIndex));
pagelist.setPage(page > 1 ?page:1);
pagelist.setSize(size);
pagelist.setTotal(pagelist.getList().size());
UninoShareIncomeRankBo uninoShareIncomeRankBo = new UninoShareIncomeRankBo(pagelist);
addToRedis(ShareOrdersKeyEnum.RANK_LIST,date,uninoShareIncomeRankBo,""+type,""+start,""+size);
return uninoShareIncomeRankBo;
}
//获取用户达人排行个人信息
private UninoShareIncomeBo getRankInfo(Integer uid,int date,Integer type) {
//查询zk中cps达人排行个人信息开关
if (!configReader.getBoolean(UNION_CPS_RANK_INFO_SWITCH, true)) {
return null;
}
UninoShareIncomeBo rankInfo = getFromRedis(ShareOrdersKeyEnum.RANK_INFO,date,UninoShareIncomeBo.class,uid.toString(), type.toString());
if (rankInfo != null) {
return rankInfo;
}
//
UninoShareIncomeRankBo rankList=queryRankList(date,type,0, RANK_LIST_MAX_NUM);
if (rankList != null && rankList.getRankList() != null && CollectionUtils.isNotEmpty(rankList.getRankList().getList())) {
for (UninoShareIncomeBo bo : rankList.getRankList().getList()) {
if (bo.getUid() == uid) {
return bo;
}
}
}
rankInfo = new UninoShareIncomeBo();
BigDecimal amount = unionShareOrdersMonthMapper.selectAmountByUid(date/100, uid, 1);//类型:1-联盟用户,2-马甲用户
if (amount == null) {
amount = new BigDecimal(0);
}
rankInfo.setAmount(amount);
DecimalFormat df = new DecimalFormat("0.00");
rankInfo.setAmountStr("¥"+df.format(rankInfo.getAmount()));
UserInfoRspBO userInfoRspBO = userServiceHelper.getUserInfoFromUic(uid);
if (userInfoRspBO != null) {
rankInfo.setImage(userInfoRspBO.getHeadIco());
rankInfo.setNickname(userInfoRspBO.getNickname());
}
addToRedis(ShareOrdersKeyEnum.RANK_INFO,date,rankInfo,uid.toString(), type.toString());
return rankInfo;
}
private PageResponseBO<UninoShareIncomeBo> queryRankListFromSql(int date,int type,int maxSize) {
List<UninoShareIncomeBo> list;
int monthDate = date / 100;//date 具体到日:20180814
if (type==1) {//1-本月,2-全部
list = unionShareOrdersMonthMapper.selectMonthRankList(monthDate, 0, maxSize);
}else {
list = unionShareOrdersMonthMapper.selectTotalRankList(0, maxSize);
}
if (CollectionUtils.isEmpty(list)) {
return null;
}
DecimalFormat df = new DecimalFormat("0.00");
for (int i = 0; i < list.size(); i++) {
UninoShareIncomeBo uninoShareIncomeBo = list.get(i);
uninoShareIncomeBo.setRankNum(i+1);
uninoShareIncomeBo.setAmountStr("¥"+df.format(uninoShareIncomeBo.getAmount()));
}
PageResponseBO<UninoShareIncomeBo> pageList = new PageResponseBO<>();
pageList.setList(list);
pageList.setPage(0);
pageList.setSize(list.size());
pageList.setTotal(list.size());
addToRedis(ShareOrdersKeyEnum.RANK_LIST,date,new UninoShareIncomeRankBo(pageList),""+type,"0",""+RANK_LIST_MAX_NUM);
return pageList;
}
... ... @@ -1879,13 +1969,13 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
}
//存入redis,注意param的顺序
public void addToRedis(ShareOrdersKeyEnum se, int uid,Object value, String... param) {
redisHashCache.put(se.getPreKey(),uid,se.getHashKey(param),value,se.getCacheTime(), TimeUnit.SECONDS);
public void addToRedis(ShareOrdersKeyEnum se, int key,Object value, String... param) {
redisHashCache.put(se.getPreKey(),key,se.getHashKey(param),value,se.getCacheTime(), TimeUnit.SECONDS);
}
//从redis取数据
public <T> T getFromRedis(ShareOrdersKeyEnum se, int uid,Class<T> clazz, String... param) {
return redisHashCache.get(se.getPreKey(), uid, se.getHashKey(param), clazz);
public <T> T getFromRedis(ShareOrdersKeyEnum se, int key,Class<T> clazz, String... param) {
return redisHashCache.get(se.getPreKey(), key, se.getHashKey(param), clazz);
}
public void clearShareOrderRedis(int uid) {
... ...
... ... @@ -2,4 +2,8 @@ union.newUserRebatesRatio=10
union.oldUserRebatesRatio=3
union.shareId = 4649
union.cps.ImageUrl = http://img12.static.yhbimg.com/article/2018/05/29/20/02044ae38a825a2faee5fcbd84eeab20a5.jpg
union.cps.applyImageUrl = http://img10.static.yhbimg.com/article/2018/05/23/16/017c0037ac8304c204686f5045c5eb9769.jpeg
\ No newline at end of file
union.cps.applyImageUrl = http://img10.static.yhbimg.com/article/2018/05/23/16/017c0037ac8304c204686f5045c5eb9769.jpeg
union.cps.rank.hour=8
union.cps.rank.monthday=1
union.cps.rankInfo.switch=true
union.cps.rankList.switch=true
\ No newline at end of file
... ...