package com.ssj.service.kmt.kmt.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.kmt.kmt.domain.KmtCourseQuestionDetail;
import com.ssj.framework.basic.utils.StringUtils;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.dao.kmt.kmt.dao.KmtCourseQuestionDetailDao;
import com.ssj.dao.kmt.kmt.dao.KmtQueryDao;
import com.ssj.service.kmt.service.KmtCourseQuestionDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.PagingAndSortingRepository;

import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Transactional
@Service(version ="1.0")
public class KmtCourseQuestionDetailServiceImpl  extends BaseServiceImpl<KmtCourseQuestionDetail, String> implements KmtCourseQuestionDetailService {
	
	@Autowired
	private KmtCourseQuestionDetailDao dao;
	
	@Autowired
	private KmtQueryDao queryDao;
	

	@Override
	public PagingAndSortingRepository<KmtCourseQuestionDetail, String> getDao() {
		return dao;
	}

	@Override
	public KmtCourseQuestionDetail findCourseQuestionDetail(
			String courseKnowId, Integer mark) {
		return dao.findCourseQuestionDetail(courseKnowId, mark);
	}
	
	@Override
	public List<Map<String,Object>> findFeedbackRatioMap(String courseKnowId) {
		return queryDao.findFeedbackRatioMap(courseKnowId);
	}

	@Override
	public Integer findHowDetailsIdByAllCount(String zuoybKnowHowDetailId, String vipId,String courseKnowId) {
		return queryDao.findHowDetailsIdByAllCount(zuoybKnowHowDetailId,vipId,courseKnowId);
	}

	@Override
	public Integer findHowDetailsIdByWrongCount(String zuoybKnowHowDetailId,String vipId,String courseId) {
		return queryDao.findHowDetailsIdByWrongCount(zuoybKnowHowDetailId, vipId,courseId);
	}

	@Override
	public List<Map<String, Object>> findFeedbackWrongRatioMap(String courseId) {
		return queryDao.findFeedbackWrongRatioMap(courseId);
	}

	@Override
	public Integer countFeedBackNum() {
		return queryDao.countFeedBackNum();
	}

	@Override
	public List<Map<String, Object>> findFeedbackRatioVipMap() {
		return queryDao.findFeedbackRatioVipMap();
	}

	@Override
	public List<Map<String, Object>> get121KnowModuleData(String vipId, String subject, String beginTime, String endTime) {
		return queryDao.get121KnowModuleData(vipId, subject, beginTime, endTime);
	}

	@Override
	public List<Map<String, Object>> get121SkillData(String vipId, String subject, String beginTime, String endTime) {
		List<Map<String, Object>> list = queryDao.get121SkillData(vipId, subject, beginTime, endTime);
		Map<String, Integer> tempMap = new HashMap<>();
		List<Map<String, Object>> result = new ArrayList<>();
		String temp = null;
		Integer tempInt  = null;
		if (list != null){
			for (Map<String, Object> map : list){
				temp = map.get("module") + "";
				//获取技能的值
				if (StringUtils.isNotEmpty(temp)){
					String[] skills = temp.split(";");
					for (String skill : skills){
						tempInt = tempMap.get(skill);
						if (tempInt != null && tempInt > 0){
							tempMap.put(skill, tempInt.intValue() + 1);
						} else {
							tempMap.put(skill, 1);
						}
					}
				}
			}
			//把map转成list
			Map<String, Object> item = null;
			for (String key : tempMap.keySet()){
				item = new HashMap<>();
				item.put("module", key);
				item.put("count", tempMap.get(key));
				result.add(item);
			}
		}
		return result;
	}

	@Override
	public List<Map<String, Object>> findCourseKnowIdMap(String courseKnowId) {
		return queryDao.findCourseKnowIdMap(courseKnowId);
	}

	@Override
	public List<KmtCourseQuestionDetail> findByCourseKnowId(String courseKnowId) {
		return dao.findByCourseKnowIdOrderByMark(courseKnowId);
	}
}
