package com.diagbot.facade;

import com.diagbot.client.AIServiceClient;
import com.diagbot.client.bean.FeatureRate;
import com.diagbot.client.bean.Response;
import com.diagbot.client.bean.ResponseData;
import com.diagbot.client.bean.SearchData;
import com.diagbot.dto.AIDTO;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.util.ListUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.vo.AIVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 调用AI服务业务实现
 * @author: gaodm
 * @time: 2018/10/25 10:09
 */
@Component
public class AIFacade {
    @Autowired
    private AIServiceClient aiServiceClient;

    /**
     * 调用ai接口业务逻辑
     *
     * @param aivo 搜索参数
     * @return AI接口返回结果
     */
    public AIDTO push(AIVO aivo, String type) {
        SearchData searchData = new SearchData();
        searchData.setAge(aivo.getAge());
        searchData.setSymptom(aivo.getSymptom());
        switch (aivo.getSex()) {
            case 1:
                searchData.setSex("M");
                break;
            case 2:
                searchData.setSex("F");
                break;
            default:
                throw new CommonException(CommonErrorCode.PARAM_ERROR, "不能输入男女以外的性别");
        }
        searchData.setFeatureType(type);
        searchData.setSysCode("1");
        switch (type) {
            case "1":
                searchData.setLength(10);
                break;
            case "7":
                searchData.setLength(10);
                break;
        }
        Response<ResponseData> res = aiServiceClient.bayesPageData(searchData);
        if (null == res || null == res.getData()) {
            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "AI没有返回结果");
        }
        AIDTO aidto = new AIDTO();
        switch (type) {
            case "1":
                aidto.setItems(res.getData().getSymptom());
                break;
            case "7":
                List<FeatureRate> items = new ArrayList<>();
                if (ListUtil.isNotEmpty(res.getData().getDis())){
                    Double rate;
                    Map<String, Double> map = new HashMap<>();
                    for (FeatureRate featureRate: res.getData().getDis()){
                        if (Double.valueOf(featureRate.getRate()) > 0.1d){
                            if (StringUtil.isNotBlank(featureRate.getExtraProperty())){
                                if (null != map.get(featureRate.getExtraProperty())){
                                    rate = map.get(featureRate.getExtraProperty())
                                            + Double.valueOf(featureRate.getRate());
                                    map.replace(featureRate.getExtraProperty(), rate);
                                } else {
                                    map.put(featureRate.getExtraProperty(), Double.valueOf(featureRate.getRate()));
                                }
                            }
                        }
                    }

                    List<Map.Entry<String,Double>> list = new ArrayList<Map.Entry<String,Double>>(map.entrySet());
                    Collections.sort(list,new Comparator<Map.Entry<String,Double>>() {
                                //降序排序
                                public int compare(Map.Entry<String, Double> o1,
                                                   Map.Entry<String, Double> o2) {
                                    return o2.getValue().compareTo(o1.getValue());
                                }
                    });
                    FeatureRate item;
                    for (Map.Entry<String,Double> entry: list){
                        if (items.size() < 2){
                            item = new FeatureRate();
                            item.setExtraProperty(entry.getKey());
                            items.add(item);
                        }
                    }
                    if (ListUtil.isEmpty(items)){
                        item = new FeatureRate();
                        item.setExtraProperty(res.getData().getDis().get(0).getExtraProperty());
                        items.add(item);
                    }
                }
//                if (ListUtil.isEmpty(items)){
//                    throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "暂时无法推理出结果");
//                }
                aidto.setItems(items);
                break;
        }
        return aidto;
    }
}
