<?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.kiisoo.ic.store.mapper.StorePromotionDataDOMapper">

    <select id="selectPromotionDataGroupByRegion" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        t3.id as regionId,
        t3.NAME AS regionName,
        sum( t4.customer_id ) AS newCusNum
        FROM
        poi_store t1
        LEFT JOIN op_seller_customer_relation t4 on t1.id = t4.store_id
        <if test="startDate != null">
            and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{startDate},'%Y-%m-%d')
        </if>
        <if test="endDate != null">
            and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{endDate},'%Y-%m-%d')
        </if>,
        poi_store_region t2,
        poi_region t3
        WHERE
        t1.id = t2.store_id
        and t2.region_id = t3.id
        <choose>
            <when test="storeIds != null and storeIds.size > 0">
                and t1.id in
                <foreach collection="storeIds"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
        <if test="regionId != null">
            and t3.id = #{regionId}
        </if>
        <if test="type != null">
            and t4.type = #{type}
        </if>
        and t1.status = 1
        GROUP BY
        t3.id
    </select>

    <select id="selectSumCusNumGroupByRegion" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        t3.id as regionId,
        sum( t4.customer_id ) AS cusNum,
        max(t4.create_time) as maxDate,
        min(t4.create_time) as minDate
        FROM
        poi_store t1,
        op_seller_customer_relation t4,
        poi_store_region t2,
        poi_region t3
        WHERE
        t1.id = t2.store_id
        and t2.region_id = t3.id
        and t1.id = t4.store_id
        <choose>
            <when test="storeIds != null and storeIds.size > 0">
                and t1.id in
                <foreach collection="storeIds"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
        <if test="regionId != null">
            and t3.id = #{regionId}
        </if>
        and t1.status = 1
        <if test="type != null">
            and t4.type = #{type}
        </if>
        GROUP BY
        t3.id
    </select>

    <select id="selectPromotionDataGroupByStore" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        t1.id as storeId,
        t1.NAME as storeName,
        sum( t4.customer_id ) AS newCusNum
        FROM
        poi_store t1
        LEFT JOIN op_seller_customer_relation t4 on t1.id = t4.store_id
        <if test="startDate != null">
            and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{startDate},'%Y-%m-%d')
        </if>
        <if test="endDate != null">
            and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{endDate},'%Y-%m-%d')
        </if>,
        poi_store_region t2
        WHERE
        t1.id = t2.store_id
        <choose>
            <when test="storeIds != null and storeIds.size > 0">
                and t1.id in
                <foreach collection="storeIds"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
        <if test="storeId != null">
            and t1.id = #{storeId}
        </if>
        <if test="regionId != null">
            and t3.id = #{regionId}
        </if>
        and t1.status = 1
        <if test="type != null">
            and t4.type = #{type}
        </if>
        GROUP BY
        t1.id
    </select>

    <select id="selectSumCusNumGroupByStore" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        t1.id as storeId,
        sum( t4.customer_id ) AS cusNum,
        max(t4.create_time) as maxDate,
        min(t4.create_time) as minDate
        FROM
        poi_store t1,
        op_seller_customer_relation t4,
        poi_store_region t2
        WHERE
        t1.id = t2.store_id
        and t1.id = t4.store_id
        <choose>
            <when test="storeIds != null and storeIds.size > 0">
                and t1.id in
                <foreach collection="storeIds"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
        <if test="storeId != null">
            and t1.id = #{storeId}
        </if>
        <if test="regionId != null">
            and t3.id = #{regionId}
        </if>
        and t1.status = 1
        <if test="type != null">
            and t4.type = #{type}
        </if>
        GROUP BY
        t1.id
    </select>

    <select id="selectPromotionDataGroupBySeller" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        t1.id AS sellerId,
        t3.NAME AS sellerName,
        sum( t2.customer_id ) AS newCusNum
        FROM
        poi_store_staff t1
        LEFT JOIN op_seller_customer_relation t2 ON t1.id = t2.user_id
        <if test="startDate != null">
            and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &gt;= DATE_FORMAT(#{startDate},'%Y-%m-%d')
        </if>
        <if test="endDate != null">
            and DATE_FORMAT(t4.create_time,'%Y-%m-%d') &lt;= DATE_FORMAT(#{endDate},'%Y-%m-%d')
        </if>,
        privilage_user t3,
        poi_store_region t4
        WHERE
        t1.user_id = t3.id
        and t1.store_id = t4.store_id
        <choose>
            <when test="storeIds != null and storeIds.size > 0">
                and t1.id in
                <foreach collection="storeIds"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
        AND t1.type = 4
        AND t1.STATUS = 1
        AND t1.deleted = 0
        AND t3.STATUS = 1
        <if test="storeId != null">
            and t1.store_id = #{storeId}
        </if>
        <if test="regionId != null">
            and t4.region_id = #{regionId}
        </if>
        <if test="type != null">
            and t2.type = #{type}
        </if>
        group by t1.id
    </select>

    <select id="selectSumCusNumGroupBySeller" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        t1.id AS sellerId,
        sum( t2.customer_id ) AS cusNum,
        max(t2.create_time) as maxDate,
        min(t2.create_time) as minDate
        FROM
        poi_store_staff t1,
        op_seller_customer_relation t2,
        privilage_user t3,
        poi_store_region t4
        WHERE
        t1.user_id = t3.id
        and t1.store_id = t4.store_id
        and t1.id = t2.user_id
        <choose>
            <when test="storeIds != null and storeIds.size > 0">
                and t1.id in
                <foreach collection="storeIds"  index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and 1 = 0
            </otherwise>
        </choose>
        AND t1.type = 4
        AND t1.STATUS = 1
        AND t1.deleted = 0
        AND t3.STATUS = 1
        <if test="storeId != null">
            and t1.store_id = #{storeId}
        </if>
        <if test="sellerId != null">
            and t1.id = #{sellerId}
        </if>
        <if test="regionId != null">
            and t4.region_id = #{regionId}
        </if>
        <if test="type != null">
            and t2.type = #{type}
        </if>
        group by t1.id
    </select>

      <select id="selectCustomer" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
         SELECT
            t1.cp_user_id AS userid,
            t2.store_id AS storeId
        FROM
            privilage_cp_user t1,
            privilage_cp_user_store t2
        WHERE
            t1.id = t2.cp_user_id
    </select>

    <insert id="insertStorePromotionData" parameterType="com.kiisoo.ic.store.entity.WxCusInfoRespDO">
        INSERT INTO poi_customer_contact_data_stat (
        store_id,region_id, new_apply_cnt,negative_feedback_cnt, new_contact_cnt,
        chat_cnt,message_cnt,reply_percentage,avg_reply_time, stat_time, create_time)
        VALUES (
        #{storeId},#{regionId}, #{new_apply_cnt},#{negative_feedback_cnt}, #{new_contact_cnt},
        #{chat_cnt}, #{message_cnt}, #{reply_percentage}, #{avg_reply_time}, #{statTime}, NOW())
    </insert>

    <select id="selectWxCusInfoByRegionId" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        sum( negative_feedback_cnt ) AS delCusNum
        FROM poi_customer_contact_data_stat
        WHERE stat_time &gt;= #{startDate}
        AND stat_time &lt;= #{endDate}
        group by region_id
    </select>

    <select id="selectWxCusInfoByStoreId" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        sum( negative_feedback_cnt ) AS delCusNum
        FROM poi_customer_contact_data_stat
        WHERE stat_time &gt;= #{startDate}
        AND stat_time &lt;= #{endDate}
        group by store_id
    </select>

    <select id="selectWxSumCusNumByConditionShopManager" resultType="java.lang.Integer">
        SELECT count(customer_id) from op_seller_customer_relation t, op_customer t2
        WHERE t.store_id = #{storeId}
        and t.customer_id = t2.id
        <if test="startDate != null">
            and date(t.create_time) &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date(t.create_time) &lt;= #{endDate}
        </if>
        group by store_id
    </select>

    <select id="selectWxSumCusNumByConditionStaff" resultType="java.lang.Integer">
        SELECT count(customer_id) from op_seller_customer_relation t1, poi_store_staff t2, op_customer t3 where t1.customer_id = t3.id and t1.user_id = t2.id
        and t2.type = 4
        <if test="userId != null">
            AND t2.user_id = #{userId}
        </if>
        <if test="startDate != null">
            and date(t1.create_time) &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date(t1.create_time) &lt;= #{endDate}
        </if>
        group by t1.user_id
    </select>

    <select id="selectStoreIdByUserId" resultType="java.lang.Long">
        SELECT store_id FROM poi_store_staff WHERE user_id = #{userId} and status = 1 and deleted = 0
    </select>

    <select id="selectWxStaffDOList" resultType="com.kiisoo.ic.store.entity.StaffDO">
        SELECT
            user_id AS id,
            count( customer_id ) AS cusNum
        FROM
            `op_seller_customer_relation` t,op_customer t2
        WHERE
        t.customer_id = t2.id
           and t. store_id = #{storeId}
        <if test="startDate != null">
            and date(t.create_time) &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date(t.create_time) &lt;= #{endDate}
        </if>
        GROUP BY
            t.user_id
    </select>
    <select id="selectWxValidStaffNameList" resultType="com.kiisoo.ic.store.entity.StaffDO">
        SELECT
            DISTINCT
            t1.id as id,
            IFNULL(t1.avatar,'') as url,
            t2.NAME AS staffName
        FROM
            poi_store_staff t1,
            privilage_user t2
        WHERE
            t1.user_id = t2.id
            AND t1.type = 4
            AND t1.STATUS = 1
            AND t1.deleted = 0
            AND t2.STATUS = 1
            AND t1.store_id = #{storeId}
    </select>
    <select id="selectStoreRankNo" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        a.storeId,
        a.newCusNum,
        @rank := @rank + 1 AS rankNo
        FROM
        (
        SELECT
        count( t2.customer_id ) AS newCusNum,
        t1.id AS storeId
        FROM
        poi_store t1
         JOIN `op_seller_customer_relation` t2 ON t1.id = t2.store_id
        JOIN op_customer t3 on t2.customer_id = t3.id
        <if test="startDate != null">
            and date(t2.create_time) &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date(t2.create_time) &lt;= #{endDate}
        </if>
        WHERE
        t1.STATUS = 1
        GROUP BY
        storeId
        ORDER BY
        newCusNum DESC, storeId DESC
        ) a,
        ( SELECT @rank := 0 ) b
    </select>
    <select id="selectStaffRankNo" resultType="com.kiisoo.ic.store.entity.StorePromotionDataDO">
        SELECT
        a.userId,
        a.newCusNum,
        @rank := @rank + 1 AS rankNo
        FROM
        ( SELECT count( t2.customer_id ) AS newCusNum, t1.user_id AS userId FROM
        poi_store_staff t1
        LEFT JOIN `op_seller_customer_relation` t2 ON t1.id = t2.user_id
        left join op_customer t3 on t2.customer_id = t3.id
        <if test="startDate != null">
            and date(t2.create_time) &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            and date(t2.create_time) &lt;= #{endDate}
        </if>
        where t1.store_id = #{storeId}
        and t1.type = 4
        GROUP BY userId,t3.id ORDER BY newCusNum DESC, userId DESC ) a,
        ( SELECT @rank := 0 ) b
    </select>
    <select id="selectLatestUpdateTime" resultType="java.util.Date">
        select max(update_time) from op_seller_customer_relation where store_id = #{storeId}
    </select>


</mapper>
