package cn.ibizlab.core.extensions.mapper;

import cn.ibizlab.core.lite.extensions.domain.EntityModel;
import cn.ibizlab.core.lite.extensions.domain.EntityObj;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface DbEntityMapper extends BaseMapper<EntityObj>{

    List<EntityObj> search(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper);
    Integer searchCount(@Param("sql") String sql, @Param("ew") Wrapper<EntityObj> wrapper);

    @Insert({"<script> "+" MERGE INTO ${model.tableName} T1\n" +
            "  USING (\n" +
            "<foreach collection=\"list\" item=\"item\" index=\"index\" separator=\"UNION ALL\">\n" +
            "  SELECT ${model.mergeColumnSegment} FROM dual\n" +
            "</foreach>\n" +
            " ) T2 \n ${model.mergeSegment}"+
            "</script>"})
    boolean saveBatch(@Param("model") EntityModel model, @Param("list") List<EntityObj> list);

    @Insert({"<script> "+" MERGE INTO ${model.tableName} T1\n" +
            "  USING (\n" +
            "  SELECT ${model.mergeColumnSegment} FROM dual\n" +
            " ) T2 \n ${model.mergeSegment}"+
            "</script>"})
    boolean save(@Param("model") EntityModel model, @Param("entity") EntityObj entity);


    @Insert({"<script> "+
            "<foreach collection=\"list\" item=\"item\" index=\"index\" separator=\";\">\n" +
            " MERGE INTO ${model.tableName} T1\n" +
            "  USING (\n" +
            "  SELECT ${model.mergeColumnSegment} FROM dual\n" +
            " ) T2 \n ${model.mergeSegment}"+
            "</foreach>\n" +
            "</script>"})
    boolean saveBatchDm(@Param("model") EntityModel model, @Param("list") List<EntityObj> list);

    @Insert({"<script> "+" MERGE INTO ${model.tableName} T1\n" +
            "  USING (\n" +
            "  SELECT ${model.mergeColumnSegment} FROM dual\n" +
            " ) T2 \n ${model.mergeSegment}"+
            "</script>"})
    boolean saveDm(@Param("model") EntityModel model, @Param("entity") EntityObj entity);

}