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

    <resultMap type="com.ruoyi.ss.model.domain.ModelVO" id="SmModelResult" autoMapping="true">
        <result property="tags" column="tags" typeHandler="com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler"/>
    </resultMap>

    <sql id="selectSmModelVo">
        select sm.model_id,
               sm.model_name,
               sm.model,
               sm.picture,
               sm.introduce,
               sm.create_by,
               sm.create_time,
               sm.update_by,
               sm.update_time,
               sm.remark,
               sm.deleted,
               sm.tags,
               sm.service_type,
               sm.service_rate,
               sm.product_id,
               sm.sn_prefix,
               sm.sort
        from ss_model sm
    </sql>

    <sql id="searchCondition">
        <if test="modelName != null  and modelName != ''"> and sm.model_name like concat('%', #{modelName}, '%')</if>
        <if test="snPrefix != null  and snPrefix != ''"> and sm.sn_prefix like concat('%', #{snPrefix}, '%')</if>
        <if test="model != null  and model != ''"> and sm.model = #{model}</if>
        <if test="serviceType != null  and serviceType != ''"> and sm.service_type = #{serviceType}</if>
        <if test="productId != null  and productId != ''"> and sm.product_id = #{productId}</if>
        <if test="deleted == null">and sm.deleted = false</if>
        <if test="deleted != null">and sm.deleted = #{deleted}</if>
        <if test="keyword != null">
            and (
            sm.model_name like concat('%', #{keyword}, '%')
            or sm.model like concat('%', #{keyword}, '%')
            )
        </if>
    </sql>

    <select id="selectSmModelList" parameterType="ModelQuery" resultMap="SmModelResult">
        <include refid="selectSmModelVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSmModelByModelId" parameterType="Long" resultMap="SmModelResult">
        <include refid="selectSmModelVo"/>
        where sm.model_id = #{modelId}
    </select>

    <select id="selectCount" resultType="java.lang.Integer">
        select count(sm.model_id)
        from ss_model sm
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectOne" resultMap="SmModelResult">
        <include refid="selectSmModelVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        limit 1
    </select>

    <insert id="insertSmModel" parameterType="Model" useGeneratedKeys="true" keyProperty="modelId">
        insert into ss_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="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>
            <if test="remark != null">remark,</if>
            <if test="deleted != null">deleted,</if>
            <if test="tags != null">tags,</if>
            <if test="serviceType != null">service_type,</if>
            <if test="serviceRate != null">service_rate,</if>
            <if test="productId != null">product_id,</if>
            <if test="snPrefix != null">sn_prefix,</if>
            <if test="sort != null">sort,</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="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="tags != null">#{tags,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
            <if test="serviceType != null">#{serviceType},</if>
            <if test="serviceRate != null">#{serviceRate},</if>
            <if test="productId != null">#{productId},</if>
            <if test="snPrefix != null">#{snPrefix},</if>
            <if test="sort != null">#{sort},</if>
        </trim>
    </insert>

    <update id="updateSmModel" parameterType="Model">
        update ss_model
        <trim prefix="SET" suffixOverrides=",">
            <if test="modelName != null">model_name = #{modelName},</if>
            <if test="model != null">model = #{model},</if>
            <if test="picture != null">picture = #{picture},</if>
            <if test="introduce != null">introduce = #{introduce},</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>
            <if test="remark != null">remark = #{remark},</if>
            <if test="deleted != null">deleted = #{deleted},</if>
            <if test="tags != null">tags = #{tags,typeHandler=com.ruoyi.system.mapper.typehandler.StringSplitListTypeHandler},</if>
            <if test="serviceType != null">service_type = #{serviceType},</if>
            <if test="serviceRate != null">service_rate = #{serviceRate},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="snPrefix != null">sn_prefix = #{snPrefix},</if>
            <if test="sort != null">sort = #{sort},</if>
        </trim>
        where model_id = #{modelId}
    </update>

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

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

    <update id="logicDeleteByIds">
        update ss_model
        set deleted = true
        where model_id in
        <foreach collection="modelIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
</mapper>
