package com.zt.plat.module.qms.common.dic.dal.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.module.qms.common.dic.controller.vo.*;
import com.zt.plat.module.qms.common.dic.controller.vo.DictionaryBusinessPageReqVO;
import com.zt.plat.module.qms.common.dic.dal.dataobject.DictionaryBusinessDO;
import com.zt.plat.module.qms.core.legend.vo.DictModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 业务参数字典 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface DictionaryBusinessMapper extends BaseMapperX<DictionaryBusinessDO> {

    @DeptDataPermissionIgnore(enable = "true")
    default List<DictionaryBusinessDO> selectListWithoutPermissionCheck(Wrapper<DictionaryBusinessDO> queryWrapper){
        return selectList(queryWrapper);
    }

    @DeptDataPermissionIgnore(enable = "true")
    default PageResult<DictionaryBusinessDO> selectPage(DictionaryBusinessPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<DictionaryBusinessDO>()
                .eqIfPresent(DictionaryBusinessDO::getIdPath, reqVO.getIdPath())
                .eqIfPresent(DictionaryBusinessDO::getParentId, reqVO.getParentId())
                .eqIfPresent(DictionaryBusinessDO::getNodeType, reqVO.getNodeType())
                .likeIfPresent(DictionaryBusinessDO::getName, reqVO.getName())
                .eqIfPresent(DictionaryBusinessDO::getKey, reqVO.getKey())
                .eqIfPresent(DictionaryBusinessDO::getValue, reqVO.getValue())
                .eqIfPresent(DictionaryBusinessDO::getCustomConfig, reqVO.getCustomConfig())
                .eqIfPresent(DictionaryBusinessDO::getSortNo, reqVO.getSortNo())
                .eqIfPresent(DictionaryBusinessDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(DictionaryBusinessDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(DictionaryBusinessDO::getRemark, reqVO.getRemark())
                .orderByAsc(DictionaryBusinessDO::getSortNo));
    }

    @DeptDataPermissionIgnore(enable = "true")
    default List<DictionaryBusinessExtendRespVO> selectItemByCategoryKeys(List<String> keys) {
    	return selectJoinList(DictionaryBusinessExtendRespVO.class, new MPJLambdaWrapperX<DictionaryBusinessDO>()
    			.leftJoin(DictionaryBusinessDO.class, "dbd", DictionaryBusinessDO::getId, DictionaryBusinessDO::getParentId)
    			.selectAll(DictionaryBusinessDO.class)
    			.selectAs("dbd", DictionaryBusinessDO::getKey, DictionaryBusinessExtendRespVO::getParentDictionaryBusinesskey)
    			.selectAs("dbd", DictionaryBusinessDO::getName, DictionaryBusinessExtendRespVO::getParentDictionaryBusinessName)
    			.in("dbd", DictionaryBusinessDO::getKey, keys)
    			.orderByAsc(DictionaryBusinessDO::getSortNo));
    } 


    /**
     * 查询自定义字典表
     * @param table 表名
     * @param text   显示字段名
     * @param code   存储字段名
     * @param filterSql 条件sql
     * @param keys 存储字段值 作为查询条件in
     */
    @DeptDataPermissionIgnore(enable = "true")
    List<DictModel> queryTableDictCustom(@Param("table") String table, @Param("text") String text, @Param("code") String code, @Param("filterSql") String filterSql,
                                         @Param("keys") List<String> keys);

    /**
     * 通过字典code获取字典数据
     * @param code
     * @param key
     */
    @DeptDataPermissionIgnore(enable = "true")
    String queryDictTextByKey(@Param("code") String code, @Param("key") String key);

    @DeptDataPermissionIgnore(enable = "true")
    String queryDictTextByID(@Param("code") String code, @Param("id") String id);

}