<?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.yida.data.user.mapper.EduFaceGroupUserMapper">
<!--  <select id="listUserFacePage" resultType="com.yida.data.user.vo.UserFacePageVO">-->
<!--    select a.id                                      face_id,-->
<!--           if(a.user_type = 0, c.id, b.id)           user_id,-->
<!--           if(a.user_type = 0, c.stu_name, b.`name`) name,-->
<!--           d.pic_path,-->
<!--           c.stu_number,-->
<!--           b.mobile,-->
<!--           if(a.user_type = 0, c.stu_sex, b.sex)     sex,-->
<!--           a.user_type,-->
<!--           a.status,-->
<!--           if(a.user_type = 0, concat((select DEPT_NAME from edu_user_dept where c.grade_id = DEPT_ID),-->
<!--                                      (select DEPT_NAME from edu_user_dept where c.class_id = DEPT_ID)),-->
<!--              group_concat(e.dept_name))             dept-->
<!--      from edu_face_group_user a-->
<!--      left join (select *-->
<!--                 from edu_staff where del_flag = 0-->
<!--    <if test="dto.keyword != null and dto.keyword != ''">-->
<!--      and ((name like concat('%', #{dto.keyword}, '%'))-->
<!--        or (mobile like concat('%', #{dto.keyword}, '%')))-->
<!--    </if>-->
<!--    ) b-->
<!--      on a.user_id = b.id and a.user_type = 1-->
<!--      left join (select *-->
<!--                 from edu_student where del_flag = 0-->
<!--    <if test="dto.keyword != null and dto.keyword != ''">-->
<!--      and ((stu_name like concat('%', #{dto.keyword}, '%'))-->
<!--        or (stu_number like concat('%', #{dto.keyword}, '%')))-->
<!--    </if>) c-->
<!--    on a.user_id = c.id and a.user_type = 0-->
<!--    left join edu_user_face d on a.user_id = d.user_id and a.user_type = d.user_type-->
<!--    left join edu_staff_dept e on a.user_id = e.staff_id and a.user_type = 1-->
<!--    <where>-->
<!--      <if test="dto.status != null and dto.status.size() > 0">-->
<!--        and a.status in-->
<!--        <foreach collection="dto.status" close=")" item="item" open="(" separator=",">-->
<!--          #{item}-->
<!--        </foreach>-->
<!--      </if>-->
<!--      <if test="dto.groupId != null">-->
<!--        and a.face_group_id = #{dto.groupId}-->
<!--      </if>-->
<!--      <if test="dto.userType != null">-->
<!--        and a.user_type = #{dto.userType}-->
<!--      </if>-->
<!--      <if test="dto.onlyShowImport != null and dto.onlyShowImport == 0">-->
<!--        and d.id is not null-->
<!--      </if>-->
<!--    </where>-->
<!--    group by a.id, a.user_id, a.user_type, a.status, d.pic_path-->
<!--    order by a.id desc-->
<!--  </select>-->
  
  <resultMap id="userFaceMap" type="com.yida.data.user.vo.UserFacePageVO">
    <id column="face_id" property="faceId"/>
    <result column="user_id" property="userId"/>
    <result column="name" property="name"/>
    <result column="pic_path" property="picPath"/>
    <result column="stu_number" property="stuNumber"/>
    <result column="mobile" property="mobile"/>
    <result column="sex" property="sex"/>
    <result column="user_type" property="userType"/>
    <result column="status" property="status"/>
    
    <association property="dept" select="getDeptName"
            javaType="java.lang.String" jdbcType="VARCHAR"
            column="{userId=user_id,userType=user_type}" />
  </resultMap>
  
  <select id="getDeptName" resultType="string">
    <if test="userType == 0">
      SELECT
        CONCAT(grade.DEPT_NAME, class.DEPT_NAME)
      FROM
        edu_student s
      LEFT JOIN edu_user_dept grade ON s.grade_id = grade.DEPT_ID
      LEFT JOIN edu_user_dept class ON s.class_id = class.DEPT_ID
      WHERE
        id = #{userId}
    </if>
    <if test="userType == 1">
      SELECT
        GROUP_CONCAT(dept_name)
      FROM
        edu_staff_dept
      WHERE
        staff_id = #{userId}
    </if>
  </select>
  
  <select id="listUserFacePage" resultMap="userFaceMap">
    SELECT
      a.*,
      f.pic_path
    FROM
      (
        <if test="dto.userType != 0">
          SELECT
            u.id AS face_id,
            es.id AS user_id,
            es.NAME AS NAME,
            es.sex AS sex,
            u.`status` AS `status`,
            u.user_type AS user_type,
            '' AS stu_number,
            es.mobile AS mobile
          FROM
            edu_staff es
          LEFT JOIN edu_face_group_user u ON ( es.id = u.user_id AND u.user_type = 1 )
          WHERE
            es.del_flag = 0
          AND u.user_type = 1
          AND u.face_group_id = #{dto.groupId}
          <if test="dto.status != null">
            AND u.status = #{dto.status}
          </if>
          <if test="dto.keyword != null and dto.keyword != ''">
            AND (
              ( es.NAME LIKE concat( '%', #{dto.keyword}, '%' ) )
              OR ( es.mobile LIKE concat( '%', #{dto.keyword}, '%' ) )
            )
          </if>
        </if>
        <if test="dto.userType == null">
          UNION ALL
        </if>
        <if test="dto.userType != 1">
          SELECT
            u.id AS face_id,
            es.id AS user_id,
            es.stu_name AS NAME,
            es.stu_sex AS sex,
            u.`status` AS `status`,
            u.user_type AS user_type,
            es.stu_number AS stu_number,
            '' AS moile
          FROM
            edu_student es
          LEFT JOIN edu_face_group_user u ON ( es.id = u.user_id AND u.user_type = 0 )
          WHERE
            es.del_flag = 0
          AND u.user_type = 0
          AND u.face_group_id = #{dto.groupId}
          <if test="dto.status != null">
            AND u.status = #{dto.status}
          </if>
          <if test="dto.keyword != null and dto.keyword != ''">
            AND (
              ( es.stu_name LIKE concat( '%', #{dto.keyword}, '%' ) )
              OR ( es.stu_number LIKE concat( '%', #{dto.keyword}, '%' ) )
            )
          </if>
        </if>
      ) AS a
    LEFT JOIN edu_user_face f ON ( a.user_id = f.user_id AND a.user_type = f.user_type )
    <if test="dto.onlyShowImport != null and dto.onlyShowImport == 0">
      and f.id IS NOT NULL
    </if>
    ORDER BY
      a.face_id DESC
  </select>

  <select id="listStudentNotImport" resultType="com.yida.data.common.core.entity.user.EduStudent">
    select a.*
    from edu_student a
      where a.del_flag = 0
        and a.school_id = #{schoolId}
        and a.id not in (select IFNULL(b.user_id, 0)
                         from edu_face_group_user b
                                inner join edu_face_group c on b.face_group_id = c.id
                         where c.id = #{groupId})
    <if test="keyword != null and keyword != ''">
      and (
        (a.stu_name like concat('%', #{keyword}, '%'))
          or (a.stu_number like concat('%', #{keyword}, '%'))
        )
    </if>
    <if test="classId != null and classId.size() != 0">
      and a.class_id in
      <foreach collection="classId" close=")" separator="," item="item" open="(">
        #{item}
      </foreach>
    </if>
    <if test="type != null">
      and a.type = #{type}
    </if>
    <if test="sex != null">
      and a.stu_sex = #{sex}
    </if>
    <if test="dormId != null">
      and a.dorm_id = #{dormId}
    </if>
    <if test="dormRoomId != null">
      and a.dorm_room_id = #{dormRoomId}
    </if>
    order by create_date desc
  </select>

  <select id="listStaffNotImport" resultType="com.yida.data.common.core.entity.user.EduStaff">
    select a.*
    from edu_staff a
      where a.del_flag = 0
        and a.school_id = #{schoolId}
        and a.id not in (select IFNULL(b.user_id, 0)
                         from edu_face_group_user b
                                inner join edu_face_group c on b.face_group_id = c.id
                         where c.id = #{groupId})
    <if test="keyword != null and keyword != ''">
      and (
        (a.name like concat('%', #{keyword}, '%'))
          or (a.mobile like concat('%', #{keyword}, '%'))
        )
    </if>
    <if test="sex != null">
      and a.sex = #{sex}
    </if>
    order by create_date desc
  </select>

  <select id="listUserFace" resultType="com.yida.data.common.core.entity.user.EduUserFace">
    select b.*, a.id faceGroupUserId
    from edu_face_group_user a
           inner join edu_user_face b on a.user_id = b.user_id and
                                         a.user_type = b.user_type
    <where>
      a.status in (0, 3)
        and
        a.id in
      <foreach collection="ids" open="(" item="item" separator="," close=")">
        #{item}
      </foreach>
    </where>
  </select>

  <select id="listUserFaceByGroup" resultType="com.yida.data.common.core.entity.user.EduUserFace">
    select b.*, a.id faceGroupUserId
    from edu_face_group_user a
           inner join edu_user_face b on a.user_id = b.user_id and
                                         a.user_type = b.user_type
    where a.status in (0, 3)
      and a.face_group_id = #{groupId}
      and b.id is not null
  </select>

  <update id="updateStatus">
    update edu_face_group_user
    set status=#{status}
      where
    <foreach collection="userList" item="item" separator="or">
      (
        user_id = #{item.userId}
          and user_type = #{item.userType}
          and face_group_id = #{item.faceGroupId}
        )
    </foreach>
  </update>
</mapper>
