com.greenline.guahao.biz.manager.statistics.impl.StatisticsManagerImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.greenline.guahao.biz.manager.statistics.impl.StatisticsManagerImpl.java

Source

/*
 * Project: guahao-portal-biz-core
 * 
 * File Created at 2012-5-7
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package com.greenline.guahao.biz.manager.statistics.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;

import com.greenline.guahao.biz.enums.StatisticsTypeEnum;
import com.greenline.guahao.biz.manager.ResponseCode;
import com.greenline.guahao.biz.manager.cache.statistics.StatisticsCacheManager;
import com.greenline.guahao.biz.manager.favorite.FavoriteManager;
import com.greenline.guahao.biz.manager.order.OrderManager;
import com.greenline.guahao.biz.manager.order.dataobject.OrderDO;
import com.greenline.guahao.biz.manager.statistics.StatisticsManager;
import com.greenline.guahao.biz.manager.statistics.dataobject.MainItemDataDO;
import com.greenline.hrs.biz.service.StatisticService;
import com.greenline.hrs.biz.service.dto.MainItemDataDTO;
import com.greenline.hrs.biz.service.dto.ResponseDTO;

/**
 * 
 * @Type StatisticsManager
 * @Desc portal?
 * @author ?? @
 * @date 2012-10-17
 * @Version V1.0
 */
public class StatisticsManagerImpl implements StatisticsManager {

    private static final Log logs = LogFactory.getLog(StatisticsManagerImpl.class);

    @Resource
    private OrderManager orderManager;
    @Resource
    private StatisticsCacheManager statisticsCacheManager;
    @Resource
    private FavoriteManager favoriteManager;
    @Resource
    private StatisticService statisticService;

    @Override
    public String getStatistics(String type, String key, Integer fType) {
        String restr = "0";
        if (StatisticsTypeEnum.NOT_TREATMENT.getValue().equals(type)) {
            // 
            restr = String.valueOf(orderManager.getMyNotTreatmentOrderCount(key));
        } else if (StatisticsTypeEnum.NOT_CONFIRM.getValue().equals(type)) {
            // 
            List<OrderDO> orderList = orderManager.getAddShareList(Long.valueOf(key));
            if (CollectionUtils.isEmpty(orderList)) {
                restr = String.valueOf(0);
            } else {
                restr = String.valueOf(orderList.size());
            }
        } else if (StatisticsTypeEnum.NO_APPEND.getValue().equals(type)) {
            // 
            List<OrderDO> orderList = orderManager.getAddTreatmentList(Long.valueOf(key));
            if (CollectionUtils.isEmpty(orderList)) {
                restr = String.valueOf(0);
            } else {
                restr = String.valueOf(orderList.size());
            }
        } else if (StatisticsTypeEnum.NEED_PAY.getValue().equals(type)) {
            // 
            restr = String.valueOf(orderManager.getMyNeedPayOrderCount(key));
        } else if (StatisticsTypeEnum.STOP_TREAT.getValue().equals(type)) {
            // ?
            restr = String.valueOf(orderManager.getMyStopTreatOrderCount(key));
        } else if (StatisticsTypeEnum.U_FAVORITE_COUNT.getValue().equals(type)) {
            // ?//
            restr = String.valueOf(favoriteManager.getFavoriteCountByUserId(Long.valueOf(key), fType));
        }

        return restr;
    }

    @Override
    public void updateStatistics(String type, String key, Integer fType, String value) {
        try {
            String str = statisticsCacheManager.getStatistics(type, key, fType);
            if (StringUtils.isNotBlank(key) && StringUtils.isNotBlank(str)) {
                long total = Long.valueOf(str) + Long.valueOf(value);
                if (total < 0) {
                    // total<0????
                    statisticsCacheManager.delStatistics(type, key, fType);
                } else {
                    this.setStatistics(type, key, fType, String.valueOf(total));
                }
            }
        } catch (Exception e) {
            // 
            statisticsCacheManager.delStatistics(type, key, fType);
            logs.error("StatisticsManagerImpl".concat(type).concat(";").concat(key).concat(";")
                    .concat(fType + StringUtils.EMPTY), e);
        }
    }

    @Override
    public void setStatistics(String type, String key, Integer fType, String value) {
        if (StringUtils.isNotBlank(key)) {
            statisticsCacheManager.setStatistics(type, key, fType, value);
        }
    }

    @Override
    public void delStatistics(String type, String key, Integer fType) {
        if (StringUtils.isNotBlank(key)) {
            statisticsCacheManager.delStatistics(type, key, fType);
        }
    }

    @Override
    public MainItemDataDO getStatisticByMainType(String mainType, String mainId) {
        if (StringUtils.isBlank(mainId)) {
            return null;
        }

        try {
            ResponseDTO<List<MainItemDataDTO>> resp = statisticService
                    .listStatisticByMainType(Integer.valueOf(mainType), new String[] { mainId });
            if (ResponseCode.isSuccess(resp.getCode())) {
                List<MainItemDataDTO> list = resp.getDataResult();
                if (list != null && list.size() > 0) {
                    MainItemDataDTO dto = list.get(0);
                    MainItemDataDO mainItemDataDO = new MainItemDataDO();
                    BeanUtils.copyProperties(dto, mainItemDataDO);
                    return mainItemDataDO;
                }
            } else {
                logs.error("statisticService.listStatisticByMainType(mainType, mainId) error. " + " message:"
                        + resp.getMessage() + " code:" + resp.getCode() + " mainType:" + mainType + " mainId:"
                        + mainId);
            }
        } catch (Exception e) {
            logs.error("statisticService.listStatisticByMainType(mainType, mainId) error. " + " message:"
                    + e.getMessage() + " mainType:" + mainType + " mainId:" + mainId);
        }
        return null;
    }
}