package com.lightinit.hsdataplatformresdir.impl.admin;

import com.lightinit.hsdataplatformresdir.dictionary.DicResourceCatelogType;
import com.lightinit.hsdataplatformresdir.entity.ResourceCatelog;
import com.lightinit.hsdataplatformresdir.entity.ResourceCatelogExample;
import com.lightinit.hsdataplatformresdir.mapper.ResourceCatelogMapper;
import com.lightinit.hsdataplatformresdir.mapper.ResourceMapper;
import com.lightinit.hsdataplatformresdir.model.admin.*;
import com.lightinit.hsdataplatformresdir.service.admin.IResourceDeptImagesService;
import com.lightinit.hsdataplatformresdir.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 ResourceDeptImagesServiceImpl implements IResourceDeptImagesService {

    @Autowired
    private ResourceMapper resourceMapper ;

    @Autowired
    private ResourceCatelogMapper resourceCatelogMapper ;

    @Override
    public ResourceAllImgFreeParentModel findAll(ResourceAllImagesSearchModel inputModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        //创建
        ResourceAllImgFreeParentModel imgFreeModel = new ResourceAllImgFreeParentModel("部门资源图谱") ;
        //查找目录
        ResourceCatelogExample example = new ResourceCatelogExample() ;
        ResourceCatelogExample.Criteria criteria = example.createCriteria() ;
        criteria.andParentIdIsNull() ;
        criteria.andCatelogCodeIsNotNull() ;
        criteria.andCatelogTypeEqualTo(DicResourceCatelogType.SECTOR) ;
        if (inputModel.getResourceName()!=""){
            criteria.andCatelogNameEqualTo(inputModel.getResourceName()) ;
        }
        List<ResourceCatelog> resourceCatelogs = resourceCatelogMapper.selectByExample(example) ;
        if (resourceCatelogs.size()==0){
            ResourceCatelogExample example1 = new ResourceCatelogExample() ;
            ResourceCatelogExample.Criteria criteria1 = example1.createCriteria() ;
            criteria1.andCatelogCodeIsNotNull() ;
            criteria1.andCatelogTypeEqualTo(DicResourceCatelogType.SECTOR) ;
            criteria1.andCatelogNameEqualTo(inputModel.getResourceName()) ;
            List<ResourceCatelog> resourceCatelogs1 = resourceCatelogMapper.selectByExample(example1) ;
            if (resourceCatelogs1.size()>0){
                resourceCatelogs.add(resourceCatelogs1.get(0)) ;
            }
        }
        List<ResourceAllImgFreeModel> list = resourceCatelogs.stream().map(resourceCatelog -> {
            if (resourceCatelog.getParentId()!=null){
                ResourceCatelog resourceLog = resourceCatelogMapper.selectByPrimaryKey(resourceCatelog.getParentId());
                ResourceAllImgFreeModel resourceAllImgFreeModel = new ResourceAllImgFreeModel(resourceLog.getCatelogName()) ;
                //查找目录下的资源
                inputModel.setCatelogCode(resourceLog.getCatelogCode());
                resourceAllImgFreeModel.setValue("主题"+resourceLog.getCatelogCode());
//            List<ResourceTreeNodeModel>  resourceTreeNodeModels= resourceMapper.selectResourceByCatelogCode(inputModel);
                List<ResourceTreeNodeModel>  resourceTreeNodeModels= resourceMapper.selectTwoLogList(inputModel);
                resourceAllImgFreeModel.setChildren(resourceTreeNodeModels);
                return  resourceAllImgFreeModel;
            }else {
                ResourceAllImgFreeModel resourceAllImgFreeModel = new ResourceAllImgFreeModel(resourceCatelog.getCatelogName()) ;
                //查找目录下的资源
                inputModel.setCatelogCode(resourceCatelog.getCatelogCode());
                resourceAllImgFreeModel.setValue("主题"+resourceCatelog.getCatelogCode());
//            List<ResourceTreeNodeModel>  resourceTreeNodeModels= resourceMapper.selectResourceByCatelogCode(inputModel);
                List<ResourceTreeNodeModel>  resourceTreeNodeModels= resourceMapper.selectTwoLogList(inputModel);
                resourceAllImgFreeModel.setChildren(resourceTreeNodeModels);
                return  resourceAllImgFreeModel;
            }
        }).collect(Collectors.toList()) ;
        //添加目录
        imgFreeModel.setChildren(list);
        return imgFreeModel;
    }

    @Override
    public ResourceAllImgFreeParentModel findThemesByName(String themeCode) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        //查找主题code
        ResourceCatelogExample example = new ResourceCatelogExample() ;
        ResourceCatelogExample.Criteria criteria = example.createCriteria() ;
        criteria.andCatelogCodeEqualTo(themeCode) ;
        List<ResourceCatelog> resourceCatelogs = resourceCatelogMapper.selectByExample(example) ;
        //创建
        ResourceAllImgFreeParentModel imgFreeModel = new ResourceAllImgFreeParentModel(resourceCatelogs.get(0).getCatelogName()) ;
        //查询下面的相关主题
        ResourceAllImagesSearchModel input = new ResourceAllImagesSearchModel() ;
        List<ResourceCatelog> resourceCatelogList = resourceCatelogMapper.selectResourceCatelogByCode(themeCode) ;
        //查找dept下的直接数据
        //==========================部门资源图谱======================
        List<ResourceAllImgFreeModel> list1 = resourceMapper.selectThisDeptDirectDate(themeCode) ;
        //查找theme下的主题数据
        List<ResourceAllImgFreeModel> list = resourceCatelogList.stream().filter(q->!q.getCatelogCode().equals(themeCode)).map(resourceCatelog -> {
            ResourceAllImgFreeModel resourceAllImgFreeModel = new ResourceAllImgFreeModel(resourceCatelog.getCatelogName()) ;
            //查找目录下的资源
            input.setCatelogCode(resourceCatelog.getCatelogCode());
            //==========================部门资源图谱======================
            List<ResourceTreeNodeModel>  resourceTreeNodeModels= resourceMapper.selectDeptByCatelogCode(input);
            resourceAllImgFreeModel.setChildren(resourceTreeNodeModels);
            return resourceAllImgFreeModel ;
        }).collect(Collectors.toList()) ;
        //添加目录
        list1.addAll(list) ;
        imgFreeModel.setChildren(list1);
        return imgFreeModel;
    }

    @Override
    public List<ResourceAllImagesListModel> findBasicsAllList(ResourceAllImagesSearchModel inputModel) {
        //========部门图谱
        return resourceMapper.findDeptAllList(inputModel) ;
    }

    @Override
    public long findBasicsAllListCount(ResourceAllImagesSearchModel inputModel) {
        //========部门图谱
        return resourceMapper.findDeptAllListCount(inputModel) ;
    }

    @Override
    public List<ResourceAllImagesListModel> findAllThemeList(ResourceNoImgThemeListModel inputModel) {
        //========部门图谱
        return resourceMapper.findAllDeptList(inputModel);
    }

    @Override
    public long findAllThemeListCount(ResourceNoImgThemeListModel inputModel) {
        //========部门图谱
        return resourceMapper.findAllDeptListCount(inputModel);
    }

    @Override
    public ResourceAllImgFreeParentModel findResourceById(String resourceId) {
        //========部门图谱
        ResourceAllImagesListModel resourceAllImagesListModel = resourceMapper.findDeptById(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.findResourceById(resourceId);
    }

}
