package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.ModuleDetailDTO;
import com.diagbot.dto.ModuleInfoDTO;
import com.diagbot.dto.QuestionDTO;
import com.diagbot.entity.ModuleDetail;
import com.diagbot.entity.ModuleInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.ModuleInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.vo.ModuleVO;
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.LinkedHashMap;
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 ModuleFacade extends ModuleInfoServiceImpl {

    @Autowired
    ModuleDetailFacade moduleDetailFacade;
    @Autowired
    QuestionFacade questionFacade;

    /**
     * 返回所有的模型结构
     *
     * @return 模型结构
     */
    public List<ModuleInfoDTO> getAll(ModuleVO moduleVO) {
        List<ModuleInfoDTO> data = new ArrayList<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        queryWrapper.orderByAsc("id");
        List<ModuleInfo> list = this.list(queryWrapper);
        data = BeanUtil.listCopyTo(list, ModuleInfoDTO.class);
        List<Long> ids = data.stream()
                .map(artist -> artist.getId())
                .collect(Collectors.toList());
        Map<Long, List<ModuleDetail>> moduleDetailMap = new LinkedHashMap<>();
        if (ListUtil.isNotEmpty(ids)) {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("ids", ids);
            paramMap.put("sexType", moduleVO.getSexType());
            paramMap.put("age", moduleVO.getAge());
            List<ModuleDetail> moduleDetailList = moduleDetailFacade.getDetailByModuleFac(paramMap);
            if (ListUtil.isNotEmpty(moduleDetailList)) {
                moduleDetailMap = EntityUtil.makeEntityListMap(moduleDetailList, "moduleId");
            }

        }
        for (ModuleInfoDTO bean : data) {
            if (moduleDetailMap.get(bean.getId()) != null) {
                List<ModuleDetailDTO> moduleDetailDTOList = BeanUtil.listCopyTo(moduleDetailMap.get(bean.getId()), ModuleDetailDTO.class);
                for (ModuleDetailDTO detailDTO : moduleDetailDTOList) {
                    if (null != detailDTO.getQuestionId()) {
                        QuestionVO questionVO = new QuestionVO();
                        questionVO.setId(detailDTO.getQuestionId());
                        questionVO.setSexType(moduleVO.getSexType());
                        questionVO.setAge(moduleVO.getAge());
                        QuestionDTO questionDTO = questionFacade.getById(questionVO);
                        BeanUtil.copyProperties(questionDTO, detailDTO);
                    }
                }
                bean.setModuleDetailDTOList(moduleDetailDTOList);
            }
        }
        return data;
    }

}
