PurchaseHandler.java 2.67 KB
package com.dili.titan.handler;

import com.dili.titan.domain.RedisKeyConstant;
import com.dili.titan.domain.gq.Purchase;
import com.dili.titan.domain.log.LogHelper;
import com.dili.titan.domain.log.LogTypeEnum;
import com.dili.titan.mq.sender.TopicProducer;
import com.dili.titan.redis.JedisClient;
import com.dili.titan.service.impl.PurchaseService;
import org.apache.commons.collections.CollectionUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * <B>Description</B> 求购信息 <br />
 * <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
 * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
 * <B>Company</B> 地利集团
 * @createTime 2014年12月22日 下午6:24:00
 * @author ywd
 */
@Component
public class PurchaseHandler implements BaseHandler {

    private ObjectMapper mapper = new ObjectMapper().configure(
            DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

     @Resource
     private PurchaseService purchaseService;

    @Resource(name = "jedisClient")
    private JedisClient jedisClient;

    @Resource(name = "topicProducer")
    private TopicProducer topicProducer;

    @Override
    public void handle(String message) throws Exception {
        try {
            JsonNode node = mapper.readTree(message);
            int operType = node.get("oper").asInt();
            Long pid = node.get("pid").asLong();
            put(pid);
            topicProducer.sendMQPurchase(pid,operType);
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.PURCHASE, e, e.getMessage());
            throw e;
        }
    }

    /**
     * this method is 根据供应ID存储供应
     * @createTime 2014年12月22日 下午5:14:24
     * @author ywd
     */
    private void put(Long pid)throws Exception{
        try {
            Purchase purchase= purchaseService.findByKey(pid);
            if(purchase == null){
                LogHelper.error(LogTypeEnum.PURCHASE ,
                        "can't find Purchase by pid={}", pid);
                return;
            }
            String json = mapper.writeValueAsString(purchase);
            jedisClient.hput(RedisKeyConstant.PURCHASE,purchase.getId().toString(), json,JedisClient.expireTime);
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.PURCHASE, e,
                    "load putPurchase error!pid={}", pid);
            throw e;
        }
    }
    
}