package com.dcsoft.system.camera.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.system.artemis.vo.ChannelVo;
import com.dcsoft.system.artemis.vo.DeviceVo;
import com.dcsoft.system.artemis.vo.VideoStreamVo;
import com.dcsoft.system.domain.SysPoint;
import com.dcsoft.system.domain.SysSpace;
import com.dcsoft.system.domain.vo.SysAuthTree;
import com.dcsoft.system.mapper.SysPointMapper;
import com.dcsoft.system.mapper.SysSpaceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcsoft.system.camera.mapper.TbCameraMapper;
import com.dcsoft.system.camera.domain.TbCamera;
import com.dcsoft.system.camera.service.ITbCameraService;

/**
 * 视频配置Service业务层处理
 *
 * @author nichun
 * @date 2023-08-10
 */
@Service
public class TbCameraServiceImpl implements ITbCameraService
{
    @Autowired
    private TbCameraMapper tbCameraMapper;

    @Autowired
    private SysSpaceMapper spaceMapper;

    @Autowired
    private SysPointMapper pointMapper;


    /**
     * 查询视频配置
     *
     * @param id 视频配置主键
     * @return 视频配置
     */
    @Override
    public TbCamera selectTbCameraById(Long id)
    {
        return tbCameraMapper.selectTbCameraById(id);
    }

    /**
     * 查询视频配置列表
     *
     * @param tbCamera 视频配置
     * @return 视频配置
     */
    @Override
    public List<TbCamera> selectTbCameraList(TbCamera tbCamera)
    {
        return tbCameraMapper.selectTbCameraList(tbCamera);
    }

    /**
     * 新增视频配置
     *
     * @param tbCamera 视频配置
     * @return 结果
     */
    @Override
    public int insertTbCamera(TbCamera tbCamera)
    {
        tbCamera.setCreateTime(DateUtils.getNowDate());
        return tbCameraMapper.insertTbCamera(tbCamera);
    }

    /**
     * 修改视频配置
     *
     * @param tbCamera 视频配置
     * @return 结果
     */
    @Override
    public int updateTbCamera(TbCamera tbCamera)
    {
        tbCamera.setUpdateTime(DateUtils.getNowDate());
        return tbCameraMapper.updateTbCamera(tbCamera);
    }

    /**
     * 批量删除视频配置
     *
     * @param ids 需要删除的视频配置主键
     * @return 结果
     */
    @Override
    public int deleteTbCameraByIds(Long[] ids)
    {
        return tbCameraMapper.deleteTbCameraByIds(ids);
    }

    /**
     * 删除视频配置信息
     *
     * @param id 视频配置主键
     * @return 结果
     */
    @Override
    public int deleteTbCameraById(Long id)
    {
        return tbCameraMapper.deleteTbCameraById(id);
    }

    @Override
    public List<SysAuthTree> selectTree() {
        List<SysAuthTree> tree=new ArrayList<SysAuthTree>();
        SysSpace space=new SysSpace();
        space.setType("0");
        List<SysSpace> spaces  =spaceMapper.selectSysSpaceList(space);
        for(SysSpace s:spaces){
            List<SysAuthTree> children=getChildrenPoint(s.getId());
            SysAuthTree a=new SysAuthTree();
            a.setId(s.getId());
            a.setLabel(s.getName());
            a.setType("0");
            a.setChildren(children);
            a.setParentId(s.getParentId());
            tree.add(a);
        }

        return tree;
    }

    @Override
    public List<TbCamera> queryCameraList(List<String> collect) {
        return pointMapper.queryCameraList(collect);
    }

    @Override
    public void saveCamera(List<ChannelVo> deviceVos) {
        pointMapper.saveCamera(deviceVos);
    }

    @Override
    public void saveVideoStream(List<VideoStreamVo> videoStreamList) {
        pointMapper.saveVideoStream(videoStreamList);
    }

    private List<SysAuthTree> getChildrenPoint(Long id) {
        List<SysAuthTree> tree=new ArrayList<>();
        SysPoint point=new SysPoint();
        point.setSpaceId(id);
        List<SysPoint> points  =pointMapper.selectSysPointList(point);
        for(SysPoint p:points){
            List<SysAuthTree> children=getChildrenCamera(p.getId());
            SysAuthTree a=new SysAuthTree();
            a.setId(p.getId());
            a.setLabel(p.getName());
            a.setType("1");
            a.setChildren(children);
            a.setParentId(id);
            tree.add(a);
        }
        return tree;
    }

    private List<SysAuthTree> getChildrenCamera(Long id) {
        List<SysAuthTree> tree=new ArrayList<>();
        TbCamera tbCamera=new TbCamera();
        tbCamera.setPointId(id);
        List<TbCamera> cameras  =tbCameraMapper.selectTbCameraList(tbCamera);
        for(TbCamera p:cameras){
            SysAuthTree a=new SysAuthTree();
            a.setId(p.getId());
            a.setLabel(p.getName());
            a.setType("2");
            a.setParentId(id);
            a.setStatus(p.getUrls());
            a.setChannelId(p.getChannelId());
            a.setDeviceId(p.getDeviceId());
            tree.add(a);
        }
        return tree;
    }


}
