<?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.dayou.mapper.GlobalConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.GlobalConfig">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="key" property="key" />
        <result column="field" property="field" />
        <result column="value" property="value" />
        <result column="description" property="description" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        `key`, field, value, description
    </sql>

    <insert id="insertOrUpdateBatch">
        insert into global_config(`key`, `value`,field,description) values
        <foreach collection="list" item="o" separator="," >
            (#{o.key},#{o.value},#{o.field},#{o.description})
        </foreach>
        on duplicate key update
        <![CDATA[`value` = case when values(`value`) <> '' then values(`value`) else `value` end,
        description = values(description)
        ]]>
    </insert>

    <update id="updateBatchByKey">
        update global_config
        set `value` = case
        <foreach collection="list" item="g" separator=" ">
            when `key`=#{g.key} and field = #{g.field} then #{g.value}
        </foreach>
        end
        where
        `key` in
        <foreach collection="list" item="g" separator="," open="(" close=")">
            #{g.key}
        </foreach>
        and field in
        <foreach collection="list" item="g" separator="," open="(" close=")">
            #{g.field}
        </foreach>
    </update>

    <select id="getValueByKeyFiled" resultType="java.lang.String">
        select value from global_config where deleted=0 and `key` = #{key} and field = #{field}
    </select>

    <select id="getValueByFiled" resultType="java.lang.String">
        select value from global_config where deleted=0 and field = #{field}
    </select>

    <select id="getConfigByKey" parameterType="java.lang.String" resultType="com.dayou.entity.GlobalConfig">
        SELECT  id,`key`,field,value,description  FROM global_config   WHERE  deleted=0  AND `key` = #{key}
    </select>

    <select id="getGlobalConfig"  resultType="com.dayou.entity.GlobalConfig">
        select id,`key`,field,value,description  FROM global_config   WHERE  deleted=0  AND `key` = #{key} AND field = #{field}
    </select>
</mapper>
