package com.persagy.proxy.dictionary.service.impl;

import com.persagy.proxy.adm.service.impl.AbstractAdmBaseServiceImpl;
import com.persagy.proxy.dictionary.model.AdmGraphicType;
import com.persagy.proxy.dictionary.model.AdmRelationType;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.dictionary.service.IAdmGraphicTypeService;
import com.persagy.proxy.dictionary.service.IAdmRelationTypeService;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * ADM 图解类型 实现
 * @author xujiaheng
 * @date 2021/09/08
 */
@Slf4j
@Service
public class AdmGraphicTypeServiceImpl extends AbstractAdmBaseServiceImpl<AdmGraphicType> implements IAdmGraphicTypeService {

    @Autowired
    private IAdmGraphicTypeService service;
    @Autowired
    private IAdmRelationTypeService admRelationTypeService;

    @Override
    public AdmResponse query(InstanceUrlParam context) {
        //查询类型
        AdmQueryCriteria criteria = new AdmQueryCriteria();
        criteria.setFilters("graphTypeName in ('强电类', '暖通空调类', '给排水类', '消防类', '测控类')");
        AdmResponse admResponse = service.doQuery(context, criteria, AdmGraphicType.class);
        List<AdmGraphicType> typeList = (List<AdmGraphicType>) admResponse.getContent();
        //id列表
        List<String> idList = typeList.stream().map(AdmGraphicType::getId).collect(Collectors.toList());
        //查询二级类型
        String idString = "(\'" + String.join("\',\'", idList) + "\'）";
        AdmQueryCriteria admQueryCriteria = new AdmQueryCriteria();
        admQueryCriteria.setFilters("parentId in ("+idString+")");
        AdmResponse response = service.doQuery(context, admQueryCriteria, AdmGraphicType.class);
        List<AdmGraphicType> graphicTypeList = (List<AdmGraphicType>) response.getContent();
        graphicTypeList.forEach(graphicType->{
            //查询三级类型
            AdmQueryCriteria queryCriteria = new AdmQueryCriteria();
            queryCriteria.setFilters("graphicId = "+graphicType.getId());
            AdmResponse doQueryResponse = admRelationTypeService.doQuery(context, queryCriteria, AdmRelationType.class);
            List<AdmRelationType> relationTypes = (List<AdmRelationType>) doQueryResponse.getContent();
            graphicType.setRelationTypeList(relationTypes);
        });
        AdmResponse res = new AdmResponse();
        res.setTotal(Long.valueOf(graphicTypeList.size()));
        res.setContent(graphicTypeList);
        return res;
    }
}
