package com.docus.server.infrastructure.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.docus.core.util.Func;
import com.docus.infrastructure.core.db.dao.impl.BaseDaoImpl;
import com.docus.infrastructure.web.request.SearchDTO;
import com.docus.infrastructure.web.response.PageResult;
import com.docus.server.entity.scheduling.management.SchCollectorVersionFile;
import com.docus.server.enums.CancelEnum;
import com.docus.server.infrastructure.dao.ISchCollectorVersionFileDao;
import com.docus.server.infrastructure.mapper.SchCollectorVersionFileMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 采集器版本列表更新包管理 数据访问实现类
 *
 * @author AutoGenerator
 * @since 2023-07-15
 */
@Repository
public class SchCollectorVersionFileDaoImpl extends BaseDaoImpl<SchCollectorVersionFileMapper, SchCollectorVersionFile> implements ISchCollectorVersionFileDao {

    /**
     * 按主键查询
     *
     * @param id 主键Id
     * @return 实体
     */
    @Override
    public SchCollectorVersionFile findById(String id) {
        return super.findById(id);
    }

    /**
     * 新增
     *
     * @param schCollectorVersionFile 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean add(SchCollectorVersionFile schCollectorVersionFile) {
        return super.save(schCollectorVersionFile);
    }

    /**
     * 编辑
     *
     * @param schCollectorVersionFile 编辑参数
     * @return 成功或失败
     */
    @Override
    public boolean edit(SchCollectorVersionFile schCollectorVersionFile) {
        return super.updateById(schCollectorVersionFile);
    }

    /**
     * 批量删除
     *
     * @param ids 主键ids
     * @return 成功或失败
     */
    @Override
    public int delete(List<Long> ids) {
        return super.deleteByIds(ids);
    }

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<SchCollectorVersionFile> search(SearchDTO searchDTO) {
        //分页
        PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize());
        LambdaQueryWrapper<SchCollectorVersionFile> query = Wrappers.lambdaQuery();
        //时间范围，默认按createTime
        if (searchDTO.getBeginTime() != null) {
            query.ge(SchCollectorVersionFile::getCreateTime, searchDTO.getBeginTime());
        }
        if (searchDTO.getEndTime() != null) {
            Date endTime = searchDTO.getEndTime();
            query.le(SchCollectorVersionFile::getCreateTime, endTime);
        }

        //默认createTime倒序排序
        query.orderByDesc(SchCollectorVersionFile::getCreateTime);
        List<SchCollectorVersionFile> list = super.find(query);
        return new PageResult<>(list);
    }

    @Override
    public SchCollectorVersionFile findByVersionIdAndCollectorId(Long collectorId, Long collectorVersionId) {
        LambdaQueryWrapper<SchCollectorVersionFile> query = Wrappers.lambdaQuery();
        if (Func.isNotEmpty(collectorId)) {
            query.eq(SchCollectorVersionFile::getCollectorId, collectorId);
        }
        if (Func.isNotEmpty(collectorVersionId)) {
            query.eq(SchCollectorVersionFile::getCollectorVersionId, collectorVersionId);
        }
        query.ne(SchCollectorVersionFile::getState, CancelEnum.CANCEL);

        return super.findOne(query);
    }

}
