package com.sz.admin.monitor.mapper;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.sz.admin.monitor.pojo.po.Camera;
import com.sz.admin.monitor.pojo.po.table.CameraTableDef;

import java.util.List;

/**
 * ClassName: BzCameraMapper
 * Package: com.sz.admin.monitor.mapper
 * Description:
 */
public interface CameraMapper extends BaseMapper<Camera> {
    // 根据NvrId删除摄像头
    default void deleteByNvrId(Long nvrId)
    {
        QueryWrapper queryWrapper = QueryWrapper.create()
            .where(CameraTableDef.CAMERA.NVR_ID.eq(nvrId));
        this.deleteByQuery(queryWrapper);
    }

    // 根据NvrId查询摄像头
    default List<Camera> selectByNvrId(Long id){
        QueryWrapper queryWrapper = QueryWrapper.create()
            .where(CameraTableDef.CAMERA.NVR_ID.eq(id));
       return this.selectListByQuery(queryWrapper);
    }

    default List<Long> selectIdsByEdgeBoxId(Long boxId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
            .select(CameraTableDef.CAMERA.ID)
            .from(CameraTableDef.CAMERA)
            .eq(Camera::getBoxId, boxId);
        List<Camera> cameraList = selectListByQuery(queryWrapper);
        return cameraList.stream().map(Camera::getId).toList();
    }

    default void updateBoxIdForCameras(Long boxId, List<Long> addedIds) {
        if (addedIds == null || addedIds.isEmpty()) {
            return;
        }
        UpdateChain.of(Camera.class)
            .set(Camera::getBoxId, boxId)
            .in(Camera::getId, addedIds)
            .update();
    }
}
