<?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.iformall.mapper.InvestCustomerMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.iformall.domain.po.invest.InvestCustomerEntity" id="investCustomerMap">
        <result property="id" column="id"/>
        <result property="tenantId" column="tenant_id"/>
        <result property="customerNo" column="customer_no"/>
        <result property="name" column="name"/>
        <result property="phone" column="phone"/>
        <result property="businessId" column="business_id"/>
        <result property="brandId" column="brand_id"/>
        <result property="type" column="type"/>
        <result property="rating" column="rating"/>
        <result property="investChannel" column="invest_channel"/>
        <result property="contractNo" column="contract_no"/>
        <result property="createDate" column="create_date"/>
        <result property="updateDate" column="update_date"/>
    </resultMap>

    <select id="listByIds" resultType="com.iformall.domain.vo.invest.InvestCustomerVo">
        SELECT c.*,d.`id` AS demandId,d.`owner` AS demandOwner FROM `invest_follow_record` f
        INNER JOIN `invest_customer` c ON c.id=f.`follow_id`
        INNER JOIN `invest_demand` d ON d.`customer_id`=c.id
        where 1=1
        <if test=" null != type and type.size()>0">
            AND c.type in
            <foreach collection="type" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
        <if test=" null != ids and ids.size()>0">
            AND c.id in
            <foreach collection="ids" index="index" item="idItem" open="(" separator="," close=")">
                #{idItem}
            </foreach>
        </if>
    </select>
    <select id="countCustomerUnassigned" resultType="int">
        SELECT count(1)
        FROM `invest_customer` c
                 INNER JOIN `invest_demand` d ON d.`customer_id` = c.id
        WHERE d.`owner` = 0 AND c.type in (0,1,3)
          AND c.`tenant_id` = #{tenantId};
    </select>

</mapper>