UnionShareOrderConsumer.java 3.01 KB
package com.yoho.unions.server.mqconsumer;

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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Created by mingdan.ge on 2018/5/10.
 */
@Component
public class UnionShareOrderConsumer implements YhConsumer {

    private Logger logger = LoggerFactory.getLogger("cps_mq");

    @Autowired
    private IUnionShareService unionShareService;

    @Autowired
    private RedisHashCache redisHashCache;

    @Override
    public void handleMessage(Object o) throws Exception{
        logger.info("UnionShareOrderConsumer,handleMessage {}", o);
        try {
            if (null == o) {
                return;
            }

            List<Object> list = JsonUtil.jsonToObject(o.toString(), List.class);
            Map<Integer,Set<String>> uids = new HashMap<>();
            list.forEach(l->{
                try {
                    ShareOrderBo bo = JsonUtil.jsonToObject(l.toString(), ShareOrderBo.class);
                    // 订单插入或更新
                    unionShareService.saveOrUpdateOrder(bo);
                    Set<String> dates=uids.get(bo.getPromoteUid());
                    if (dates == null) {
                        dates = new HashSet<>();
                    }
                    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());
                }
            });
            logger.info("UnionShareOrderConsumer,begin to updateMonthData,uids is {},o is {}",uids, o);
            //更新dates月用户月收益数据
            uids.forEach((u,dates)->{
                //todo 更新uid月预估收益,里面是否需要校验一下log表和order表
                try {
                    unionShareService.updateMonthData(u,dates);
                } catch (Exception e) {
                    logger.warn("UnionShareOrderConsumer,updateMonthData error,uid is {},dates is {},e is {}",u, dates,e.getMessage());
                }
            });
            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);
        } catch (Exception e) {
            logger.warn("UnionShareOrderConsumer,handleMessage fail! obj is {}, e {}",o,e.getMessage());
        }
    }
}