package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.scene.entity.SceneResource;
import com.fdkankan.scene.entity.SceneResourceCamera;
import com.fdkankan.scene.entity.SceneResourceCooperation;
import com.fdkankan.scene.mapper.ISceneResourceMapper;
import com.fdkankan.scene.service.ISceneResourceCameraService;
import com.fdkankan.scene.service.ISceneResourceCooperationService;
import com.fdkankan.scene.service.ISceneResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 场景资源表 服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class SceneResourceServiceImpl extends ServiceImpl<ISceneResourceMapper, SceneResource> implements ISceneResourceService {

    @Autowired
    ISceneResourceCooperationService sceneResourceCooperationService;
    @Autowired
    ISceneResourceCameraService sceneResourceCameraService;

    @Override
    public List<SceneResource> findByCooperationId(Long id) {

        List<SceneResourceCooperation> sceneResourceCooperationList = sceneResourceCooperationService.list(
                new LambdaQueryWrapper<SceneResourceCooperation>()
                        .select(SceneResourceCooperation::getSceneResourceId)
                        .eq(SceneResourceCooperation::getTbStatus, TbStatus.VALID.code())
                        .eq(SceneResourceCooperation::getSceneCooperationId, id));
        if(CollUtil.isEmpty(sceneResourceCooperationList)){
            return null;
        }
        List<Long> sceneResIdList = sceneResourceCooperationList.stream().map(srCoo -> {
            return srCoo.getSceneResourceId();
        }).collect(Collectors.toList());

        return this.list(new LambdaQueryWrapper<SceneResource>().in(SceneResource::getId, sceneResIdList));
    }

    @Override
    public List<SceneResource> findByCameraId(Long id) {

        List<SceneResourceCamera> sceneResourceCameraList = sceneResourceCameraService.list(
                new LambdaQueryWrapper<SceneResourceCamera>()
                        .select(SceneResourceCamera::getSceneResourceId)
                        .eq(SceneResourceCamera::getTbStatus, TbStatus.VALID.code())
                        .eq(SceneResourceCamera::getCameraId, id));
        if(CollUtil.isEmpty(sceneResourceCameraList)){
            return null;
        }
        List<Long> sceneResIdList = sceneResourceCameraList.stream().map(SceneResourceCamera::getSceneResourceId).collect(Collectors.toList());

        return this.list(new LambdaQueryWrapper<SceneResource>().in(SceneResource::getId, sceneResIdList));

    }
}
