<?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="org.jeecg.modules.system.app.mapper.AppGameMapper">
    <resultMap id="appGameDTOResult"  type="org.jeecg.modules.system.app.dto.AppGameDTO">
        <id property="id" column="a_id"/>
        <collection property="game" ofType="org.jeecg.modules.system.app.dto.AppGameCuDTO" columnPrefix="a_">
            <id property="id" column="id" />
            <result property="orgCode" column="org_code"/>
            <result property="tenantId" column="tenant_id"/>
            <result property="name" column="name"/>
            <result property="cover" column="cover"/>
            <result property="backgroundImage" column="background_image"/>
            <result property="organizers" column="organizers"/>
            <result property="provinceCode" column="province_code"/>
            <result property="cityCode" column="city_code"/>
            <result property="areaCode" column="area_code"/>
            <result property="address" column="address"/>
            <result property="latitude" column="latitude" javaType="java.math.BigDecimal"/>
            <result property="longitude" column="longitude" javaType="java.math.BigDecimal"/>
            <result property="applicationEndTime" column="application_end_time"/>
            <result property="insureIds" column="insure_ids"/>
            <result property="startTime" column="start_time"/>
            <result property="endTime" column="end_time"/>
            <result property="reminder" column="reminder"/>
            <result property="aptitudes" column="aptitudes"/>
            <result property="gameResults" column="game_results"/>
            <result property="siteType" column="site_type"/>
            <result property="siteId" column="site_id"/>
        </collection>
        <collection property="gamePriceRules" ofType="org.jeecg.modules.system.app.dto.AppGameRuleDTO" javaType="java.util.List" columnPrefix="c_">
            <id property="id" column="id"/>
            <result property="gameId" column="game_id"/>
            <result property="categoryId" column="category_id"/>
            <result property="categoryName" column="category_name"/>
            <result property="sellingPrice" column="selling_price" javaType="java.math.BigDecimal"/>
            <result property="peopleNum" column="people_num"/>
            <result property="type" column="cType"/>
        </collection>
        <collection property="scheduleDTOS" ofType="org.jeecg.modules.system.app.dto.AppGameScheduleDTO" javaType="java.util.List" columnPrefix="e_">
            <id property="id" column="id"/>
            <result property="name" column="scheduleName"/>
            <result property="startTime" column="eStartTime"/>
            <result property="endTime" column="eEndTime"/>
            <result property="arrange" column="arrange"/>
        </collection>
    </resultMap>

    <resultMap id="gameResultMap" type="org.jeecg.modules.app.vo.game.FindByIdResponse">
        <id property="id" column="id"/>
        <result property="cover" column="cover"/>
        <result property="name" column="name"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="organizers" column="organizers"/>
        <result property="reminder" column="reminder"/>
        <result property="backgroundImage" column="background_image"/>
        <result property="applicationEndTime" column="application_end_time"/>
        <result property="address" column="address"/>
        <result property="isEntry" column="is_entry"/>
        <result property="km" column="km"/>
        <result property="type" column="type"/>
        <result property="phone" column="mobile"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>

        <!-- 关联查询 - 赛程安排 -->
        <collection property="gameScheduleVOList" ofType="org.jeecg.modules.app.vo.game.GameScheduleVO" javaType="java.util.List" columnPrefix="c_">
            <id property="id" column="id"/>
            <result property="arrange" column="arrange"/>
            <result property="name" column="name"/>
            <result property="startTime" column="start_time"/>
            <result property="endTime" column="end_time"/>
            <result property="startStatus" column="startStatus"/>
        </collection>
        <!-- 关联查询 - 比赛项目价格规则 -->
        <collection property="gamePriceRulesVOList" ofType="org.jeecg.modules.app.vo.game.GamePriceRulesVO" javaType="java.util.List" columnPrefix="b_">
            <id property="id" column="id"/>
            <result property="categoryName" column="category_name"/>
            <result property="categoryId" column="category_id"/>
            <result property="sellingPrice" column="selling_price" javaType="java.math.BigDecimal"/>
            <result property="peopleNum" column="people_num"/>
        </collection>
    </resultMap>


    <resultMap id="findByGameIdPriceVoMap" type="org.jeecg.modules.app.vo.game.FindByGameIdPriceVo">
        <id property="id" column="id"/>
        <result property="aptitudes" column="aptitudes"/>
        <result property="insureIds" column="insure_ids"/>
        <!-- 关联查询 - 比赛项目安排 -->
        <collection property="gamePriceRulesTypeVOList" ofType="org.jeecg.modules.app.vo.game.GamePriceRulesTypeVO" javaType="java.util.List">
            <id property="type" column="type"/>
            <result property="typeName" column="type_name"/>
        </collection>
    </resultMap>

    <select id="convertSearchCompetitionVOPage" resultType="org.jeecg.modules.app.vo.SearchVO$SearchRaceVO" parameterType="org.jeecg.modules.app.dto.SearchDTO">
        SELECT
        a.id,
        a.NAME,
        COALESCE ( g.min_selling_price, 0 ) AS selling_price,
        a.cover,
        a.org_code,
        a.start_time,
        a.end_time,
        a.site_type,
        a.site_id,
        a.latitude,
        a.longitude,
        e.address AS placeName,
        (
        a.application_end_time >= NOW()) AS registrationOpen
        FROM
        nm_game a
        LEFT JOIN ( SELECT game_id, MIN( selling_price ) AS min_selling_price FROM nm_game_price_rules GROUP BY game_id ) g ON a.id = g.game_id
        LEFT JOIN sys_depart e ON a.org_code = e.org_code
        WHERE
        a.end_time > NOW()
        <if test="searchDTO.keyword != null and searchDTO.keyword != ''">
            AND (a.name LIKE CONCAT('%',#{searchDTO.keyword},'%') OR a.address LIKE CONCAT('%',#{searchDTO.keyword},'%'))
        </if>
        ORDER BY a.end_time ASC;
    </select>
<!--    <select id="findPage" resultMap="appGameDTOResult">-->
<!--        select  a.id as a_id, a.org_code as a_org_code, a.tenant_id as a_tenant_id, a.name as a_name, a.cover as a_cover,-->
<!--                a.background_image as a_background_image, a.organizers as a_organizers, a.site_type as a_site_type,-->
<!--                a.site_id as a_site_id, a.province_code as a_province_code,a.city_code as a_city_code,-->
<!--                a.area_code as a_area_code, IFNULL(b.name, a.address) as a_address, IFNULL(b.latitude, a.latitude) as a_latitude,IFNULL(b.longitude, a.longitude) as a_longitude,-->
<!--                a.application_end_time as a_application_end_time, a.insure_ids as a_insure_ids, a.start_time as a_start_time,-->
<!--                a.end_time as a_end_time, a.reminder as  a_reminder, a.aptitudes as a_aptitudes, a.game_results as a_game_results,a.create_time as a_create_time,-->
<!--                c.id AS c_id,c.game_id as c_game_id,c.type as c_type,d.name as c_category_name,c.category_id as c_category_id,c.selling_price as  c_selling_price-->
<!--                ,c.people_num as c_people_num,c.type as c_type,e.id as e_id,e.name as e_scheduleName,e.start_time as e_eStartTime,e.end_time as e_eEndTime, e.arrange as e_arrange-->
<!--                from nm_game a-->
<!--                 left join nm_site b on  a.site_id = b.id and b.del_flag=0-->
<!--                 left join  nm_game_price_rules c on  c.game_id = a.id and c.del_flag=0-->
<!--                 left join nm_game_schedule e on e.game_id =a.id-->
<!--                 left join nm_category d on  c.category_id = d.id and d.del_flag=0-->
<!--        where a.del_flag = 0-->
<!--        <if test="appGamePageDTO != null and appGamePageDTO != ''">-->
<!--            <if test="appGamePageDTO.name != null and appGamePageDTO.name != ''">-->
<!--                AND a.name LIKE CONCAT('%',#{appGamePageDTO.name},'%')-->
<!--            </if>-->
<!--            <if test="appGamePageDTO.categoryId != null and appGamePageDTO.categoryId != ''">-->
<!--                AND a.id in (select game_id from nm_game_price_rules where category_id  = #{appGamePageDTO.categoryId}  )-->
<!--            </if>-->
<!--            <if test="appGamePageDTO.endTime!=null  and staffRequestVO.endTime!=''">-->
<!--                <![CDATA[   AND DATE_FORMAT(a.application_end_time, '%Y-%m-%d')<=  DATE_FORMAT(#{appGamePageDTO.endTime}, '%Y-%m-%d') ]]>-->
<!--            </if>-->
<!--            <if test="appGamePageDTO.startTime!=null  and appGamePageDTO.startTime!=''">-->
<!--                <![CDATA[   AND DATE_FORMAT(a.application_end_time, '%Y-%m-%d')<=  DATE_FORMAT(#{appGamePageDTO.startTime}, '%Y-%m-%d') ]]>-->
<!--            </if>-->
<!--            <if test="appGamePageDTO.orgCode != null and appGamePageDTO.orgCode != ''">-->
<!--                AND a.org_code LIKE CONCAT(#{appGamePageDTO.orgCode},'%')-->
<!--            </if>-->
<!--        </if>-->
<!--    </select>-->

    <select id="findPage" resultMap="appGameDTOResult">
        select  a.id as a_id, a.org_code as a_org_code, a.tenant_id as a_tenant_id, a.name as a_name, a.cover as a_cover,
        a.background_image as a_background_image, a.organizers as a_organizers, a.site_type as a_site_type,
        a.site_id as a_site_id, a.province_code as a_province_code,a.city_code as a_city_code,
        a.area_code as a_area_code, IFNULL(b.name, a.address) as a_address, IFNULL(b.latitude, a.latitude) as a_latitude,IFNULL(b.longitude, a.longitude) as a_longitude,
        a.application_end_time as a_application_end_time, a.insure_ids as a_insure_ids, a.start_time as a_start_time,
        a.end_time as a_end_time, a.reminder as  a_reminder, a.aptitudes as a_aptitudes, a.game_results as a_game_results,a.create_time as a_create_time
        from nm_game a
        left join nm_site b on  a.site_id = b.id and b.del_flag=0
        left join  nm_game_price_rules c on  c.game_id = a.id and c.del_flag=0
        left join nm_game_schedule e on e.game_id =a.id
        left join nm_category d on  c.category_id = d.id and d.del_flag=0
        where a.del_flag = 0
        <if test="appGamePageDTO != null and appGamePageDTO != ''">
            <if test="appGamePageDTO.name != null and appGamePageDTO.name != ''">
                AND a.name LIKE CONCAT('%',#{appGamePageDTO.name},'%')
            </if>
            <if test="appGamePageDTO.categoryId != null and appGamePageDTO.categoryId != ''">
                AND a.id in (select game_id from nm_game_price_rules where category_id  = #{appGamePageDTO.categoryId}  )
            </if>
            <if test="appGamePageDTO.endTime!=null  and appGamePageDTO.endTime!=''">
                <![CDATA[   AND DATE_FORMAT(a.application_end_time, '%Y-%m-%d')<=  DATE_FORMAT(#{appGamePageDTO.endTime}, '%Y-%m-%d') ]]>
            </if>
            <if test="appGamePageDTO.startTime!=null  and appGamePageDTO.startTime!=''">
                <![CDATA[   AND DATE_FORMAT(a.application_end_time, '%Y-%m-%d')>=  DATE_FORMAT(#{appGamePageDTO.startTime}, '%Y-%m-%d') ]]>
            </if>
            <if test="appGamePageDTO.orgCode != null and appGamePageDTO.orgCode != ''">
                AND a.org_code LIKE CONCAT(#{appGamePageDTO.orgCode},'%')
            </if>
        </if>
        group by  a.id
        order by a.application_end_time desc
    </select>
    <select id="findConcerts" resultType="org.jeecg.modules.app.vo.game.FindPagResponse">
        <![CDATA[
        select
        a.id,a.cover,a.name,a.start_time,a.end_time,MIN(b.selling_price) as min_price,
        IFNULL(d.name, a.address) as address
        ,a.application_end_time
        ,CASE
        WHEN a.application_end_time <= NOW()
        THEN 1
        ELSE 0
        END AS is_entry
        , ST_Distance_Sphere(
        POINT(#{longitude}, #{latitude}),
        POINT(IFNULL(d.longitude, a.longitude),IFNULL(d.latitude, a.latitude))
        ) /1000 AS km,
        CASE
        WHEN EXISTS (SELECT 1 FROM nm_game_price_rules WHERE game_id = a.id AND del_flag = 0 AND type = 0)
        AND EXISTS (SELECT 1 FROM nm_game_price_rules WHERE game_id = a.id AND del_flag = 0 AND type = 1)
        THEN 2
        ELSE MAX(b.type)
        END AS type
        from nm_game a
        left JOIN nm_game_price_rules b on a.id = b.game_id and b.del_flag=0
        LEFT JOIN nm_game_schedule c on a.id = c.game_id and c.del_flag=0
        left join nm_site d on  a.site_id = d.id and d.del_flag=0
        where a.del_flag=0
        GROUP BY  a.id
        ORDER BY a.application_end_time DESC
        ]]>
    </select>

    <select id="findById" resultMap="gameResultMap">
        <![CDATA[
        select
            a.id ,a.cover ,a.name,a.start_time ,a.end_time ,a.organizers,a.reminder,
            a.background_image,f.mobile,IFNULL(d.longitude, a.longitude) as longitude,IFNULL(d.latitude, a.latitude) as latitude,
            IFNULL(d.name, a.address) as address
            ,a.application_end_time as application_end_time,
            CASE
                WHEN a.application_end_time < NOW() THEN 1
                ELSE 0
                END AS is_entry,
            ST_Distance_Sphere(
                    POINT(#{longitude}, #{latitude}),
                    POINT(IFNULL(d.longitude, a.longitude),IFNULL(d.latitude, a.latitude))
                ) /1000 AS km,
            CASE
                WHEN EXISTS (SELECT 1 FROM nm_game_price_rules WHERE game_id = a.id AND del_flag = 0 AND type = 0)
                    AND EXISTS (SELECT 1 FROM nm_game_price_rules WHERE game_id = a.id AND del_flag = 0 AND type = 1)
                    THEN 3
                ELSE MAX(b.type)
                END AS type,
            c.name as c_name ,c.start_time as c_start_time,c.end_time as c_end_time,c.arrange as c_arrange,
            CASE
                WHEN NOW() < c.start_time THEN '未开始'
                WHEN NOW() >= c.end_time THEN '已结束'
                ELSE '进行中'
                END AS c_startStatus,c.id as c_id,
        e.name as b_category_name,b.category_id as b_category_id,b.id as b_id,b.selling_price as b_selling_price,b.people_num as b_people_num
        from nm_game a
        left JOIN nm_game_price_rules b on a.id = b.game_id and b.del_flag=0
        LEFT JOIN nm_game_schedule c on a.id = c.game_id and c.del_flag=0
        left join nm_site d on  a.site_id = d.id and d.del_flag=0
        left join nm_category e on  b.category_id = e.id and e.del_flag=0
        left join sys_depart f on  a.org_code = f.org_code and f.del_flag=0
        WHERE a.del_flag=0 and a.id =#{id}
        GROUP BY  a.id,b.id,c.id
        ORDER BY a.application_end_time DESC,c_start_time
        ]]>
    </select>

    <select id="findByGameId" resultMap="findByGameIdPriceVoMap">
        select a.id,a.aptitudes,b.type,a.insure_ids,
        CASE
        WHEN b.type = 0 THEN '个人赛项目'
        WHEN b.type = 1 THEN '团队赛项目'
        ELSE '未知类型项目'
        END AS type_name
        from  nm_game a
        LEFT JOIN nm_game_price_rules b on a.id = b.game_id and b.del_flag=0
        where a.del_flag=0 and a.id=#{id}
        group by a.id, b.type
    </select>

    <select id="exportCondition" resultType="org.jeecg.modules.system.app.vo.ExportConditionOutVO">
        SELECT
            d.`name`,
            b.type,
            e.NAME AS projectName,
            a.game_certification,
            b.user_name,
            b.user_phone
        FROM
            nm_order a
                INNER JOIN nm_order_pro_info b ON a.id = b.order_id
                AND (b.type = 3 OR b.type = 4)
                LEFT JOIN nm_game_price_rules c ON a.product_ids = c.id
                LEFT JOIN nm_game d ON c.game_id = d.id
                LEFT JOIN nm_category e ON c.category_id = e.id
        WHERE 1=1
        <if test="exportConditionDTO.gameId != null and exportConditionDTO.gameId != '' and exportConditionDTO.gameId != 'ALL'">
            AND c.game_id = {exportConditionDTO.gameId}
        </if>
        <if test="exportConditionDTO.projectId != null and exportConditionDTO.projectId != '' and exportConditionDTO.projectId != 'ALL'">
            AND c.category_id = {exportConditionDTO.projectId}
        </if>
        <if test="exportConditionDTO.startTime != null and exportConditionDTO.startTime != ''">
            AND d.start_time <![CDATA[ >= ]]> #{exportConditionDTO.startTime}
        </if>
        <if test="exportConditionDTO.endTime != null and exportConditionDTO.endTime != ''">
            AND d.end_time <![CDATA[ <= ]]> #{exportConditionDTO.endTime}
        </if>
    </select>
</mapper>