Authored by mingdan.ge

cps月统计数据

... ... @@ -3,11 +3,17 @@
*/
package com.yoho.unions.helper;
import com.netflix.config.DynamicLongProperty;
import com.netflix.config.DynamicPropertyFactory;
import com.yoho.core.rest.client.ServiceCaller;
import com.yoho.core.rest.client.hystrix.AsyncFuture;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yoho.service.model.order.request.OrdersStatusStatisticsRequest;
import com.yoho.service.model.order.response.CountBO;
import com.yoho.service.model.response.UserInfoRspBO;
import com.yoho.service.model.union.request.UnionUicUserReqBO;
import com.yoho.unions.common.redis.RedisValueCache;
import com.yoho.unions.vo.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
... ... @@ -17,42 +23,50 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @author ping.huang
* 2016年12月29日
* 2016年12月29日
*/
@Component
public class UserServiceHelper {
static Logger log = LoggerFactory.getLogger(UserServiceHelper.class);
@Resource
ServiceCaller serviceCaller;
@Resource
RedisValueCache redisValueCache;
@Resource
ClientSecretHelper clientSecretHelper;
@Value("${gateway.url}")
private String gatewayUrl;
private static final String USERINFO_KEY = "yh:union:share:userinfo:";
/**
* 根据uid,查询订单数
*
* @param uid
* @return
* @throws ServiceException
*/
public CountBO getOrderCountByUid(int uid) throws ServiceException {
log.info("start with getOrderCountByUid. uid is {}", uid);
if (uid == 0) {
log.warn("getOrderCountByUid error with uid={}", uid);
return null;
}
OrdersStatusStatisticsRequest request = new OrdersStatusStatisticsRequest();
request.setUid(uid);
CountBO result = serviceCaller.call("order.getOrdersCountByUidAndStatus", request, CountBO.class);
log.info("getOrderCountByUid success with response={}. uid is {}", result, uid);
request.setUid(uid);
CountBO result = serviceCaller.call("order.getOrdersCountByUidAndStatus", request, CountBO.class);
log.info("getOrderCountByUid success with response={}. uid is {}", result, uid);
return result;
}
... ... @@ -72,11 +86,41 @@ public class UserServiceHelper {
AsyncFuture<ApiResponse> response = serviceCaller.get("user.getProfileByUid", gatewayUrl + "?" + param, null, ApiResponse.class, null);
ApiResponse apiResponse = response.get();
if(apiResponse.getCode() != 200){
if (apiResponse.getCode() != 200) {
log.warn("call app.passport.profile error e={}", apiResponse.getMessage());
return new ApiResponse();
}
log.info("getProfileByUid success with response={}. uid is {}", apiResponse, uid);
return apiResponse;
}
public UserInfoRspBO getUserInfoFromUic(int uid) throws ServiceException {
if (1 > uid) {
log.warn("getUserInfoFromUic: param uid is null.");
throw new ServiceException(ServiceError.UID_IS_NULL);
}
UserInfoRspBO userInfo = redisValueCache.get(USERINFO_KEY, UserInfoRspBO.class);
if (userInfo != null) {
log.info("getUserInfoFromUic: get userInfo from cache: uid is {}", uid);
return userInfo;
}
try {
UnionUicUserReqBO uicUserReqBO = new UnionUicUserReqBO();
uicUserReqBO.setYohoUid(uid);
uicUserReqBO.setChannel(1);
userInfo = serviceCaller.call("uic.getUserInfoByYohoUid", uicUserReqBO, UserInfoRspBO.class);
if (userInfo == null) {
return null;
}
redisValueCache.set(USERINFO_KEY+uid,userInfo,180, TimeUnit.SECONDS);
log.info("getUserInfoFromUic: end call uic.getUserInfo, uid is {}, userInfo is {}", uid, userInfo);
} catch (Exception e) {
log.warn("get user info from uic failed, uid is {}", uid);
return new UserInfoRspBO();
}
return userInfo;
}
}
... ...
... ... @@ -3,6 +3,7 @@ package com.yoho.unions.dal;
import com.yoho.service.model.union.bo.UnionShareOrdersUidBo;
import com.yoho.service.model.union.request.UnionShareOrderReqBO;
import com.yoho.unions.dal.model.UnionShareOrders;
import com.yoho.unions.dal.model.UnionShareOrdersMonth;
import org.apache.ibatis.annotations.Param;
import java.math.BigDecimal;
... ... @@ -38,6 +39,8 @@ public interface UnionShareOrdersMapper {
BigDecimal selectAmountBySettleCode(@Param("promoteUid")int promoteUid,@Param("settlementCode")String settlementCode);
UnionShareOrdersMonth selectMonthData(@Param("beginTime") int beginTime, @Param("endTime") int endTime, @Param("promoteUid") Integer promoteUid);
List<UnionShareOrders> selectListByCondition(@Param("promoteUid")int promoteUid, @Param("status")String status,@Param("updateTime")int updateTime, @Param("offset") int offset, @Param("rows") int rows);
List<String> selectOrderCodesByCondition(@Param("settlementCode")String settlementCode, @Param("offset") int offset, @Param("rows") int rows);
... ...
package com.yoho.unions.dal;
import java.math.BigDecimal;
import java.util.List;
import com.yoho.service.model.union.request.UnionShareOrdersMonthReqBO;
import com.yoho.unions.dal.model.UnionShareOrdersMonth;
import org.apache.ibatis.annotations.Param;
public interface UnionShareOrdersMonthMapper {
int deleteByPrimaryKey(Integer id);
... ... @@ -14,6 +16,12 @@ public interface UnionShareOrdersMonthMapper {
UnionShareOrdersMonth selectByPrimaryKey(Integer id);
List<UnionShareOrdersMonth> selectMonthData(@Param("date") int date, @Param("promoteUid") Integer promoteUid, @Param("type") Integer type);
BigDecimal selectAmountByUid(@Param("date") Integer date, @Param("promoteUid") Integer promoteUid, @Param("type") Integer type);
int updateByUidAndMonth(UnionShareOrdersMonth record);
int updateByPrimaryKeySelective(UnionShareOrdersMonth record);
int updateByPrimaryKey(UnionShareOrdersMonth record);
... ...
... ... @@ -360,6 +360,17 @@
and order_time &lt;#{endTime}
</if>
</select>
<select id="selectMonthData" resultType="com.yoho.unions.dal.model.UnionShareOrdersMonth">
select sum(amount) as amount,sum(extra_amount) as extraAmount,sum(order_amount) as orderAmount,count(order_code) as orderNum
from union_share_orders
where promote_uid = #{promoteUid} and status in (10,20,30,40)
<if test="beginTime != 0" >
and order_time &gt;= #{beginTime}
</if>
<if test="endTime != 0" >
and order_time &lt; #{endTime}
</if>
</select>
<select id="selectActivityOrderByUid" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
... ...
... ... @@ -25,6 +25,21 @@
from union_share_orders_month
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectMonthData" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from union_share_orders_month
where uid = #{promoteUid,jdbcType=INTEGER} and date=#{date,jdbcType=INTEGER} and type=#{type,jdbcType=INTEGER}
order by id desc
</select>
<select id="selectAmountByUid" resultType="java.math.BigDecimal" >
select sum(amount)
from union_share_orders_month
where uid = #{promoteUid,jdbcType=INTEGER} and type=#{type,jdbcType=INTEGER}
<if test="date != null">
and date=#{date,jdbcType=INTEGER}
</if>
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from union_share_orders_month
where id = #{id,jdbcType=INTEGER}
... ... @@ -120,6 +135,34 @@
</if>
</trim>
</insert>
<update id="updateByUidAndMonth" parameterType="com.yoho.unions.dal.model.UnionShareOrdersMonth" >
update union_share_orders_month
<set >
<if test="nickname != null" >
nickname = #{nickname,jdbcType=INTEGER},
</if>
<if test="image != null" >
image = #{image,jdbcType=INTEGER},
</if>
<if test="amount != null" >
amount = #{amount,jdbcType=DECIMAL},
</if>
<if test="orderAmount != null" >
order_amount = #{orderAmount,jdbcType=DECIMAL},
</if>
<if test="extraAmount != null" >
extra_amount = #{extraAmount,jdbcType=DECIMAL},
</if>
<if test="orderNum != null" >
order_num = #{orderNum,jdbcType=INTEGER},
</if>
<if test="updateTime != null" >
update_time = #{updateTime,jdbcType=INTEGER},
</if>
</set>
where uid = #{uid,jdbcType=INTEGER} and type = #{type,jdbcType=INTEGER} and date = #{date,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeySelective" parameterType="com.yoho.unions.dal.model.UnionShareOrdersMonth" >
update union_share_orders_month
<set >
... ...
... ... @@ -32,17 +32,17 @@ public class UnionShareOrderConsumer implements YhConsumer {
}
List<Object> list = JsonUtil.jsonToObject(o.toString(), List.class);
Map<Integer,Set<Integer>> uids = new HashMap<>();
Map<Integer,Set<String>> uids = new HashMap<>();
list.forEach(l->{
try {
ShareOrderBo bo = JsonUtil.jsonToObject(l.toString(), ShareOrderBo.class);
// 订单插入或更新
unionShareService.saveOrUpdateOrder(bo);
Set<Integer> dates=uids.get(bo.getPromoteUid());
Set<String> dates=uids.get(bo.getPromoteUid());
if (dates == null) {
dates = new HashSet<>();
}
dates.add(Integer.valueOf(DateUtil.getDateStrBySecond(bo.getOrderTime(),"yyyyMM")));
dates.add(DateUtil.getDateStrBySecond(bo.getOrderTime(),"yyyyMM"));
uids.put(bo.getPromoteUid(), dates);
} catch (Exception e) {
logger.warn("UnionShareOrderConsumer,handleMessage fail! bo is {}, e {}",l,e.getMessage());
... ...
... ... @@ -45,7 +45,7 @@ public interface IUnionShareService {
*/
void saveOrUpdateOrder(ShareOrderBo bo);
void updateMonthData(int promoteUid, Set<Integer> date);
void updateMonthData(int promoteUid, Set<String> date);
List<UnionShareOrdersActivity> queryWaitActivity();
... ...
... ... @@ -18,6 +18,8 @@ import java.util.stream.Collectors;
import javax.annotation.Resource;
import com.yoho.service.model.response.UserInfoRspBO;
import com.yoho.unions.helper.UserServiceHelper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.collect.Maps;
... ... @@ -155,6 +157,9 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
private ConfigReader configReader;
@Autowired
private UserServiceHelper userServiceHelper;
@Autowired
private RedisHashCache redisHashCache;
private AtomicInteger atomicInt = new AtomicInteger(0);
... ... @@ -1099,17 +1104,40 @@ 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);
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());
}
}
}
return null;
return result;
}
//获取排行榜数据
//todo 获取排行榜数据
private PageResponseBO<UninoShareIncomeBo> getRankList(UninoShareIncomeRankReqBo req) {
return null;
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;
}
... ... @@ -2035,8 +2063,63 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
* 更新联盟用户月预估收入
* */
@Override
public void updateMonthData(int promoteUid, Set<Integer> date){
@Database(ForceMaster = true)
public void updateMonthData(int promoteUid, Set<String> date){
date.forEach(d->{
updateOneMonthData(promoteUid, d);
});
}
private void updateOneMonthData(int promoteUid, String date){
//todo
Integer dateInt = Integer.valueOf(date);
Calendar time = Calendar.getInstance();
time.setTime(DateUtil.stringToDate(date, "yyyyMM"));
int beginTime=(int) time.getTimeInMillis()/1000;
time.add(Calendar.DAY_OF_MONTH,1);
int endTime=((int) time.getTimeInMillis()/1000)-1;
UnionShareOrdersMonth unionShareOrdersMonth = getMonthData(promoteUid,dateInt);
UnionShareOrdersMonth month=unionShareOrdersMapper.selectMonthData(promoteUid, beginTime, endTime);
if (month == null|| month.getAmount()==null) {
if (unionShareOrdersMonth != null) {
//月预估收益清了
deleteMonthData(unionShareOrdersMonth.getId());
}
return;
}
UserInfoRspBO userInfoRspBO = userServiceHelper.getUserInfoFromUic(promoteUid);
if (userInfoRspBO != null) {
month.setImage(userInfoRspBO.getHeadIco());
month.setNickname(userInfoRspBO.getNickname());
}
month.setType(1);//类型:1-联盟用户,2-马甲用户
month.setDate(dateInt);
month.setUpdateTime(DateUtil.getCurrentTimeSecond());
if (unionShareOrdersMonth != null) {
//月预估收益更新
int result=unionShareOrdersMonthMapper.updateByUidAndMonth(month);
if (result > 0) {
return;
}
}
month.setCreateTime(month.getUpdateTime());
unionShareOrdersMonthMapper.insertSelective(month);
}
private UnionShareOrdersMonth getMonthData(int promoteUid, int date) {
List<UnionShareOrdersMonth> unionShareOrdersMonth = unionShareOrdersMonthMapper.selectMonthData(date, promoteUid,1);
if (unionShareOrdersMonth == null ) {
return null;
}
for (int i = 1; i < unionShareOrdersMonth.size(); i++) {
deleteMonthData(unionShareOrdersMonth.get(i).getId());
}
return unionShareOrdersMonth.get(0);
}
private void deleteMonthData(int id) {
unionShareOrdersMonthMapper.deleteByPrimaryKey(id);
}
/**
... ... @@ -2046,4 +2129,5 @@ public class UnionShareServiceImpl implements IUnionShareService,IBusinessExport
public void dealWithVirtualUserTask(UnionShareVirtualAddBo bo){
//todo
}
//todo 后台操作向redis塞待执行任务
}
... ...