<?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.agri.generator.mapper.SysApiInfoMapper">
    
    <resultMap type="SysApiInfo" id="SysApiInfoResult">
        <result property="id"    column="id"    />
        <result property="requestPath"    column="request_path"    />
        <result property="apiVendor"    column="api_vendor"    />
        <result property="apiDesc"    column="api_desc"    />
        <result property="apiRemark"    column="api_remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isGenRequest"    column="is_gen_request"    />
        <result property="isGenResponse"    column="is_gen_response"    />
        <result property="isDeleted"    column="is_deleted"    />
    </resultMap>

    <resultMap id="SysApiInfoSysApiParamResult" type="SysApiInfo" extends="SysApiInfoResult">
        <collection property="sysApiParamList" ofType="SysApiParam" column="id" select="selectSysApiParamList" />
    </resultMap>

    <resultMap type="SysApiParam" id="SysApiParamResult">
        <result property="id"    column="id"    />
        <result property="apiId"    column="api_id"    />
        <result property="paramType"    column="param_type"    />
        <result property="paramName"    column="param_name"    />
        <result property="required"    column="required"    />
        <result property="paramDataType"    column="param_data_type"    />
        <result property="paramDesc"    column="param_desc"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDeleted"    column="is_deleted"    />
    </resultMap>

    <sql id="selectSysApiInfoVo">
        select id, request_path, api_vendor, api_desc, api_remark, create_time, update_time,is_gen_request, is_gen_response, is_deleted from sys_api_info
    </sql>

    <select id="selectSysApiInfoList" parameterType="SysApiInfo" resultMap="SysApiInfoResult">
        <include refid="selectSysApiInfoVo"/>
        <where>  
            <if test="requestPath != null  and requestPath != ''"> and request_path like concat('%', #{requestPath}, '%')</if>
            <if test="apiVendor != null "> and api_vendor = #{apiVendor}</if>
            <if test="apiDesc != null  and apiDesc != ''"> and api_desc like concat('%', #{apiDesc}, '%')</if>
            <if test="apiRemark != null  and apiRemark != ''"> and api_remark = #{apiRemark}</if>
            <if test="isDeleted != null "> and is_deleted = #{isDeleted}</if>
        </where>
    </select>
    
    <select id="selectSysApiInfoById" parameterType="Long" resultMap="SysApiInfoSysApiParamResult">
        select id, request_path, api_vendor, api_desc, api_remark, create_time, update_time, is_gen_request, is_gen_response, is_deleted
        from sys_api_info
        where id = #{id}
    </select>

    <select id="selectSysApiParamList" resultMap="SysApiParamResult">
        select id, api_id, param_type, param_name, required, param_data_type, param_desc, gen_entity, create_time, update_time, is_deleted
        from sys_api_param
        where api_id = #{api_id}
    </select>

    <insert id="insertSysApiInfo" parameterType="SysApiInfo" useGeneratedKeys="true" keyProperty="id">
        insert into sys_api_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="requestPath != null and requestPath != ''">request_path,</if>
            <if test="apiVendor != null">api_vendor,</if>
            <if test="apiDesc != null and apiDesc != ''">api_desc,</if>
            <if test="apiRemark != null">api_remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDeleted != null">is_deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="requestPath != null and requestPath != ''">#{requestPath},</if>
            <if test="apiVendor != null">#{apiVendor},</if>
            <if test="apiDesc != null and apiDesc != ''">#{apiDesc},</if>
            <if test="apiRemark != null">#{apiRemark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDeleted != null">#{isDeleted},</if>
         </trim>
    </insert>

    <update id="updateSysApiInfo" parameterType="SysApiInfo">
        update sys_api_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="requestPath != null and requestPath != ''">request_path = #{requestPath},</if>
            <if test="apiVendor != null">api_vendor = #{apiVendor},</if>
            <if test="apiDesc != null and apiDesc != ''">api_desc = #{apiDesc},</if>
            <if test="apiRemark != null">api_remark = #{apiRemark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDeleted != null">is_deleted = #{isDeleted},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysApiInfoById" parameterType="Long">
        delete from sys_api_info where id = #{id}
    </delete>

    <delete id="deleteSysApiInfoByIds" parameterType="String">
        delete from sys_api_info where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteSysApiParamByApiIds" parameterType="String">
        delete from sys_api_param where api_id in 
        <foreach item="apiId" collection="array" open="(" separator="," close=")">
            #{apiId}
        </foreach>
    </delete>

    <delete id="deleteSysApiParamByApiId" parameterType="Long">
        delete from sys_api_param where api_id = #{apiId}
    </delete>

    <insert id="batchSysApiParam">
        insert into sys_api_param( id, api_id, param_type, param_name, required, param_data_type, param_desc, gen_entity, create_time, update_time, is_deleted) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.apiId}, #{item.paramType}, #{item.paramName}, #{item.required}, #{item.paramDataType}, #{item.paramDesc}, #{item.createTime}, #{item.updateTime}, #{item.isDeleted})
        </foreach>
    </insert>
</mapper>