<?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.EduAttendanceRuleMapper">
  <resultMap id="lisResult" type="com.yida.data.attendance.vo.RuleListVO">
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="status" property="status"/>
    <result column="type" property="type"/>
    <result column="create_date" property="createDate"/>
    <result column="create_name" property="createName"/>
    <result column="create_id" property="createId"/>
    <result column="school_id" property="schoolId"/>
    <result column="dormitory_flag" property="dormitoryFlag"/>

    <collection property="deptList"
      ofType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleDept"
      select="selectDept"
      column="id">
    </collection>

    <collection property="dateList"
      ofType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleDate"
      select="selectDate" column="id">
    </collection>

    <collection property="timeList"
      ofType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleTime"
      select="selectTime" column="id">
    </collection>

    <collection property="deviceList"
      ofType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleDevice"
      select="selectDevice" column="id">
    </collection>
  </resultMap>

  <select id="selectDate"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleDate">
    select *
    from edu_attendance_rule_date
    where rule_id = #{id}
    order by start_date
  </select>

  <select id="selectTime" resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleTime">
    select *
    from edu_attendance_rule_time
    where rule_id = #{id}
    order by day_of_week, time1
  </select>

  <select id="selectDept"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleDept">
    select *
    from edu_attendance_rule_dept
    where rule_id = #{id}
  </select>

  <select id="selectDevice"
    resultType="com.yida.data.common.core.entity.attendance.EduAttendanceRuleDevice">
    select id, rule_id ruleId, device_id deviceId, device_sn deviceSn
    from edu_attendance_rule_device
    where rule_id = #{id}
  </select>

  <select id="listRulePage" resultMap="lisResult">
    select eau.id,
    eau.name,
    eau.status,
    eau.type,
    eau.create_date,
    eau.create_id,
    eau.create_name
    from edu_attendance_rule eau
    <where>
      <if test="schoolId != null">
        and eau.school_id=#{schoolId}
      </if>
      <if test="campusId != null">
        and campus_id=#{campusId}
      </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="status != null">
        and eau.status=#{status}
      </if>
    </where>
    order by eau.create_date desc
  </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 edu_attendance_rule a
    left join edu_attendance_rule_dept b on a.id = b.rule_id
    left join edu_attendance_rule_date c on a.id = c.rule_id
    left join edu_attendance_rule_time d on a.id=d.rule_id
    where a.type=#{type}
    <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 (
    <foreach collection="date" separator="or" item="item">
      ((c.start_date <![CDATA[>=]]> #{item.startDate} and c.end_date <![CDATA[<=]]> #{item.endDate})
      or (c.start_date <![CDATA[<=]]> #{item.startDate} and c.end_date <![CDATA[>=]]> #{item.endDate})
      or (c.start_date <![CDATA[<=]]> #{item.startDate} and c.end_date <![CDATA[>=]]> #{item.startDate})
      or (c.start_date <![CDATA[<=]]> #{item.endDate} and c.end_date <![CDATA[>=]]> #{item.endDate}))
    </foreach>
    )
    and
    d.day_of_week in
    <foreach collection="time" item="item" close=")" open="(" separator=",">
      #{item.dayOfWeek}
    </foreach>
  </select>

  <select id="listValidRule" resultMap="lisResult">
    SELECT DISTINCT a.*
    FROM edu_attendance_rule a
    LEFT JOIN edu_attendance_rule_dept b ON a.id = b.rule_id
    LEFT JOIN edu_attendance_rule_date c ON a.id = c.rule_id
    left join edu_attendance_rule_time d on a.id=d.rule_id
    LEFT JOIN edu_attendance_rule_device e ON a.id = e.rule_id
    where a.status = 0
    <if test="deptIds != null and deptIds.size() != 0">
      and b.dept_id in
      <foreach collection="deptIds" item="item" separator="," close=")" open="(">
        #{item}
      </foreach>
    </if>
    <if test="type != null">
      and a.type=#{type}
    </if>
    <if test="schoolId != null">
      and a.school_id=#{schoolId}
    </if>
    <if test="deviceSn != null and deviceSn!=''">
      and e.device_sn=#{deviceSn}
    </if>
    and c.start_date
    <![CDATA[<=]]> #{date} and c.end_date <![CDATA[>=]]> #{date}
    and d.day_of_week=#{dayOfWeek}
  </select>

  <update id="updateRuleStatus">
    update edu_attendance_rule a,edu_attendance_rule_date b
    set a.status=#{status}
    where a.id=b.rule_id
    <choose>
      <when test="status == 0">
        and a.status = 3
        and b.start_date <![CDATA[<=]]> #{now}
        and b.end_date <![CDATA[>=]]> #{now}
      </when>
      <when test="status == 2">
        and a.status <![CDATA[!=]]> 2
        and b.end_date <![CDATA[<]]> #{now}
      </when>
    </choose>
  </update>

  <select id="listExpireRule" resultType="java.lang.Long">
    select a.id
    from edu_attendance_rule a
           left join edu_attendance_rule_date b on a.id = b.rule_id
    where a.status <![CDATA[!=]]> 2
    group by a.id
    having sum(if(b.end_date <![CDATA[>=]]> #{now}, 1, 0)) = 0
  </select>

  <select id="listNotStartRule" resultType="java.lang.Long">
    select a.id
    from edu_attendance_rule a
           left join edu_attendance_rule_date b on a.id = b.rule_id
    where a.status = 3
      and start_date <![CDATA[<=]]> #{now}
      and b.end_date <![CDATA[>=]]> #{now}
  </select>

  <select id="listWillStartRule" resultType="java.lang.Long">
    select a.id
    from edu_attendance_rule a
           left join edu_attendance_rule_date b on a.id = b.rule_id
    group by a.id
    having sum(if(b.start_date <![CDATA[<=]]> #{now} and b.end_date <![CDATA[>=]]> #{now}, 1, 0)) = 0
       and sum(if(b.end_date <![CDATA[>=]]> #{now}, 1, 0)) != 0
  </select>

</mapper>