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

import java.util.Arrays;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.core.utils.bean.BeanUtils;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.core.access.domain.CoreMonitoringTaskRegistration;
import com.xunmei.core.access.mapper.CoreMonitoringTaskRegistrationMapper;
import com.xunmei.core.access.service.ICoreMonitoringTaskRegistrationService;
import com.xunmei.core.access.vo.CoreMonitoringTaskRegistrationVO;
import com.xunmei.common.core.domain.retrieval.domain.CoreMonitoringRetrievalTask;
import com.xunmei.core.retrieval.mapper.CoreMonitoringRetrievalTaskMapper;
import com.xunmei.core.retrieval.service.ICoreMonitoringRetrievalTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-28
 */
@Service
public class CoreMonitoringTaskRegistrationServiceImpl extends ServiceImpl<CoreMonitoringTaskRegistrationMapper, CoreMonitoringTaskRegistration> implements ICoreMonitoringTaskRegistrationService {
  private static  final Long ONE=1L;
  private static final Long TWO=2L;
    @Autowired
    private CoreMonitoringTaskRegistrationMapper coreMonitoringTaskRegistrationMapper;
@Autowired
private ICoreMonitoringRetrievalTaskService iCoreMonitoringRetrievalTaskService;
@Autowired
private CoreMonitoringRetrievalTaskMapper coreMonitoringRetrievalTaskMapper;
  /*  @Override
    public TableDataInfo<CoreMonitoringTaskRegistration> selectPage(CoreMonitoringTaskRegistration coreMonitoringTaskRegistration) {
        //未删除
        coreMonitoringTaskRegistration.setDeleted(0L);
        Page<CoreMonitoringTaskRegistration> page;
        //分页
        if (coreMonitoringTaskRegistration.getPageNum()!=null&&coreMonitoringTaskRegistration.getPageSize()!=null)
        {
            page = new Page<>(coreMonitoringTaskRegistration.getPageNum(), coreMonitoringTaskRegistration.getPageSize());
        }else{
            page = new Page<>();
        }
        //查询条件
        QueryWrapper<CoreMonitoringTaskRegistration> query = new QueryWrapper<>(coreMonitoringTaskRegistration);
        //下穿
        if (coreMonitoringTaskRegistration.getCheckSub()){
            List<Long> ids = orgService.selectCheckSubOrgIdList(coreMonitoringTaskRegistration.getOrgId());
            //清空前端传递的org_id
            coreMonitoringTaskRegistration.setOrgId(null);
            //添加in条件
            query.in("org_id",ids);
        }
        //时间范围查询
        if (coreMonitoringTaskRegistration.getParams().get("beginTime")!=null&&coreMonitoringTaskRegistration.getParams().get("endTime")!=null){
            query.between("create_time", coreMonitoringTaskRegistration.getParams().get("beginTime"), coreMonitoringTaskRegistration.getParams().get("endTime"));
        }
        //获取数据
        page = coreMonitoringTaskRegistrationMapper.selectPage(page, query);
        //抓换为TableDataInfo适配前端
        return TableDataInfo.build(page);
    }*/


    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public CoreMonitoringTaskRegistrationVO selectCoreMonitoringTaskRegistrationById(Long id) {
        CoreMonitoringTaskRegistrationVO coreMonitoringTaskRegistrationVO=new CoreMonitoringTaskRegistrationVO();
        CoreMonitoringTaskRegistration coreMonitoringTaskRegistration = coreMonitoringTaskRegistrationMapper.selectById(id);
        BeanUtils.copyProperties(coreMonitoringTaskRegistration,coreMonitoringTaskRegistrationVO);
        CoreMonitoringRetrievalTask coreMonitoringRetrievalTask = iCoreMonitoringRetrievalTaskService.selectCoreMonitoringRetrievalTaskById(coreMonitoringTaskRegistration.getTaskId());
        coreMonitoringTaskRegistrationVO.setTaskName(coreMonitoringRetrievalTask.getTaskName());
        return coreMonitoringTaskRegistrationVO;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param coreMonitoringTaskRegistration 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<CoreMonitoringTaskRegistration> selectCoreMonitoringTaskRegistrationList(CoreMonitoringTaskRegistration coreMonitoringTaskRegistration) {
        return coreMonitoringTaskRegistrationMapper.selectList(new QueryWrapper<>(coreMonitoringTaskRegistration));
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param coreMonitoringTaskRegistration 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertCoreMonitoringTaskRegistration(CoreMonitoringTaskRegistration coreMonitoringTaskRegistration) {
        coreMonitoringTaskRegistration.setCreateTime(DateUtils.getNowDate());
        coreMonitoringTaskRegistration.setTaskStartTime(DateUtils.getNowDate());
        coreMonitoringTaskRegistration.setDelFlag("0");
        coreMonitoringTaskRegistration.setCreateBy(SecurityUtils.getUsername());
        //修改任务状态为进行中
        CoreMonitoringRetrievalTask coreMonitoringRetrievalTask = coreMonitoringRetrievalTaskMapper.selectCoreMonitoringRetrievalTaskById(coreMonitoringTaskRegistration.getTaskId());
        coreMonitoringRetrievalTask.setStatus(ONE);
        coreMonitoringRetrievalTaskMapper.updateById(coreMonitoringRetrievalTask);
        return coreMonitoringTaskRegistrationMapper.insert(coreMonitoringTaskRegistration);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param coreMonitoringTaskRegistration 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCoreMonitoringTaskRegistration(CoreMonitoringTaskRegistration coreMonitoringTaskRegistration) {
        coreMonitoringTaskRegistration.setUpdateTime(DateUtils.getNowDate());
        coreMonitoringTaskRegistration.setUpdateBy(SecurityUtils.getUsername());
        //修改任务状态已完成
        CoreMonitoringRetrievalTask coreMonitoringRetrievalTask = coreMonitoringRetrievalTaskMapper.selectById(coreMonitoringTaskRegistration.getTaskId());
        coreMonitoringRetrievalTask.setStatus(TWO);
        coreMonitoringRetrievalTask.setUpdateBy(SecurityUtils.getUsername());
        coreMonitoringRetrievalTask.setUpdateTime(DateUtils.getNowDate());
        coreMonitoringRetrievalTaskMapper.updateById(coreMonitoringRetrievalTask);
        return coreMonitoringTaskRegistrationMapper.updateById(coreMonitoringTaskRegistration);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCoreMonitoringTaskRegistrationByIds(Long[] ids) {
        return coreMonitoringTaskRegistrationMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCoreMonitoringTaskRegistrationById(Long id) {
        return coreMonitoringTaskRegistrationMapper.deleteById(id);
    }
}
