<?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.miaxis.customer.mapper.CustomerInfoMapper">

    <resultMap type="CustomerInfo" id="CustomerInfoResult">
        <result property="id"    column="id"    />
        <result property="corporateName"    column="corporate_name"    />
        <result property="businessType"    column="business_type"    />
        <result property="corporateLogo"    column="corporate_logo"    />
        <result property="customerSource"    column="customer_source"    />
        <result property="cityCode"    column="city_code"    />
        <result property="attributedPersonnel"    column="attributed_personnel"    />
        <result property="detailedAddress"    column="detailed_address"    />
        <result property="industryType"    column="industry_type"    />
        <result property="corporatePhone"    column="corporate_phone"    />
        <result property="corporateContacts"    column="corporate_contacts"    />
        <result property="phone"    column="phone"    />
        <result property="customerStar"    column="customer_star"    />
        <result property="enterpriseTaxNumber"    column="enterprise_tax_number"    />
        <result property="invoiceTitle"    column="invoice_title"    />
        <result property="bankOfDeposit"    column="bank_of_deposit"    />
        <result property="bankAccount"    column="bank_account"    />
        <result property="financePhone"    column="finance_phone"    />
        <result property="faxNumber"    column="fax_number"    />
        <result property="appletAddress"    column="applet_address"    />
        <result property="appletLogo"    column="applet_logo"    />
        <result property="appletQrCode"    column="applet_qr_code"    />
        <result property="appletIntroduce"    column="applet_introduce"    />
        <result property="shelfStatus"    column="shelf_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectCustomerInfoVo">
        select id, corporate_name, business_type, corporate_logo, customer_source, city_code, attributed_personnel, detailed_address, industry_type, corporate_phone, corporate_contacts, phone, customer_star, enterprise_tax_number, invoice_title, bank_of_deposit, bank_account, finance_phone, fax_number, applet_address, applet_logo, applet_qr_code, applet_introduce, shelf_status, create_time, update_time, status from customer_info
    </sql>

    <select id="selectCustomerInfoList" parameterType="CustomerInfo" resultMap="CustomerInfoResult">
        <include refid="selectCustomerInfoVo"/>
        <where>
            <if test="corporateName != null  and corporateName != ''"> and corporate_name like concat('%', #{corporateName}, '%')</if>
            <if test="businessType != null "> and business_type = #{businessType}</if>
            <if test="corporateLogo != null "> and corporate_logo = #{corporateLogo}</if>
            <if test="customerSource != null "> and customer_source = #{customerSource}</if>
            <if test="cityCode != null  and cityCode != ''"> and city_code = #{cityCode}</if>
            <if test="attributedPersonnel != null "> and attributed_personnel = #{attributedPersonnel}</if>
            <if test="detailedAddress != null  and detailedAddress != ''"> and detailed_address = #{detailedAddress}</if>
            <if test="industryType != null "> and industry_type = #{industryType}</if>
            <if test="corporatePhone != null  and corporatePhone != ''"> and corporate_phone = #{corporatePhone}</if>
            <if test="corporateContacts != null  and corporateContacts != ''"> and corporate_contacts = #{corporateContacts}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="customerStar != null "> and customer_star = #{customerStar}</if>
            <if test="enterpriseTaxNumber != null  and enterpriseTaxNumber != ''"> and enterprise_tax_number = #{enterpriseTaxNumber}</if>
            <if test="invoiceTitle != null  and invoiceTitle != ''"> and invoice_title = #{invoiceTitle}</if>
            <if test="bankOfDeposit != null  and bankOfDeposit != ''"> and bank_of_deposit = #{bankOfDeposit}</if>
            <if test="bankAccount != null  and bankAccount != ''"> and bank_account = #{bankAccount}</if>
            <if test="financePhone != null  and financePhone != ''"> and finance_phone = #{financePhone}</if>
            <if test="appletAddress != null  and appletAddress != ''"> and applet_address = #{appletAddress}</if>
            <if test="shelfStatus != null "> and shelf_status = #{shelfStatus}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <select id="getCustomerById" resultType="com.miaxis.customer.vo.CustomerInfoVo">
        select
        ci.id,
        ci.corporate_name,
        ci.business_type,
        ci.corporate_logo,
        f1.file_url as corporateLogoFileUrl,
        ci.customer_source,
        ci.city_code,
        ci.attributed_personnel,
        ci.detailed_address,
        ci.industry_type,
        ci.corporate_phone,
        ci.corporate_contacts,
        ci.phone,
        ci.customer_star,
        ci.enterprise_tax_number,
        ci.invoice_title,
        ci.bank_of_deposit,
        ci.bank_account,
        ci.finance_phone,
        ci.fax_number,
        ci.applet_address,
        ci.applet_logo,
        f2.file_url as appletLogoFileUrl,
        ci.applet_qr_code,
        f3.file_url as appletQrCodeFileUrl,
        ci.applet_introduce,
        ci.shelf_status,
        ci.create_time,
        ci.update_time,
        ci.status
        from customer_info ci
        LEFT JOIN file_info f1 on f1.file_id = ci.corporate_logo
        LEFT JOIN file_info f2 on f2.file_id = ci.applet_logo
        LEFT JOIN file_info f3 on f3.file_id = ci.applet_qr_code
        WHERE ci.id = #{id}

    </select>

    <select id="getCustomerListByProductId" resultType="com.miaxis.customer.vo.ExhibitionCustomerInfoVo">
        select
        ci.id,
        ci.corporate_name,
        f1.file_url as corporateLogoFileUrl,
        ci.applet_address,
        f2.file_url as appletLogoFileUrl,
        f3.file_url as appletQrCodeFileUrl,
        ci.applet_introduce,
        ci.create_time,
        ci.update_time,
        ci.status
        from customer_info ci
        LEFT JOIN file_info f1 on f1.file_id = ci.corporate_logo
        LEFT JOIN file_info f2 on f2.file_id = ci.applet_logo
        LEFT JOIN file_info f3 on f3.file_id = ci.applet_qr_code
        WHERE ci.industry_type = #{productId}

    </select>


    <select id="getCustomerListByCollectionCount" resultType="com.miaxis.customer.vo.ExhibitionCustomerInfoVo">
        SELECT
        ci.id,
        (select COUNT(1) from collection_info coi where coi.customer_id = ci.id) as collectionCount,
        ci.corporate_name,
        f1.file_url as corporateLogoFileUrl,
        ci.applet_address,
        f2.file_url as appletLogoFileUrl,
        f3.file_url as appletQrCodeFileUrl,
        ci.applet_introduce,
        ci.create_time,
        ci.update_time,
        ci.status
        FROM `customer_info` ci
        LEFT JOIN file_info f1 on f1.file_id = ci.corporate_logo
        LEFT JOIN file_info f2 on f2.file_id = ci.applet_logo
        LEFT JOIN file_info f3 on f3.file_id = ci.applet_qr_code
        ORDER BY collectionCount DESC
    </select>


    <select id="getCollectionList" resultType="com.miaxis.collection.vo.CollectionCustomerVo">
        SELECT
        coi.id,
        ci.id as customerId,
        ci.corporate_name,
        ci.applet_address,
        f1.file_url as corporateLogoFileUrl,
        f2.file_url as appletLogoFileUrl,
        f3.file_url as appletQrCodeFileUrl,
        ci.applet_introduce,
        coi.create_time,
        ci.status
        FROM `customer_info` ci
        LEFT JOIN file_info f1 on f1.file_id = ci.corporate_logo
        LEFT JOIN file_info f2 on f2.file_id = ci.applet_logo
        LEFT JOIN file_info f3 on f3.file_id = ci.applet_qr_code
        LEFT JOIN collection_info coi ON coi.customer_id = ci.id
        WHERE ci.id IN ( select coi.customer_id from collection_info coi where coi.user_id = #{id})

    </select>


</mapper>
