
package com.jeelowcode.core.framework.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.jeelowcode.core.framework.entity.FormFieldDictEntity;
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 FormFieldDictMapper extends BaseMapper<FormFieldDictEntity> {

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

    default void delByDbformIdAndCode(Long dbFormId,String fieldCode){
        QueryWrapper<FormFieldDictEntity> dictWrapper = new QueryWrapper();
        dictWrapper.eq("dbform_id", dbFormId);
        dictWrapper.eq("field_code", fieldCode);
        this.delete(dictWrapper);
    }



    @JeelowCodeCache(cacheNames = "'FormFieldDictMapper:getByDbFormId:' + #dbformId", reflexClass = FormFieldDictEntity.class,nullIsSave = true)
    default List<FormFieldDictEntity> getByDbFormId(Long dbformId){
        LambdaQueryWrapper<FormFieldDictEntity> dictWrapper = new LambdaQueryWrapper<>();
        dictWrapper.eq(FormFieldDictEntity::getDbformId, dbformId);
        dictWrapper.orderByAsc(FormFieldDictEntity::getId);
        return this.selectList(dictWrapper);
    }

    default FormFieldDictEntity getFormFieldSysDictEntity(Long dbformId,String fieldCode){

        //获取字典表信息
        LambdaQueryWrapper<FormFieldDictEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(FormFieldDictEntity::getDictType, FormFieldDictEntity::getDictCode, FormFieldDictEntity::getDictTable, FormFieldDictEntity::getDictText, FormFieldDictEntity::getDictTableColumn,FormFieldDictEntity::getDictTextFormatter);
        wrapper.eq(FormFieldDictEntity::getDbformId, dbformId);
        wrapper.eq(FormFieldDictEntity::getFieldCode, fieldCode);
        wrapper.isNotNull(FormFieldDictEntity::getDictCode);

        FormFieldDictEntity dictEntity = this.selectOne(wrapper);
        return dictEntity;
    }

}
