package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.IntroduceDTO;
import com.diagbot.entity.IntroduceDetail;
import com.diagbot.entity.IntroduceInfo;
import com.diagbot.entity.IntroduceMap;
import com.diagbot.entity.QuestionInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.enums.QuestionTypeEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.IntroduceInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.vo.IntroduceByQuestionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2018/11/16 9:27
 */
@Component
public class IntroduceInfoFacade extends IntroduceInfoServiceImpl {
    @Autowired
    IntroduceMapFacade introduceMapFacade;
    @Autowired
    IntroduceDetailFacade introduceDetailFacade;
    @Autowired
    QuestionFacade questionFacade;

    /**
     * 根据标签id获取提示信息
     *
     * @param introduceByQuestionVO
     * @return
     */
    public IntroduceDTO getByQuestionId(IntroduceByQuestionVO introduceByQuestionVO) {
        if (introduceByQuestionVO.getQuestionId() == null) {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "请输入标签id");
        }
        QueryWrapper<IntroduceMap> introduceMapQueryWrapper = new QueryWrapper<>();
        introduceMapQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                eq("question_id", introduceByQuestionVO.getQuestionId()).
                eq("type", introduceByQuestionVO.getType());
        IntroduceMap introduceMap = introduceMapFacade.getOne(introduceMapQueryWrapper);
        if (introduceMap == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息未维护");
        }

        IntroduceDTO introduceDTO = this.getRecordById(introduceMap.getIntroduceId());
        return introduceDTO;
    }

    /**
     * 根据标签名称获取提示信息
     *
     * @param introduceByQuestionVO
     * @return
     */
    public IntroduceDTO getByQuestionName(IntroduceByQuestionVO introduceByQuestionVO) {
        if (StringUtil.isBlank(introduceByQuestionVO.getTagName())) {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "请输入标签名称");
        }
        QueryWrapper<QuestionInfo> questionInfoQueryWrapper = new QueryWrapper<>();
        questionInfoQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
                .eq("tag_name", introduceByQuestionVO.getTagName())
                .eq("type", QuestionTypeEnum.Disease.getKey());
        QuestionInfo questionInfo = questionFacade.getOne(questionInfoQueryWrapper);
        if (questionInfo == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "标签不存在");
        }

        QueryWrapper<IntroduceMap> introduceMapQueryWrapper = new QueryWrapper<>();
        introduceMapQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                eq("question_id", questionInfo.getId()).
                eq("type", introduceByQuestionVO.getType());
        IntroduceMap introduceMap = introduceMapFacade.getOne(introduceMapQueryWrapper);
        if (introduceMap == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息未维护");
        }

        IntroduceDTO introduceDTO = this.getRecordById(introduceMap.getIntroduceId());
        return introduceDTO;
    }

    /**
     * 根据id获取提示信息
     *
     * @param id
     * @return
     */
    public IntroduceDTO getRecordById(Long id) {
        IntroduceDTO introduceDTO = new IntroduceDTO();
        IntroduceInfo introduceInfo = this.getById(id);
        if (introduceInfo == null) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息未维护");
        } else if (introduceInfo.getIsDeleted().equals(IsDeleteEnum.Y.getKey())) {
            throw new CommonException(CommonErrorCode.NOT_EXISTS, "提示信息已删除");
        }
        BeanUtil.copyProperties(introduceInfo, introduceDTO);

        QueryWrapper<IntroduceDetail> introduceDetailQueryWrapper = new QueryWrapper<>();
        introduceDetailQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                eq("introduce_id", introduceInfo.getId()).
                orderByAsc("order_no");
        List<IntroduceDetail> introduceDetailList = introduceDetailFacade.list(introduceDetailQueryWrapper);
        introduceDTO.setIntroduceDetailList(introduceDetailList);
        return introduceDTO;
    }
}