<?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.trade.service.enterprise.mapper.EnterpriseMapper">

    <resultMap type="com.trade.service.enterprise.domain.Enterprise" id="EnterpriseMap">
        <result property="id" column="id"/>
        <result property="customerId" column="customer_id"/>
        <result property="customerIdentifierNo" column="customer_identifier_no"/>
        <result property="enterpriseType" column="enterprise_type"/>
        <result property="countryCode" column="country_code"/>
        <result property="name" column="name"/>
        <result property="certNo" column="cert_no"/>
        <result property="registrationNumber" column="registration_number"/>
        <result property="establishDate" column="establish_date"/>
        <result property="registerCapital" column="register_capital"/>
        <result property="registerAddress" column="register_address"/>
        <result property="sameRegisterAndBusinessAddress" column="same_register_and_business_address"/>
        <result property="businessAddress" column="business_address"/>
        <result property="certificationStatus" column="certification_status"/>
        <result property="exportType" column="export_type"/>
        <result property="proxyDistrictCode" column="proxy_district_code"/>
        <result property="authorizationFile" column="authorization_file"/>
        <result property="businessProduct" column="business_product"/>
        <result property="changeRecordId" column="change_record_id"/>
        <result property="remark" column="remark"/>
        <result property="deleted" column="deleted"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="gmtCreate" column="gmt_create"/>
        <result property="gmtModified" column="gmt_modified"/>
    </resultMap>

    <sql id="Base_Column_List">
        `id`, `customer_id`, `enterprise_type`, `country_code`, `name`, `cert_no`, `registration_number`,`customer_identifier_no`,
        `establish_date`, `register_capital`, `register_address`, `business_address`,`same_register_and_business_address`,
        `certification_status`, `export_type`, `business_product`, `proxy_district_code`, `authorization_file`,
        `change_record_id`, `remark`, `deleted`, `create_by`, `update_by`, `gmt_create`, `gmt_modified`
    </sql>

    <resultMap type="com.trade.service.enterprise.dto.EnterpriseBaseInfoDto" id="enterpriseBaseInfoResultMap">
        <result property="customerId" column="customer_id"/>
        <result property="customerIdentifierNo" column="customer_identifier_no"/>
        <result property="enterpriseType" column="enterprise_type"/>
        <result property="enterpriseName" column="enterprise_name"/>
        <result property="enterpriseId" column="enterprise_id"/>
        <result property="certNo" column="cert_no"/>
        <result property="establishDate" column="establish_date"/>
        <result property="registrationDate" column="registration_date"/>
        <result property="establishDate" column="establish_date"/>
        <result property="certificationStatus" column="certification_status"/>
        <result property="businessProduct" column="business_product"/>
        <result property="riskManager" column="risk_manager"/>
        <result property="riskManagerId" column="risk_manager_id"/>
        <result property="businessManager" column="business_manager"/>
        <result property="businessManagerId" column="business_manager_id"/>
    </resultMap>

    <select id="findBySearch" resultMap="enterpriseBaseInfoResultMap">
        SELECT e.id AS enterprise_id, e.customer_id, u.identifier_no,e.customer_identifier_no,
        e.enterprise_type, e.name AS enterprise_name, e.cert_no,
        e.establish_date, u.gmt_create AS registration_date, e.certification_status,
        e.business_product,assign.rc_user_name AS risk_manager, assign.rc_user_id AS risk_manager_id,
        assign.bd_user_name AS business_manager,assign.bd_user_id AS business_manager_id
        FROM enterprise e
        LEFT JOIN customer_user u ON e.customer_id = u.id
        LEFT JOIN customer_assign assign ON e.customer_id = assign.customer_user_id
        WHERE e.deleted = 0
        <if test="enterpriseName!= null and enterpriseName!= ''">
            AND e.name LIKE CONCAT('%', #{enterpriseName}, '%')
        </if>
        <if test="certNo!= null and certNo!= ''">
            AND e.cert_no LIKE CONCAT('%', #{certNo}, '%')
        </if>
        <if test="customerIdentifierNo!= null and customerIdentifierNo!= ''">
            AND u.identifier_no = #{customerIdentifierNo}
        </if>
        <if test="enterpriseType!= null and enterpriseType!= ''">
            AND e.enterprise_type = #{enterpriseType}
        </if>
        <if test="certificationStatus!= null and certificationStatus!= ''">
            AND e.certification_status = #{certificationStatus}
        </if>
        <if test="businessProduct!= null and businessProduct!= ''">
            AND e.business_product = #{businessProduct}
        </if>
        <if test="managerName != null and managerName != ''">
            AND (
                assign.rc_user_name = CONCAT('%',#{managerName},'%') OR assign.bd_user_name = CONCAT('%',#{managerName},'%')
                OR to_pinyin(assign.rc_user_name) LIKE CONCAT( '%',#{managerName},'%') OR to_pinyin(assign.bd_user_name) LIKE CONCAT( '%',#{managerName},'%')
                )
        </if>
        <if test="managerId != null">
            AND (assign.rc_user_id = #{managerId} OR assign.bd_user_id = #{managerId} )
        </if>
        <if test="registrationStartDate != null">
            <![CDATA[ AND DATE_FORMAT(u.gmt_create, '%Y-%m-%d')>=  DATE_FORMAT(#{registrationStartDate}, '%Y-%m-%d') ]]>
        </if>
        <if test="registrationEndDate != null">
            <![CDATA[ AND DATE_FORMAT(u.gmt_create, '%Y-%m-%d')<=  DATE_FORMAT(#{registrationEndDate}, '%Y-%m-%d') ]]>
        </if>
        ORDER BY e.gmt_modified DESC
    </select>

    <select id="findByCustomerInfo" resultMap="enterpriseBaseInfoResultMap">
        SELECT e.id AS enterprise_id, e.customer_id, e.customer_identifier_no,
               e.enterprise_type, e.name AS enterprise_name, e.cert_no,
               e.establish_date, e.certification_status, e.business_product
        FROM enterprise e
        <if test="customerInfo != null and customerInfo != ''">
            AND e.name LIKE CONCAT ('%',#{customerInfo},'%') OR e.customer_identifier_no = #{customerInfo}
        </if>
    </select>
</mapper>

