package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.QuestionDTO;
import com.diagbot.dto.QuestionEntryDTO;
import com.diagbot.entity.QcQuestionInfo;
import com.diagbot.entity.wrapper.QuestionInfoWrapper;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.QcQuestionInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.vo.QuestionIdsVO;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author:zhoutg
 * @time: 2018/11/23 11:37
 */
@Component
public class QcQuestionFacade extends QcQuestionInfoServiceImpl {

    /**
     * 根据参数返回标签内容
     *
     * @param questionVO 获取标签内容参数
     * @return 标签内容
     */
    public QuestionDTO getById(QuestionVO questionVO) {
        Map paramMap = new HashMap<>();
        paramMap.put("id", questionVO.getId());

        QcQuestionInfo questionInfo = this.getOne(new QueryWrapper<QcQuestionInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("id", questionVO.getId()), false
        );
        if (questionInfo == null) { //无数据直接返回，不通过报错的形式返回
            return new QuestionDTO();
        }
        QuestionDTO res = new QuestionDTO();
        BeanUtil.copyProperties(questionInfo, res);

        //获取子标签
        List<QuestionInfoWrapper> questionMapping = this.getByQuestionMapping(paramMap);
        List<QuestionDTO> sonQuestionList = BeanUtil.listCopyTo(questionMapping, QuestionDTO.class);
        res.setQuestionMapping(sonQuestionList);
        return res;
    }


//    /**
//     * 查询多个id返回标签内容
//     *
//     * @param questionIdsVO 获取标签内容参数
//     * @return 标签内容
//     */
//    public Map<Long, QuestionDTO> getByIds(QuestionIdsVO questionIdsVO) {
//        Map<Long, QuestionDTO> data = new HashMap<>();
//        if (StringUtil.isEmpty(questionIdsVO.getIds())) {
//            return data;
//        }
//        String[] ids = questionIdsVO.getIds().split(",");
//        for (String id : ids) {
//            QuestionVO questionVO = new QuestionVO();
//            questionVO.setId(Long.parseLong(id));
//            QuestionDTO item = this.getById(questionVO);
//            data.put(item.getId(), item);
//        }
//        return data;
//    }


    /**
     * 查询多个id返回标签内容【优化版】
     *
     * @param questionIdsVO 获取标签内容参数
     * @return 标签内容Fac
     */
    public Map<Long, QuestionDTO> getByIds(QuestionIdsVO questionIdsVO) {
        List<Long> idList = questionIdsVO.getIds();
        if (ListUtil.isEmpty(idList)) {
            return new HashMap<>();
        }
        List<QcQuestionInfo> qcQuestionInfoList = this.list(new QueryWrapper<QcQuestionInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .in("id", idList)
        );
        List<QuestionDTO> questionDTOList = BeanUtil.listCopyTo(qcQuestionInfoList, QuestionDTO.class);
        if (ListUtil.isNotEmpty(questionDTOList)) {
            List<Long> ids = questionDTOList.stream().map(r -> r.getId()).collect(Collectors.toList());
            Map paramMap = new HashMap<>();
            paramMap.put("ids", ids);
            //获取子标签
            List<QuestionInfoWrapper> questionMapping = this.getByQuestionMapping(paramMap);
            Map<Long, List<QuestionInfoWrapper>> map = EntityUtil.makeEntityListMap(questionMapping, "parentQuestion");
            for (QuestionDTO bean : questionDTOList) {
                List<QuestionInfoWrapper> questionInfoWrapperList = map.get(bean.getId());
                if (ListUtil.isNotEmpty(questionInfoWrapperList)) {
                    bean.setQuestionMapping(BeanUtil.listCopyTo(questionInfoWrapperList, QuestionDTO.class));
                }
            }
        }
        return questionDTOList.stream().collect(Collectors.toMap(r -> r.getId(), r -> r));
    }


    public Map<Long, List<QuestionEntryDTO>> getByCaseEntryIdsFac(Map map) {
        List<QuestionEntryDTO> questionEntryDTOList = this.getByCaseEntryIds(map);
        return  EntityUtil.makeEntityListMap(questionEntryDTOList, "casesEntryId");
    }
}