package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.Resource;
import com.lightinit.hsdataplatform.entity.ResourceCatelog;
import com.lightinit.hsdataplatform.entity.ResourceCatelogExample;
import com.lightinit.hsdataplatform.entity.ResourceExample;
import com.lightinit.hsdataplatform.mapper.ResourceCatelogMapper;
import com.lightinit.hsdataplatform.mapper.ResourceMapper;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.IResourceShareImagesService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ResourceShareImagesServiceImpl implements IResourceShareImagesService {

    @Autowired
    private ResourceMapper resourceMapper ;

    @Autowired
    private ResourceCatelogMapper resourceCatelogMapper ;

    @Override
    public ResourceAllImgFreeParentModel findAll(ResourceAllImagesSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        //创建
        ResourceAllImgFreeParentModel imgFreeModel = new ResourceAllImgFreeParentModel("共享资源图谱") ;
        //查找目录
        List<String> shares = resourceMapper.getShareType() ;
        if (shares.size()>0){
            List<ResourceAllImgFreeModel> children = new ArrayList<>() ;
            for (String share : shares) {
                ResourceAllImgFreeModel resourceAllImgFreeModel = new ResourceAllImgFreeModel() ;
                resourceAllImgFreeModel.setName(share);
                resourceAllImgFreeModel.setValue("主题"+share);
                children.add(resourceAllImgFreeModel) ;
            }
            imgFreeModel.setChildren(children);
        }
        return imgFreeModel;
    }

    @Override
    public ResourceAllImgFreeParentModel findThemesByName(String shareType) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        //创建
        ResourceAllImgFreeParentModel imgFreeModel = new ResourceAllImgFreeParentModel(shareType) ;
        //查询下面的相关主题
        ResourceExample example = new ResourceExample() ;
        ResourceExample.Criteria criteria = example.createCriteria() ;
        criteria.andShareTypeEqualTo(shareType) ;
        List<Resource> resources = resourceMapper.selectByExample(example);
        if (resources.size()>0){
            List<ResourceAllImgFreeModel> children = new ArrayList<>() ;
            for (Resource resource : resources) {
                ResourceAllImgFreeModel resourceAllImgFreeModel = new ResourceAllImgFreeModel() ;
                resourceAllImgFreeModel.setName(resource.getResourceName());
                resourceAllImgFreeModel.setValue("资源"+resource.getId());
                children.add(resourceAllImgFreeModel) ;
            }
            imgFreeModel.setChildren(children);
        }

        return imgFreeModel;
    }

    @Override
    public List<ResourceAllImagesListModel> findBasicsAllList(ResourceAllImagesSearchModel inputModel) {
        //========共享图谱
        return resourceMapper.findShareAllList(inputModel) ;
    }

    @Override
    public long findBasicsAllListCount(ResourceAllImagesSearchModel inputModel) {
        //========共享图谱
        return resourceMapper.findShareAllListCount(inputModel) ;
    }

    @Override
    public List<ResourceAllImagesListModel> findAllThemeList(ResourceNoImgThemeListModel inputModel) {
        //========共享图谱
        return resourceMapper.findAllShareList(inputModel);
    }

    @Override
    public long findAllThemeListCount(ResourceNoImgThemeListModel inputModel) {
        //========共享图谱
        return resourceMapper.findAllShareListCount(inputModel);
    }

    @Override
    public ResourceAllImgFreeParentModel findResourceById(String resourceId) {
        //========部门图谱
        ResourceAllImagesListModel resourceAllImagesListModel = resourceMapper.findShareById(resourceId) ;
        ResourceAllImgFreeParentModel resourceAllImgFreeParentModel = new ResourceAllImgFreeParentModel(resourceAllImagesListModel.getResourceName()) ;
        ResourceAllImgFreeModel resourceAllImgFreeModel = new ResourceAllImgFreeModel("资源类型") ;
        resourceAllImgFreeModel.getChildren().add(new ResourceTreeNodeModel(resourceAllImagesListModel.getResourceType()));

        ResourceAllImgFreeModel resourceAllImgFreeModel1 = new ResourceAllImgFreeModel("所属机构") ;
        resourceAllImgFreeModel1.getChildren().add(new ResourceTreeNodeModel(resourceAllImagesListModel.getProviderName()));

        ResourceAllImgFreeModel resourceAllImgFreeModel2 = new ResourceAllImgFreeModel("授权属性") ;
        resourceAllImgFreeModel2.getChildren().add(new ResourceTreeNodeModel(resourceAllImagesListModel.getOpenType()+"/"+resourceAllImagesListModel.getShareType()));

        ResourceAllImgFreeModel resourceAllImgFreeModel3 = new ResourceAllImgFreeModel("标签") ;
        resourceAllImgFreeModel3.getChildren().add(new ResourceTreeNodeModel(resourceAllImagesListModel.getServiceTag()));

        resourceAllImgFreeParentModel.getChildren().add(resourceAllImgFreeModel) ;
        resourceAllImgFreeParentModel.getChildren().add(resourceAllImgFreeModel1) ;
        resourceAllImgFreeParentModel.getChildren().add(resourceAllImgFreeModel2) ;
        resourceAllImgFreeParentModel.getChildren().add(resourceAllImgFreeModel3) ;
        return  resourceAllImgFreeParentModel;
    }

    @Override
    public ResourceAllImagesListModel resourceList(String resourceId) {
        return resourceMapper.findShareResourceById(resourceId);
    }

}
