<?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.liquidnet.service.kylin.mapper.ReportPerformanceMapper">

    <resultMap id="getProvinceDataListResult" type="com.liquidnet.service.kylin.dao.report.PerformanceProvinceDataDto">
        <result column="performance_id" property="performanceId"/>
        <result column="province" property="province"/>
        <result column="total_number" property="totalNumber"/>
    </resultMap>
    <resultMap id="getCityDataListResult" type="com.liquidnet.service.kylin.dao.report.PerformanceCityDataDto">
        <result column="performance_id" property="performanceId"/>
        <result column="city_name" property="cityName"/>
        <result column="total_number" property="totalNumber"/>
    </resultMap>
    <resultMap id="getAgeDataListResult" type="com.liquidnet.service.kylin.dao.report.PerformanceAgeDataDto">
        <result column="performance_id" property="performanceId"/>
        <result column="age" property="age"/>
        <result column="total_number" property="totalNumber"/>
    </resultMap>
    <resultMap id="getSexDataListResult" type="com.liquidnet.service.kylin.dao.report.PerformanceSexDataDto">
        <result column="sex" property="sex"/>
        <result column="total_number" property="totalNumber"/>
    </resultMap>
    <!-- 演出按省统计-->
    <select id="getProvinceDataList" parameterType="java.lang.String" resultMap="getProvinceDataListResult">
        select x.* from (
                            SELECT kotr.performance_id AS      'performance_id',
                                   ifnull(ca.province, '未知')   province,
                                   count(1)                    total_number
                            FROM kylin_order_ticket_relations kotr
                                     inner join kylin_order_tickets AS kot on kotr.order_id = kot.order_tickets_id
                                     inner JOIN kylin_order_ticket_status AS kots ON kots.order_id = kot.order_tickets_id
                                     inner join kylin_order_ticket_entities kote on kot.order_tickets_id = kote.order_id
                                     left join sys_dict_id_card_area ca
                                               on substr(kote.enter_id_code, 1, 6)= ca.code
                            WHERE 1 > 0
                              and kotr.performance_id = ${performanceId}
                            and kots.`status` in (1, 3, 6)
                            and kots.transfer_status in('0','1','3','5')
                            and kot.coupon_type = 'no'
                            group by kotr.performance_id,ca.province
                        ) x order by x.total_number desc
    </select>
    <!-- 演出按城市统计-->
    <select id="getCityDataList" parameterType="java.lang.String" resultMap="getCityDataListResult">
        select x.* from (
                            SELECT
                                kotr.performance_id AS 'performance_id',
                                ifnull(ca.pre_city,'未知') city_name,
                                count(1) total_number
                            FROM
                                kylin_order_ticket_relations kotr
                                    inner join kylin_order_tickets AS kot on kotr.order_id = kot.order_tickets_id
                                    inner JOIN kylin_order_ticket_status AS kots ON kots.order_id = kot.order_tickets_id
                                    inner join kylin_order_ticket_entities kote on kot.order_tickets_id = kote.order_id
                                    left join sys_dict_id_card_area ca on substr(kote.enter_id_code,1,6) = ca.code
                            WHERE 1>0 and kotr.performance_id = #{performanceId}
                              and kots.`status` in(1,3,6)
                              and kots.transfer_status in('0','1','3','5')
                              and kot.coupon_type ='no'
                            group by kotr.performance_id,ca.pre_city
                        ) x order by x.total_number desc limit 20
    </select>
    <!-- 演出按年龄统计-->
    <select id="getAgeDataList" parameterType="java.lang.String" resultMap="getAgeDataListResult">
        select * from (
                          SELECT kotr.performance_id AS                                     'performance_id',
                                 CONCAT(substr(curdate(), 1, 4) - substr(kote.enter_id_code, 7, 4),'')  age,
                                 count(1)                                                   total_number
                          FROM kylin_order_ticket_relations kotr
                                   inner join kylin_order_tickets AS kot on kotr.order_id = kot.order_tickets_id
                                   inner JOIN kylin_order_ticket_status AS kots ON kots.order_id = kot.order_tickets_id
                                   inner join kylin_order_ticket_entities kote on kot.order_tickets_id = kote.order_id
                          WHERE 1 > 0
                            and kotr.performance_id = #{performanceId}
                            and kots.`status` in (1, 3, 6)
                            and kots.transfer_status in('0','1','3','5')
                            and kot.coupon_type = 'no'
                            and length(kote.enter_id_code)=18
                          group by kotr.performance_id, substr(curdate(), 1, 4) - substr(kote.enter_id_code, 7, 4)
                      )x order by x.age
    </select>
    <!-- 演出按性别统计-->
    <select id="getSexDataList" parameterType="java.lang.String" resultMap="getSexDataListResult">
        select x.sex,count(1) total_number from (
                                                    SELECT kotr.performance_id AS                                     'performance_id',
                                                           case when substr(kote.enter_id_code,17,1)%2=0 then '女' else '男' end sex
                                                    FROM kylin_order_ticket_relations kotr
                                                             inner join kylin_order_tickets AS kot on kotr.order_id = kot.order_tickets_id
                                                             inner JOIN kylin_order_ticket_status AS kots ON kots.order_id = kot.order_tickets_id
                                                             inner join kylin_order_ticket_entities kote on kot.order_tickets_id = kote.order_id
                                                    WHERE 1 > 0
                                                      and kotr.performance_id  = ${performanceId}
                                                      and kots.`status` in (1, 3, 6)
                                                      and kots.transfer_status in('0','1','3','5')
                                                      and kot.coupon_type = 'no'
                                                      and length(kote.enter_id_code)=18
                                                )x group by x.sex
    </select>
</mapper>
