package com.xinelu.familydoctor.service.impl;

import com.xinelu.common.utils.DateUtils;
import com.xinelu.familydoctor.entity.EvaluateAnswer;
import com.xinelu.familydoctor.entity.EvaluateRecord;
import com.xinelu.familydoctor.mapper.EvaluateAnswerMapper;
import com.xinelu.familydoctor.mapper.EvaluateRecordMapper;
import com.xinelu.familydoctor.service.EvaluateRecordService;
import com.xinelu.familydoctor.vo.EvaluateRecordResultVO;
import com.xinelu.familydoctor.vo.EvaluateRecordTimelineVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author gaoyu
 * @description 自评记录
 * @date 2023-10-16 9:23
 */
@Service
public class EvaluateRecordServiceImpl implements EvaluateRecordService {
    @Resource
    private EvaluateRecordMapper evaluateRecordMapper;
    @Resource
    private EvaluateAnswerMapper evaluateAnswerMapper;

    @Override
    public void submit(EvaluateRecord entity) {
        entity.setCreateTime(new Date());
        evaluateRecordMapper.insert(entity);
        List<EvaluateAnswer> answers = entity.getAnswers();
        if (CollectionUtils.isNotEmpty(answers)) {
            for (EvaluateAnswer answer : answers) {
                answer.setRecordId(entity.getId());
                evaluateAnswerMapper.insert(answer);
            }
        }
    }

    @Override
    public List<EvaluateRecordTimelineVO> getEvaluateTimeline(Long userId) {
        List<EvaluateRecord> records = evaluateRecordMapper.getTimeline(userId);
        List<EvaluateRecordTimelineVO> timeline = new ArrayList<>();
        Map<String, List<EvaluateRecordResultVO>> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(records)) {
            records.forEach(r -> {
                List<EvaluateRecordResultVO> resultList;
                if (!map.containsKey(DateUtils.formatDate(r.getCreateTime(), "yyyy-MM-dd"))) {
                    resultList = new ArrayList<>();
                } else {
                    resultList = map.get(DateUtils.formatDate(r.getCreateTime(), "yyyy-MM-dd"));
                }
                EvaluateRecordResultVO resultVO = new EvaluateRecordResultVO();
                resultVO.setRecordId(r.getId());
                resultVO.setEvaluateResult(r.getEvaluateResult());
                resultVO.setSurveySubject(r.getSurveySubject());
                resultVO.setAdvice(r.getAdvice());
                resultVO.setTime(DateUtils.formatDate(r.getCreateTime(), "HH:mm:ss"));
                resultVO.setDate(DateUtils.formatDate(r.getCreateTime(), "yyyy-MM-dd"));
                resultList.add(resultVO);
                map.put(DateUtils.formatDate(r.getCreateTime(), "yyyy-MM-dd"), resultList);
            });
            map.forEach((k, v) -> {
                EvaluateRecordTimelineVO recordLine = new EvaluateRecordTimelineVO();
                recordLine.setDate(k);
                recordLine.setRecords(v);
                timeline.add(recordLine);
            });
        }
        return timeline;
    }

    @Override
    public List<EvaluateAnswer> getAnswers(Long recordId) {
        return evaluateAnswerMapper.getAnswers(recordId);
    }
}
