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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

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.AnalysisCount;
import com.ssj.dao.sys.analysis.dao.AnalysisCountDao;
import com.ssj.dao.sys.analysis.dao.AnalysisQueryDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.sys.analysis.service.AnalysisCountService;

@Service
@Transactional
public class AnalysisCountServiceImpl extends BaseServiceImpl<AnalysisCount, String> implements AnalysisCountService {

    @Autowired
    private AnalysisCountDao analysisCountDao;
    
    @Autowired
    private AnalysisQueryDao analysisQueryDao;

    @Override
	public PagingAndSortingRepository<AnalysisCount, String> getDao() {
		return analysisCountDao;
	}

    //0点定时任务，统计Analysis数据到AnalysisCount表
	@Override
	public void runTaskAnalysisCount() {
		List<Map<String, Object>> list=analysisQueryDao.countAnalysisCountCommonByCountTask();
		for (Map<String, Object> map : list) {
			try {
				String libId=map.get("lib_id").toString();
				String dayTime=map.get("day_time").toString();
				Integer source=map.get("source")!=null ? Integer.parseInt(map.get("source").toString()): 0;
				Integer type=map.get("type")!=null ? Integer.parseInt(map.get("type").toString()): 0;
				Integer count=map.get("count")!=null ? Integer.parseInt(map.get("count").toString()): 0;
				
				AnalysisCount analysisCount=this.getAnalysisCountByDay(libId, source, type, dayTime);
				analysisCount.setCount(count);
				this.save(analysisCount);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
    
    
	@Override
	public AnalysisCount getAnalysisCountByDay(String libId, Integer source, Integer type, String dayTime) {
		AnalysisCount analysisCount=analysisCountDao.getAnalysisCountByDay(libId, source, type, dayTime);
		if(analysisCount==null) {
			analysisCount=new AnalysisCount();
			analysisCount.setCount(0);
			analysisCount.setLibId(libId);
			analysisCount.setSource(source);
			analysisCount.setType(type);
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				analysisCount.setDayTime(sdf.parse(dayTime));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return analysisCount;
	}

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

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

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

}
