package com.tashow.cloud.ai.dal.mysql.aisample;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tashow.cloud.ai.controller.admin.aisample.vo.AiSamplePageReqVO;
import com.tashow.cloud.ai.dal.dataobject.aisample.AiSampleDO;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.mybatis.mybatis.core.mapper.BaseMapperX;
import com.tashow.cloud.mybatis.mybatis.core.query.LambdaQueryWrapperX;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * 样本库 Mapper
 */
@Mapper
public interface AiSampleMapper extends BaseMapperX<AiSampleDO> {

    default PageResult<AiSampleDO> selectPage(AiSamplePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<AiSampleDO>()
                .likeIfPresent(AiSampleDO::getSampleName, reqVO.getSampleName())
                .orderByDesc(AiSampleDO::getId));
    }


    @Select("<script>" +
            "SELECT t.id, t.sample_file_path sampleFilePath,t.sample_name sampleName,t.sample_time sampleTime" +
            ",sample_mine_type sampleMineType ,sample_size sampleSize, remark,creator,updater,create_time createTime" +
            ",update_time updateTime,deleted,tenant_id tenantId " +
            "FROM tz_ai_sample t " +
            "LEFt JOIN tz_ai_sample_tag_relate r ON t.id = r.sample_id " +
            "<where>" +
             "AND t.deleted =0"+
            "    <if test=\"pageReqVO.tagIds != null and pageReqVO.tagIds != ''\">" +
            "        AND FIND_IN_SET(r.sample_tag_id, '${pageReqVO.tagIds}') " +
            "    </if>" +
            "    <if test=\"pageReqVO.sampleName != null and pageReqVO.sampleName != ''\">" +
            "        AND t.sample_name LIKE CONCAT('%',#{pageReqVO.sampleName},'%')" +
            "    </if>" +
            "    <if test=\"pageReqVO.sampleMineType != null and pageReqVO.sampleMineType != ''\">" +
            "        AND t.sample_mine_type LIKE CONCAT('%',#{pageReqVO.sampleMineType},'%')" +
            "    </if>" +
            "</where>" +
            "GROUP BY t.id ORDER BY t.id DESC" +
            "</script>")
    IPage<AiSampleDO> getAiSamplePage(Page<AiSampleDO> objectPage, AiSamplePageReqVO pageReqVO);
}