<?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.supplier.mapper.SupplierAuditingMapper">
  <resultMap id="supplierAuditingMap" type="com.tmerclub.cloud.supplier.model.SupplierAuditing">
    <id column="supplier_auditing_id" property="supplierAuditingId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="user_id" property="userId"/>
    <result column="supplier_id" property="supplierId"/>
    <result column="auditor_id" property="auditorId"/>
    <result column="status" property="status"/>
    <result column="remarks" property="remarks"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `supplier_auditing_id`,`create_time`,`update_time`,`user_id`,`supplier_id`,`auditor_id`,`status`,`remarks`
  </sql>
  <select id="list" resultType="com.tmerclub.cloud.supplier.vo.SupplierAuditingVO">
    select <include refid="Vo_Column_List"/> from supplier_auditing order by supplier_auditing_id desc
  </select>
  <select id="getBySupplierAuditingId" resultType="com.tmerclub.cloud.supplier.vo.SupplierAuditingVO">
    select <include refid="Vo_Column_List"/> from supplier_auditing where supplier_auditing_id = #{supplierAuditingId}
  </select>
  <insert id="save">
    insert into supplier_auditing (`user_id`,`supplier_id`,`auditor_id`,`status`,`remarks`)
    values (#{supplierAuditing.userId},#{supplierAuditing.supplierId},#{supplierAuditing.auditorId},#{supplierAuditing.status},#{supplierAuditing.remarks});
  </insert>
  <update id="update">
    update supplier_auditing
    <set>
      <if test="supplierAuditing.userId != null">
        `user_id` = #{supplierAuditing.userId},
      </if>
      <if test="supplierAuditing.supplierId != null">
        `supplier_id` = #{supplierAuditing.supplierId},
      </if>
      <if test="supplierAuditing.auditorId != null">
        `auditor_id` = #{supplierAuditing.auditorId},
      </if>
      <if test="supplierAuditing.status != null">
        `status` = #{supplierAuditing.status},
      </if>
      <if test="supplierAuditing.remarks != null">
        `remarks` = #{supplierAuditing.remarks},
      </if>
      <if test="supplierAuditing.updateTime != null">
        `update_time` = #{supplierAuditing.updateTime},
      </if>
    </set>
    where supplier_auditing_id = #{supplierAuditing.supplierAuditingId}
  </update>
  <delete id="deleteById">
    delete from supplier_auditing where supplier_auditing_id = #{supplierAuditingId}
  </delete>
  <select id="getBySupplierId" resultType="com.tmerclub.cloud.supplier.vo.SupplierAuditingVO">
    select <include refid="Vo_Column_List"/> from supplier_auditing where supplier_id = #{supplierId}
  </select>
  <select id="auditingInfoList" resultType="com.tmerclub.cloud.supplier.vo.SupplierAuditingInfoVO">
    SELECT  sa.supplier_auditing_id,sa.user_id,sa.`status`,
            sd.supplier_id,sd.supplier_name,sd.intro,sd.supplier_logo,sd.intro,sd.supplier_status,sd.notice_mobile,
            IF(sa.update_time IS NULL,sd.create_time,sa.update_time) AS create_time
    FROM supplier_detail sd
    LEFT JOIN supplier_auditing sa ON sa.supplier_id = sd.supplier_id
        <trim prefix="where" prefixOverrides="and | or" >
          <if test="auditingInfo.supplierAuditingId != null">
            and sa.supplier_auditing_id = #{auditingInfo.supplierAuditingId}
          </if>
          <if test="auditingInfo.status != null">
            and sa.status = #{auditingInfo.status}
          </if>
          <if test="auditingInfo.intro != null">
            and sd.intro like CONCAT('%',#{auditingInfo.intro},'%')
          </if>
          <if test="auditingInfo.beginTime != null and auditingInfo.endTime != null">
            and date(sa.create_time) between #{auditingInfo.beginTime} and #{auditingInfo.endTime}
          </if>
          <if test="auditingInfo.supplierStatus != null">
            and sd.supplier_status = #{auditingInfo.supplierStatus}
          </if>
          <if test="auditingInfo.supplierName != null and auditingInfo.supplierName != ''">
            and sd.supplier_name like CONCAT('%',#{auditingInfo.supplierName},'%')
          </if>
            and sd.supplier_status != 1
        </trim>
    ORDER BY sd.create_time DESC
  </select>
  <update id="updateToFail">
    update supplier_auditing set status = -1
    where status = 0 and supplier_id in
    <foreach collection="supplierIds" item="supplierId" open="(" close=")" separator=",">
      #{supplierId}
    </foreach>
  </update>
</mapper>
