<?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.SupplierCompanyAuditingMapper">
    <sql id="Vo_Column_List">
        `company_auditing_id`,`user_id`,`supplier_id`,`supplier_company_id`,`auditor_id`,`create_time`,`update_time`,`status`,`remarks`
    </sql>

    <select id="list" resultType="com.tmerclub.cloud.supplier.vo.SupplierCompanyAuditingVO">
        select ca.`company_auditing_id`,
        ca.`user_id`,
        ca.`supplier_id`,
        ca.`supplier_company_id`,
        ca.`auditor_id`,
        ca.`create_time`,
        ca.`update_time`,
        ca.`status`,
        ca.`remarks`,
        sd.supplier_name,
        sd.intro
        from supplier_company_auditing ca
        JOIN supplier_detail sd ON ca.supplier_id = sd.supplier_id
        where ca.status = 0 and sd.supplier_status != -1 and sd.supplier_status != 3 and sd.supplier_status != 4
        <if test="supplierCompanyAuditingDTO.supplierName != null and supplierCompanyAuditingDTO.supplierName != ''">
            and sd.supplier_name like concat('%', #{supplierCompanyAuditingDTO.supplierName}, '%')
        </if>
        <if test="supplierCompanyAuditingDTO.beginTime != null and supplierCompanyAuditingDTO.endTime != null">
            and ca.create_time between #{supplierCompanyAuditingDTO.beginTime} and #{supplierCompanyAuditingDTO.endTime}
        </if>
        order by ca.update_time desc
    </select>

    <update id="update">
        update supplier_company_auditing
        <set>
            <if test="supplierCompanyAuditing.userId != null">
                `user_id` = #{supplierCompanyAuditing.userId},
            </if>
            <if test="supplierCompanyAuditing.supplierId != null">
                `supplier_id` = #{supplierCompanyAuditing.supplierId},
            </if>
            <if test="supplierCompanyAuditing.supplierCompanyId != null">
                `supplier_company_id` = #{supplierCompanyAuditing.supplierCompanyId},
            </if>
            <if test="supplierCompanyAuditing.auditorId != null">
                `auditor_id` = #{supplierCompanyAuditing.auditorId},
            </if>
            <if test="supplierCompanyAuditing.status != null">
                `status` = #{supplierCompanyAuditing.status},
            </if>
            <if test="supplierCompanyAuditing.remarks != null">
                `remarks` = #{supplierCompanyAuditing.remarks},
            </if>
        </set>
        where company_auditing_id = #{supplierCompanyAuditing.companyAuditingId}
    </update>
    <select id="getById" resultType="com.tmerclub.cloud.supplier.model.SupplierCompanyAuditing">
        select
        <include refid="Vo_Column_List"/>
        from supplier_company_auditing
        where company_auditing_id = #{supplierCompanyAuditingId}
    </select>
    <select id="getLatestAuditingBySupplierId" resultType="com.tmerclub.cloud.supplier.vo.SupplierCompanyAuditingVO">
        select ca.`company_auditing_id`,
               ca.`user_id`,
               ca.`supplier_id`,
               ca.`supplier_company_id`,
               ca.`auditor_id`,
               ca.`create_time`,
               ca.`update_time`,
               ca.`status`,
               ca.`remarks`,
               sd.supplier_name,
               sd.intro
        from supplier_company_auditing ca
        join supplier_detail sd on ca.supplier_id = sd.supplier_id
        where ca.supplier_id = #{supplierId}
        order by create_time desc
        limit 1
    </select>
    <insert id="insert" useGeneratedKeys="true" keyProperty="companyAuditingId">
        insert into supplier_company_auditing(`user_id`, `supplier_id`, `supplier_company_id`, `auditor_id`, `status`, `remarks`)
        values (#{supplierCompanyAuditing.userId}, #{supplierCompanyAuditing.supplierId},
                #{supplierCompanyAuditing.supplierCompanyId}, #{supplierCompanyAuditing.auditorId},
                #{supplierCompanyAuditing.status}, #{supplierCompanyAuditing.remarks});
    </insert>
    <delete id="deleteById">
        delete
        from supplier_company_auditing
        where company_auditing_id = #{supplierCompanyAuditingId}
    </delete>

    <select id="getNeedAuditList" resultType="com.tmerclub.cloud.supplier.vo.SupplierCompanyAuditingVO">
        select `company_auditing_id`,`supplier_id`,`supplier_company_id`
        from supplier_company_auditing where status = 0
    </select>
</mapper>
