<?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.inspect.metadata.alg.mapper.BasedataAlgMapper">
    
    <resultMap type="com.inspect.metadata.alg.domain.BasedataAlg" id="BasedataAlgResult">
        <result property="algId"    column="alg_id"    />
        <result property="algName"    column="alg_name"    />
        <result property="algSubtypeId"    column="sub_alg_subtype_id"    />
        <result property="algSubtypeCode"    column="sub_alg_subtype_code"    />
        <result property="algSubtypeName"    column="sub_alg_subtype_name"    />
        <result property="lowerValue"    column="sub_lower_value"    />
        <result property="upperValue"    column="sub_upper_value"    />
        <result property="delFlag"    column="sub_del_flag"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateTime"    column="sub_update_time"    />
    </resultMap>

    <sql id="selectBasedataAlgVo">
        select alg_id, alg_name, del_flag, create_by, create_time, update_by, update_time from basedata_alg
    </sql>

    <select id="selectBasedataAlgList" parameterType="com.inspect.metadata.alg.domain.BasedataAlg" resultMap="BasedataAlgResult">
        <include refid="selectBasedataAlgVo"/>
        <where>  
            <if test="algName != null  and algName != ''"> and alg_name like concat('%', #{algName}, '%')</if>
        </where>
    </select>
    
    <select id="selectBasedataAlgByAlgId" parameterType="Long" resultMap="BasedataAlgResult">
        <include refid="selectBasedataAlgVo"/>
        where alg_id = #{algId}
    </select>
        
    <insert id="insertBasedataAlg" parameterType="com.inspect.metadata.alg.domain.BasedataAlg" useGeneratedKeys="true" keyProperty="algId">
        insert into basedata_alg
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="algName != null">alg_name,</if>
            <if test="delFlag != null">del_flag,</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="algName != null">#{algName},</if>
            <if test="delFlag != null">#{delFlag},</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="updateBasedataAlg" parameterType="com.inspect.metadata.alg.domain.BasedataAlg">
        update basedata_alg
        <trim prefix="SET" suffixOverrides=",">
            <if test="algName != null">alg_name = #{algName},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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 alg_id = #{algId}
    </update>

    <delete id="deleteBasedataAlgByAlgId" parameterType="Long">
        delete from basedata_alg where alg_id = #{algId}
    </delete>

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

    <select id="getAlgTypeList" resultMap="BasedataAlgResult">
        select alg_id,alg_name from basedata_alg where del_flag = '0'
    </select>

    <select id="selectBasedataAlgTypeList" resultMap="BasedataAlgResult">
        SELECT
            a.alg_id,
            a.alg_name,
            b.alg_subtype_id,
            b.alg_subtype_code,
            b.alg_subtype_name,
            b.lower_value,
            b.upper_value
        FROM
            basedata_alg a
        LEFT JOIN basedata_alg_subtype b ON b.alg_id = a.alg_id
    </select>

    <select id="checkAlgNameIsExist" parameterType="String" resultType="int">
        SELECT count(0) FROM basedata_alg
        <where>
        1=1
            and alg_name like concat('%', #{algName}, '%')
            and alg_id != #{algId}
        </where>
    </select>


</mapper>