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

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.core.access.domain.CoreMonitoringTaskMonitorInfo;
import com.xunmei.core.access.domain.CoreMonitoringTaskRegistrationMonitor;
import com.xunmei.core.access.dto.CoreMonitoringTaskRegistrationMonitorDTO;
import com.xunmei.core.access.mapper.CoreMonitoringTaskMonitorInfoMapper;
import com.xunmei.core.access.mapper.CoreMonitoringTaskRegistrationMonitorMapper;
import com.xunmei.core.access.service.ICoreMonitoringTaskMonitorInfoService;
import com.xunmei.core.access.vo.CoreMonitoringTaskRegistrationMonitorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.page.TableDataInfo;


/**
 * 调阅任务登记检查情况Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-29
 */
@Service
public class CoreMonitoringTaskMonitorInfoServiceImpl extends ServiceImpl<CoreMonitoringTaskMonitorInfoMapper, CoreMonitoringTaskMonitorInfo> implements ICoreMonitoringTaskMonitorInfoService {
    @Autowired
    private CoreMonitoringTaskMonitorInfoMapper coreMonitoringTaskMonitorInfoMapper;
    @Autowired
    private CoreMonitoringTaskRegistrationMonitorMapper coreMonitoringTaskRegistrationMonitorMapper;

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


    /**
     * 查询调阅任务登记检查情况
     *
     * @param id 调阅任务登记检查情况主键
     * @return 调阅任务登记检查情况
     */
    @Override
    public CoreMonitoringTaskMonitorInfo selectCoreMonitoringTaskMonitorInfoById(Long id) {
        return coreMonitoringTaskMonitorInfoMapper.selectById(id);
    }

    /**
     * 查询调阅任务登记检查情况列表
     *
     * @param coreMonitoringTaskMonitorInfo 调阅任务登记检查情况
     * @return 调阅任务登记检查情况
     */
    @Override
    public List<CoreMonitoringTaskMonitorInfo> selectCoreMonitoringTaskMonitorInfoList(CoreMonitoringTaskMonitorInfo coreMonitoringTaskMonitorInfo) {
        return coreMonitoringTaskMonitorInfoMapper.selectList(new QueryWrapper<>(coreMonitoringTaskMonitorInfo));
    }

    /**
     * 新增调阅任务登记检查情况
     *
     * @param coreMonitoringTaskRegistrationMonitorDTO 调阅任务登记检查情况
     * @return 结果
     */
    @Override
    public int insertCoreMonitoringTaskMonitorInfo(CoreMonitoringTaskRegistrationMonitorDTO coreMonitoringTaskRegistrationMonitorDTO) {
        //新增监控
        CoreMonitoringTaskRegistrationMonitor coreMonitoringTaskMonitorInfo = new CoreMonitoringTaskRegistrationMonitor();
        coreMonitoringTaskMonitorInfo.setDelFlag("0");
        coreMonitoringTaskMonitorInfo.setHost(coreMonitoringTaskRegistrationMonitorDTO.getHost());
        coreMonitoringTaskMonitorInfo.setVideoChannel(coreMonitoringTaskRegistrationMonitorDTO.getVideoChannel());
        coreMonitoringTaskMonitorInfo.setTaskRegistrationId(coreMonitoringTaskRegistrationMonitorDTO.getTaskRegistrationId());
        coreMonitoringTaskMonitorInfo.setCreateTime(DateUtils.getNowDate());
        coreMonitoringTaskMonitorInfo.setCreateBy(SecurityUtils.getUsername());
     //   coreMonitoringTaskRegistrationMonitorMapper.insertCoreMonitoringTaskRegistrationMonitor(coreMonitoringTaskMonitorInfo);
        coreMonitoringTaskRegistrationMonitorMapper.insert(coreMonitoringTaskMonitorInfo);
        //新增项目
        List<CoreMonitoringTaskMonitorInfo> coreMonitoringTaskMonitorInfoList = coreMonitoringTaskRegistrationMonitorDTO.getCoreMonitoringTaskMonitorInfoList();
        int i = 0;
        for (CoreMonitoringTaskMonitorInfo c : coreMonitoringTaskMonitorInfoList) {
            c.setCreateTime(DateUtils.getNowDate());
            c.setCreateBy(SecurityUtils.getUsername());
          //  i = coreMonitoringTaskMonitorInfoMapper.insertCoreMonitoringTaskMonitorInfo(c);
          i=  coreMonitoringTaskMonitorInfoMapper.insert(c);
            if (i < 1) {
                return i;
            }
        }
        return i;
    }

    /**
     * 修改调阅任务登记检查情况
     *
     * @param coreMonitoringTaskRegistrationMonitorDTO 调阅任务登记检查情况
     * @return 结果
     */
    @Override
    public int updateCoreMonitoringTaskMonitorInfo(CoreMonitoringTaskRegistrationMonitorDTO coreMonitoringTaskRegistrationMonitorDTO) {
        //修改监控
        CoreMonitoringTaskRegistrationMonitor coreMonitoringTaskRegistrationMonitor = coreMonitoringTaskRegistrationMonitorMapper.selectById(coreMonitoringTaskRegistrationMonitorDTO.getId());
        coreMonitoringTaskRegistrationMonitor.setHost(coreMonitoringTaskRegistrationMonitorDTO.getHost());
        coreMonitoringTaskRegistrationMonitor.setVideoChannel(coreMonitoringTaskRegistrationMonitorDTO.getVideoChannel());
        coreMonitoringTaskRegistrationMonitor.setUpdateTime(DateUtils.getNowDate());
        coreMonitoringTaskRegistrationMonitor.setUpdateBy(SecurityUtils.getUsername());
        coreMonitoringTaskRegistrationMonitorMapper.updateById(coreMonitoringTaskRegistrationMonitor);
        int i = 0;
        for (CoreMonitoringTaskMonitorInfo c : coreMonitoringTaskRegistrationMonitorDTO.getCoreMonitoringTaskMonitorInfoList()) {
            c.setUpdateTime(DateUtils.getNowDate());
            c.setUpdateBy(SecurityUtils.getUsername());
            i = coreMonitoringTaskMonitorInfoMapper.updateById(c);
            if (i < 1) {
                return i;
            }
        }
        return i;
    }

    /**
     * 批量删除调阅任务登记检查情况
     *
     * @param ids 需要删除的调阅任务登记检查情况主键
     * @return 结果
     */
    @Override
    public int deleteCoreMonitoringTaskMonitorInfoByIds(Long[] ids) {
        return coreMonitoringTaskMonitorInfoMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除调阅任务登记检查情况信息
     *
     * @param id 调阅任务登记检查情况主键
     * @return 结果
     */
    @Override
    public int deleteCoreMonitoringTaskMonitorInfoById(Long id) {
        return coreMonitoringTaskMonitorInfoMapper.deleteById(id);
    }
}
