package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.GetModuleInfoOneDTO;
import com.diagbot.dto.QcModuleDetailDTO;
import com.diagbot.dto.QuestionDTO;
import com.diagbot.entity.QcModuleDetail;
import com.diagbot.entity.QcModuleInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.QcModuleInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.vo.GetModuleInfoOneVO;
import com.diagbot.vo.GetModuleMapVO;
import com.diagbot.vo.QuestionIdsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

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/11/23 11:37
 */
@Component
public class QcModuleInfoFacade extends QcModuleInfoServiceImpl {

    @Autowired
    QcModuleDetailFacade qcModuleDetailFacade;
    @Autowired
    QcQuestionFacade qcQuestionFacade;

    /**
     * 根据id获取模板信息
     *
     * @param getModuleInfoOneVO
     * @return
     */
    public GetModuleInfoOneDTO getByIdFac(GetModuleInfoOneVO getModuleInfoOneVO) {
        //获取模板信息
        GetModuleInfoOneDTO getModuleInfoOneDTO = this.getById(getModuleInfoOneVO);

        if (null == getModuleInfoOneDTO) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
                    "模板已删除【" + getModuleInfoOneVO.getModuleId() + "】");
        }
        List<QcModuleDetail> qcModuleDetailList = qcModuleDetailFacade.list(new QueryWrapper<QcModuleDetail>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("module_id", getModuleInfoOneDTO.getId())
                .orderByAsc("order_no")
        );
        List<QcModuleDetailDTO> qcModuleDetailDTOList = BeanUtil.listCopyTo(qcModuleDetailList, QcModuleDetailDTO.class);

        // 根据questionId获取所有的标签
        List<Long> questionIds = qcModuleDetailList.stream().map(r -> r.getQuestionId()).collect(Collectors.toList());
        QuestionIdsVO questionIdsVO = new QuestionIdsVO();
        questionIdsVO.setIds(questionIds);
        Map<Long, QuestionDTO> questionDTOMap = qcQuestionFacade.getByIds(questionIdsVO);
        for (QcModuleDetailDTO bean : qcModuleDetailDTOList) {
            QuestionDTO questionDTO = questionDTOMap.get(bean.getQuestionId());
            if (questionDTO != null) {
                bean.setQuestionDTO(questionDTO);
            }
        }
        Map<Integer, List<QcModuleDetailDTO>> map = EntityUtil.makeEntityListMap(qcModuleDetailDTOList, "groupId");
        getModuleInfoOneDTO.setModuleDetail(map);
        return getModuleInfoOneDTO;
    }

    /**
     * 根据模块获取模板信息
     *
     * @param getModuleMapVO
     * @return
     */
    @Cacheable(value = "cache", key = "'qcmodule:hospId_'+#getModuleMapVO.hospitalId")
    public Map<Long, GetModuleInfoOneDTO> getModuleMap(GetModuleMapVO getModuleMapVO) {
        Map<Long, GetModuleInfoOneDTO> res = new LinkedHashMap<>();
        List<QcModuleInfo> qcModuleInfoList = this.list(new QueryWrapper<QcModuleInfo>()
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq(getModuleMapVO.getModeId() != null, "mode_id", getModuleMapVO.getModeId())
                .eq("hospital_id", getModuleMapVO.getHospitalId())
        );
        List<Long> ids = qcModuleInfoList.stream().map(r -> r.getId()).collect(Collectors.toList());
        if (ListUtil.isEmpty(ids)) {
            return new HashMap<>();
        }
        GetModuleInfoOneVO getModuleInfoOneVO = new GetModuleInfoOneVO();
        for (Long id : ids) {
            getModuleInfoOneVO.setModuleId(id);
            res.put(id, getByIdFac(getModuleInfoOneVO));
        }
        return res;
    }
}
