
package com.lideeyunji.core.framework.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.lideeyunji.core.framework.entity.FormEntity;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.common.constant.lideeYunJiBaseConstant;
import com.lideeyunji.tool.framework.yunji.annotation.lideeYunjiCache;
import com.lideeyunji.tool.framework.yunji.constant.lideeRedisConstants;
import com.lideeyunji.tool.framework.yunji.enums.TableTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;

import java.util.List;
import java.util.stream.Collectors;

@DS(lideeYunJiBaseConstant.DS_lideeyunji)
public interface FormMapper extends BaseMapper<FormEntity> {

    default FormMapper getMapper(){
        return SpringUtils.getBean(FormMapper.class);
    }


    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_DBFORM + ":dbform:getById:' + #dbFormId", reflexClass = FormEntity.class)
    default FormEntity getById(Long dbFormId) {
        LambdaQueryWrapper<FormEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormEntity::getId, dbFormId);
        return getMapper().selectOne(wrapper);
    }

    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_DBFORM + ":getAllSubTableNameList:' + #dbFormId", reflexClass = String.class,nullIsSave = true)
    default List<String> getAllSubTableNameList(Long dbFormId) {
        LambdaQueryWrapper<FormEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormEntity::getId, dbFormId);
        wrapper.eq(FormEntity::getTableType, TableTypeEnum.MAIN.getType());//主表
        wrapper.select(FormEntity::getSubTableListStr);
        FormEntity formEntity = getMapper().selectOne(wrapper);
        if (Func.isEmpty(formEntity)) {
            return null;
        }
        String subListStr = formEntity.getSubTableListStr();
        if (Func.isEmpty(subListStr)) {
            return null;
        }
        return Func.toStrList(subListStr);
    }

    default Long getFormIdByTableName(String tableName){
        LambdaQueryWrapper<FormEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormEntity::getTableName, tableName);
        wrapper.select(FormEntity::getId);
        FormEntity formEntity = getMapper().selectOne(wrapper);
        if(Func.isEmpty(formEntity)){
            return null;
        }
        return formEntity.getId();
    }
    default FormEntity getByTableName(String tableName){
        LambdaQueryWrapper<FormEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormEntity::getTableName, tableName);
        wrapper.select(FormEntity::getId);
        FormEntity formEntity = getMapper().selectOne(wrapper);
        return formEntity;
    }

    default List<Long> getDbFormIdByTableName(List<String> subTableNameList){
        LambdaQueryWrapper<FormEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FormEntity::getTableName, subTableNameList);
        wrapper.select(FormEntity::getId);
        wrapper.orderByAsc(FormEntity::getSubTableSort, FormEntity::getId);
        List<FormEntity> list = getMapper().selectList(wrapper);

        List<Long> idList = list.stream()
                .map(FormEntity::getId)  // 使用方法引用来简化代码
                .collect(Collectors.toList());// 收集结果到列表

        return idList;
    }


}
