package com.xunmei.core.access.service.impl;

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


import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.core.access.domain.CoreMonitoringTaskMonitorInfo;
import com.xunmei.core.access.domain.CoreMonitoringTaskRegistrationMonitor;
import com.xunmei.core.access.mapper.CoreMonitoringTaskMonitorInfoMapper;
import com.xunmei.core.access.mapper.CoreMonitoringTaskRegistrationMonitorMapper;
import com.xunmei.core.access.service.ICoreMonitoringTaskRegistrationMonitorService;
import com.xunmei.core.access.vo.CoreMonitoringTaskRegistrationMonitorVO;
import com.xunmei.core.access.vo.CoreMonitoringTaskRegistrationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


/**
 * 调阅任务登记监控Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-29
 */
@Service
public class CoreMonitoringTaskRegistrationMonitorServiceImpl extends ServiceImpl<CoreMonitoringTaskRegistrationMonitorMapper, CoreMonitoringTaskRegistrationMonitor> implements ICoreMonitoringTaskRegistrationMonitorService {
    @Autowired
    private CoreMonitoringTaskRegistrationMonitorMapper coreMonitoringTaskRegistrationMonitorMapper;
    @Autowired
    private CoreMonitoringTaskMonitorInfoMapper coreMonitoringTaskMonitorInfoMapper;

/*    @Override
    public TableDataInfo<CoreMonitoringTaskRegistrationMonitor> selectPage(CoreMonitoringTaskRegistrationMonitor coreMonitoringTaskRegistrationMonitor) {
        //未删除
        coreMonitoringTaskRegistrationMonitor.setDeleted(0L);
        Page<CoreMonitoringTaskRegistrationMonitor> page;
        //分页
        if (coreMonitoringTaskRegistrationMonitor.getPageNum()!=null&&coreMonitoringTaskRegistrationMonitor.getPageSize()!=null)
        {
            page = new Page<>(coreMonitoringTaskRegistrationMonitor.getPageNum(), coreMonitoringTaskRegistrationMonitor.getPageSize());
        }else{
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<CoreMonitoringTaskRegistrationMonitor> query = new QueryWrapper<>(coreMonitoringTaskRegistrationMonitor);
        //下穿
        if (coreMonitoringTaskRegistrationMonitor.getCheckSub()){
            List<Long> ids = orgService.selectCheckSubOrgIdList(coreMonitoringTaskRegistrationMonitor.getOrgId());
            //清空前端传递的org_id
            coreMonitoringTaskRegistrationMonitor.setOrgId(null);
            //添加in条件
            query.in("org_id",ids);
        }
        //时间范围查询
        if (coreMonitoringTaskRegistrationMonitor.getParams().get("beginTime")!=null&&coreMonitoringTaskRegistrationMonitor.getParams().get("endTime")!=null){
            query.between("create_time", coreMonitoringTaskRegistrationMonitor.getParams().get("beginTime"), coreMonitoringTaskRegistrationMonitor.getParams().get("endTime"));
        }
        //获取数据
        page = coreMonitoringTaskRegistrationMonitorMapper.selectPage(page, query);
        //抓换为TableDataInfo适配前端
        return TableDataInfo.build(page);
    }*/


    /**
     * 查询调阅任务登记监控
     *
     * @param id 调阅任务登记监控主键
     * @return 调阅任务登记监控
     */
    @Override
    public CoreMonitoringTaskRegistrationMonitor selectCoreMonitoringTaskRegistrationMonitorById(String id) {
        return coreMonitoringTaskRegistrationMonitorMapper.selectById(id);
    }

    /**
     * 查询调阅任务登记监控列表
     *
     * @param coreMonitoringTaskRegistrationMonitor 调阅任务登记监控
     * @return 调阅任务登记监控
     */
    @Override
    public List<CoreMonitoringTaskRegistrationMonitor> selectCoreMonitoringTaskRegistrationMonitorList(CoreMonitoringTaskRegistrationMonitor coreMonitoringTaskRegistrationMonitor) {
        return coreMonitoringTaskRegistrationMonitorMapper.selectList(new QueryWrapper<>(coreMonitoringTaskRegistrationMonitor));
    }

    /**
     * 新增调阅任务登记监控
     *
     * @param coreMonitoringTaskRegistrationMonitor 调阅任务登记监控
     * @return 结果
     */
    @Override
    public int insertCoreMonitoringTaskRegistrationMonitor(CoreMonitoringTaskRegistrationMonitor coreMonitoringTaskRegistrationMonitor) {
        coreMonitoringTaskRegistrationMonitor.setCreateTime(DateUtils.getNowDate());
        return coreMonitoringTaskRegistrationMonitorMapper.insert(coreMonitoringTaskRegistrationMonitor);
    }

    /**
     * 修改调阅任务登记监控
     *
     * @param coreMonitoringTaskRegistrationMonitor 调阅任务登记监控
     * @return 结果
     */
    @Override
    public int updateCoreMonitoringTaskRegistrationMonitor(CoreMonitoringTaskRegistrationMonitor coreMonitoringTaskRegistrationMonitor) {
        coreMonitoringTaskRegistrationMonitor.setUpdateTime(DateUtils.getNowDate());
        return coreMonitoringTaskRegistrationMonitorMapper.updateById(coreMonitoringTaskRegistrationMonitor);
    }

    /**
     * 批量删除调阅任务登记监控
     *
     * @param ids 需要删除的调阅任务登记监控主键
     * @return 结果
     */
    @Override
    public int deleteCoreMonitoringTaskRegistrationMonitorByIds(String[] ids) {
        return coreMonitoringTaskRegistrationMonitorMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除调阅任务登记监控信息
     *
     * @param id 调阅任务登记监控主键
     * @return 结果
     */
    @Override
    public int deleteCoreMonitoringTaskRegistrationMonitorById(String id) {
        return coreMonitoringTaskRegistrationMonitorMapper.deleteById(id);
    }

    @Override
    public CoreMonitoringTaskRegistrationVO selectCoreMonitoringTaskRegistrationList(Long taskId) {
       //查询任务
        CoreMonitoringTaskRegistrationVO coreMonitoringTaskRegistrationVO = coreMonitoringTaskRegistrationMonitorMapper.selectTaskName(taskId);
        if (null != coreMonitoringTaskRegistrationVO) {
            //查询视频
            List<CoreMonitoringTaskRegistrationMonitorVO> coreMonitoringTaskRegistrationMonitorVOS = coreMonitoringTaskRegistrationMonitorMapper.selectMonitorByTaskRegistrationId(coreMonitoringTaskRegistrationVO.getId());
            if (CollectionUtils.isNotEmpty(coreMonitoringTaskRegistrationMonitorVOS)) {
                coreMonitoringTaskRegistrationMonitorVOS.forEach(c -> {
                    //查询项目
                    List<CoreMonitoringTaskMonitorInfo> coreMonitoringTaskMonitorInfos = coreMonitoringTaskMonitorInfoMapper.selectCoreMonitoringTaskMonitorInfoByMonitorId(c.getId());
                    c.setCoreMonitoringTaskMonitorInfoList(coreMonitoringTaskMonitorInfos);
                });
                coreMonitoringTaskRegistrationVO.setCoreMonitoringTaskRegistrationMonitorVOList(coreMonitoringTaskRegistrationMonitorVOS);
            }
        }
        return coreMonitoringTaskRegistrationVO;
    }

    @Override
    public CoreMonitoringTaskRegistrationMonitorVO selectMonitorById(Long id) {
        //查询视频
        CoreMonitoringTaskRegistrationMonitorVO coreMonitoringTaskRegistrationMonitorVO = baseMapper.selectMonitorById(id);
       //根据视频查询项目
        List<CoreMonitoringTaskMonitorInfo> coreMonitoringTaskMonitorInfos = coreMonitoringTaskMonitorInfoMapper.selectCoreMonitoringTaskMonitorInfoByMonitorId(coreMonitoringTaskRegistrationMonitorVO.getId());
        coreMonitoringTaskRegistrationMonitorVO.setCoreMonitoringTaskMonitorInfoList(coreMonitoringTaskMonitorInfos);
        return coreMonitoringTaskRegistrationMonitorVO;
    }
}
