<?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.ym.mec.biz.dal.dao.SysTenantConfigDao">
  <resultMap id="SysTenantConfig" type="com.ym.mec.biz.dal.entity.SysTenantConfig">
    <id column="id_" jdbcType="INTEGER" property="id" />
    <result column="sys_config_id_" jdbcType="INTEGER" property="sysConfigId" />
    <result column="tenant_id_" jdbcType="INTEGER" property="tenantId" />
    <result column="param_value_" jdbcType="VARCHAR" property="paramValue" />
    <result column="create_time_" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    id_, sys_config_id_, tenant_id_, param_value_, create_time_, update_time_
  </sql>
  <select id="get" parameterType="java.lang.Integer" resultMap="SysTenantConfig">
    SELECT
    <include refid="Base_Column_List" />
    FROM sys_tenant_config
    WHERE id_ = #{id,jdbcType=INTEGER}
  </select>
  <delete id="delete" parameterType="java.lang.Integer">
    DELETE FROM sys_tenant_config
    WHERE id_ = #{id,jdbcType=INTEGER}
  </delete>
  <delete id="delByConfigId">
    DELETE FROM sys_tenant_config WHERE tenant_id_ = #{tenantId}
    AND sys_config_id_ IN
    <foreach collection="configIdList" open="(" close=")" item="configId" separator=",">
      #{configId}
    </foreach>
  </delete>
  <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.SysTenantConfig" useGeneratedKeys="true">
    insert into sys_tenant_config (sys_config_id_, tenant_id_, param_value_,
      create_time_, update_time_)
    values (#{sysConfigId,jdbcType=INTEGER}, #{tenantId,jdbcType=INTEGER}, #{paramValue,jdbcType=VARCHAR},
      #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP})
  </insert>
  <insert id="batchInsert">
    INSERT INTO sys_tenant_config (sys_config_id_, tenant_id_, param_value_,create_time_, update_time_)
    VALUES
    <foreach collection="sysConfigs" item="item" separator=",">
      (#{item.id}, #{tenantId}, #{item.paranValue},NOW(),NOW())
    </foreach>
  </insert>
  <update id="update" parameterType="com.ym.mec.biz.dal.entity.SysTenantConfig">
    update sys_tenant_config
    <set>
      <if test="sysConfigId != null">
        sys_config_id_ = #{sysConfigId,jdbcType=INTEGER},
      </if>
      <if test="tenantId != null">
        tenant_id_ = #{tenantId,jdbcType=INTEGER},
      </if>
      <if test="paramValue != null">
        param_value_ = #{paramValue,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time_ = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time_ = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id_ = #{id,jdbcType=INTEGER}
  </update>

  <!-- 分页查询 -->
  <select id="queryPage" resultMap="SysTenantConfig" parameterType="map">
    SELECT * FROM sys_config ORDER BY id_
    <include refid="global.limit" />
  </select>

  <!-- 查询当前表的总记录数 -->
  <select id="queryCount" resultType="int">
    SELECT COUNT(*) FROM sys_config
  </select>

  <select id="findAll" resultMap="SysTenantConfig">
    SELECT * FROM sys_config
  </select>
  <select id="queryALl" resultMap="com.ym.mec.biz.dal.dao.SysConfigDao.SysConfig">
    SELECT sc.id_,sc.param_name_,sc.group_,CASE WHEN stc.id_ IS NULL THEN sc.paran_value_ ELSE stc.param_value_ END paran_value_,stc.create_time_ create_on_
    FROM sys_config sc
    LEFT JOIN sys_tenant_config stc ON sc.id_ = stc.sys_config_id_ AND stc.tenant_id_ = #{tenantId}
    <where>
      <if test="group != null">
        AND sc.group_ = #{group}
      </if>
      <if test="group == null or group == ''">
        AND sc.group_ != 'DEFAULT'
      </if>
    </where>
    ORDER BY sc.group_
  </select>
  <select id="getConfigValue" resultType="java.lang.String">
    SELECT CASE WHEN stc.id_ IS NULL THEN sc.paran_value_ ELSE stc.param_value_ END paran_value_ FROM sys_config sc
    LEFT JOIN sys_tenant_config stc ON sc.id_ = stc.sys_config_id_ AND stc.tenant_id_ = #{tenantId}
    WHERE sc.group_ IS NOT NULL AND sc.group_ != '' AND sc.param_name_ = #{paramName}
  </select>
  <select id="queryAllMap" resultType="java.util.Map">
    SELECT sc.param_name_ 'key',CASE WHEN stc.id_ IS NULL THEN sc.paran_value_ ELSE stc.param_value_ END 'value' FROM sys_config sc
    LEFT JOIN sys_tenant_config stc ON sc.id_ = stc.sys_config_id_ AND stc.tenant_id_ = #{tenantId}
    WHERE sc.group_ IS NOT NULL AND sc.group_ != ''
  </select>
</mapper>