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

import com.ssj.bean.kmt.kmt.domain.KmtImgFaceDetail;
import com.ssj.dao.kmt.kmt.dao.KmtImgFaceDetailDao;
import com.ssj.dao.kmt.kmt.dao.KmtQueryDao;
import com.ssj.framework.basic.utils.StringUtils;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.service.kmt.service.KmtImgFaceDetailService;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Transactional
@Service
public class KmtImgFaceDetailServiceImpl extends BaseServiceImpl<KmtImgFaceDetail, String> implements KmtImgFaceDetailService {

	@Autowired
	private KmtImgFaceDetailDao dao;
	
	@Autowired
	private KmtQueryDao kmtQueryDao;


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


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


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

    @Override
    public List<Map<String, Object>> get121SkillWrongData(String vipId, String subject, String beginTime, String endTime) {
        List<Map<String, Object>> list = kmtQueryDao.get121SkillWrongData(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<KmtImgFaceDetail> findByFaceId(String faceId) {
        return dao.findByFaceId(faceId);
    }
}
