<?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.attendance.mapper.AttendanceRuleMapper">

  <resultMap id="baseMap" type="com.yida.data.attendance.vo.AttendanceRuleVO">
    <id column="id" property="id"/>
    <result column="create_id" property="createId"/>
    <result column="create_date" property="createDate"/>
    <result column="update_id" property="updateId"/>
    <result column="update_date" property="updateDate"/>
    <result column="del_flag" property="delFlag"/>
    <result column="school_id" property="schoolId"/>
    <result column="name" property="name"/>
    <result column="user_type" property="userType"/>
    <result column="type" property="type"/>
    <result column="week" property="week"/>
    <result column="holiday_attendance" property="holidayAttendance"/>
    <result column="role_rule" property="roleRule"/>
    <result column="role_user" property="roleUser"/>
    <result column="role_date" property="roleDate"/>
    <result column="role_remind" property="roleRemind"/>
    <result column="photo_take" property="photoTake"/>
    <result column="face_take" property="faceTake"/>
    <result column="photo_remark" property="photoRemark"/>
    <result column="apply_amendment" property="applyAmendment"/>
    <result column="reissue_num" property="reissueNum"/>
    <result column="reissue_end_date" property="reissueEndDate"/>
    <result column="status" property="status"/>
    <result column="start_date" property="startDate"/>
    <result column="end_date" property="endDate"/>

    <collection property="remindDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleRemindDTO"
      select="selectRemind"
      column="id">
    </collection>
    <collection property="attendanceRuleObjList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleObjDTO"
      select="selectObj"
      column="id">
    </collection>

    <collection property="specialDateDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleSpecialDateDTO"
      select="selectSpecialDate"
      column="id">
    </collection>

    <collection property="whiteListDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleWhiteListDTO"
      select="selectWhiteList"
      column="id">
    </collection>

    <collection property="deviceDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleDeviceDTO"
      select="selectDevice"
      column="id">
    </collection>

    <collection property="wifiDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleWifiDTO"
      select="selectWifi"
      column="id">
    </collection>

    <collection property="addressDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleAddressDTO"
      select="selectAddress"
      column="id">
    </collection>

    <collection property="timeDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleTimeDTO"
      select="selectTime"
      column="id">
    </collection>
    <collection property="adminDTOList"
      ofType="com.yida.data.attendance.dto.AddAttendanceRuleAdminDTO"
      select="selectAdmin"
      column="id">
    </collection>
  </resultMap>


  <select id="selectRemind"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleRemindDTO">
    select *
    from attendance_rule_remind
    where rule_id = #{id}
  </select>
  <select id="selectObj"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleObjDTO">
    select *
    from attendance_rule_obj
    where rule_id = #{id}
  </select>

  <select id="selectSpecialDate"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleSpecialDateDTO">
    select *
    from attendance_rule_special_date
    where rule_id = #{id}
  </select>

  <select id="selectWhiteList"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleWhiteListDTO">
    select *
    from attendance_rule_white_list
    where rule_id = #{id}
      and del_flag = 0
  </select>


  <select id="selectDevice"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleDeviceDTO">
    select *
    from attendance_rule_device
    where rule_id = #{id}
  </select>

  <select id="selectWifi"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleWifiDTO">
    select *
    from attendance_rule_wifi
    where rule_id = #{id}
  </select>

  <select id="selectAddress"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleAddressDTO">
    select *
    from attendance_rule_address
    where rule_id = #{id}
  </select>

  <select id="selectTime"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleTimeDTO">
    select *
    from attendance_rule_time
    where rule_id = #{id}
  </select>

  <select id="selectAdmin"
    resultType="com.yida.data.attendance.dto.AddAttendanceRuleAdminDTO">
    select *
    from attendance_rule_admin
    where rule_id = #{id}
  </select>


  <select id="getRule" resultMap="baseMap">
    select *
    from attendance_rule
    where del_flag = 0
      and id = #{id}
  </select>

  <select id="listRulePage" resultMap="baseMap">
    select eau.*
    from attendance_rule eau
    <where>
      del_flag = 0
      <if test="schoolId != null">
        and eau.school_id=#{schoolId}
      </if>
      <if test="ruleName != null and ruleName != ''">
        and eau.name like concat('%',#{ruleName},'%')
      </if>
      <if test="type != null">
        and eau.type=#{type}
      </if>
      <if test="userType != null">
        and eau.user_type=#{userType}
      </if>
      <if test="status != null">
        and eau.status=#{status}
      </if>
    </where>
    order by eau.create_date desc
  </select>

  <select id="listRule" resultMap="baseMap">
    select eau.*
    from attendance_rule eau
    <where>
      del_flag = 0
      <if test="schoolId != null">
        and eau.school_id=#{schoolId}
      </if>
      <if test="ruleName != null and ruleName != ''">
        and eau.name like concat('%',#{ruleName},'%')
      </if>
      <if test="type != null">
        and eau.type=#{type}
      </if>
      <if test="userType != null">
        and eau.user_type=#{userType}
      </if>
      <if test="status != null">
        and eau.status=#{status}
      </if>
    </where>
    order by eau.create_date desc
  </select>

  <select id="listRuleAttendanceUser" resultType="com.yida.data.attendance.vo.AttendanceRuleUserVO">
    SELECT * FROM attendance_rule_obj_user
    <where>
      <if test="dto.userType != null">
        and user_type=#{dto.userType}
      </if>
      <if test="dto.ruleId != null">
        and rule_id=#{dto.ruleId}
      </if>
      <if test="dto.userIdList != null and dto.userIdList.size()>0">
        and user_id in
        <foreach collection="dto.userIdList" item="item" separator="," close=")" open="(">
          #{item}
        </foreach>
      </if>
    </where>
  </select>

  <select id="listRuleAttendanceAdminUser" resultType="com.yida.data.attendance.vo.AttendanceRuleAdminUserVO">
    SELECT * FROM attendance_rule_admin
    <where>
      <if test="dto.ruleId != null">
        and rule_id=#{dto.ruleId}
      </if>
      <if test="dto.staffId != null">
        and staff_id=#{dto.staffId}
      </if>
      <if test="dto.staffIdList != null and dto.staffIdList.size()>0">
        and staff_id in
        <foreach collection="dto.staffIdList" item="item" separator="," close=")" open="(">
          #{item}
        </foreach>
      </if>
      <if test="dto.deptIdList != null and dto.deptIdList.size()>0">
        and dept_id in
        <foreach collection="dto.deptIdList" item="item" separator="," close=")" open="(">
          #{item}
        </foreach>
      </if>
    </where>
  </select>

  <select id="checkNormalRule" resultType="com.yida.data.attendance.vo.ConflictRuleVO">
    select
    a.id ruleId,
    a.`name` ruleName,
    b.dept_id deptId,
    b.dept_name deptName
    from attendance_rule a
    left join attendance_rule_obj b on a.id = b.rule_id
    where a.del_flag=0 and a.status = 1
    and a.user_type= #{userType}
    and a.start_date  <![CDATA[>=]]> #{startDate}
    and a.end_date  <![CDATA[<=]]> #{endDate}
    <if test="ruleId != null">
      and a.id!=#{ruleId}
    </if>
    and b.dept_id in
    <foreach collection="deptIds" close=")" item="item" open="(" separator=",">
      #{item}
    </foreach>
    and a.week like concat('%',#{week},'%')
  </select>


  <select id="findUserMatchRuleByDevice" resultMap="baseMap">
    SELECT DISTINCT a.*
    FROM attendance_rule a
    LEFT JOIN attendance_rule_obj b ON a.id = b.rule_id
    LEFT JOIN attendance_rule_device e ON a.id = e.rule_id
    where a.del_flag=0 and a.status = 1
    <if test="deptIdList != null and deptIdList.size() != 0">
      and b.dept_id in
      <foreach collection="deptIdList" item="item" separator="," close=")" open="(">
        #{item}
      </foreach>
    </if>
    <if test="userType != null">
      and b.user_type=#{userType}
    </if>
    <if test="schoolId != null">
      and a.school_id=#{schoolId}
    </if>
    <if test="deviceSn != null">
      and e.device_sn=#{deviceSn}
    </if>
    and a.start_date <![CDATA[<=]]> #{currentDate}
    and a.end_date <![CDATA[>=]]> #{currentDate}
    and a.week like concat('%',#{dayOfWeek},'%')
  </select>

  <select id="findUserMatchRuleByWifi" resultMap="baseMap">
    SELECT DISTINCT a.*
    FROM attendance_rule a
    LEFT JOIN attendance_rule_obj b ON a.id = b.rule_id
    LEFT JOIN attendance_rule_wifi e ON a.id = e.rule_id
    where a.del_flag=0 and a.status = 1
    <if test="deptIdList != null and deptIdList.size() != 0">
      and b.dept_id in
      <foreach collection="deptIdList" item="item" separator="," close=")" open="(">
        #{item}
      </foreach>
    </if>
    <if test="wifiName != null and wifiName!=''">
      and e.wifi_name=#{wifiName}
    </if>
    <if test="wifiBssid != null and wifiBssid!=''">
      and e.wifi_bssid=#{wifiBssid}
    </if>
    <if test="userType != null">
      and b.user_type=#{userType}
    </if>
    <if test="schoolId != null">
      and a.school_id=#{schoolId}
    </if>
    <if test="ip != null">
      and e.ip=#{ip}
    </if>
    and a.start_date <![CDATA[<=]]> #{currentDate}
    and a.end_date <![CDATA[>=]]> #{currentDate}
    <if test="dayOfWeek != null">
      and a.week like concat('%',#{dayOfWeek},'%')
    </if>
  </select>


  <select id="findUserMatchRuleByUserId" resultMap="baseMap">
    SELECT ru.*
    FROM attendance_rule ru
           LEFT JOIN attendance_rule_obj_user ruu ON ru.id = ruu.rule_id
    WHERE ruu.user_type = #{userType}
      AND ruu.user_id = #{userId}
      and ru.status = 1
      and ru.del_flag = 0
      and ru.start_date <![CDATA[<=]]> now()
      and ru.end_date <![CDATA[>=]]> now()
  </select>


  <select id="findByUserDeptListAndUserType" resultMap="baseMap">
    SELECT r.*
    FROM attendance_rule r
    LEFT JOIN attendance_rule_obj o on r.id = o.rule_id
    WHERE r.school_id = #{schoolId}
    AND r.del_flag = 0 and r.status = 1 and o.user_type=#{userType}
    <if test="deptIdList != null and deptIdList.size() != 0">
      and o.dept_id in
      <foreach collection="deptIdList" item="item" separator="," close=")" open="(">
        #{item}
      </foreach>
    </if>
  </select>


  <select id="findByUserIdAndUserType" resultMap="baseMap">
    SELECT r.*
    FROM attendance_rule r
           LEFT JOIN attendance_rule_obj_user o on r.id = o.rule_id
    WHERE r.del_flag = 0
      and r.status = 1
      and o.user_type = #{userType}
      and o.user_id = #{userId}
  </select>

  <select id="findUserManageRuleList" resultType="com.yida.data.attendance.vo.AttendanceRuleVO">
    SELECT DISTINCT ru.*
    FROM attendance_rule ru
           LEFT JOIN attendance_rule_admin admin ON ru.id = admin.rule_id
    WHERE ru.del_flag = 0
      AND ru.`status` = 1
      AND admin.staff_id = #{userId}
      AND admin.user_type = #{userType}
  </select>

  <select id="getSchoolIdList" resultType="java.lang.Long">
    SELECT DISTINCT school_id
    FROM attendance_rule
    WHERE del_flag = 0
      AND `status` = 1
  </select>

  <select id="schoolId" resultType="com.yida.data.attendance.vo.AttendanceRuleVO">

  </select>
</mapper>
