<?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.ruoyi.bst.model.mapper.ModelMapper">

    <resultMap type="ModelVO" id="ModelResult" autoMapping="true">
        <result property="modelId"    column="model_id"    />
        <result property="modelName"    column="model_name"    />
        <result property="model"    column="model"    />
        <result property="picture"    column="picture"    />
        <result property="introduce"    column="introduce"    />
        <result property="createTime"    column="create_time"    />
        <result property="remark"    column="remark"    />
        <result property="deleted"    column="deleted"    />
        <result property="tags"    column="tags"    />
        <result property="productId"    column="product_id"    />
        <result property="snPrefix"    column="sn_prefix"    />
        <result property="chip"    column="chip"    />
    </resultMap>

    <sql id="selectModelVo">
        select
            bm.model_id,
            bm.model_name,
            bm.model,
            bm.picture,
            bm.introduce,
            bm.create_time,
            bm.remark,
            bm.deleted,
            bm.tags,
            bm.product_id,
            bm.sn_prefix,
            bm.chip
        from bst_model bm
    </sql>

    <sql id="searchCondition">
        <if test="query.modelName != null  and query.modelName != ''"> and model_name like concat('%', #{query.modelName}, '%')</if>
        <if test="query.model != null  and query.model != ''"> and model = #{query.model}</if>
        <if test="query.picture != null  and query.picture != ''"> and picture = #{query.picture}</if>
        <if test="query.introduce != null  and query.introduce != ''"> and introduce = #{query.introduce}</if>
        <if test="query.deleted != null "> and bm.deleted = #{query.deleted}</if>
        <if test="query.tags != null  and query.tags != ''"> and tags = #{query.tags}</if>
        <if test="query.productId != null  and query.productId != ''"> and product_id = #{query.productId}</if>
        <if test="query.snPrefix != null  and query.snPrefix != ''"> and sn_prefix = #{query.snPrefix}</if>
        <if test="query.chip != null  and query.chip != ''"> and chip = #{query.chip}</if>
        ${query.params.dataScope}
    </sql>

    <select id="selectModelList" parameterType="ModelQuery" resultMap="ModelResult">
        <include refid="selectModelVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectModelByModelId" parameterType="Long" resultMap="ModelResult">
        <include refid="selectModelVo"/>
        where model_id = #{modelId}
    </select>

    <insert id="insertModel" parameterType="Model" useGeneratedKeys="true" keyProperty="modelId">
        insert into bst_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="modelName != null">model_name,</if>
            <if test="model != null">model,</if>
            <if test="picture != null">picture,</if>
            <if test="introduce != null">introduce,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="deleted != null">deleted,</if>
            <if test="tags != null">tags,</if>
            <if test="productId != null">product_id,</if>
            <if test="snPrefix != null">sn_prefix,</if>
            <if test="chip != null">chip,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="modelName != null">#{modelName},</if>
            <if test="model != null">#{model},</if>
            <if test="picture != null">#{picture},</if>
            <if test="introduce != null">#{introduce},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="tags != null">#{tags},</if>
            <if test="productId != null">#{productId},</if>
            <if test="snPrefix != null">#{snPrefix},</if>
            <if test="chip != null">#{chip},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="Model" useGeneratedKeys="true" keyProperty="modelId">
        insert into bst_model
        <trim prefix="(" suffix=")" suffixOverrides=",">
            model_name,
            model,
            picture,
            introduce,
            create_time,
            remark,
            deleted,
            tags,
            product_id,
            sn_prefix,
            chip,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.modelName != null ">#{i.modelName},</if>
                <if test="i.modelName == null ">default,</if>
                <if test="i.model != null ">#{i.model},</if>
                <if test="i.model == null ">default,</if>
                <if test="i.picture != null ">#{i.picture},</if>
                <if test="i.picture == null ">default,</if>
                <if test="i.introduce != null ">#{i.introduce},</if>
                <if test="i.introduce == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
                <if test="i.remark != null ">#{i.remark},</if>
                <if test="i.remark == null ">default,</if>
                <if test="i.deleted != null ">#{i.deleted},</if>
                <if test="i.deleted == null ">default,</if>
                <if test="i.tags != null ">#{i.tags},</if>
                <if test="i.tags == null ">default,</if>
                <if test="i.productId != null ">#{i.productId},</if>
                <if test="i.productId == null ">default,</if>
                <if test="i.snPrefix != null ">#{i.snPrefix},</if>
                <if test="i.snPrefix == null ">default,</if>
                <if test="i.chip != null ">#{i.chip},</if>
                <if test="i.chip == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update bst_model
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="model_name = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.modelName != null ">
                        WHEN #{item.model_id} THEN #{item.modelName}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `model_name`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="model = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.model != null ">
                        WHEN #{item.model_id} THEN #{item.model}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `model`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="picture = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.picture != null ">
                        WHEN #{item.model_id} THEN #{item.picture}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `picture`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="introduce = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.introduce != null ">
                        WHEN #{item.model_id} THEN #{item.introduce}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `introduce`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.model_id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="remark = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.remark != null ">
                        WHEN #{item.model_id} THEN #{item.remark}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `remark`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="deleted = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.deleted != null ">
                        WHEN #{item.model_id} THEN #{item.deleted}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `deleted`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="tags = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.tags != null ">
                        WHEN #{item.model_id} THEN #{item.tags}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `tags`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="product_id = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.productId != null ">
                        WHEN #{item.model_id} THEN #{item.productId}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `product_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="sn_prefix = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.snPrefix != null ">
                        WHEN #{item.model_id} THEN #{item.snPrefix}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `sn_prefix`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="chip = CASE model_id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.chip != null ">
                        WHEN #{item.model_id} THEN #{item.chip}
                    </when>
                    <otherwise>
                        WHEN #{item.model_id} THEN `chip`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where model_id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.modelId}
        </foreach>
    </update>

    <update id="updateModel" parameterType="Model">
        update bst_model
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where model_id = #{data.modelId}
    </update>

    <!--logicDel-->
    <update id="logicDel">
        update bst_model bm
        set bm.deleted = true
        where bm.model_id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
        and bm.deleted = false
    </update>


    <sql id="updateColumns">
        <if test="data.modelName != null">model_name = #{data.modelName},</if>
        <if test="data.model != null">model = #{data.model},</if>
        <if test="data.picture != null">picture = #{data.picture},</if>
        <if test="data.introduce != null">introduce = #{data.introduce},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
        <if test="data.deleted != null">deleted = #{data.deleted},</if>
        <if test="data.tags != null">tags = #{data.tags},</if>
        <if test="data.productId != null">product_id = #{data.productId},</if>
        <if test="data.snPrefix != null">sn_prefix = #{data.snPrefix},</if>
        <if test="data.chip != null">chip = #{data.chip},</if>
    </sql>

    <delete id="deleteModelByModelId" parameterType="Long">
        delete from bst_model where model_id = #{modelId}
    </delete>

    <delete id="deleteModelByModelIds" parameterType="String">
        delete from bst_model where model_id in
        <foreach item="modelId" collection="array" open="(" separator="," close=")">
            #{modelId}
        </foreach>
    </delete>
</mapper>
