<?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="org.springblade.transport.mapper.CustomerArchiveMapper">

    <resultMap id="customerArchiveResultMap" type="org.springblade.transport.pojo.vo.CustomerArchiveVO">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="customer_code" property="customerCode"/>
        <result column="short_name" property="shortName"/>
        <result column="full_name" property="fullName"/>
        <result column="customer_nature" property="customerNature"/>
        <result column="unified_credit_code" property="unifiedCreditCode"/>
        <result column="customer_type" property="customerType"/>
        <result column="project_name" property="projectName"/>
        <result column="registered_address" property="registeredAddress"/>
        <result column="registered_region_name" property="registeredRegionName"/>
        <result column="registered_detail_address" property="registeredDetailAddress"/>
        <result column="legal_person" property="legalPerson"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="dept_id" property="deptId"/>
        <result column="dept_ids" property="deptIds"/>
        <result column="dept_name" property="deptName"/>
        <result column="invoice_tax_rate" property="invoiceTaxRate"/>
        <result column="business_scope" property="businessScope"/>
        <result column="business_term_type" property="businessTermType"/>
        <result column="business_end_date" property="businessEndDate"/>
        <result column="registered_capital" property="registeredCapital"/>
        <result column="principal" property="principal"/>
        <result column="mnemonic_code" property="mnemonicCode"/>
        <result column="customer_level" property="customerLevel"/>
        <result column="max_credit_limit" property="maxCreditLimit"/>
        <result column="apply_credit_limit" property="applyCreditLimit"/>
        <result column="remark" property="remark"/>
        <result column="qualification_attachments" property="qualificationAttachments"/>
        <result column="access_type" property="accessType"/>
        <result column="approval_status" property="approvalStatus"/>
        <result column="current_node" property="currentNode"/>
        <result column="current_processor" property="currentProcessor"/>
        <result column="approved_time" property="approvedTime"/>
    </resultMap>

    <select id="selectCustomerArchivePage" resultMap="customerArchiveResultMap">
        SELECT
            id,
            tenant_id,
            create_user,
            create_dept,
            create_time,
            update_user,
            update_time,
            status,
            is_deleted,
            customer_code,
            short_name,
            full_name,
            customer_nature,
            unified_credit_code,
            customer_type,
            project_name,
            registered_address,
            registered_region_name,
            registered_detail_address,
            legal_person,
            contact_phone,
            dept_id,
            dept_ids,
            dept_name,
            invoice_tax_rate,
            business_scope,
            business_term_type,
            business_end_date,
            registered_capital,
            principal,
            mnemonic_code,
            customer_level,
            max_credit_limit,
            apply_credit_limit,
            remark,
            qualification_attachments,
            access_type,
            approval_status,
            current_node,
            current_processor,
            approved_time
        FROM blade_customer_archive
        WHERE is_deleted = 0
        <if test="userId != null">
            AND EXISTS (
                SELECT 1
                FROM blade_user_customer_scope bucs
                WHERE bucs.user_id = #{userId}
                  AND bucs.customer_id = blade_customer_archive.id
            )
        </if>
        <if test="customer.customerCode != null and customer.customerCode != ''">
            <bind name="customerCodeLike" value="'%' + customer.customerCode + '%'"/>
            AND customer_code LIKE #{customerCodeLike}
        </if>
        <if test="customer.fullName != null and customer.fullName != ''">
            <bind name="fullNameLike" value="'%' + customer.fullName + '%'"/>
            AND full_name LIKE #{fullNameLike}
        </if>
        <if test="customer.shortName != null and customer.shortName != ''">
            <bind name="shortNameLike" value="'%' + customer.shortName + '%'"/>
            AND short_name LIKE #{shortNameLike}
        </if>
        <if test="customer.unifiedCreditCode != null and customer.unifiedCreditCode != ''">
            <bind name="creditCodeLike" value="'%' + customer.unifiedCreditCode + '%'"/>
            AND unified_credit_code LIKE #{creditCodeLike}
        </if>
        <if test="customer.customerNature != null and customer.customerNature != ''">
            AND customer_nature = #{customer.customerNature}
        </if>
        <if test="customer.customerType != null and customer.customerType != ''">
            <bind name="customerTypeLike" value="'%' + customer.customerType + '%'"/>
            AND customer_type LIKE #{customerTypeLike}
        </if>
        <if test="customer.accessType != null and customer.accessType != ''">
            AND access_type = #{customer.accessType}
        </if>
        <if test="customer.approvalStatus != null and customer.approvalStatus != ''">
            AND approval_status = #{customer.approvalStatus}
        </if>
        <if test="customer.status != null">
            AND status = #{customer.status}
        </if>
        <if test="customer.deptName != null and customer.deptName != ''">
            <bind name="deptNameLike" value="'%' + customer.deptName + '%'"/>
            AND dept_name LIKE #{deptNameLike}
        </if>
        <if test="customer.createTimeStart != null and customer.createTimeStart != ''">
            AND create_time &gt;= #{customer.createTimeStart}
        </if>
        <if test="customer.createTimeEnd != null and customer.createTimeEnd != ''">
            AND create_time &lt;= #{customer.createTimeEnd}
        </if>
        ORDER BY create_time DESC
    </select>

    <select id="selectCustomerPermissionCount" resultType="int">
        SELECT COUNT(1)
        FROM blade_customer_archive ca
        WHERE ca.id = #{customerId}
          AND ca.is_deleted = 0
          AND EXISTS (
              SELECT 1
              FROM blade_user_customer_scope bucs
              WHERE bucs.user_id = #{userId}
                AND bucs.customer_id = ca.id
          )
    </select>

    <select id="selectIncludeNewCustomerUserIds" resultType="long">
        SELECT id
        FROM blade_user
        WHERE tenant_id = #{tenantId}
          AND include_new_customer = 1
          AND is_deleted = 0
    </select>

</mapper>
