
package com.jeelowcode.core.framework.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.jeelowcode.core.framework.entity.FormEntity;
import com.jeelowcode.core.framework.utils.Func;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.utils.annotation.JeelowCodeCache;
import com.jeelowcode.framework.utils.enums.TableTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jeelowcode.framework.utils.tool.spring.SpringUtils;

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

@DS(JeeLowCodeBaseConstant.DS_JEELOWCODE)
public interface FormMapper extends BaseMapper<FormEntity> {

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


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


    @JeelowCodeCache(cacheNames = "'FormMapper:getByWebId:' + #webId", reflexClass = FormEntity.class)
    default FormEntity getByWebId(Long webId) {
        LambdaQueryWrapper<FormEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormEntity::getDesformWebId, webId);
        return getMapper().selectOne(wrapper);
    }

    @JeelowCodeCache(cacheNames = "'FormMapper: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;
    }


}
