package com.ssj.service.conch.parents.service.impl;

import com.ssj.bean.sys.appinfo.bean.AppInfo;
import com.ssj.bean.sys.knowledge.domain.KnowDimension;
import com.ssj.framework.weixin.util.DateUtil;
import com.ssj.service.conch.parents.service.ParentService;
import com.ssj.service.conch.parents.v1.dto.CorrectDetailDto4;
import com.ssj.service.sys.appinfo.service.AppInfoService;
import com.ssj.service.sys.homework.service.HomeworkService;
import com.ssj.service.sys.knowledge.service.KnowDimensionService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ParentServiceImpl implements ParentService {

	private   Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private HomeworkService homeworkService;





	public static final String CTQ_RELATION_PARENT = "CTQ-RELATION-PARENT-";
	@Autowired
	private KnowDimensionService knowDimensionService;
    @Autowired
    private AppInfoService appInfoService;



	/**
	 * 查询维度
	 * @param subject
	 * @param type
	 * @return
	 */
	public Map<String, String> getDimension (String subject, int type) {
		List<KnowDimension> list = knowDimensionService.findBySubjectAndType(subject, type);
		Map<String, String> result = new HashMap<String, String>();
		if (list != null){
			for (KnowDimension kd : list){
				result.put(kd.getName(), kd.getRemark());
			}
		}
		return result;
	}






    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public AppInfo getVersion(int type) {
        return appInfoService.latestVersion(type);
    }








	@Override
	public CorrectDetailDto4 correctDetail4(String vipId, String subject, String date) {
		Date beginTime;
		CorrectDetailDto4 result  = null;
		if (StringUtils.isNotBlank(date)) {
			try {
				beginTime = DateUtil.parse_yyyyMMdd(date);
			} catch (Exception e) {
				beginTime = new Date();
			}
		} else {
			beginTime = new Date();
		}
		result = homeworkService.getRedisCorrectDetail(vipId, subject, beginTime);
		if (result == null){
			result = homeworkService.getCorrectDetailDto4(homeworkService.findDateSubjectHomework(vipId, subject, beginTime));
		}
		return result;
	}

}
