<?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.sckw.system.dao.KwsEntCertificateDao">
  <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsEntCertificate">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="ent_id" jdbcType="BIGINT" property="entId" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="certificate_main" jdbcType="VARCHAR" property="certificateMain" />
    <result column="certificate_revolt" jdbcType="VARCHAR" property="certificateRevolt" />
    <result column="expire_time" jdbcType="TIMESTAMP" property="expireTime" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="BIGINT" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
  </resultMap>

  <sql id="Base_Column_List">
    id, ent_id, type, code, certificate_main, certificate_revolt, remark, status, create_by,
    create_time, update_by, update_time, del_flag, expire_time
  </sql>

  <select id="selectByKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from kws_ent_certificate
    where id = #{id,jdbcType=BIGINT}
  </select>

  <select id="selectByEntId" resultType="com.sckw.system.model.KwsEntCertificate">
    select a.* from kws_ent_certificate a where a.ent_id = #{id} and a.del_flag = 0
  </select>

  <select id="findByEntType" resultType="com.sckw.system.model.KwsEntCertificate">
    select a.* from kws_ent_certificate a where a.ent_id = #{entId} and a.type = #{type} and a.del_flag = 0 and a.status = 0
  </select>

  <select id="findPrevious" resultType="com.sckw.system.model.KwsEntCertificate">
    select a.* from kws_ent_certificate a where a.ent_id = #{entId} and a.type = #{type} and a.del_flag = 0 and a.status in (0, 1)
  </select>

  <insert id="insert" parameterType="com.sckw.system.model.KwsEntCertificate">
    insert into kws_ent_certificate
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="entId != null">
        ent_id,
      </if>
      <if test="type != null">
        type,
      </if>
      <if test="code != null">
        code,
      </if>
      <if test="certificateMain != null">
        certificate_main,
      </if>
      <if test="certificateRevolt != null">
        certificate_revolt,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="delFlag != null">
        del_flag,
      </if>
      <if test="expireTime != null">
        expire_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="entId != null">
        #{entId,jdbcType=BIGINT},
      </if>
      <if test="type != null">
        #{type,jdbcType=INTEGER},
      </if>
      <if test="code != null">
        #{code,jdbcType=VARCHAR},
      </if>
      <if test="certificateMain != null">
        #{certificateMain,jdbcType=VARCHAR},
      </if>
      <if test="certificateRevolt != null">
        #{certificateRevolt,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="expireTime != null">
        #{expireTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>

    <insert id="saveBatch">
      insert into kws_ent_certificate
      <trim prefix="(" suffix=")" suffixOverrides=",">
            id,
            ent_id,
            type,
            code,
            certificate_main,
            certificate_revolt,
            remark,
            status,
            create_by,
            create_time,
            update_by,
            update_time,
            del_flag,
            expire_time
      </trim>
      values
        <foreach collection="list" item="item" separator=",">
          <trim prefix="(" suffix=")" suffixOverrides=",">
            #{item.id,jdbcType=BIGINT},
            #{item.entId,jdbcType=BIGINT},
            #{item.type,jdbcType=INTEGER},
            #{item.code,jdbcType=VARCHAR},
            #{item.certificateMain,jdbcType=VARCHAR},
            #{item.certificateRevolt,jdbcType=VARCHAR},
            #{item.remark,jdbcType=VARCHAR},
            #{item.status,jdbcType=INTEGER},
            #{item.createBy,jdbcType=BIGINT},
            #{item.createTime,jdbcType=TIMESTAMP},
            #{item.updateBy,jdbcType=BIGINT},
            #{item.updateTime,jdbcType=TIMESTAMP},
            #{item.delFlag,jdbcType=INTEGER},
            #{item.expireTime,jdbcType=TIMESTAMP},
          </trim>
        </foreach>
    </insert>

    <update id="update" parameterType="com.sckw.system.model.KwsEntCertificate">
    update kws_ent_certificate
    <set>
      <if test="entId != null">
        ent_id = #{entId,jdbcType=BIGINT},
      </if>
      <if test="type != null">
        type = #{type,jdbcType=INTEGER},
      </if>
      <if test="code != null">
        code = #{code,jdbcType=VARCHAR},
      </if>
      <if test="certificateMain != null">
        certificate_main = #{certificateMain,jdbcType=VARCHAR},
      </if>
      <if test="certificateRevolt != null">
        certificate_revolt = #{certificateRevolt,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        del_flag = #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="expireTime != null">
        expire_time = #{expireTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

</mapper>