package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.PartDTO;
import com.diagbot.dto.PartSymptomDTO;
import com.diagbot.entity.Part;
import com.diagbot.entity.SymptomWrapper;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.service.impl.PartServiceImpl;
import com.diagbot.util.EntityUtil;
import com.diagbot.util.ListUtil;
import com.diagbot.vo.PartSymptomVO;
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;

/**
 * @Description: 部位facade
 * @Author: ztg
 * @Date: 2018/10/23 16:33
 */
@Component
public class PartFacade extends PartServiceImpl {

    @Autowired
    SymptomFacade symptomFacade;

    /**
     * 根据已选部位返回对应的症状
     *
     * @param partSymptomVO 参数
     * @return 部位症状关联信息
     */
    public List<PartSymptomDTO> getSymptomByPartIds(PartSymptomVO partSymptomVO) {
        List<Long> partIds = partSymptomVO.getPartIds();
        if(ListUtil.isEmpty(partIds)) {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "请选择部位");
        }
        if(partSymptomVO.getSexType() == null) {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "请选择性别");
        }
        if(partSymptomVO.getAge() == null) {
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "请选择年龄");
        }
        if(partSymptomVO.getAge().intValue() < 0 || partSymptomVO.getAge().intValue() > 200) {
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR,
                    "超出年龄范围【0 — 200】");
        }
        //添加默认部位-全身
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", "全身");
        Part part = this.getOne(wrapper);
        partIds.add(part.getId());
        Map paramMap = new HashMap<>();
        paramMap.put("ids", partIds);
        List<PartSymptomDTO> res = this.getByPartId(paramMap);
        List<PartDTO> partList = this.getByParentId(paramMap);

        //添加二级部位信息
        Map<Long, List<PartDTO>> keyMap = EntityUtil.makeEntityListMap(partList, "parentId");
        for(PartSymptomDTO bean : res) {
            bean.setPartDTO(keyMap.get(bean.getId()));
        }

        //添加部位和症状的关联信息
        List<Long> idList = new ArrayList<>();
        for(PartDTO bean : partList) {
            idList.add(bean.getId());
        }
        Map paramMap1 = new HashMap<>();
        paramMap1.put("ids", idList);
        paramMap1.put("sexType", partSymptomVO.getSexType());
        paramMap1.put("age", partSymptomVO.getAge());
        List<SymptomWrapper> symptomList = symptomFacade.getByPartIdsFac(paramMap1);
        Map<Long, List<SymptomWrapper>> partSymptomMap = EntityUtil.makeEntityListMap(symptomList, "partId");
        for(PartDTO bean : partList) {
            bean.setSymptomList(partSymptomMap.get(bean.getId()));
        }

        // 如二级部位无关联的症状，删除二级部位
        for(int i = 0; i < res.size(); i++) {
            List<PartDTO> partDTO = res.get(i).getPartDTO();
            for(int j = 0; j < partDTO.size(); j++) {
                if(ListUtil.isEmpty(partDTO.get(j).getSymptomList())) {
                    partDTO.remove(j--);
                }
            }
        }
        return res;
    }


}
