package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.diagbot.client.UserServiceClient;
import com.diagbot.dto.GetQuestionInfoDTO;
import com.diagbot.dto.QuestionDTO;
import com.diagbot.dto.QuestionPageDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.CommonParam;
import com.diagbot.entity.DeptInfo;
import com.diagbot.entity.DeptVital;
import com.diagbot.entity.ModuleDetail;
import com.diagbot.entity.ModuleInfo;
import com.diagbot.entity.QuestionDetail;
import com.diagbot.entity.QuestionInfo;
import com.diagbot.entity.QuestionMapping;
import com.diagbot.entity.RetrievalMapping;
import com.diagbot.entity.VitalOrder;
import com.diagbot.entity.wrapper.QuestionInfoWrapper;
import com.diagbot.entity.wrapper.QuestionMappingWrapper;
import com.diagbot.entity.wrapper.QuestionWrapper;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.enums.TagTypeEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.QuestionDetailService;
import com.diagbot.service.QuestionInfoService;
import com.diagbot.service.QuestionMappingService;
import com.diagbot.service.impl.QuestionInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.util.UserUtils;
import com.diagbot.vo.GetQuestionIdsByTypeVO;
import com.diagbot.vo.GetQuestionInfoVO;
import com.diagbot.vo.GetQuestionUsualByDeptVO;
import com.diagbot.vo.QuestionIdsVO;
import com.diagbot.vo.QuestionIndexVO;
import com.diagbot.vo.QuestionPageVO;
import com.diagbot.vo.QuestionSaveVO;
import com.diagbot.vo.QuestionSpecVO;
import com.diagbot.vo.QuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
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 QuestionFacade extends QuestionInfoServiceImpl {

    @Autowired
    QuestionDetailFacade questionDetailFacade;
    @Autowired
    @Qualifier("questionInfoServiceImpl")
    QuestionInfoService questionInfoService;
    @Autowired
    @Qualifier("questionDetailServiceImpl")
    QuestionDetailService questionDetailService;
    @Autowired
    @Qualifier("questionMappingServiceImpl")
    QuestionMappingService questionMappingService;
    @Autowired
    QuestionMappingFacade questionMappingFacade;
    @Autowired
    ModuleDetailFacade moduleDetailFacade;
    @Autowired
    DeptInfoFacade deptInfoFacade;
    @Autowired
    UserServiceClient userServiceClient;
    @Autowired
    ModuleInfoFacade moduleInfoFacade;
    @Autowired
    VitalOrderFacade vitalOrderFacade;
    @Autowired
    DeptVitalFacade deptVitalFacade;
    @Autowired
    RetrievalMappingFacade retrievalMappingFacade;


    /**
     * 标签保存
     *
     * @param questionSaveVO
     */
    public void saveOrUpdate(QuestionSaveVO questionSaveVO) {
        QuestionWrapper questionWrapper = questionSaveVO.getQuestionWrapper();
        CommonParam param = initCommonParam();
        saveQuestionInfo(questionWrapper, param);
        switch (TagTypeEnum.getEnum(questionWrapper.getTagType())) {
            case T1:   //单项，例如：程度、体温
                saveQuestionDetail(questionWrapper, param);
                break;
            case T2:  //组合项，例如：杂音，修改主表和mapping
            case T3:  //组合项，例如：血压
            case T4:  //组合项，例如：咳嗽
            case T5:  //组合项，例如：有无治疗
            case T6:  // 组合项，例如：既往史
            case T7:  //化验
            case T11: //推送类型
                saveQuestionMapping(questionWrapper, param);
                break;
            case T8:  //文字，无单独维护
            case T9:  //空标签，只维护主表信息
            case T10: //组合，无单独维护
                break;
            default:
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "请选择标签显示类型");
        }
    }


    /**
     * 初始化参数
     *
     * @return
     */
    public CommonParam initCommonParam() {
        CommonParam param = new CommonParam();
        String person = UserUtils.getCurrentPrincipleID();
        param.setNow(DateUtil.now());
        param.setPerson(person);
        param.setSaveOrUpdate("save");
        return param;
    }


    /**
     * 保存映射关系
     *
     * @param questionWrapper
     * @param param
     */
    public void saveQuestionMapping(QuestionWrapper questionWrapper, CommonParam param) {
        QuestionInfo questionInfo = param.getQuestionInfo();
        Date now = param.getNow();
        String person = param.getPerson();
        //先删除原明细，再插入新明细
        deleteMapping(questionInfo.getId(), param);//删除原映射关系
        List<QuestionMappingWrapper> questionMappings = questionWrapper.getQuestionMappings();
        if (ListUtil.isNotEmpty(questionMappings)) {
            for (QuestionMappingWrapper bean : questionMappings) {
                QuestionWrapper sonQues = bean.getQuestionMappingSon();
                if (ListUtil.isNotEmpty(sonQues.getQuestionMappings())) {
                    QuestionInfo ques = new QuestionInfo();
                    BeanUtil.copyProperties(sonQues, ques);
                    if (ques.getId() != null) {
                        ques = this.getOne(new QueryWrapper<QuestionInfo>()
                                .eq("is_deleted", IsDeleteEnum.N.getKey())
                                .eq("id", ques.getId()));
                        if (ques == null) {
                            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "标签被修改，请刷新后重新保存");
                        }
                    } else {
                        String name = "组合项_" + DateUtil.format(now, DateUtil.FORMAT_FULL);
                        ques.setTagName(name);
                        ques.setName(name);
                        ques.setTagType(TagTypeEnum.T10.getKey());
                        ques.setGmtCreate(now);
                        ques.setCreator(person);
                        ques.setSubType(1);
                    }
                    ques.setType(questionInfo.getType());
                    ques.setGmtModified(now);
                    ques.setModifier(person);
                    this.saveOrUpdate(ques);

                    //删除原二级映射关系
                    deleteMapping(ques.getId(), param);

                    //添加二级映射关系
                    saveMapping(sonQues.getQuestionMappings(), new HashMap<String, Long>(), person, now, ques.getId());

                    //设置主映射关系
                    bean.setSonQuestion(ques.getId());
                    bean.setParentQuestion(questionInfo.getId());
                }
            }
        }
        if (ListUtil.isNotEmpty(questionMappings)) {
            //预处理文字标签，之后统一添加映射关系
            Map<String, Long> map = getNameMap(questionMappings, now, person, questionInfo.getType());
            saveMapping(questionMappings, map, person, now, questionInfo.getId());
        }
    }


    /**
     * 删除映射关系
     *
     * @param questionId
     * @param param
     */
    public void deleteMapping(Long questionId, CommonParam param) {
        questionMappingFacade.update(new QuestionMapping(),
                new UpdateWrapper<QuestionMapping>()
                    .eq("parent_question", questionId)
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .set("gmt_modified", param.getNow())
                    .set("modifier", param.getPerson())
                    .set("is_deleted", IsDeleteEnum.Y.getKey()));
    }


    /**
     * 保存映射关系
     *
     * @param questionMappings
     * @param map
     * @param person
     * @param now
     * @param id
     */
    public void saveMapping(List<QuestionMappingWrapper> questionMappings, Map<String, Long> map, String person, Date now, Long id) {
        List<QuestionMapping> saveMapping = new ArrayList<>();
        int i = 1;
        for (QuestionMappingWrapper mapping : questionMappings) {
            String text = mapping.getText();
            if (StringUtil.isNotEmpty(text)) {
                mapping.setSonQuestion(map.get(text));
            }
            QuestionMapping bean = new QuestionMapping();
            BeanUtil.copyProperties(mapping, bean);
            bean.setId(null); //防止前端传参，将前端的id置空自动插入
            bean.setCreator(person);
            bean.setGmtCreate(now);
            bean.setModifier(person);
            bean.setGmtModified(now);
            bean.setParentQuestion(id);
            bean.setOrderNo(i++);
            saveMapping.add(bean);
        }
        questionMappingService.saveBatch(saveMapping);
    }


    /**
     * 获取name对应map列表，key为name，value为id
     *
     * @param questionMappings
     * @param now
     * @param person
     * @param type
     * @return
     */
    public Map<String, Long> getNameMap(List<QuestionMappingWrapper> questionMappings, Date now, String person, Integer type) {
        List<String> nameList = questionMappings.stream().filter(row -> StringUtil.isNotEmpty(row.getText())).map(row -> row.getText()).collect(Collectors.toList());
        List<QuestionInfo> questionInfos = this.list(new QueryWrapper<QuestionInfo>()
                .eq("type", type)
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("tag_type", TagTypeEnum.T8.getKey())
                .in("name", nameList)
        );
        Map<String, Long> map = questionInfos.stream().collect(Collectors.toMap(row -> row.getName(), row -> row.getId(), (k1, k2) -> k1));
        List<QuestionInfo> addBatch = new ArrayList<>();
        List<String> nameExist = new ArrayList<>();
        for (String s : nameList) {
            if (map.get(s) == null && !nameExist.contains(s)) {
                nameExist.add(s);
                QuestionInfo bean = new QuestionInfo();
                bean.setModifier(person);
                bean.setCreator(person);
                bean.setGmtCreate(now);
                bean.setGmtModified(now);
                bean.setTagType(TagTypeEnum.T8.getKey());
                bean.setTagName(s);
                bean.setName(s);
                bean.setType(type);
                bean.setSubType(1); //描述类型
                addBatch.add(bean);
            }
        }
        questionInfoService.saveBatch(addBatch); //批量插入question
        for (QuestionInfo bean : addBatch) {
            if (map.get(bean.getName()) == null) {
                map.put(bean.getName(), bean.getId());
            }
        }
        return map;
    }


    /**
     * 保存明细
     *
     * @param questionWrapper
     * @param param
     */
    public void saveQuestionDetail(QuestionWrapper questionWrapper, CommonParam param) {
        QuestionInfo questionInfo = param.getQuestionInfo();
        Date now = param.getNow();
        String person = param.getPerson();
        //先删除原明细，再插入新明细
        questionDetailFacade.update(new QuestionDetail(), //删除原明细
                new UpdateWrapper<QuestionDetail>()
                        .eq("question_id", questionInfo.getId())
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .set("gmt_modified", now)
                        .set("modifier", person)
                        .set("is_deleted", IsDeleteEnum.Y.getKey()));
        List<QuestionDetail> questionDetails = questionWrapper.getQuestionDetails();
        if (ListUtil.isNotEmpty(questionDetails)) {
            List<QuestionDetail> saveDetail = new ArrayList<>();
            int i = 1;
            for (QuestionDetail detail : questionDetails) {
                QuestionDetail bean = new QuestionDetail();
                BeanUtil.copyProperties(detail, bean);
                bean.setId(null); //防止前端传参，将前端的id置空自动插入
                bean.setCreator(person);
                bean.setGmtCreate(now);
                bean.setModifier(person);
                bean.setGmtModified(now);
                bean.setQuestionId(questionInfo.getId());
                bean.setOrderNo(i++);
                saveDetail.add(bean);
            }
            questionDetailService.saveBatch(saveDetail);
        }
    }


    /**
     * 保存主表
     *
     * @param questionWrapper
     * @param param
     */
    public void saveQuestionInfo(QuestionWrapper questionWrapper, CommonParam param) {
        QuestionInfo questionInfo = new QuestionInfo();
        if (questionWrapper.getId() != null) {
            questionInfo = this.getOne(new QueryWrapper<QuestionInfo>()
                    .eq("id", questionWrapper.getId())
                    .eq("is_deleted", IsDeleteEnum.N.getKey()));
            if (questionInfo == null) { //校验
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "标签不存在");
            }
            param.setSaveOrUpdate("update");
        }
        BeanUtil.copyProperties(questionWrapper, questionInfo);
        List<QuestionInfo> questionInfoList = this.list(new QueryWrapper<QuestionInfo>()
                .eq("tag_name", questionInfo.getTagName())
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("type", questionInfo.getType())
                .ne("id", questionInfo.getId() == null ? -1 : questionInfo.getId()));
        if (ListUtil.isNotEmpty(questionInfoList)) { //标签type、tagName唯一
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "标签系统名称已重复，无法建立");
        }
        if ("save".equals(param.getSaveOrUpdate())) {
            questionInfo.setCreator(param.getPerson()); //创建人
            questionInfo.setGmtCreate(param.getNow());//创建时间
        }
        questionInfo.setGmtModified(param.getNow());//修改时间
        questionInfo.setModifier(param.getPerson());//修改人
        param.setQuestionInfo(questionInfo);
        this.saveOrUpdate(questionInfo);
    }


    /**
     * 标签列表
     *
     * @param questionPageVO
     * @return
     */
    public IPage<QuestionPageDTO> getListFac(QuestionPageVO questionPageVO) {
        IPage<QuestionPageDTO> res = this.getList(questionPageVO);
        if (res.getTotal() <= 0) {
            return res;
        }
        List<String> personIds = res.getRecords().stream().map(row -> row.getModifier()).collect(Collectors.toList());
        RespDTO<Map<String, String>> mapRespDTO = userServiceClient.getUserInfoByIds(personIds);
        if (mapRespDTO == null || !CommonErrorCode.OK.getCode().equals(mapRespDTO.code)) {
            throw new CommonException(CommonErrorCode.RPC_ERROR, "获取操作人失败");
        }
        for (QuestionPageDTO bean : res.getRecords()) {
            bean.setModifier(mapRespDTO.data.get(bean.getModifier()));
        }
        return res;
    }


    /**
     * 根据id删除标签
     *
     * @param ids
     * @return
     */
    public Boolean deleteByIdsFac(String ids) {
        if (StringUtil.isEmpty(ids)) {
            return true;
        }
        List<String> idList = Arrays.asList(ids.split(","));
        StringBuffer errMsg = new StringBuffer();
        Date now = DateUtil.now();
        String person = UserUtils.getCurrentPrincipleID();
        for (String id : idList) {
            // 查询映射关系中是否存在父级
            List<QuestionInfo> info = this.getParentQuestion(Long.parseLong(id));
            for(QuestionInfo bean : info) {
                if(bean.getTagType() != null && bean.getTagType() == TagTypeEnum.T10.getKey()) {
                    List<QuestionInfo> info2 = this.getParentQuestion(bean.getId());
                    List<String> msg = info2.stream().map(row -> "【" + row.getTagName() + "】").collect(Collectors.toList());
                    errMsg.append(String.join("", msg));
                } else {
                    errMsg.append("【").append(bean.getTagName()).append("】");
                }
            }
            if(StringUtil.isNotEmpty(errMsg.toString())) {
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "请先删除标签的关联：" + errMsg);
            }

            // 查询模板中是否存在
            List<ModuleDetail> moduleDetails = moduleDetailFacade.list(new QueryWrapper<ModuleDetail>()
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .eq("question_id", id));
            if (ListUtil.isNotEmpty(moduleDetails)) {
                List<Long> moduleIds = moduleDetails.stream().map(row -> row.getModuleId()).collect(Collectors.toList());
                List<String> moduleName = moduleInfoFacade.list(new QueryWrapper<ModuleInfo>()
                        .eq("is_deleted", IsDeleteEnum.N.getKey())
                        .in("id", moduleIds))
                        .stream().map(row -> "{" + row.getName() + "}").collect(Collectors.toList());
                throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "请先删除模板的关联：" + String.join("、", moduleName));
            }

            Map paramMap = new HashMap<>();
            paramMap.put("delete", IsDeleteEnum.Y.getKey());
            paramMap.put("ids", Arrays.asList(ids.split(",")));
            paramMap.put("gmtModified", now);
            paramMap.put("modifier", person);
            //删除自身
            this.deleteByIds(paramMap);
            //删除明细
            questionDetailFacade.deleteByQuestionIdFac(paramMap);
            //删除映射关系
            questionMappingFacade.deleteByIdsFac(paramMap);
            //删除查体模板
            vitalOrderFacade.update(new VitalOrder(), new UpdateWrapper<VitalOrder>()
                    .eq("question_id", id)
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .set("gmt_modified", now)
                    .set("modifier", person)
                    .set("is_deleted", IsDeleteEnum.Y.getKey()));
            deptVitalFacade.update(new DeptVital(), new UpdateWrapper<DeptVital>()
                    .eq("vital_id", id)
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .set("gmt_modified", now)
                    .set("modifier", person)
                    .set("is_deleted", IsDeleteEnum.Y.getKey()));
            //删除检索映射
            retrievalMappingFacade.update(new RetrievalMapping(), new UpdateWrapper<RetrievalMapping>()
                    .eq("question_id", id)
                    .eq("is_deleted", IsDeleteEnum.N.getKey())
                    .set("gmt_modified", now)
                    .set("modifier", person)
                    .set("is_deleted", IsDeleteEnum.Y.getKey()));
        }
        return true;
    }


    /**
     * 检索
     *
     * @param questionIndexVO
     * @return
     */
    public List<QuestionInfo> indexFac(QuestionIndexVO questionIndexVO) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("age", questionIndexVO.getAge());
        paramMap.put("sexType", questionIndexVO.getSexType());
        paramMap.put("tagName", questionIndexVO.getTagName());
        paramMap.put("type", questionIndexVO.getType());
        paramMap.put("controlType", questionIndexVO.getControlType());
        paramMap.put("notIds", questionIndexVO.getNotIds());
        paramMap.put("tagType", questionIndexVO.getTagType());
        paramMap.put("existName", questionIndexVO.getExistName());
        paramMap.put("notTagType", questionIndexVO.getNotTagType());
        return this.index(paramMap);
    }
    
    /**
     * 别名新增标签检索
     *
     * @param questionIndexVO
     * @return
     */
    public List<QuestionInfo> indexRev(QuestionIndexVO questionIndexVO) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("age", questionIndexVO.getAge());
        paramMap.put("sexType", questionIndexVO.getSexType());
        paramMap.put("tagName", questionIndexVO.getTagName());
        paramMap.put("type", questionIndexVO.getType());
        paramMap.put("controlType", questionIndexVO.getControlType());
        paramMap.put("notIds", questionIndexVO.getNotIds());
        paramMap.put("tagType", questionIndexVO.getTagType());
        paramMap.put("existName", questionIndexVO.getExistName());
        paramMap.put("notTagType", questionIndexVO.getNotTagType());
        List<QuestionInfo> list = this.index(paramMap);
        if(ListUtil.isEmpty(list)){
        	return list;
        }
        Map<Long,Long> couMap = retrievalMappingFacade.getMapCouByQuesIds(list.stream().map(i->i.getId()).collect(Collectors.toList()));
        return list.stream().filter(i->{
        	if(couMap.get(i.getId())!=null){
        		return false;
        	}
        	return true;
        }).collect(Collectors.toList());
    }


    /**
     * 获取特殊标签（同伴|无）
     *
     * @param questionSpecVO
     * @return
     */
    public List<QuestionInfo> getSpecialFac(QuestionSpecVO questionSpecVO) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("age", questionSpecVO.getAge());
        paramMap.put("sexType", questionSpecVO.getSexType());
        paramMap.put("type", questionSpecVO.getType());
        paramMap.put("notIds", questionSpecVO.getNotIds());
        return this.getSpecial(paramMap);
    }


    /**
     * 根据标签名获取标签信息
     *
     * @param getQuestionInfoVO
     * @return
     */
    public List<GetQuestionInfoDTO> getQuestiongInfosByName(GetQuestionInfoVO getQuestionInfoVO) {
        List<GetQuestionInfoDTO> getQuestionInfoDTOS = this.getQuestiongInfoByName(getQuestionInfoVO);
        return getQuestionInfoDTOS;
    }

    public List<Long> getQuestionIdsByType(GetQuestionIdsByTypeVO getQuestionIdsByTypeVO){
        QueryWrapper<QuestionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",getQuestionIdsByTypeVO.getQuestionIds() )
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("type", getQuestionIdsByTypeVO.getQuestionType());
        List<QuestionInfo> questionInfoList = this.list(queryWrapper);
        List<Long> questionIds = questionInfoList.stream().map(questionInfo -> questionInfo.getId()).collect(Collectors.toList());
        return questionIds;
    }


    /**
     * 根据科室id获取常用标签信息
     *
     * @param getQuestionUsualByDeptVO
     * @return
     */
    public List<GetQuestionInfoDTO> getQuestionUsualByDept(GetQuestionUsualByDeptVO getQuestionUsualByDeptVO) {
        //先判断科室是不是已被删除
        QueryWrapper<DeptInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("id", getQuestionUsualByDeptVO.getDeptId());
        List<DeptInfo> deptInfoList = deptInfoFacade.list(queryWrapper);
        if (ListUtil.isEmpty(deptInfoList)) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "科室信息不存在");
        }
        //获取常用标签信息
        List<GetQuestionInfoDTO> getQuestionInfoDTOList = this.getQuestionUsualsByDept(getQuestionUsualByDeptVO);
        return getQuestionInfoDTOList;
    }


    /********************************************以下从icss项目拷贝过来*****************************************************/

    /**
     * 根据参数返回标签内容
     *
     * @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);
            data.put(item.getId(), item);
        }
        return data;
    }
}