<?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.tmerclub.cloud.platform.mapper.SysAccessKeyMapper">
  <resultMap id="sysAccessKeyMap" type="com.tmerclub.cloud.platform.model.SysAccessKey">
    <id column="sys_access_key_id" property="sysAccessKeyId" />
    <result column="name" property="name"/>
    <result column="remark" property="remark"/>
    <result column="access_id" property="accessId"/>
    <result column="access_key" property="accessKey"/>
    <result column="prem_type" property="premType"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
  </resultMap>

  <resultMap id="sysAccessKeyVOMap" type="com.tmerclub.cloud.api.platform.vo.SysAccessKeyVO">
    <id column="sys_access_key_id" property="sysAccessKeyId" />
    <result column="name" property="name"/>
    <result column="remark" property="remark"/>
    <result column="access_id" property="accessId"/>
    <result column="access_key" property="accessKey"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="sys_type" property="sysType"/>
    <result column="prem_type" property="premType"/>
    <result column="usability_shop_id" property="usabilityShopId"/>
    <collection property="menuIdList" ofType="java.lang.Long">
      <result column="menu_id" property="value"/>
    </collection>
    <collection property="menuPermissionId" ofType="java.lang.Long">
      <result column="menu_permission_id" property="value"/>
    </collection>
  </resultMap>
  <sql id="Vo_Column_List">
    `sys_access_key_id`,`name`,`remark`,`access_id`,`access_key`,`sys_type`,`prem_type`,`usability_shop_id`,`create_time`,`update_time`
  </sql>
  <select id="list" resultMap="sysAccessKeyMap">
    select `sys_access_key_id`,`name`,`remark`,`access_id`,`create_time`,`update_time`
    from sys_access_key
    <where>
      <if test="sysAccessKeyDTO.accessId != null and sysAccessKeyDTO.accessId != ''">
        `access_id` like concat('%',#{sysAccessKeyDTO.accessId},'%')
      </if>
      <if test="sysAccessKeyDTO.name != null and sysAccessKeyDTO.name != ''">
        and `name` like  concat('%',#{sysAccessKeyDTO.name},'%')
      </if>
    </where>
    order by sys_access_key_id desc
  </select>
  <select id="getBySysAccessKeyId" resultMap="sysAccessKeyVOMap">
    select sak.*, sam.menu_id,samp.menu_permission_id from sys_access_key sak
    left join sys_access_menu sam ON sam.sys_access_key_id = sak.sys_access_key_id
    left join sys_access_menu_permission samp ON samp.sys_access_key_id = sak.sys_access_key_id
    where sak.sys_access_key_id = #{sysAccessKeyId}
  </select>
  <select id="getByAccessId" resultMap="sysAccessKeyVOMap">
    select sak.*, sam.menu_id,samp.menu_permission_id from sys_access_key sak
    left join sys_access_menu sam ON sam.sys_access_key_id = sak.sys_access_key_id
    left join sys_access_menu_permission samp ON samp.sys_access_key_id = sak.sys_access_key_id
    where sak.access_id = #{accessId}
  </select>
  <insert id="save" useGeneratedKeys="true" keyProperty="sysAccessKey.sysAccessKeyId">
    insert into sys_access_key (`name`,`remark`,`access_id`,`access_key`,sys_type,usability_shop_id,prem_type)
    values (#{sysAccessKey.name},#{sysAccessKey.remark},#{sysAccessKey.accessId},#{sysAccessKey.accessKey},#{sysAccessKey.sysType},
    #{sysAccessKey.usabilityShopId},#{sysAccessKey.premType});
  </insert>
  <update id="update">
    update sys_access_key
    <set>
      <if test="sysAccessKey.name != null">
        `name` = #{sysAccessKey.name},
      </if>
      <if test="sysAccessKey.remark != null">
        `remark` = #{sysAccessKey.remark},
      </if>
      <if test="sysAccessKey.accessId != null">
        `access_id` = #{sysAccessKey.accessId},
      </if>
      <if test="sysAccessKey.accessKey != null">
        `access_key` = #{sysAccessKey.accessKey},
      </if>
      <if test="sysAccessKey.premType != null">
        `prem_type` = #{sysAccessKey.premType},
      </if>
      <if test="sysAccessKey.usabilityShopId != null">
        `usability_shop_id` = #{sysAccessKey.usabilityShopId},
      </if>
      <if test="sysAccessKey.sysType != null">
        `sys_type` = #{sysAccessKey.sysType},
      </if>
    </set>
    where sys_access_key_id = #{sysAccessKey.sysAccessKeyId}
  </update>
  <delete id="deleteById">
    delete from sys_access_key where sys_access_key_id = #{sysAccessKeyId}
  </delete>

</mapper>
