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

import java.util.Date;
import java.util.HashMap;
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.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ssj.bean.sys.analysis.bean.AnalysisWeek;
import com.ssj.bean.sys.fx.domain.Merchant;
import com.ssj.dao.sys.analysis.dao.AnalysisQueryDao;
import com.ssj.dao.sys.analysis.dao.AnalysisWeekDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.analysis.service.AnalysisWeekService;
import com.ssj.service.sys.fx.service.MerchantService;
import com.ssj.service.weixin.library.service.ILibJoinService;

@Service
@Transactional
public class AnalysisWeekServiceImpl extends BaseServiceImpl<AnalysisWeek, String> implements AnalysisWeekService {

	private  Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @Autowired
    private AnalysisWeekDao analysisWeekDao;

    @Autowired
    private AnalysisQueryDao analysisQueryDao;
    
    @Autowired
    private MerchantService merchantService;
    
    @Autowired
    private ILibJoinService libJoinService;
    
    @Override
	public PagingAndSortingRepository<AnalysisWeek, String> getDao() {
		return analysisWeekDao;
	}

	@Override
	public void runTaskByAnalysisWeek() {
		List<Merchant> merchants=merchantService.findMerchantByType(new int[] {1});
		for (Merchant merchant : merchants) {
			if(StringUtils.isEmpty(merchant.getLibId())) {
				continue;
			}
			String libId=merchant.getLibId();
			
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("libId", libId);
			Integer activeNum=analysisQueryDao.countActiveNumCommonByLibCountWeek(params);
			Integer memberNum=analysisQueryDao.countMemberNumCommonByLibCountWeek(params);
			Integer activeRatio=(memberNum!=0 ? activeNum*100/memberNum : 0);
			
			AnalysisWeek analysisWeek=analysisWeekDao.getAnalysisWeekByCurWeek(libId);
			if(analysisWeek==null) {
				analysisWeekDao.saveAnalysisWeek(libId, memberNum, activeNum, activeRatio);
			}else {
				analysisWeek.setActiveNum(activeNum);
				analysisWeek.setMemberNum(memberNum);
				analysisWeek.setActiveRatio(activeRatio);
				analysisWeek.setUpdateTime(new Date());
				this.save(analysisWeek);
			}
		}
	}

	@Override
	public Map<String, Object> countAnalysisWeekCountCommonByCountCurDate(Map<String, Object> params) {
		return analysisQueryDao.countAnalysisWeekCountCommonByCountCurDate(params);
	}

	@Override
	public Map<String, Object> countAnalysisWeekCountCommonByCountMonthDate(Map<String, Object> params) {
		Map<String, Object> map=new HashMap<String, Object>();
		List<Map<String, Object>> list= analysisQueryDao.countAnalysisWeekCountCommonByCountMonthDate(params);
		map.put("count_0", list!=null?list.size():0);
		map.put("count_1", merchantService.findMerchantByType(new int[] {1}).size());
		return map;
	}

	@Override
	public Map<String, Object> countLibJoinCountCommonByCountDate(Map<String, Object> params) {
		Map<String, Object> map=new HashMap<String, Object>();
		List<Map<String, Object>> list= analysisQueryDao.countLibJoinCountCommonByCountDate(params);
		map.put("count_0", list!=null?list.size():0);
		map.put("count_1", libJoinService.findAll().size());
		return map;
	}

	@Override
	public Map<String, Object> countVipActivateCountCommonByCountDate(Map<String, Object> params) {
		return analysisQueryDao.countVipActivateCountCommonByCountDate(params);
	}

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

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

}
