
package com.jeelowcode.core.framework.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.jeelowcode.core.framework.entity.FormFieldForeignkeyEntity;
import com.jeelowcode.framework.global.JeeLowCodeBaseConstant;
import com.jeelowcode.framework.utils.annotation.JeelowCodeCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.List;

@DS(JeeLowCodeBaseConstant.DS_JEELOWCODE)
public interface FormFieldForeignkeyMapper extends BaseMapper<FormFieldForeignkeyEntity> {

    default void delByDbformId(Long dbFormId){
        QueryWrapper<FormFieldForeignkeyEntity> foreignkeyWrapper = new QueryWrapper();
        foreignkeyWrapper.eq("dbform_id", dbFormId);
        this.delete(foreignkeyWrapper);
    }

    @JeelowCodeCache(cacheNames = "'FormFieldForeignkeyMapper:getByFormId:' + #dbFormId", reflexClass = FormFieldForeignkeyEntity.class)
    default List<FormFieldForeignkeyEntity> getByFormId(Long dbFormId){
        LambdaQueryWrapper<FormFieldForeignkeyEntity> foreignkeyWrapper = new LambdaQueryWrapper<>();
        foreignkeyWrapper.eq(FormFieldForeignkeyEntity::getDbformId, dbFormId);
        foreignkeyWrapper.orderByAsc(FormFieldForeignkeyEntity::getId);
        return this.selectList(foreignkeyWrapper);
    }

    default FormFieldForeignkeyEntity getByFormIdAndMainTable(Long dbFormId,String mainTable){
        LambdaQueryWrapper<FormFieldForeignkeyEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FormFieldForeignkeyEntity::getDbformId, dbFormId);
        wrapper.eq(FormFieldForeignkeyEntity::getMainTable, mainTable);
        wrapper.isNotNull(FormFieldForeignkeyEntity::getMainTable);
        return this.selectOne(wrapper);

    }
}
