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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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
@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;
						}
					}
				}
			}
			//libId不能为空
			if(StringUtils.isEmpty(libId)) {
				return false;
			}
			//userId和vipId不能同时为空
			if(StringUtils.isEmpty(userId) && StringUtils.isEmpty(vipId)) {
				return false;
			}
			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("保存接口分析数据异常...");
		}
		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>> 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>> countFxMerchantCommonByCountDate(Map<String, Object> params, Pageable pageable) {
		return analysisQueryDao.countFxMerchantCommonByCountDate(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);
	}

	@Override
	public Analysis getAnalysisByDay(String libId, String userId, String vipId, Integer source, Integer type,String dayTime) {
		return analysisQueryDao.getAnalysisByDay(libId, userId, vipId, source, type, dayTime);
	}


	
 
}
