package com.ssj.service.homework.composition;

import com.alibaba.dubbo.config.annotation.Service;
import com.ssj.bean.sys.template.CommentTemplate;
import com.ssj.service.conch.parents.v1.dto.CommentTemplateDTO;
import com.ssj.service.conch.parents.v1.dto.CompositionCommentModuleDTO;
import com.ssj.service.mapper.CommentTemplateMapper;
import com.ssj.service.sys.template.CommentTemplateMapperService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @description 作文评语查询
 * @author yrf
 * @time 2019/8/27 18:18
 */
@Service(version = "1.0")
public class CommentTemplateMapperServiceImpl implements CommentTemplateMapperService {

	@Autowired
	private CommentTemplateMapper commentTemplateMapper;

    @Override
    public List<CompositionCommentModuleDTO> getCompositionCommentModuleDTOList(String homeworkPictureId) {
        List<CommentTemplateDTO> commentTemplateDTOList = commentTemplateMapper.findByHomeworkPictureId(homeworkPictureId);
        List<CompositionCommentModuleDTO> compositionCommentModuleDTOList = new LinkedList<>();
        commentTemplateDTOList.stream()
                .collect(Collectors.groupingBy(CommentTemplateDTO::getModule, LinkedHashMap::new, Collectors.toList()))
                .forEach((k, v) -> {
                    CompositionCommentModuleDTO compositionCommentModuleDTO = new CompositionCommentModuleDTO();
                    compositionCommentModuleDTO.setModule(k);
                    compositionCommentModuleDTO.setCompositionCommentList(v);
                    compositionCommentModuleDTOList.add(compositionCommentModuleDTO);
                });
        return compositionCommentModuleDTOList;
    }

    @Override
    public List<String> findCompositionCommentByHomeworkPictureId(String homeworkPictureId) {
        List<CommentTemplateDTO> commentTemplateDTOList = commentTemplateMapper.findByHomeworkPictureId(homeworkPictureId);
        return commentTemplateDTOList.stream().map(CommentTemplateDTO::getComment).collect(Collectors.toList());
    }

    @Override
    public boolean checkSelectedCompositionComment(List<String> compositionCommentIdList) {
        List<Map<String, Object>> resultList = commentTemplateMapper.countModuleAndLevelById(compositionCommentIdList);
        if (resultList == null || resultList.size() < 3) {
            return false;
        }

        for (Map<String, Object> map : resultList) {
            int levelCount = Integer.parseInt(Optional.ofNullable(map.get("levelCount")).orElse("0").toString());
            if (levelCount > 1) {
                return false;
            }
        }

        return true;
    }

    @Override
    public List<CommentTemplate> findCompositionByIds(List<String> ids) {
        return commentTemplateMapper.findCommentByIds(ids);
    }
}
