package com.template.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.template.model.pojo.SmartDataSource;
import com.template.model.pojo.SmartDataSourceJobParams;
import com.template.model.pojo.SmartDataTask;
import com.template.model.pojo.SmartDepartment;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 数据源配置 Mapper 接口
 * </p>
 *
 * @author ceshi
 * @since 2023-12-05
 */
@Repository
public interface SmartDataSourceMapper extends BaseMapper<SmartDataSource> {

    @Select("SELECT " +
            "    ds_id " +
            "    ,c.ds_cls_id ds_cls_id " +
            "    ,ds_cls_name " +
            "    ,ds_cls_driver " +
            "    ,ds_name " +
            "    ,ds_url " +
            "    ,ds_user " +
            "    ,ds_password " +
            "    ,ds_status " +
            "    ,ds_descrition " +
            "    ,ds_source " +
            "    ,ds_target " +
            "FROM " +
            "    smart_data_class c, " +
            "    smart_data_source s  " +
            "WHERE " +
            "    s.ds_id = #{id} " +
            "    AND c.ds_cls_id = s.ds_cls_id ")
    SmartDataSourceJobParams getDataSourceInfo(Integer id);

    @Select("SELECT " +
            "    *  " +
            "FROM " +
            "    smart_department ")
    List<SmartDepartment> getDepart();

    @Select({
            "<script>",
            "   SELECT ",
            "      ds_id, s.ds_cls_id, ds_cls_name, ds_name, ds_url, ds_user, ds_password, ds_status, ds_descrition, ds_source ",
            "      , ds_target, ds_create_time, ds_update_time ",
            "   FROM smart_data_source s ",
            "      LEFT JOIN smart_data_class c ON s.ds_cls_id = c.ds_cls_id ",
            "   <where>",
            "      <if test='dsClsId != null'>AND s.ds_cls_id = #{dsClsId}</if>",
            "      <if test='dsStatus != null'>AND ds_status = #{dsStatus}</if>",
            "      <if test='dsName != null'>AND ds_name like CONCAT('%', #{dsName}, '%')</if>",
            "      <if test='dsUrl != null'>AND ds_url like CONCAT('%', #{dsUrl}, '%')</if>",
            "      <if test='dsDescrition != null'>AND ds_descrition like CONCAT('%', #{dsDescrition}, '%')</if>",
            "   </where>",
            "   ORDER BY ds_update_time DESC",
            "</script>"
    })
    List<SmartDataSource> selectPageDsSource(SmartDataSource smartDataSource);

    @Select(" SELECT " +
            "    COUNT(*) " +
            " FROM " +
            "    smart_data_task " +
            " WHERE " +
            "    tk_ds_id_source = #{id} " +
            " Or " +
            "    tk_ds_id_destination = #{id} ")
    int checkTaskExist(int id);
}
