package com.castalia.market.blh.impl;
import java.io.Serializable;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import com.castalia.base.util.ObjectUtils;

import com.castalia.market.util.IPUtil;
import com.castalia.market.util.ServerDetector;
import com.castalia.market.vo.CampaignPrizeRange;
import com.castalia.market.vo.ConfigSms;
import com.castalia.market.blh.IgetCampaignPrizeFromCacheBlh;
import com.castalia.market.dao.ICampaignPrizeDao;

public class CampaignPrizeCacheBlhImpl  {
    
    private static final Logger logger = Logger.getLogger(CampaignPrizeCacheBlhImpl.class);
    
        private ConfigSms configSms;
        private JdbcTemplate jdbcTemplate;
        private ICampaignPrizeDao campaignPrizeDao;
          private IgetCampaignPrizeFromCacheBlh getCampaignPrizeFromCache;
        public ConfigSms getConfigSms() {
            return configSms;
        }
        public void setConfigSms(ConfigSms configSms) {
            this.configSms = configSms;
        }
        
        public JdbcTemplate getJdbcTemplate() {
            return jdbcTemplate;
        }
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }

        public IgetCampaignPrizeFromCacheBlh getGetCampaignPrizeFromCache() {
            return getCampaignPrizeFromCache;
        }
        public void setGetCampaignPrizeFromCache(
                IgetCampaignPrizeFromCacheBlh getCampaignPrizeFromCache) {
            this.getCampaignPrizeFromCache = getCampaignPrizeFromCache;
        }
        public ICampaignPrizeDao getCampaignPrizeDao() {
        return campaignPrizeDao;
    }
    public void setCampaignPrizeDao(ICampaignPrizeDao campaignPrizeDao) {
        this.campaignPrizeDao = campaignPrizeDao;
    }
    private void loadData(String serverIndex, long campaignId,long prizeTypeId )
    {
          String key=serverIndex+campaignId+prizeTypeId;
          String procedure = "{call campaignprize_range(?,?,?,?)}";
            Object[] objs =ObjectUtils. asArray(campaignId, prizeTypeId, serverIndex);
             String serverName = "tomcat";
                if(ServerDetector.isTomcat()){
                    serverName = "tomcat";
                }
                else if(ServerDetector.isWebSphere()){
                    serverName = "webSphere";
                }
                logger.info("服务器名称为: "+serverName);
            logger.info("执行存储过程,参数:"+serverIndex+","+campaignId+","+prizeTypeId);
            Object retval = campaignPrizeDao.runProcedure(procedure, objs, serverName);
            String retStr=retval.toString();
            logger.info("执行结束,结果:"+retStr);
            try{
            if(retStr!="")
            { 
                if(retStr.toCharArray()[0]!='E')
                {
                    retStr=retStr.substring(0,retStr.length()-1);
                    List<CampaignPrizeRange> campaignPrizeRanges=new ArrayList<CampaignPrizeRange>();
                     StringTokenizer stringTokenizer = new StringTokenizer(retStr, "$");
                        while (stringTokenizer.hasMoreTokens()) {
                            String level1Token = stringTokenizer.nextToken();
                            
                            CampaignPrizeRange campaignPrizeRange =new CampaignPrizeRange();
                            String[] StartAndEnd=level1Token.split(",");
                            if(StartAndEnd==null||StartAndEnd.length<2){
                                continue;
                            }
                            logger.info("startAndEnd---[0]:"+StartAndEnd[0]);
                            logger.info("startAndEnd---[1]:"+StartAndEnd[1]);
                            campaignPrizeRange.setStartValue(Long.parseLong(StartAndEnd[0]));
                            campaignPrizeRange.setEndValue(Long.parseLong(StartAndEnd[1]));
                            campaignPrizeRanges.add(campaignPrizeRange);
                        }
                    
                    getCampaignPrizeFromCache.saveToCache(key,campaignPrizeRanges);
                }
             
            }
            }
            catch(Exception e)
            {
                logger.info(e.getMessage());
               logger.info("getcampaignprize error:"+campaignId+" "+prizeTypeId);
            }
    
    }
    public class getCampaignPrizeRangeBlh implements Runnable {
            
            private String serverIndex;
            private long campaignId,prizeTypeId;
            public getCampaignPrizeRangeBlh(String ServerIndex, long CampaignId,long PrizeTypeId)
            {
             this.serverIndex=ServerIndex;
             this.campaignId=CampaignId;
             this.prizeTypeId=PrizeTypeId;
            }
            @Override
            public void run() {
                // TODO Auto-generated method stub
                loadData( this.serverIndex, this.campaignId,this.prizeTypeId);
          }
        }
    public CampaignPrizeCacheBlhImpl(ConfigSms configSms,JdbcTemplate jdbcTemplate,ICampaignPrizeDao campaignPrizeDao,IgetCampaignPrizeFromCacheBlh getCampaignPrizeFromCacheBlh )
    {
        this.configSms=configSms;
        this.jdbcTemplate=jdbcTemplate;
        this.campaignPrizeDao=campaignPrizeDao;
        this.getCampaignPrizeFromCache=getCampaignPrizeFromCacheBlh;
         CacheCampaiginPrize();    
    }
    
    public void CacheCampaiginPrize() {
        // TODO Auto-generated method stub
         String localHostAddress = IPUtil.getAddress();
       
         String[] hashServerArra=configSms.getHashServer().split(",");
         String ServerNum="";
         for(int i=0;i<hashServerArra.length;i++)
         {
              if(hashServerArra[i].trim().contains(localHostAddress))
              {
                  ServerNum=String.format("%02d", i);
                  break;
              }
         }
         if(ServerNum=="")
         { 
            logger.info("当前服务器IP:"+localHostAddress+"分列数据配置文件中配置的服务器无此地址,无法完成分列数据到缓存,无法进行后续的派奖工作");
            return;
         }
         getCampaignPrizeFromCache.saveToCache("ServerIndex",ServerNum);
         startHashCampaignPrize(ServerNum);
    }
    private void startHashCampaignPrize(String ServerIndex)
    {   
        try
        {
         String sql = "select distinct t.campaignid,t.prizetypeid from mkt_campaignprize t join mkt_marketcampaign m on t.campaignid=m.campaignid and m.state=0 where t.issend=0 and t.hashcode like '"+ServerIndex+"%' group by t.campaignid, t.prizetypeid ";
         List rows = jdbcTemplate.queryForList(sql);
         Iterator it = rows.iterator();
         while(it.hasNext()) {
              Map queryresult = (Map) it.next();
              long campaigid =Long.parseLong( queryresult.get("campaignid").toString());
              long prizetypeid =Long.parseLong(queryresult.get("prizetypeid").toString());
              logger.info(prizetypeid);
             // loadData(ServerIndex,campaigid,prizetypeid);
              new Thread(new getCampaignPrizeRangeBlh(ServerIndex,campaigid,prizetypeid)).start();
         }
        }
        catch(Exception e)
        {
            logger.info(e.getMessage());
        }
    }

}

 

posted on 2015-07-20 16:43  linbl  阅读(744)  评论(0编辑  收藏  举报