package com.ssj.service.sys.analysis.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.analysis.bean.Analysis;
import com.ssj.bean.weixin.libmy.domain.LibVip;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.dao.sys.analysis.dao.AnalysisDao;
import com.ssj.dao.sys.analysis.dao.AnalysisQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.analysis.service.AnalysisService;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.weixin.library.service.ILibVipService;

@Service(version="1.0")
@Transactional
public class AnalysisServiceImpl extends BaseServiceImpl<Analysis, String> implements AnalysisService {

	private  Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @Autowired
    private AnalysisDao analysisDao;

    @Autowired
    private AnalysisQueryDao analysisQueryDao;
    
    @Autowired
    private ILibVipService libVipService;
    
    @Autowired
    private IBookManagerService managerService;

    @Override
	public PagingAndSortingRepository<Analysis, String> getDao() {
		return analysisDao;
	}
    
	@Override
	public boolean save(String libId, String userId, String vipId,Integer source, Integer type) {
		try {
			if(StringUtils.isEmpty(libId)) {
				if(StringUtils.isNotEmpty(vipId)) {
					LibVip libVip=libVipService.getById(vipId);
					libId=libVip.getLibId();
					if(StringUtils.isEmpty(userId)) {
						userId=libVip.getUserId();
					}
				}
				if(StringUtils.isEmpty(libId) && StringUtils.isNotEmpty(userId)) {
					List<TbLibManager> libManagers=managerService.getByUserId(userId);
					for (TbLibManager tbLibManager : libManagers) {
						libId=tbLibManager.getLibId();
						if(StringUtils.isNotEmpty(libId)) {
							break;
						}
					}
				}
			}
			Analysis analysis=new Analysis();
			analysis.setLibId(libId);
			analysis.setUserId(userId);
			analysis.setVipId(vipId);
			analysis.setSource(source);
			analysis.setType(type);
			analysis.setCreateTime(new Date());
			this.save(analysis);
			return true;
		} catch (Exception e) {
			logger.info("saveAnalysis异常...");
		}
		return false;
	}

	@Override
	public Long countLibJoinConsumeSumByLibService(String date) {
		return analysisQueryDao.countLibJoinConsumeSumByLibService(date);
	}

	@Override
	public Long countFxMerchantSumByPortNum() {
		return analysisQueryDao.countFxMerchantSumByPortNum();
	}

	@Override
	public List<Map<String, Object>> countFxMerchantSumByTop(String date) {
		return analysisQueryDao.countFxMerchantSumByTop(date);
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantHomeworkByDay(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantHomeworkByDay(params,pageable);
	}
	
	@Override
	public Page<Map<String, Object>> countFxMerchantServiceByVipDate(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantServiceByVipDate(params, pageable);
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantServiceByDate(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantServiceByDate(params, pageable);
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantHomeworkByVipDay(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantHomeworkByVipDay(params, pageable);
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByDate(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantCommonByDate(params, pageable);
	}
	
	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByCountUserDate(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantCommonByCountUserDate(params, pageable);
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByVipDate(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantCommonByVipDate(params, pageable);
	}

	@Override
	public Page<Map<String, Object>> countFxMerchantCommonByUserDate(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantCommonByUserDate(params, pageable);
	}


	
 
}
