package com.yanzhu.jh.video.service.impl;

import com.ruoyi.common.enums.PublicStateEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.yanzhu.jh.video.domain.SurProjectVideoConfig;
import com.yanzhu.jh.video.domain.SurProjectVideoPassage;
import com.yanzhu.jh.video.mapper.SurProjectVideoConfigMapper;
import com.yanzhu.jh.video.service.ISurProjectVideoConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 视频配置Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2023-08-25
 */
@Service
public class SurProjectVideoConfigServiceImpl implements ISurProjectVideoConfigService
{
    @Autowired
    private SurProjectVideoConfigMapper surProjectVideoConfigMapper;

    /**
     * 查询视频配置
     * 
     * @param id 视频配置主键
     * @return 视频配置
     */
    @Override
    public SurProjectVideoConfig selectSurProjectVideoConfigById(Long id)
    {
        return surProjectVideoConfigMapper.selectSurProjectVideoConfigById(id);
    }

    /**
     * 查询视频配置列表
     * 
     * @param surProjectVideoConfig 视频配置
     * @return 视频配置
     */
    @Override
    public List<SurProjectVideoConfig> selectSurProjectVideoConfigList(SurProjectVideoConfig surProjectVideoConfig)
    {
        return surProjectVideoConfigMapper.selectSurProjectVideoConfigList(surProjectVideoConfig);
    }

    /**
     * 新增视频配置
     * 
     * @param surProjectVideoConfig 视频配置
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSurProjectVideoConfig(SurProjectVideoConfig surProjectVideoConfig)
    {

        surProjectVideoConfig.setVideoPassageCount(surProjectVideoConfig.getSurProjectVideoPassageList().size());
        surProjectVideoConfig.setPassagePassCount(surProjectVideoConfig.getVideoPassageCount());
        surProjectVideoConfig.setIsDel(PublicStateEnum.OK.getCode());
        //新增的视频为离线
        surProjectVideoConfig.setSignalState(PublicStateEnum.VIDEO_UNKNOWN.getCode());
        surProjectVideoConfig.setCreateBy(SecurityUtils.getUsername());
        surProjectVideoConfig.setCreateTime(DateUtils.getNowDate());
        int rows = surProjectVideoConfigMapper.insertSurProjectVideoConfig(surProjectVideoConfig);
        insertSurProjectVideoPassage(surProjectVideoConfig);
        return rows;
    }

    /**
     * 修改视频配置
     * 
     * @param surProjectVideoConfig 视频配置
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSurProjectVideoConfig(SurProjectVideoConfig surProjectVideoConfig)
    {
        if(surProjectVideoConfig.getProjectId()==null){
            surProjectVideoConfig.setProjectId(0L);
        }
        surProjectVideoConfig.setVideoPassageCount(surProjectVideoConfig.getSurProjectVideoPassageList().size());
        surProjectVideoConfig.setPassagePassCount(surProjectVideoConfig.getVideoPassageCount());
        surProjectVideoConfig.setUpdateBy(SecurityUtils.getUsername());
        surProjectVideoConfig.setUpdateTime(DateUtils.getNowDate());
        surProjectVideoConfigMapper.deleteSurProjectVideoPassageByVideoId(surProjectVideoConfig.getId());
        insertSurProjectVideoPassage(surProjectVideoConfig);
        return surProjectVideoConfigMapper.updateSurProjectVideoConfig(surProjectVideoConfig);
    }

    /**
     * 修改视频配置
     *
     * @param surProjectVideoConfig 视频配置
     * @return 结果
     */
    @Transactional
    @Override
    public int updateVideoConfig(SurProjectVideoConfig surProjectVideoConfig){
        return surProjectVideoConfigMapper.updateSurProjectVideoConfig(surProjectVideoConfig);
    }

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

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

    /**
     * 修改视频通道状态
     *
     * @param SurProjectVideoPassage 视频通道
     * @return 结果
     */
    @Override
    @Transactional
    public int editPassageState(SurProjectVideoPassage SurProjectVideoPassage) {
        return surProjectVideoConfigMapper.updateProjectVideoPassageState(SurProjectVideoPassage);
    }

    /**
     * 修改设备状态
     *
     * @param videoDvrNumber 设备编号
     */
    @Override
    @Transactional
    public int updateSurProjectVideoConfigSignalState(String videoDvrNumber){
        SurProjectVideoConfig surProjectVideoConfig = surProjectVideoConfigMapper.selectSurProjectVideoConfigByDvrNumber(videoDvrNumber);
        int res = 0;
        for(SurProjectVideoPassage passage:surProjectVideoConfig.getSurProjectVideoPassageList()){
            if(passage.getPassageState().equals(PublicStateEnum.VIDEO_ONLINE.getCode())){
                res++;
            }
        }
        surProjectVideoConfig.setPassagePassCount(res);
        //判断设备是否在线，一个通道在线则显示设备在线
        if(res>0){
            surProjectVideoConfig.setSignalState(PublicStateEnum.VIDEO_ONLINE.getCode());
        }else{
            surProjectVideoConfig.setSignalState(PublicStateEnum.VIDEO_OFFLINE.getCode());
        }
        surProjectVideoConfigMapper.updateSurProjectVideoConfig(surProjectVideoConfig);
        return 1;
    }

    /**
     * 获取设备通道
     *
     * @param videoDvrNumberd 设备编号
     * @return 结果
     */
    public List<SurProjectVideoPassage> getVideoPassage(String videoDvrNumberd) {
        return surProjectVideoConfigMapper.selectSurProjectVideoConfigByDvrNumber(videoDvrNumberd).getSurProjectVideoPassageList();
    }

    /**
     * 新增视频通道信息
     * 
     * @param surProjectVideoConfig 视频配置对象
     */
    public void insertSurProjectVideoPassage(SurProjectVideoConfig surProjectVideoConfig)
    {
        List<SurProjectVideoPassage> surProjectVideoPassageList = surProjectVideoConfig.getSurProjectVideoPassageList();
        Long id = surProjectVideoConfig.getId();
        if (StringUtils.isNotNull(surProjectVideoPassageList))
        {
            List<SurProjectVideoPassage> list = new ArrayList<SurProjectVideoPassage>();
            for (SurProjectVideoPassage surProjectVideoPassage : surProjectVideoPassageList)
            {
                surProjectVideoPassage.setVideoId(id);
                surProjectVideoPassage.setVideoDvrNumber(surProjectVideoConfig.getVideoDvrNumber());
                surProjectVideoPassage.setPassageState(PublicStateEnum.VIDEO_UNKNOWN.getCode());
                list.add(surProjectVideoPassage);
            }
            if (list.size() > 0)
            {
                surProjectVideoConfigMapper.batchSurProjectVideoPassage(list);
            }
        }
    }
}
