package com.diagbot.facade;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.entity.LisMapping;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.impl.LisMappingServiceImpl;
import com.diagbot.util.EntityUtil;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author:zhaops
 * @time: 2018/12/27 15:13
 */
@Component
public class LisMappingFacade extends LisMappingServiceImpl {

    /**
     * 获取化验公表映射关系-itemName不为空 Map<mealName,Map<itemName,uniqueName>>
     *
     * @return
     */
    public Map<String, Map<String, String>> getLisMapping_NotEmptyItemName() {
        Map<String, Map<String, String>> retMap = new LinkedHashMap<>();
        QueryWrapper<LisMapping> lisMappingQueryWrapper = new QueryWrapper<>();
        lisMappingQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                isNotNull("item_name").and(i->i.ne("item_name",""));
        List<LisMapping> lisMappingList = this.list(lisMappingQueryWrapper);
        Map<String, List<LisMapping>> lisMappingMap = EntityUtil.makeEntityListMap(lisMappingList, "mealName");
        for (Map.Entry<String, List<LisMapping>> entry : lisMappingMap.entrySet()) {
            if (entry.getValue().size() > 0) {
                retMap.put(entry.getKey(), EntityUtil.makeMapWithKeyValue(entry.getValue(), "itemName", "uniqueName"));
            } else {
                retMap.put(entry.getKey(), null);
            }
        }
        return retMap;
    }

    /**
     * 根据医院编码获取化验公表映射关系-itemName为空 Map<mealName,uniqueName>
     *
     * @return
     */
    public Map<String, String> getLisConfig_EmptyItemName() {
        QueryWrapper<LisMapping> lisMappingQueryWrapper = new QueryWrapper<>();
        lisMappingQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey()).
                isNull("item_name").or(i -> i.eq("item_name", ""));
        List<LisMapping> lisMappingList = this.list(lisMappingQueryWrapper);
        Map<String, String> retMap = EntityUtil.makeMapWithKeyValue(lisMappingList, "mealName", "uniqueName");
        return retMap;
    }

    /**
     * 化验公表映射关系  Map<uniqueName,List<mealName>>
     *
     * @return
     */
    public Map<String, List<String>> getLisMappingByUniqueName() {
        Map<String, List<String>> retMap = new LinkedHashMap<>();
        QueryWrapper<LisMapping> lisMappingQueryWrapper = new QueryWrapper<>();
        lisMappingQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
        List<LisMapping> lisMappingList = this.list(lisMappingQueryWrapper);
        Map<String, List<LisMapping>> lisMappingMap = EntityUtil.makeEntityListMap(lisMappingList, "uniqueName");
        for (Map.Entry<String, List<LisMapping>> entry : lisMappingMap.entrySet()) {
            retMap.put(entry.getKey(), EntityUtil.makeListByFieldName(entry.getValue(), "mealName"));
        }
        return retMap;
    }
}