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.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.recovery.RcvSysParam;
import com.docus.server.infrastructure.dao.IRcvSysParamDao;
import com.docus.server.infrastructure.mapper.RcvSysParamMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 系统参数表 数据访问实现类
 *
 * @author AutoGenerator
 * @since 2023-09-13
 */
@Repository
public class RcvSysParamDaoImpl extends BaseDaoImpl<RcvSysParamMapper, RcvSysParam> implements IRcvSysParamDao {


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

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

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

    /**
     * 关键字搜索
     *
     * @param searchDTO 搜索参数
     * @return 分页列表
     */
    @Override
    public PageResult<RcvSysParam> search(SearchDTO searchDTO) {
        //分页
        PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize());
        LambdaQueryWrapper<RcvSysParam> query = Wrappers.lambdaQuery();
        //时间范围，默认按createTime
        if (searchDTO.getBeginTime() != null) {
            query.ge(RcvSysParam::getCreateTime, searchDTO.getBeginTime());
        }
        if (searchDTO.getEndTime() != null) {
            Date endTime = searchDTO.getEndTime();
            query.le(RcvSysParam::getCreateTime, endTime);
        }
        //关键字比较，多个列or
        //if (!StringUtils.isEmpty(searchDTO.getKeyword())) {
        //单个列用like
        //query. like(RcvSysParam::getxxx, searchDTO.getKeyword());
        //多个列用like
        //query. and(sub -> sub.like(RcvSysParam::getxx1, searchDTO.getKeyword())
        // оr(). like(RcvSysParam::getXX2, searchDTO.getKeyword()))
        //);
        //}
        //默认createTime倒序排序
        query.orderByDesc(RcvSysParam::getCreateTime);
        List<RcvSysParam> list = super.find(query);
        return new PageResult<>(list);
    }

    /**
     * 名称不重复
     *
     * @param id   主键
     * @param name 名称
     * @return 名称重复数量
     */
    @Override
    public int findByIdAndName(Serializable id, String name) {
        LambdaQueryWrapper<RcvSysParam> query = Wrappers.lambdaQuery();
        if (id != null) {
            query.ne(RcvSysParam::getId, id);
        }
        //if (Func.isNotBlank(name)) {
        //   query.eq(RcvSysParam::getName, name);
        //}

        return baseMapper.selectCount(query);
    }
}
