<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.datai.integration.mapper.DataiIntegrationObjectMapper">
    
    <resultMap type="DataiIntegrationObject" id="DataiIntegrationObjectResult">
        <result property="id" column="id" />
        <result property="deptId" column="dept_id" />
        <result property="api" column="api" />
        <result property="label" column="label" />
        <result property="labelPlural" column="label_plural" />
        <result property="keyPrefix" column="key_prefix" />
        <result property="namespace" column="namespace" />
        <result property="isQueryable" column="is_queryable" />
        <result property="isCreateable" column="is_createable" />
        <result property="isUpdateable" column="is_updateable" />
        <result property="isDeletable" column="is_deletable" />
        <result property="isReplicateable" column="is_replicateable" />
        <result property="isRetrieveable" column="is_retrieveable" />
        <result property="isSearchable" column="is_searchable" />
        <result property="isCustom" column="is_custom" />
        <result property="isCustomSetting" column="is_custom_setting" />
        <result property="isWork" column="is_work" />
        <result property="isIncremental" column="is_incremental" />
        <result property="objectIndex" column="object_index" />
        <result property="batchField" column="batch_field" />
        <result property="isPartitioned" column="is_partitioned" />
        <result property="totalRows" column="total_rows" />
        <result property="lastSyncDate" column="last_sync_date" />
        <result property="lastBatchDate" column="last_batch_date" />
        <result property="syncStatus" column="sync_status" />
        <result property="errorMessage" column="error_message" />
        <result property="remark" column="remark" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectDataiIntegrationObjectVo">
        select 
            dio.id,
            dio.dept_id,
            dio.api,
            dio.label,
            dio.label_plural,
            dio.key_prefix,
            dio.namespace,
            dio.is_queryable,
            dio.is_createable,
            dio.is_updateable,
            dio.is_deletable,
            dio.is_replicateable,
            dio.is_retrieveable,
            dio.is_searchable,
            dio.is_custom,
            dio.is_custom_setting,
            dio.is_work,
            dio.is_incremental,
            dio.object_index,
            dio.batch_field,
            dio.is_partitioned,
            dio.total_rows,
            dio.last_sync_date,
            dio.last_batch_date,
            dio.sync_status,
            dio.error_message,
            dio.remark,
            dio.create_by,
            dio.create_time,
            dio.update_by,
            dio.update_time
        from datai_integration_object dio
    </sql>

    <select id="selectDataiIntegrationObjectList" parameterType="DataiIntegrationObject" resultMap="DataiIntegrationObjectResult">
        <include refid="selectDataiIntegrationObjectVo"/>
        <where>
            <if test="deptId != null "> and dio.dept_id = #{deptId}</if>
            <if test="api != null  and api != ''"> and dio.api = #{api}</if>
            <if test="label != null  and label != ''"> and dio.label = #{label}</if>
            <if test="labelPlural != null  and labelPlural != ''"> and dio.label_plural = #{labelPlural}</if>
            <if test="keyPrefix != null  and keyPrefix != ''"> and dio.key_prefix = #{keyPrefix}</if>
            <if test="namespace != null  and namespace != ''"> and dio.namespace = #{namespace}</if>
            <if test="isQueryable != null "> and dio.is_queryable = #{isQueryable}</if>
            <if test="isCreateable != null "> and dio.is_createable = #{isCreateable}</if>
            <if test="isUpdateable != null "> and dio.is_updateable = #{isUpdateable}</if>
            <if test="isDeletable != null "> and dio.is_deletable = #{isDeletable}</if>
            <if test="isReplicateable != null "> and dio.is_replicateable = #{isReplicateable}</if>
            <if test="isRetrieveable != null "> and dio.is_retrieveable = #{isRetrieveable}</if>
            <if test="isSearchable != null "> and dio.is_searchable = #{isSearchable}</if>
            <if test="isCustom != null "> and dio.is_custom = #{isCustom}</if>
            <if test="isCustomSetting != null "> and dio.is_custom_setting = #{isCustomSetting}</if>
            <if test="isWork != null "> and dio.is_work = #{isWork}</if>
            <if test="isIncremental != null "> and dio.is_incremental = #{isIncremental}</if>
            <if test="objectIndex != null "> and dio.object_index = #{objectIndex}</if>
            <if test="batchField != null  and batchField != ''"> and dio.batch_field = #{batchField}</if>
            <if test="isPartitioned != null "> and dio.is_partitioned = #{isPartitioned}</if>
            <if test="totalRows != null "> and dio.total_rows = #{totalRows}</if>
            <if test="lastSyncDate != null "> and dio.last_sync_date = #{lastSyncDate}</if>
            <if test="lastBatchDate != null "> and dio.last_batch_date = #{lastBatchDate}</if>
            <if test="syncStatus != null "> and dio.sync_status = #{syncStatus}</if>
            <if test="errorMessage != null  and errorMessage != ''"> and dio.error_message = #{errorMessage}</if>
        </where>
    </select>
    
    <select id="selectDataiIntegrationObjectById" parameterType="Integer" resultMap="DataiIntegrationObjectResult">
        <include refid="selectDataiIntegrationObjectVo"/>
        where dio.id = #{id}
    </select>

    <select id="selectDataiIntegrationObjectByApi" parameterType="String" resultMap="DataiIntegrationObjectResult">
        <include refid="selectDataiIntegrationObjectVo"/>
        where dio.api = #{api}
    </select>
        
    <insert id="insertDataiIntegrationObject" parameterType="DataiIntegrationObject" useGeneratedKeys="true" keyProperty="id">
        insert into datai_integration_object
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="api != null and api != ''">api,</if>
            <if test="label != null">label,</if>
            <if test="labelPlural != null">label_plural,</if>
            <if test="keyPrefix != null">key_prefix,</if>
            <if test="namespace != null">namespace,</if>
            <if test="isQueryable != null">is_queryable,</if>
            <if test="isCreateable != null">is_createable,</if>
            <if test="isUpdateable != null">is_updateable,</if>
            <if test="isDeletable != null">is_deletable,</if>
            <if test="isReplicateable != null">is_replicateable,</if>
            <if test="isRetrieveable != null">is_retrieveable,</if>
            <if test="isSearchable != null">is_searchable,</if>
            <if test="isCustom != null">is_custom,</if>
            <if test="isCustomSetting != null">is_custom_setting,</if>
            <if test="isWork != null">is_work,</if>
            <if test="isIncremental != null">is_incremental,</if>
            <if test="objectIndex != null">object_index,</if>
            <if test="batchField != null">batch_field,</if>
            <if test="isPartitioned != null">is_partitioned,</if>
            <if test="totalRows != null">total_rows,</if>
            <if test="lastSyncDate != null">last_sync_date,</if>
            <if test="lastBatchDate != null">last_batch_date,</if>
            <if test="syncStatus != null">sync_status,</if>
            <if test="errorMessage != null">error_message,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="api != null and api != ''">#{api},</if>
            <if test="label != null">#{label},</if>
            <if test="labelPlural != null">#{labelPlural},</if>
            <if test="keyPrefix != null">#{keyPrefix},</if>
            <if test="namespace != null">#{namespace},</if>
            <if test="isQueryable != null">#{isQueryable},</if>
            <if test="isCreateable != null">#{isCreateable},</if>
            <if test="isUpdateable != null">#{isUpdateable},</if>
            <if test="isDeletable != null">#{isDeletable},</if>
            <if test="isReplicateable != null">#{isReplicateable},</if>
            <if test="isRetrieveable != null">#{isRetrieveable},</if>
            <if test="isSearchable != null">#{isSearchable},</if>
            <if test="isCustom != null">#{isCustom},</if>
            <if test="isCustomSetting != null">#{isCustomSetting},</if>
            <if test="isWork != null">#{isWork},</if>
            <if test="isIncremental != null">#{isIncremental},</if>
            <if test="objectIndex != null">#{objectIndex},</if>
            <if test="batchField != null">#{batchField},</if>
            <if test="isPartitioned != null">#{isPartitioned},</if>
            <if test="totalRows != null">#{totalRows},</if>
            <if test="lastSyncDate != null">#{lastSyncDate},</if>
            <if test="lastBatchDate != null">#{lastBatchDate},</if>
            <if test="syncStatus != null">#{syncStatus},</if>
            <if test="errorMessage != null">#{errorMessage},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateDataiIntegrationObject" parameterType="DataiIntegrationObject">
        update datai_integration_object
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="api != null and api != ''">api = #{api},</if>
            <if test="label != null">label = #{label},</if>
            <if test="labelPlural != null">label_plural = #{labelPlural},</if>
            <if test="keyPrefix != null">key_prefix = #{keyPrefix},</if>
            <if test="namespace != null">namespace = #{namespace},</if>
            <if test="isQueryable != null">is_queryable = #{isQueryable},</if>
            <if test="isCreateable != null">is_createable = #{isCreateable},</if>
            <if test="isUpdateable != null">is_updateable = #{isUpdateable},</if>
            <if test="isDeletable != null">is_deletable = #{isDeletable},</if>
            <if test="isReplicateable != null">is_replicateable = #{isReplicateable},</if>
            <if test="isRetrieveable != null">is_retrieveable = #{isRetrieveable},</if>
            <if test="isSearchable != null">is_searchable = #{isSearchable},</if>
            <if test="isCustom != null">is_custom = #{isCustom},</if>
            <if test="isCustomSetting != null">is_custom_setting = #{isCustomSetting},</if>
            <if test="isWork != null">is_work = #{isWork},</if>
            <if test="isIncremental != null">is_incremental = #{isIncremental},</if>
            <if test="objectIndex != null">object_index = #{objectIndex},</if>
            <if test="batchField != null">batch_field = #{batchField},</if>
            <if test="isPartitioned != null">is_partitioned = #{isPartitioned},</if>
            <if test="totalRows != null">total_rows = #{totalRows},</if>
            <if test="lastSyncDate != null">last_sync_date = #{lastSyncDate},</if>
            <if test="lastBatchDate != null">last_batch_date = #{lastBatchDate},</if>
            <if test="syncStatus != null">sync_status = #{syncStatus},</if>
            <if test="errorMessage != null">error_message = #{errorMessage},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where datai_integration_object.id = #{id}
    </update>

    <delete id="deleteDataiIntegrationObjectById" parameterType="Integer">
        delete from datai_integration_object where id = #{id}
    </delete>

    <delete id="deleteDataiIntegrationObjectByIds" parameterType="String">
        delete from datai_integration_object where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectObjectSyncStatistics" resultType="java.util.Map">
        select 
            count(*) as totalSyncCount,
            sum(case when sync_status = true then 1 else 0 end) as successSyncCount,
            sum(case when sync_status = false then 1 else 0 end) as failedSyncCount,
            sum(sync_num) as totalSyncRecords,
            avg(cost) as avgSyncTime,
            min(start_time) as firstSyncTime,
            max(end_time) as lastSyncTime,
            min(sync_start_time) as firstSyncStartTime,
            max(sync_end_time) as lastSyncEndTime,
            sum(case when sync_type = 'full' then 1 else 0 end) as fullSyncCount,
            sum(case when sync_type = 'incremental' then 1 else 0 end) as incrementalSyncCount
        from datai_integration_batch_history
        where api = #{api}
    </select>

    <select id="selectObjectDependencies" resultType="java.util.Map">
        select 
            f.field as fieldApi,
            f.label as fieldLabel,
            f.reference_to as referencedObjects,
            f.relationship_name as relationshipName,
            f.field_data_type as fieldDataType
        from datai_integration_field f
        where f.api = #{api}
        and f.reference_to is not null 
        and f.reference_to != ''
        order by f.field
    </select>
</mapper>