package cn.iocoder.yudao.module.pms.service.yanfan.sip.device;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.device.vo.YfSipDevicePageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.yanfan.sip.device.vo.YfSipDeviceSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.device.YfSipDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.yanfan.sip.device.channel.YfSipDeviceChannelDO;
import cn.iocoder.yudao.module.pms.dal.mysql.yanfan.sip.device.YfSipDeviceMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.yanfan.sip.device.channel.YfSipDeviceChannelMapper;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.device.channel.YfSipDeviceChannelService;
import cn.iocoder.yudao.module.pms.service.yanfan.sip.model.BaseTree;
import liquibase.pro.packaged.Y;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * yf监控设备 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class YfSipDeviceServiceImpl implements YfSipDeviceService {

    @Resource
    private YfSipDeviceMapper yfSipDeviceMapper;
    @Autowired
    private YfSipDeviceChannelService yfSipDeviceChannelService;
    @Autowired
    private YfSipDeviceChannelMapper yfSipDeviceChannelMapper;

    @Override
    @TenantIgnore
    public YfSipDeviceDO selectSipDeviceBySipId(String sipId)
    {
        return yfSipDeviceMapper.selectOne("device_sip_id", sipId);
    }
    @Override
    public boolean exists(String sipId) {
        return yfSipDeviceMapper.selectSipDeviceBySipId(sipId) != null;
    }

    @Override
    public int updateSipDeviceStatus(YfSipDeviceDO sipDevice) {
        sipDevice.setUpdateTime(LocalDateTime.now());
        return yfSipDeviceMapper.updateSipDeviceStatus(sipDevice);
    }


    @Override
    public List<BaseTree<YfSipDeviceChannelDO>> selectSipDeviceChannelList(String deviceId) {
        return yfSipDeviceChannelService.queryVideoDeviceTree(deviceId,null,false);
    }


    @Override
    @TenantIgnore
    public boolean updateDevice(YfSipDeviceDO device) {
        YfSipDeviceDO devicetemp = yfSipDeviceMapper.selectOne("device_sip_id", device.getDeviceSipId());
        if (devicetemp == null) {
            return yfSipDeviceMapper.insert(device) > 0;
        }else {
            device.setDeviceId(devicetemp.getDeviceId());
            return yfSipDeviceMapper.updateById(device) > 0;
        }
    }

    @Override
    public Long createYfSipDevice(YfSipDeviceSaveReqVO createReqVO) {
        // 插入
        YfSipDeviceDO yfSipDevice = BeanUtils.toBean(createReqVO, YfSipDeviceDO.class);
        yfSipDeviceMapper.insert(yfSipDevice);
        // 返回
        return yfSipDevice.getDeviceId();
    }

    @Override
    public void updateYfSipDevice(YfSipDeviceSaveReqVO updateReqVO) {
        // 校验存在
        validateYfSipDeviceExists(updateReqVO.getDeviceId());
        // 更新
        YfSipDeviceDO updateObj = BeanUtils.toBean(updateReqVO, YfSipDeviceDO.class);
        yfSipDeviceMapper.updateById(updateObj);
    }

    @Override
    public void deleteYfSipDevice(Long id) {
        // 校验存在
        validateYfSipDeviceExists(id);
        // 删除
        yfSipDeviceMapper.deleteById(id);
    }

    private void validateYfSipDeviceExists(Long id) {
        if (yfSipDeviceMapper.selectById(id) == null) {
            throw exception(new ErrorCode(9,"不存在"));
        }
    }

    @Override
    public YfSipDeviceDO getYfSipDevice(Long id) {
        return yfSipDeviceMapper.selectById(id);
    }

    @Override
    public PageResult<YfSipDeviceDO> getYfSipDevicePage(YfSipDevicePageReqVO pageReqVO) {
        return yfSipDeviceMapper.selectPage(pageReqVO);
    }


    @Override
    public int deleteSipDeviceBySipId(String SipId)
    {
        yfSipDeviceMapper.deleteSipDeviceByByDeviceSipId(SipId);
        return yfSipDeviceChannelMapper.deleteSipDeviceChannelByDeviceId(SipId);
    }
}
