
package com.lideeyunji.core.framework.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.lideeyunji.core.framework.entity.FormFieldWebEntity;
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;
import java.util.stream.Collectors;

@DS(lideeYunJiBaseConstant.DS_lideeyunji)
public interface FormFieldWebMapper extends BaseMapper<FormFieldWebEntity> {

    //获取用户控件的fieldCode列表
    default List<String> getUserControlTypeFieldCodeList(Long dbFormId){
        LambdaQueryWrapper<FormFieldWebEntity> webWrapper=new LambdaQueryWrapper<>();
        webWrapper.eq(FormFieldWebEntity::getDbformId,dbFormId);
        webWrapper.eq(FormFieldWebEntity::getControlType,"userSelect");
        webWrapper.select(FormFieldWebEntity::getFieldCode);
        List<FormFieldWebEntity> webEntityList = this.selectList(webWrapper);
        //遍历返回list<String>
        List<String> fieldCodeList = webEntityList.stream()
                .map(FormFieldWebEntity::getFieldCode)
                .collect(Collectors.toList());
        return fieldCodeList;
    }

    //获取部门控件的fieldCode列表
    default List<String> getDeptControlTypeFieldCodeList(Long dbFormId){
        LambdaQueryWrapper<FormFieldWebEntity> webWrapper=new LambdaQueryWrapper<>();
        webWrapper.eq(FormFieldWebEntity::getDbformId,dbFormId);
        webWrapper.eq(FormFieldWebEntity::getControlType,"deptSelect");
        webWrapper.select(FormFieldWebEntity::getFieldCode);
        List<FormFieldWebEntity> webEntityList = this.selectList(webWrapper);
        //遍历返回list<String>
        List<String> fieldCodeList = webEntityList.stream()
                .map(FormFieldWebEntity::getFieldCode)
                .collect(Collectors.toList());
        return fieldCodeList;
    }
    //区域控件
    default List<String> getAreaControlTypeFieldCodeList(Long dbFormId){
        LambdaQueryWrapper<FormFieldWebEntity> webWrapper=new LambdaQueryWrapper<>();
        webWrapper.eq(FormFieldWebEntity::getDbformId,dbFormId);
        webWrapper.eq(FormFieldWebEntity::getControlType,"regionSelect");
        webWrapper.select(FormFieldWebEntity::getFieldCode);
        List<FormFieldWebEntity> webEntityList = this.selectList(webWrapper);
        //遍历返回list<String>
        List<String> fieldCodeList = webEntityList.stream()
                .map(FormFieldWebEntity::getFieldCode)
                .collect(Collectors.toList());
        return fieldCodeList;
    }

    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_DBFORM + ":web:webEntityListAndFormatConfigIsNotNull:' + #dbFormId", reflexClass = FormFieldWebEntity.class)
    default List<FormFieldWebEntity> webEntityListAndFormatConfigIsNotNull(Long dbFormId){
        LambdaQueryWrapper<FormFieldWebEntity> webWrapper=new LambdaQueryWrapper<>();
        webWrapper.eq(FormFieldWebEntity::getDbformId,dbFormId);
        webWrapper.isNotNull(FormFieldWebEntity::getFormatConfig);
        List<FormFieldWebEntity> webEntityList = this.selectList(webWrapper);
        return webEntityList;
    }

    default List<FormFieldWebEntity> getFormatConfig(Long dbFormId){
        LambdaQueryWrapper<FormFieldWebEntity> webWrapper=new LambdaQueryWrapper<>();
        webWrapper.eq(FormFieldWebEntity::getDbformId,dbFormId);
        webWrapper.isNotNull(FormFieldWebEntity::getFormatConfig);
        return this.selectList(webWrapper);
    }

    default FormFieldWebEntity getFormatConfig(Long dbFormId,String fieldCode){
        LambdaQueryWrapper<FormFieldWebEntity> webWrapper=new LambdaQueryWrapper<>();
        webWrapper.eq(FormFieldWebEntity::getDbformId,dbFormId);
        webWrapper.eq(FormFieldWebEntity::getFieldCode,fieldCode);
        webWrapper.isNotNull(FormFieldWebEntity::getFormatConfig);
        return this.selectOne(webWrapper);
    }

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


    @lideeYunjiCache(cacheNames = "'"+ lideeRedisConstants.REDIS_DBFORM +":web:getByFormId:' + #dbFormId", reflexClass = FormFieldWebEntity.class)
    default List<FormFieldWebEntity> getByFormId(Long dbFormId){
        LambdaQueryWrapper<FormFieldWebEntity> webWrapper = new LambdaQueryWrapper<>();
        webWrapper.eq(FormFieldWebEntity::getDbformId, dbFormId);
        webWrapper.orderByAsc(FormFieldWebEntity::getId);
        return this.selectList(webWrapper);
    }

}
