<?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.AppInstructorMapper">

    <resultMap id="appCoachDetailsVOResult" type="org.jeecg.modules.app.vo.coach.AppCoachDetailsVO">
        <result property="id" column="id"/>
        <result property="realname" column="realname"/>
        <result property="trainingPrograms" column="training_programs"/>
        <result property="goodRate" column="good_rate"/>
        <result property="classesNumber" column="classesNumber"/>
        <result property="teachingPhilosophy" column="teaching_philosophy"/>
        <result property="excelMsg" column="excel_msg"/>
        <result property="certificateInnocence" column="certificate_innocence"/>
        <result property="healthy" column="healthy"/>
        <result property="honor" column="honor"/>
        <collection property="courseInfoVOList" ofType="org.jeecg.modules.app.vo.stadium.ConcertsDetailsVO" javaType="java.util.List">
            <result property="id" column="id" />
            <result property="startTime" column="start_time" />
            <result property="endTime" column="end_time"/>
            <result property="concertStatus" column="concertStatus" />
            <result property="price" column="selling_price" javaType="java.math.BigDecimal"/>
        </collection>
    </resultMap>

    <resultMap id="staffVOResult" type="org.jeecg.modules.system.app.vo.staff.StaffVO">
        <result property="staffId" column="staffId"/>
        <result property="instructorId" column="instructorId"/>
        <result property="backgroundImg" column="background_img"/>
        <result property="username" column="username"/>
        <result property="realname" column="realname"/>
        <result property="status" column="status"/>
        <result property="password" column="password"/>
        <result property="trainingProgramsList" column="training_programs"/>
        <result property="teachingPhilosophy" column="teaching_philosophy"/>
        <result property="excelMsg" column="excel_msg"/>
        <result property="certificateInnocence" column="certificate_innocence"/>
        <result property="healthy" column="healthy"/>
        <result property="honorList" column="honor"/>
        <result property="selecteddeparts" column="selecteddeparts"/>
        <result property="selectedroles" column="selectedroles"/>
<!--        <collection property="selecteddeparts" ofType="java.lang.String" javaType="java.util.List">-->
<!--            <result column="deptId" />-->
<!--        </collection>-->
<!--        <collection property="selectedroles" ofType="java.lang.String" javaType="java.util.List">-->
<!--            <result  column="roleId" />-->
<!--        </collection>-->
<!--        <collection property="selecteddepartsList"-->
<!--                column="staffId"-->
<!--        select="selectDeptIdsByUserId"-->
<!--        ofType="java.lang.String"-->
<!--         javaType="java.util.List"/>-->

<!--        &lt;!&ndash; 通过子查询加载角色ID列表 &ndash;&gt;-->
<!--        <collection-->
<!--                property="selectedrolesList"-->
<!--                column="staffId"-->
<!--                select="selectRoleIdsByUserId"-->
<!--                ofType="java.lang.String"-->
<!--                javaType="java.util.List" />-->
    </resultMap>
    <update id="updateDelStatus">
        UPDATE nm_instructor SET update_by = #{appInstructor.updateBy}, update_time =  #{appInstructor.updateTime} ,del_flag = 1 WHERE id = #{appInstructor.id}  AND del_flag = 0
    </update>
    <select id="convertSearchInstructorVOPage" resultType="org.jeecg.modules.app.vo.SearchVO$SearchInstructorVO">
        SELECT
        a.id,
        b.realname AS name,
        b.avatar,
        s.name AS orgName,
        a.excel_msg,
        a.teaching_philosophy,
        a.good_rate,
        a.classes_number,
        b.id AS userId,
        a.org_code AS orgCode,
        COUNT(DISTINCT d.user_id, c2.id) AS orderCount,
        a.training_programs
        FROM nm_instructor a
        LEFT JOIN sys_user b ON a.user_id = b.id
        LEFT JOIN sys_depart c ON b.org_code = c.org_code
        LEFT JOIN nm_site s ON c.org_code = s.org_code
        LEFT JOIN nm_courses d ON a.user_id = d.user_id
        LEFT JOIN nm_order_pro_info c2 ON d.user_id = c2.family_user_id
        WHERE 1=1
        <if test="searchDTO.keyword != null and searchDTO.keyword != ''">
            AND b.realname LIKE CONCAT('%',#{searchDTO.keyword},'%')
        </if>
        GROUP BY a.id
        ORDER BY orderCount DESC
    </select>
    <select id="findCoachList" resultType="org.jeecg.modules.app.vo.coach.AppCoachVO">
        select a.user_id as id,b.realname,a.training_programs,b.avatar  from nm_instructor a
            left join  sys_user b on a.user_id = b.id
    </select>
    <select id="findById" resultType="org.jeecg.modules.app.vo.coach.AppCoachDetailsVO">
        select a.id,a.user_id,b.realname,a.training_programs,a.good_rate,a.teaching_philosophy,a.excel_msg,b.avatar,a.background_img,
               b.certificate_innocence,b.healthy,b.honor  from nm_instructor a left join  sys_user b on a.user_id = b.id where a.user_id = #{id}

    </select>
    <select id="findPage" resultType="org.jeecg.modules.system.app.vo.staff.StaffResponseVO">
        SELECT
        a.id,
        a.username,
        a.realname,
        a.status,
        a.create_time,
        GROUP_CONCAT(c.depart_name SEPARATOR ',') AS depart_name
        FROM sys_user a
        LEFT JOIN sys_user_depart b ON a.id = b.user_id
        LEFT JOIN sys_depart c ON b.dep_id = c.id
        AND c.id IN (SELECT id FROM sys_depart WHERE org_code LIKE CONCAT('', #{orgCode}, '%'))
        LEFT JOIN nm_instructor d ON a.id = d.user_id AND d.del_flag = 0
        WHERE a.del_flag = 0
        AND c.id IN (SELECT id FROM sys_depart WHERE org_code LIKE CONCAT('', #{orgCode}, '%'))
        <if test="staffRequestVO != null and staffRequestVO != ''">
            <if test="staffRequestVO.deptId != null and staffRequestVO.deptId != ''">
                AND c.id = #{staffRequestVO.deptId}
            </if>
            <if test="staffRequestVO.phone != null and staffRequestVO.phone != ''">
                AND a.phone = #{staffRequestVO.phone}
            </if>
            <if test="staffRequestVO.username != null and staffRequestVO.username != ''">
                AND a.username = #{staffRequestVO.username}
            </if>
            <if test="staffRequestVO.status != null and staffRequestVO.status != ''">
                AND a.status = #{staffRequestVO.status}
            </if>
            <if test="staffRequestVO.endTime!=null  and staffRequestVO.endTime!=''">
                <![CDATA[   AND DATE_FORMAT(a.create_time, '%Y-%m-%d')<=  DATE_FORMAT(#{staffRequestVO.endTime}, '%Y-%m-%d') ]]>
            </if>
            <if test="staffRequestVO.startTime!=null  and staffRequestVO.startTime!=''">
                <![CDATA[   AND DATE_FORMAT(a.create_time, '%Y-%m-%d')>=  DATE_FORMAT(#{staffRequestVO.startTime}, '%Y-%m-%d') ]]>
            </if>
        </if>
        GROUP BY a.id
        ORDER BY a.create_time DESC
    </select>
    <select id="findByStaffId"  resultMap="staffVOResult">
        SELECT
            a.id AS staffId,
            b.id AS instructorId,
            a.username,
            a.realname,
            a.status,
            NULL AS password,
            b.training_programs,
            b.teaching_philosophy,
            b.excel_msg,
            a.certificate_innocence,
            a.healthy,
            a.honor,
            b.background_img,
            (SELECT GROUP_CONCAT(DISTINCT c.dep_id ORDER BY c.dep_id SEPARATOR ',')
             FROM sys_user_depart c
             WHERE c.user_id = a.id) AS	selecteddeparts,
            (SELECT GROUP_CONCAT(DISTINCT d.role_id ORDER BY d.role_id SEPARATOR ',')
             FROM sys_user_role d
             WHERE d.user_id = a.id) AS selectedroles
        FROM sys_user a
        LEFT JOIN  nm_instructor b ON a.id = b.user_id AND b.del_flag = 0
        where a.id=#{id} and a.del_flag=0
    </select>

<!--    <select id="findByStaffId" resultMap="staffVOResult">-->
<!--        SELECT-->
<!--            a.id AS staffId,-->
<!--            b.id AS instructorId,-->
<!--            a.username,-->
<!--            a.realname,-->
<!--            a.status,-->
<!--            a.password,-->
<!--            b.training_programs,-->
<!--            b.teaching_philosophy,-->
<!--            b.excel_msg,-->
<!--            a.certificate_innocence,-->
<!--            a.healthy,-->
<!--            a.honor,-->
<!--            b.background_img-->
<!--        FROM sys_user a-->
<!--                 LEFT JOIN nm_instructor b ON a.id = b.user_id AND b.del_flag = 0-->
<!--        WHERE a.id = #{id} AND a.del_flag = 0-->
<!--    </select>-->

<!--    &lt;!&ndash; 单独查询部门ID列表 &ndash;&gt;-->
<!--    <select id="selectDeptIdsByUserId" resultType="string">-->
<!--        SELECT c.id FROM sys_user_depart c-->
<!--        WHERE c.user_id = #{userId}-->
<!--    </select>-->

<!--    &lt;!&ndash; 单独查询角色ID列表 &ndash;&gt;-->
<!--    <select id="selectRoleIdsByUserId" resultType="string">-->
<!--        SELECT d.id FROM sys_user_role d-->
<!--        WHERE d.user_id = #{userId}-->
<!--    </select>-->

</mapper>