package com.sckw.system.repository;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sckw.system.dao.SysDictTypeDao;
import com.sckw.system.model.SysDict;
import com.sckw.system.model.SysDictType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 字典类型服务层（MyBatis-Plus 版本）
 * @author system
 * @date 2024
 */
@Repository
public class SysDictTypeRepository extends ServiceImpl<SysDictTypeDao, SysDictType> {

    /**
     * 根据类型查询字典类型
     */
    public SysDictType selectByType(String type,String name) {
        return getOne(Wrappers.<SysDictType>lambdaQuery()
                .eq(SysDictType::getType, type)
                .eq(SysDictType::getName, name)
                .eq(SysDictType::getDelFlag, 0)
                .last("LIMIT 1"));
    }

    /**
     * 逻辑删除
     */
    public int logicDeleteById(Long id) {
        return this.baseMapper.deleteById(id);
    }

    /**
     * 更新字典类型
     */
    public int update(SysDictType entity) {
        return this.baseMapper.updateById(entity);
    }

    /**
     * 分页查询字典类型
     */
    public IPage<SysDictType> selectPage(int pageNum, int pageSize, String name, String type, Integer status,String  keyword) {
        return this.page(new Page<>(pageNum, pageSize), Wrappers.<SysDictType>lambdaQuery()
                .eq(SysDictType::getDelFlag, 0)
                .eq(Objects.nonNull( status), SysDictType::getStatus, status)
                .like(StringUtils.isNotBlank( name),SysDictType::getName, name)
                .like(StringUtils.isNotBlank(type), SysDictType::getType, type)
                .and(StringUtils.isNotBlank(keyword),w -> w.like(SysDictType::getName, keyword)
                        .or()
                        .like(SysDictType::getType, keyword))
                .orderByDesc(SysDictType::getCreateTime)
               );
    }

    /**
     * 搜索字典类型（支持下拉选择）
     */
    public List<SysDictType> searchForSelect(String keyword) {
        return list(Wrappers.<SysDictType>lambdaQuery()
                .select(SysDictType::getId, SysDictType::getName, SysDictType::getType)
                .eq(SysDictType::getDelFlag, 0)
                .eq(SysDictType::getStatus, 0)
                .and(StringUtils.isNotBlank(keyword),w -> w.like(SysDictType::getName, keyword)
                        .or()
                        .like(SysDictType::getType, keyword)));
    }

    public List<SysDictType> queryByTypes(Set<String> types) {
        return list(Wrappers.<SysDictType>lambdaQuery()
                .select(SysDictType::getId, SysDictType::getName, SysDictType::getType)
                .eq(SysDictType::getDelFlag, 0)
                .eq(SysDictType::getStatus, 0)
                .in(SysDictType::getType, types));
    }
}
