package com.fdkankan.scene.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.scene.entity.SceneResourceCamera;
import com.fdkankan.scene.mapper.ISceneResourceCameraMapper;
import com.fdkankan.scene.service.ISceneResourceCameraService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 场景资源和相机关联表 服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class SceneResourceCameraServiceImpl extends ServiceImpl<ISceneResourceCameraMapper, SceneResourceCamera> implements ISceneResourceCameraService {

    @Override
    public void deleteByCameraId(Long id) {
        this.update(new LambdaUpdateWrapper<SceneResourceCamera>()
                .eq(SceneResourceCamera::getCameraId, id)
                .set(SceneResourceCamera::getTbStatus, TbStatus.DELETE.code()));
    }

    @Override
    public List<SceneResourceCamera> findListByCameraId(Long id) {
        return this.list(new LambdaQueryWrapper<SceneResourceCamera>()
                .eq(SceneResourceCamera::getTbStatus, TbStatus.VALID.code())
                .eq(SceneResourceCamera::getTbStatus, TbStatus.VALID.code()).eq(SceneResourceCamera::getCameraId, id));
    }

    @Override
    public void saveByResourceIds(Long cameraId, String resourceIds) {
        if(StringUtil.isNotEmpty(resourceIds)){
            SceneResourceCamera sceneResourceCameraEntity = null;
            String[] ids = resourceIds.split(",");
            for(String resourceId : ids){
                sceneResourceCameraEntity = new SceneResourceCamera();
                sceneResourceCameraEntity.setSceneResourceId(Long.valueOf(resourceId));
                sceneResourceCameraEntity.setCameraId(cameraId);
                this.save(sceneResourceCameraEntity);
            }
        }
    }
}
