package com.lightinit.hsdataportal.impl;

import com.lightinit.hsdataportal.dictionary.DicDataPublishState;
import com.lightinit.hsdataportal.dictionary.DicPlatformType;
import com.lightinit.hsdataportal.dictionary.DicResourceType;
import com.lightinit.hsdataportal.entity.*;
import com.lightinit.hsdataportal.mapper.ResourceApiMapper;
import com.lightinit.hsdataportal.mapper.ResourceCatelogMapper;
import com.lightinit.hsdataportal.mapper.ResourceMicroappMapper;
import com.lightinit.hsdataportal.mapper.SectorMapper;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IResourceCatelogService;
import com.lightinit.hsdataportal.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ResourceCatelogServiceImpl implements IResourceCatelogService {

    @Autowired
    private ResourceCatelogMapper resourceCatelogMapper ;

    @Autowired
    private SectorMapper sectorMapper ;

    @Autowired
    private ResourceApiMapper resourceApiMapper ;

    @Autowired
    private ResourceMicroappMapper resourceMicroappMapper ;

    @Override
    public List<ResourceCatelogSearchList> getResourceCatelogNameAndId(String resourceType, String platformType, String ResourceCatelogType) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return resourceCatelogMapper.getResourceCatelogNameAndId(resourceType,platformType,ResourceCatelogType,DicDataPublishState.PUBLISHED);
    }

    @Override
    public TagsSearchListOrTagsModel getTagsSearchList(String resourceType,String resourceCatelogType) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        TagsSearchListOrTagsModel tagsSearchListOrTagsModel = new TagsSearchListOrTagsModel() ;
        List<SearchApiOrMicTagListModel> searchApiOrMicTagListModels = resourceCatelogMapper.SearchApiOrMicTagList(resourceType,resourceCatelogType) ;
        tagsSearchListOrTagsModel.setTags(searchApiOrMicTagListModels);
        if (resourceType.equals(DicResourceType.API)) {
            List<TagsSearchListModel> tagsSearchListModelList = searchApiOrMicTagListModels.stream().map(searchApiOrMicTagListModel -> {
                TagsSearchListModel tagsSearchListModel = new TagsSearchListModel();
                List<TagsSearchChildListModel> tagsSearchChildListModels = resourceApiMapper.selectSearchChildList(searchApiOrMicTagListModel.getCatelogCode());
                tagsSearchListModel.setMainTag(searchApiOrMicTagListModel.getCatelogName());
                tagsSearchListModel.setMinorList(tagsSearchChildListModels);
                return tagsSearchListModel;
            }).collect(Collectors.toList());
            tagsSearchListOrTagsModel.setLists(tagsSearchListModelList) ;
            return tagsSearchListOrTagsModel;
        }else{
            List<TagsSearchListModel> tagsSearchListModelList = searchApiOrMicTagListModels.stream().map(searchApiOrMicTagListModel -> {
                TagsSearchListModel tagsSearchListModel = new TagsSearchListModel();
                List<TagsSearchChildListModel> tagsSearchChildListModels = resourceMicroappMapper.selectSearchChildList(searchApiOrMicTagListModel.getCatelogCode());
                tagsSearchListModel.setMainTag(searchApiOrMicTagListModel.getCatelogName());
                tagsSearchListModel.setMinorList(tagsSearchChildListModels);
                return tagsSearchListModel;
            }).collect(Collectors.toList());
            tagsSearchListOrTagsModel.setLists(tagsSearchListModelList) ;
            return tagsSearchListOrTagsModel;
        }
    }

    @Override
    public List<SectorTagSearchList> getSectorNameList() {
        SectorExample sectorExample = new SectorExample() ;
        SectorExample.Criteria criteria = sectorExample.createCriteria() ;
        criteria.andParentIdIsNull() ;
        List<Sector> sectors = sectorMapper.selectByExample(sectorExample) ;
        return sectors.stream().map(sector -> {
            SectorTagSearchList sectorTagSearchList = new SectorTagSearchList() ;
            sectorTagSearchList.setSectorCode(sector.getSectorCode());
            sectorTagSearchList.setSectorId(sector.getId());
            sectorTagSearchList.setSectorName(sector.getSectorName());
            return sectorTagSearchList ;
        }).collect(Collectors.toList()) ;
    }

    @Override
    public List<ResourceCatelogSearchList> getResourceCatelogNameByType(String resourceCatelogType,String resourceType,String platformType) {
        ResourceCatelogExample example = new ResourceCatelogExample() ;
        ResourceCatelogExample.Criteria criteria = example.createCriteria() ;
        criteria.andParentIdIsNull() ;
        criteria.andCatelogTypeEqualTo(resourceCatelogType) ;
        if (platformType.equals(DicPlatformType.OPEN)){
            criteria.andIsOpenEqualTo((byte)1) ;
        }else{
            criteria.andIsShareEqualTo((byte)1) ;
        }
        if (resourceType.equals(DicResourceType.API)){
            criteria.andIsApiCatelogEqualTo((byte) 1) ;
        }
        if (resourceType.equals(DicResourceType.MICROAPP)){
            criteria.andIsMicroappCatelogEqualTo((byte) 1) ;
        }
        if (resourceType.equals(DicResourceType.DATA)){
            criteria.andIsDataCatelogEqualTo((byte) 1) ;
        }
        List<ResourceCatelog> resourceCatelogs = resourceCatelogMapper.selectByExample(example) ;
        List<ResourceCatelogSearchList> resourceCatelogSearchLists = resourceCatelogs.stream().map(resourceCatelog -> {
            ResourceCatelogSearchList resourceCatelogSearchList = new ResourceCatelogSearchList() ;
            resourceCatelogSearchList.setCateLogId(resourceCatelog.getId());
            resourceCatelogSearchList.setCateLogName(resourceCatelog.getCatelogName());
            resourceCatelogSearchList.setCatelogCode(resourceCatelog.getCatelogCode());
            return resourceCatelogSearchList ;
        }).collect(Collectors.toList()) ;
        return resourceCatelogSearchLists;
    }

    public String getTagsSearchListByName(String resourceType,String resourceCatelogType,String tagName) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        SearchApiOrMicTagListModel searchApiOrMicTagListModel = resourceCatelogMapper.SearchApiOrMicTagOrTagName(resourceType,resourceCatelogType,tagName) ;
        if (searchApiOrMicTagListModel!=null){
            return searchApiOrMicTagListModel.getCatelogCode() ;
        }else{
            return null ;
        }
    }

    @Override
    public List<ResourceCatelog> getTypeList(String theme) {
        ResourceCatelogExample example = new ResourceCatelogExample() ;
        example.createCriteria().andCatelogTypeEqualTo(theme) ;
        return resourceCatelogMapper.selectByExample(example);
    }

}
