<?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.qryord.project.master.model.mapper.ModelMapper">
    
    <resultMap type="Model" id="ModelResult">
        <result property="id"    column="id"    />
        <result property="mkId"    column="mkId"    />
        <result property="modelName"    column="modelName"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="modelKindName"    column="modelKindName"    />

    </resultMap>

    <sql id="selectModelVo">
        select mm.id, mm.mkId, mm.modelName, mm.del_flag, mm.create_by, mm.create_time, mm.update_by, mm.update_time,mmk.kindName modelKindName from m_model mm
        inner join m_model_kind mmk on mm.mkId = mmk.id
    </sql>

    <select id="selectModelList" parameterType="Model" resultMap="ModelResult">
        <include refid="selectModelVo"/>
        <where>
            <if test="mkId != null "> and mm.mkId = #{mkId}</if>
            <if test="modelName != null  and modelName != ''"> and mm.modelName like concat('%', #{modelName}, '%')</if>
            <if test="delFlag != null and delFlag != ''"> and mm.del_flag = #{delFlag}</if>
        </where>
    </select>
    
    <select id="selectModelById" parameterType="Long" resultMap="ModelResult">
        <include refid="selectModelVo"/>
        where mm.id = #{id}
    </select>
        
    <insert id="insertModel" parameterType="Model" useGeneratedKeys="true" keyProperty="id">
        insert into m_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mkId != null "> mkId,</if>
            <if test="modelName != null">modelName,</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="mkId != null "> #{mkId},</if>
            <if test="modelName != null">#{modelName},</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="updateModel" parameterType="Model">
        update m_model
        <trim prefix="SET" suffixOverrides=",">
            <if test="mkId != null "> mkId = #{mkId},</if>
            <if test="modelName != null">modelName = #{modelName},</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 id = #{id}
    </update>

    <update id="deleteModelById" parameterType="Long">
        update m_model
        set del_flag = '2'
        where id = #{id}
    </update>

    <update id="deleteModelByIds" parameterType="String">
        update m_model
        set del_flag = '2'
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>