package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.QuestionDTO;
import com.diagbot.entity.QuestionDetail;
import com.diagbot.entity.QuestionInfo;
import com.diagbot.entity.wrapper.QuestionInfoWrapper;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.QuestionInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.vo.QuestionIdsVO;
import com.diagbot.vo.QuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @Description: 标签业务层
 * @author: zhoutg
 * @time: 2018/8/6 9:11
 */
@Component
public class QuestionFacade extends QuestionInfoServiceImpl {

    @Autowired
    QuestionDetailFacade questionDetailFacade;

    /**
     * 根据参数返回标签内容
     *
     * @param questionVO 获取标签内容参数
     * @return 标签内容
     */
    public QuestionDTO getById(QuestionVO questionVO) {
        Map paramMap = new HashMap<>();
        paramMap.put("sexType", questionVO.getSexType());
        paramMap.put("age", questionVO.getAge());
        paramMap.put("id", questionVO.getId());
        QuestionInfo questionInfo = this.getByParam(paramMap);
        if (questionInfo == null) { //无数据直接返回，不通过报错的形式返回
            return new QuestionDTO();
        }
        QuestionDTO res = new QuestionDTO();
        BeanUtil.copyProperties(questionInfo, res);

        //获取明细项
        List<QuestionDetail> questionDetailList = questionDetailFacade.getByQuestionId(questionVO.getId());
        res.setQuestionDetailList(questionDetailList);

        //获取子标签
        List<QuestionInfoWrapper> questionMapping = this.getByQuestionMapping(paramMap);
        List<QuestionDTO> sonQuestionList = BeanUtil.listCopyTo(questionMapping, QuestionDTO.class);
        res.setQuestionMapping(sonQuestionList);
        //获取子标签的明细项
        if (ListUtil.isNotEmpty(sonQuestionList)) {
            Map paramMap1 = new HashMap<>();
            paramMap1.put("sexType", questionVO.getSexType());
            paramMap1.put("age", questionVO.getAge());
            List<Long> sonQuestionIds = sonQuestionList.stream()
                    .map(artist -> artist.getId())
                    .collect(Collectors.toList());
            Map<Long, List<QuestionDetail>> sonQuestionMap = questionDetailFacade.getByQuestionIds(sonQuestionIds);
            paramMap1.put("ids", sonQuestionIds);
            //获取第二级子标签
            List<QuestionInfoWrapper> questionMapping1 = this.getByQuestionMapping(paramMap1);
            Map<Long, List<QuestionInfoWrapper>> qm1 = EntityUtil.makeEntityListMap(questionMapping1, "parentQuestion");
            for (QuestionDTO bean : sonQuestionList) {
                List<QuestionDetail> questionDetailList1 = sonQuestionMap.get(bean.getId());
                if (ListUtil.isEmpty(questionDetailList1)) {
                    questionDetailList1 = new ArrayList<>();
                }
                bean.setQuestionDetailList(questionDetailList1);
                List<QuestionDTO> sonQuestionList1 = new ArrayList<>();
                if (ListUtil.isNotEmpty(qm1.get(bean.getId()))) {
                    sonQuestionList1 = BeanUtil.listCopyTo(qm1.get(bean.getId()), QuestionDTO.class);
                }
                bean.setQuestionMapping(sonQuestionList1);

                List<Long> sonQuestionIds1 = sonQuestionList1.stream()
                        .map(artist -> artist.getId())
                        .collect(Collectors.toList());
                Map<Long, List<QuestionDetail>> sonQuestionMap1 = questionDetailFacade.getByQuestionIds(sonQuestionIds1);
                for (QuestionDTO questionDTO : sonQuestionList1) {
                    //获取二级子明细项
                    List<QuestionDetail> questionDetailList2 = sonQuestionMap1.get(questionDTO.getId());
                    if (ListUtil.isEmpty(questionDetailList2)) {
                        questionDetailList2 = new ArrayList<>();
                    }
                    questionDTO.setQuestionDetailList(questionDetailList2);
                }

            }
        }
        return res;
    }


    /**
     * 查询多个id返回标签内容
     *
     * @param questionIdsVO 获取标签内容参数
     * @return 标签内容
     */
    public Map<Long, Object> getByIds(QuestionIdsVO questionIdsVO) {
        Map<Long, Object> 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));
            questionVO.setAge(questionIdsVO.getAge());
            questionVO.setSexType(questionIdsVO.getSexType());
            QuestionDTO item = this.getById(questionVO);
            if (item != null && item.getId() != null) {
                data.put(item.getId(), item);
            }
        }
        return data;
    }

    /**
     * 获取标签信息
     *
     * @return
     */
    public Map<Long, QuestionInfo> getQuestionInfos(List<Long> questionIdList) {
        QueryWrapper<QuestionInfo> questionInfoQueryWrapper = new QueryWrapper<>();
        questionInfoQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
                .in("id", questionIdList);
        List<QuestionInfo> questionInfoList = this.list(questionInfoQueryWrapper);
        Map<Long, QuestionInfo> map = questionInfoList.stream().collect(Collectors.toMap(QuestionInfo::getId, questionInfo -> questionInfo));
        return map;
    }
}
