<?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.yunzhi.marketing.mapper.TaQrcodeMapper">

    <select id="listQrCodeByCondition" resultType="com.yunzhi.marketing.entity.TaQrcode">
    SELECT
        t.*,
        t2.building_name,
        t3.channel_name
    FROM
        ta_qrcode t
        LEFT JOIN ta_building t2 ON t.building_id = t2.building_id
        LEFT JOIN ta_channel t3 ON t.channel_id = t3.channel_id
    WHERE
        t.`status` > - 1
        AND t.org_id = #{orgId}
        <if test = "buildingId != '' and buildingId != null">
            AND t.building_id = #{buildingId}
        </if>
        <if test = "targetType != '' and targetType != null">
            AND t.target_type = #{targetType}
        </if>
        <if test = "targetName != '' and targetName != null">
            AND t.target_name like concat ('%',#{targetName},'%')
        </if>
        <if test = "channelId != null">
            AND t.channel_id = #{channelId}
        </if>
        <if test = "userId != null">
            AND t.create_user = #{userId}
        </if>
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND (t.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
                OR t.building_id is null)
        </if>
      ORDER BY
        t.create_date DESC
    </select>

    <select id="getVisitPersons" resultType="java.lang.Integer">
        SELECT
            count( DISTINCT t.person_id )
        FROM
            ta_person_visit_record t
        WHERE
            t.org_id = #{orgId}
            AND t.qr_code_id = #{qrCodeId}
    </select>

    <select id="getVisitNum" resultType="java.lang.Integer">
        SELECT
            count( * )
        FROM
            ta_person_visit_record t
        WHERE
            t.org_id = #{orgId}
            AND t.qr_code_id = #{qrCodeId}
    </select>

    <select id="getNewPersons" resultType="java.lang.Integer">
        SELECT
            count( DISTINCT t.person_id )
        FROM
            ta_customer_from t
        WHERE
            t.org_id = #{orgId}
            AND t.is_org_first =1
            AND t.qr_code_id = #{qrCodeId}
    </select>

    <select id="getVisitPersonsRecord" resultType="com.yunzhi.marketing.entity.TaPersonVisitRecord">
        SELECT
            t.*,
            IFNULL(t2.`name`,t2.nickname) userName,
            t2.phone userTel,
            t2.gender sex,
            t3.shortname city,
            count(*) accessCount
        FROM
            ta_person_visit_record t
            left join ta_person t2 on t.person_id = t2.person_id
            LEFT JOIN td_city t3 ON t2.city = t3.id
        WHERE
            t.org_id = #{orgId}
            AND t.qr_code_id = #{qrCodeId}
        GROUP BY
	        t.person_id
	    ORDER BY
		    t.visit_time DESC
    </select>

    <select id="getVisitPersonsRecordExport" resultType="com.yunzhi.marketing.excel.QrCodeStatistics.QrCodeVisitPersonNum">
        SELECT
            IFNULL( t2.`name`, t2.nickname ) nickName,
            t2.phone phone,
            (case t2.gender when 1 then '男' when 2 then '女' else '未知' end)  gender,
            t3.shortname province,
            count( * ) visitNum
        FROM
            ta_person_visit_record t
            LEFT JOIN ta_person t2 ON t.person_id = t2.person_id
            LEFT JOIN td_city t3 ON t2.city = t3.id
        WHERE
            t.org_id = #{orgId}
            AND t.qr_code_id = #{qrCodeId}
        GROUP BY
            t.person_id
        ORDER BY
		    t.visit_time DESC
    </select>

    <select id="getVisitNumRecord" resultType="com.yunzhi.marketing.entity.TaPersonVisitRecord">
        SELECT
            t.*,
            IFNULL( t2.`name`, t2.nickname ) userName,
            t2.phone userTel,
            t2.gender sex,
            t3.shortname city
        FROM
            ta_person_visit_record t
            LEFT JOIN ta_person t2 ON t.person_id = t2.person_id
            LEFT JOIN td_city t3 ON t2.city = t3.id
        WHERE
            t.org_id = #{orgId}
            AND t.qr_code_id = #{qrCodeId}
        ORDER BY
            t.visit_time desc
    </select>

    <select id="getVisitNumRecordExport" resultType="com.yunzhi.marketing.excel.QrCodeStatistics.QrCodeVisitNum">
        SELECT
            IFNULL( t2.`name`, t2.nickname ) nickName,
            t2.phone phone,
             (case t2.gender when 1 then '男' when 2 then '女' else '未知' end) gender,
            t3.shortname province,
			t.visit_time visitTime
        FROM
            ta_person_visit_record t
            LEFT JOIN ta_person t2 ON t.person_id = t2.person_id
            LEFT JOIN td_city t3 ON t2.city = t3.id
        WHERE
            t.org_id = #{orgId}
            AND t.qr_code_id = #{qrCodeId}
        ORDER BY
            t.visit_time desc
    </select>

    <select id="getNewPersonsRecord" resultType="com.yunzhi.marketing.entity.TaCustomerFrom">
        SELECT
            t.*,
            t2.`name` userName,
            t2.phone userTel,
            t2.sex,
            t3.shortname city,
            t2.create_date registerTime
        FROM
            ta_customer_from t
            LEFT JOIN ta_recommend_customer t2 ON t.person_id = t2.person_id
            LEFT JOIN td_city t3 ON t2.city = t3.id
        WHERE
            t.org_id = #{orgId}
            AND t.is_org_first =1
            AND t.qr_code_id = #{qrCodeId}
        GROUP BY
	        t.customer_id
	    order by
	        t2.create_date desc
    </select>

    <select id="getNewPersonsRecordExport" resultType="com.yunzhi.marketing.excel.QrCodeStatistics.QrCodeNewPersonNum">
        SELECT
            t2.`name` nickName,
            t2.phone,
            (case t2.sex when 1 then '男' when 2 then '女' else '未知' end) gender,
            t3.shortname province,
            t2.create_date registerTime
        FROM
            ta_customer_from t
            LEFT JOIN ta_recommend_customer t2 ON t.person_id = t2.person_id
            LEFT JOIN td_city t3 ON t2.city = t3.id
        WHERE
            t.org_id = #{orgId}
            AND t.is_org_first =1
            AND t.qr_code_id = #{qrCodeId}
        GROUP BY
	        t.customer_id
	    order by
	        t2.create_date desc
    </select>

</mapper>
