<?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="jnpf.storecertificatephoto.mapper.BaseParamMapper">

    <resultMap id="BaseParamResultMap" type="jnpf.model.warningnotice.po.FtbParamEntity">
        <id property="id" column="id"/>
        <result property="key" column="key"/>
        <result property="type" column="type"/>
        <result property="value" column="value"/>
        <result property="sort" column="sort"/>
    </resultMap>

    <select id="selectByKey" resultMap="BaseParamResultMap">
        select id, `key`, `type`, `value`, `sort`
        from ftb_param
        where `key` = #{key}
        limit 1
    </select>

    <update id="updateByKey">
        update ftb_param
        set `key` = #{entity.key},
            `type` = #{entity.type},
            `value` = #{entity.value},
            `sort` = #{entity.sort}
        where `key` = #{sourceKey}
    </update>

    <insert id="insertParam">
        insert into ftb_param (
            id,
            `key`,
            `type`,
            `value`,
            `sort`
        ) values (
            #{entity.id},
            #{entity.key},
            #{entity.type},
            #{entity.value},
            #{entity.sort}
        )
    </insert>

    <insert id="batchInsertParam">
        insert into ftb_param (
            id,
            `key`,
            `type`,
            `value`,
            `sort`
        )
        values
        <foreach collection="entities" item="entity" separator=",">
            (
                #{entity.id},
                #{entity.key},
                #{entity.type},
                #{entity.value},
                #{entity.sort}
            )
        </foreach>
    </insert>

    <update id="batchUpdateById">
        update ftb_param
        set
            `key` = case id
            <foreach collection="entities" item="entity">
                when #{entity.id} then #{entity.key}
            </foreach>
            end,
            `type` = case id
            <foreach collection="entities" item="entity">
                when #{entity.id} then #{entity.type}
            </foreach>
            end,
            `value` = case id
            <foreach collection="entities" item="entity">
                when #{entity.id} then #{entity.value}
            </foreach>
            end,
            `sort` = case id
            <foreach collection="entities" item="entity">
                when #{entity.id} then #{entity.sort}
            </foreach>
            end
        where id in
        <foreach collection="entities" item="entity" open="(" separator="," close=")">
            #{entity.id}
        </foreach>
    </update>

    <update id="incrementValueByKey">
        update ftb_param
        set `value` = CAST(IFNULL(`value`, '0') AS UNSIGNED) + #{number}
        where `key` = #{key}
    </update>

    <delete id="deleteByKey">
        delete
        from ftb_param
        where `key` = #{key}
    </delete>


    <delete id="deleteByTypeNotKey">
        delete
        from ftb_param
        where `type` = #{type} and `key` != #{notKey}
    </delete>
</mapper>
