<?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.zm.order.tool.server.dao.MetadataDao">

    <resultMap id="MetadataMap" type="com.zm.order.tool.server.model.Metadata">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="biz_type" jdbcType="VARCHAR" property="bizType"/>
        <result column="property" jdbcType="VARCHAR" property="property"/>
        <result column="value" jdbcType="VARCHAR" property="value"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
    </resultMap>

    <sql id="tb">
        metadata
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
        biz_type, property, value, description, created_at, updated_at, is_deleted
    </sql>

    <sql id="values">
      #{id}, #{bizType}, #{property}, #{value}, #{description}, now(), now(), 0
  </sql>

    <insert id="insert" parameterType="com.zm.order.tool.server.model.Metadata" keyColumn="id" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>


    <select id="selectList" parameterType="map" resultMap="MetadataMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="bizType != null and bizType != '' ">
                AND biz_type = #{bizType}
            </if>
            <if test="property != null and property != '' ">
                AND property = #{property}
            </if>
            <if test="value != null and value != '' ">
                AND value = #{value}
            </if>
        and is_deleted = 0
        </where>
        order by updated_at desc, id asc
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
    </select>


    <select id="selectOne" parameterType="map" resultMap="MetadataMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
       <where>
        <if test="bizType != null">
            AND biz_type = #{bizType}
        </if>
        <if test="property != null">
            AND property = #{property}
        </if>
        <if test="value != null">
            AND value = #{value}
        </if>
        and is_deleted = 0
       </where>
    </select>


    <update id="update" parameterType="com.zm.order.tool.server.model.Metadata">
        update
        <include refid="tb"/>
        <set>
            <if test="value != null">
                value = #{value},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            updated_at= now()
        </set>
        where
        id =#{id}
    </update>


    <delete id="delete" parameterType="list">
        delete
        from
        <include refid="tb"/>
        where
        id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>


    <select id="count" parameterType="map" resultType="int">
        select
        count(1)
        from
        <include refid="tb"/>
        <where>
            <if test="bizType != null and bizType != '' ">
                AND biz_type = #{bizType}
            </if>
            <if test="property != null and property != ''">
                AND property = #{property}
            </if>
            <if test="value != null and value != '' ">
                AND value = #{value}
            </if>
            and is_deleted = 0
        </where>
    </select>



</mapper>