<?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.AppCoursesMapper">
    <select id="convertSearchTrainVOPage" resultType="org.jeecg.modules.app.vo.SearchVO$SearchTrainVO">
        SELECT
            a.id,
            a.`name`,
            b.address,
            a.cover,
            a.original_price,
            a.selling_price,
            COALESCE(d.sales, 0) AS sales,
            a.good_rate,
            a.start_time,
            a.end_time,
            b.id AS siteId
        FROM
            nm_courses a
                LEFT JOIN nm_site b ON a.tenant_id = b.tenant_id
                LEFT JOIN (
                SELECT
                    product_id,
                    COUNT(id) AS sales
                FROM
                    nm_order_product
                GROUP BY
                    product_id
            ) d ON a.id = d.product_id WHERE 1 = 1
        <if test="searchDTO.keyword != null and searchDTO.keyword != ''">
            AND (a.name LIKE CONCAT('%',#{searchDTO.keyword},'%') OR b.address LIKE CONCAT('%',#{searchDTO.keyword},'%'))
        </if>
        <if test="searchDTO.venueType != null and searchDTO.venueType == '1-2'">
            ORDER BY  a.good_rate DESC
        </if>
        <if test="searchDTO.venueType != null and searchDTO.venueType == '1-3'">
            ORDER BY sales DESC
        </if>
    </select>
    <select id="getCourseInfo" resultType="org.jeecg.modules.app.vo.CourseInfoVO">
        SELECT
            a.id,
            a.NAME,
            a.original_price,
            a.selling_price,
            a.cover,
            a.video,
            b.address,
            a.details,
            a.site_id,
            a.insure_ids,
            a.fit_people,
            a.start_time,
            a.end_time,
            COUNT( c.id ) AS classHour,
            a.limit_num,
            a.reminder,
            a.address_site_id,
            a.user_id AS instructorId,
            d.username AS instructorName,
            d.avatar AS instructorAvatar,
            b.longitude,
            b.latitude
        FROM
            nm_courses a
                LEFT JOIN nm_site b ON a.site_id = b.id
                LEFT JOIN nm_courses_price_rules c ON a.id = c.courses_id LEFT JOIN sys_user d ON d.id = a.user_id
        WHERE
            a.STATUS = 0
          AND a.del_flag = 0
          AND a.id = #{id}
        GROUP BY
            a.id,
            a.NAME,
            a.original_price,
            a.selling_price,
            a.cover,
            a.video,
            b.address,
            a.start_time,
            a.end_time,
            a.limit_num,
            a.reminder,
            instructorId,
            instructorName,
            instructorAvatar;
    </select>
    <select id="findByCourseList" resultType="org.jeecg.modules.app.vo.course.CourseResponseVo">
        select b.`name`,e.address,b.original_price,b.selling_price
             ,b.id,b.price_type,b.good_rate,e.latitude,e.longitude,count(c.courses_id) as coursesNum
        FROM nm_courses b
        LEFT JOIN nm_courses_price_rules c on b.id= c.courses_id
        LEFT JOIN nm_site e on e.id = b.address_site_id
        where 1=1
        <if test="courseRequestVo.instructorId != null and courseRequestVo.instructorId !=''">
            and  b.user_id=#{courseRequestVo.instructorId}
        </if>
        <if test="courseRequestVo.siteId != null and courseRequestVo.siteId !=''">
            and  b.address_site_id=#{courseRequestVo.siteId}
        </if>
        <if test="courseRequestVo.priceType != null">
            and  b.price_type=#{courseRequestVo.priceType}
        </if>
        GROUP BY b.id
    </select>
    <select id="findByLaLoList" resultType="org.jeecg.modules.app.vo.course.CourseResponseVo">
        select b.`name`,e.address,b.original_price,b.selling_price,b.cover
             ,b.id,b.price_type,ROUND(b.good_rate * 100)  as  good_rate ,e.latitude,e.longitude,count(c.courses_id) as coursesNum,
               ST_Distance_Sphere(
                       POINT(#{courseRequestVo.longitude}, #{courseRequestVo.latitude}),
                       POINT(e.longitude, e.latitude)
                   ) /1000 AS km
        FROM nm_courses b
        LEFT JOIN nm_courses_price_rules c on b.id= c.courses_id
        LEFT JOIN nm_site e on e.id = b.address_site_id
        where 1=1
        <if test="courseRequestVo.instructorId != null and courseRequestVo.instructorId !=''">
            and  b.user_id=#{courseRequestVo.instructorId}
        </if>
        <if test="courseRequestVo.siteId != null and courseRequestVo.siteId !=''">
            and  b.address_site_id=#{courseRequestVo.siteId}
        </if>
        <if test="courseRequestVo.priceType != null">
            and  b.price_type=#{courseRequestVo.priceType}
        </if>
        GROUP BY b.id
        <if test="courseRequestVo.orderByType != null and courseRequestVo.orderByType == '1'">
            ORDER BY km DESC
        </if>
        <if test="courseRequestVo.orderByType != null and courseRequestVo.orderByType == '2'">
            ORDER BY b.create_time DESC
        </if>
        <if test="courseRequestVo.orderByType != null and courseRequestVo.orderByType == '0'">
            ORDER BY b.create_time DESC
        </if>
        <if test="courseRequestVo.orderByType != null and courseRequestVo.orderByType == '0'">
            ORDER BY b.good_rate DESC
        </if>
    </select>
    <select id="findByList" resultType="org.jeecg.modules.app.vo.course.CourseResponseVo">
        select b.`name`,e.address,b.original_price,b.selling_price
        ,b.id,b.price_type,b.good_rate,e.latitude,e.longitude,count(c.courses_id) as coursesNum
        FROM nm_courses b
        LEFT JOIN nm_courses_price_rules c on b.id= c.courses_id
        LEFT JOIN nm_site e on e.id = b.address_site_id
        where 1=1
        <if test="courseRequestVo.instructorId != null and courseRequestVo.instructorId !=''">
            and  b.user_id=#{courseRequestVo.instructorId}
        </if>
        <if test="courseRequestVo.siteId != null and courseRequestVo.siteId !=''">
            and  b.address_site_id=#{courseRequestVo.siteId}
        </if>
        <if test="courseRequestVo.priceType != null">
            and  b.price_type=#{courseRequestVo.priceType}
        </if>
        GROUP BY b.id
        <if test="courseRequestVo.orderByType != null and courseRequestVo.orderByType == '1'">
            ORDER BY b.create_time DESC
        </if>
        <if test="courseRequestVo.orderByType != null and courseRequestVo.orderByType == '2'">
            ORDER BY b.create_time DESC
        </if>
        <if test="courseRequestVo.orderByType != null and courseRequestVo.orderByType == '0'">
            ORDER BY b.create_time DESC
        </if>
    </select>
    <select id="queryWitchPriceRulesPage" resultType="org.jeecg.modules.system.app.dto.AppCoursesInfoPageDTO">
        SELECT
            c.id,
            c.`name`,
            c.cover,
            c.start_time,
            c.end_time,
            s.address,
            c.original_price,
            c.selling_price,
            c.racking_status,
            c.courses_status,
            c.category_id,
            c.create_time
        FROM
            `nm_courses` c
            left JOIN nm_site s ON c.address_site_id = s.id
        WHERE 1=1 and c.del_flag = '0'
        <if test="dto.orgCode != null and dto.orgCode != ''">
            AND c.org_code LIKE CONCAT(#{ dto.orgCode },'%')
        </if>
        <if test="dto.name != null and dto.name != ''">
            AND c.name LIKE CONCAT('%',#{ dto.name },'%')
        </if>
        <if test="dto.rackingStatus != null ">
            AND c.racking_status = #{ dto.rackingStatus }
        </if>
        <if test="dto.coursesStatus != null ">
            AND c.courses_status = #{ dto.coursesStatus }
        </if>
        <if test="dto.categoryId != null">
            AND c.category_id LIKE CONCAT('%',#{ dto.categoryId },'%')
        </if>
        ORDER BY c.create_time DESC
    </select>
    <select id="findByInstructorId" resultType="java.lang.String">
        select  b.id  from nm_instructor a
         left join  nm_courses  b   on a.user_id =b.user_id and a.del_flag =0
         where a.id =#{instructorId}
    </select>
    <select id="getPageCourse" resultType="org.jeecg.modules.app.vo.AppCoursesPageVO">
        SELECT
            c.id AS appCourseId,
            s.address AS siteAddress,
            c.cover,
            c.name,
            c.class_num,
            c.start_time,
            c.end_time
        FROM
            nm_courses c
            LEFT JOIN nm_site s ON c.address_site_id = s.id
        <where> c.del_flag = 0
            <if test="orgCode != null and orgCode != ''">
                and c.org_code = #{orgCode}
            </if>
            <if test="userId != null and userId != ''">
                and c.user_id = #{userId}
            </if>
        </where>
        ORDER BY c.create_time DESC
    </select>

</mapper>