<?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.ycwl.basic.mapper.StatisticsMapper">
    <insert id="addStatisticsRecord">
        insert into statistics(member_id,type,morph_id,scenic_id)
        value (#{memberId},#{type},#{morphId},#{scenicId})

    </insert>
    <select id="countOrderAmount" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_price),0) as payPrice
        from `order`
        where
            (status = 1 or status = 2) and scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_at >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_at &lt;= #{endTime}
            </if>
    </select>
    <select id="countPreviewVideoOfMember" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(1), 0) AS count
        FROM (
            select count(1) as count
            from statistics
            where type=2 and scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_time >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_time &lt;= #{endTime}
            </if>
            group by member_id
        )a
    </select>
    <select id="countScanCodeOfMember" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(1), 0) AS count
        FROM (
            select 1
            from statistics
            where type=10 and morph_id in (1047,1048,1049) and scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_time >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_time &lt;= #{endTime}
            </if>
            group by member_id
        ) AS subquery;
    </select>
    <select id="countClickPayOfMember" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(1), 0) AS count
        FROM (
        select count(1) as count
        from `order`
        where scenic_id = #{scenicId}
        <if test="startTime!= null">
            and create_at >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_at &lt;= #{endTime}
        </if>
        group by member_id
        )a
    </select>
    <select id="countPayOfMember" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(1), 0) AS count
        FROM (
        select count(1) as count
        from `statistics`
        where scenic_id = #{scenicId} and type in (3,4)
        <if test="startTime!= null">
            and create_time >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_time &lt;= #{endTime}
        </if>
        group by member_id
        )a
    </select>
    <select id="countSceneOrderNum" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(count), 0) AS count
        FROM (
        select count(1) as count
        from statistics
        where type=4 and scenic_id = #{scenicId}
        <if test="startTime!= null">
            and create_time >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_time &lt;= #{endTime}
        </if>
        group by morph_id
        )a
    </select>
    <select id="countPushOrderNum" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(count), 0) AS count
        FROM (
        select count(1) as count
            from statistics
            where type=3 and scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_time >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_time &lt;= #{endTime}
            </if>
            group by morph_id
        )a
    </select>
    <select id="countPushOfMember" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(1), 0) AS count
        FROM (
        select count(1) as count
            from statistics
            where type=6 and scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_time >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_time &lt;= #{endTime}
            </if>
            group by member_id
        )a
    </select>
    <select id="countCameraShotOfMember" resultType="java.lang.Integer">
            select count(1) as count
            from face_sample
            where scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_at >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_at &lt;= #{endTime}
            </if>
    </select>
    <select id="countUploadFaceOfMember" resultType="java.lang.Integer">
        select ifnull(count(1),0) as count
        from(
        select count(1) as count
        from face
        where scenic_id = #{scenicId}
            <if test="startTime!= null">and create_at >= #{startTime}</if>
            <if test="endTime!= null">and create_at &lt;= #{endTime}</if>
            group by member_id
        ) a
    </select>
    <select id="countCompleteVideoOfMember" resultType="java.lang.Integer">
        select ifnull(count(1),0) as count
        from(
            select mv.member_id
            from member_video mv
            left join face f on f.id = mv.face_id
            where f.scenic_id = #{scenicId}
            <if test="startTime!= null">
                and f.create_at >= #{startTime}
            </if>
            <if test="endTime!= null">
                and f.create_at &lt;= #{endTime}
            </if>
            group by member_id
        )a
    </select>
    <select id="countTotalVisitorOfMember" resultType="java.lang.Integer">
        SELECT
        IFNULL(count(1), 0) AS count
        FROM (
            select count(1) as count
            from statistics
            where type in (10) and scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_time >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_time &lt;= #{endTime}
            </if>
            group by member_id
        ) AS a;
    </select>
    <select id="countCompleteOfVideo" resultType="java.lang.Integer">
        select count(1) as count
        from video
        where scenic_id = #{scenicId}
        <if test="startTime!= null">
            and create_time >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_time &lt;= #{endTime}
        </if>
    </select>
    <select id="countPreviewOfVideo" resultType="java.lang.Integer">
        SELECT
        IFNULL(SUM(count), 0) AS count
        FROM (
            select count(1) as count
            from statistics
            where type=0 and scenic_id = #{scenicId}
            <if test="startTime!= null">
                and create_time >= #{startTime}
            </if>
            <if test="endTime!= null">
                and create_time &lt;= #{endTime}
            </if>
            group by morph_id
        )a
    </select>
    <select id="countPayOfOrder" resultType="java.lang.Integer">
        select count(1) as count
        from `order`
        where
        pay_at is not null  and scenic_id = #{scenicId}
        <if test="startTime!= null">
            and create_at >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_at &lt;= #{endTime}
        </if>
    </select>
    <select id="countRefundOfOrder" resultType="java.lang.Integer">
        select count(1) as count
        from `order`
        where
        refund_status = 1  and scenic_id = #{scenicId}
        <if test="startTime!= null">
            and create_at >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_at &lt;= #{endTime}
        </if>
    </select>
    <select id="countRefundAmount" resultType="java.math.BigDecimal">
        select ifnull(sum(pay_price),0) as payPrice
        from `order`
        where
        refund_status = 1  and scenic_id = #{scenicId}
        <if test="startTime!= null">
            and create_at >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_at &lt;= #{endTime}
        </if>
    </select>
    <select id="getBrokerIdListForUser" resultType="java.lang.Long">
        select morph_id
        from (
        select morph_id, max(create_time) as createTime
        from statistics
        where type = 20 and member_id = #{memberId}
        <if test="startTime!= null">
            and create_time >= #{startTime}
        </if>
        <if test="endTime!= null">
            and create_time &lt;= #{endTime}
        </if>
        group by morph_id
             ) a
        order by createTime desc
    </select>
    <select id="getUserRecentEnterType" resultType="java.lang.Long">
        select morph_id
        from statistics
        where type = 10 and member_id = #{memberId}
        <if test="endTime!= null">
            and create_time &lt;= #{endTime}
        </if>
        order by create_time desc
        limit 1
    </select>
</mapper>