
package com.lideeyunji.core.framework.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.lideeyunji.core.framework.entity.FormFieldForeignkeyEntity;
import com.lideeyunji.tool.framework.common.constant.lideeYunJiBaseConstant;
import com.lideeyunji.tool.framework.yunji.annotation.lideeYunjiCache;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lideeyunji.tool.framework.yunji.constant.lideeRedisConstants;

import java.util.List;

@DS(lideeYunJiBaseConstant.DS_lideeyunji)
public interface FormFieldForeignkeyMapper extends BaseMapper<FormFieldForeignkeyEntity> {

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

    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_DBFORM +":foreignkey: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);

    }
}
