<?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="jnpf.attendance.mapper.AttendanceMachineManageMapper">

    <!-- 更新考勤机的考勤组关联 -->
    <update id="updateRelationGroup">
        update ftb_attendance_machine_manage set F_GroupIds = #{groupIds} where F_Id = #{id}
    </update>
    <!-- 查询考勤机绑定的考勤组 -->
    <select id="getGroupIdsByMachine" resultType="java.lang.String">
        select F_GroupIds from ftb_attendance_machine_manage where F_Id = #{machineId}
    </select>
    <!-- 查询总条数 -->
    <select id="getTotal" resultType="java.lang.Integer">
        select count(1) from ftb_attendance_machine_manage where F_DeleteMark = 0
    </select>
    <!-- 查询名称数量 -->
    <select id="getMachineNameCount" resultType="java.lang.Integer">
        select count(1) from ftb_attendance_machine_manage
        where F_DeleteMark = 0 and F_Name = #{name}
        <if test="null != id and '' != id">
            and F_Id != #{id}
        </if>
    </select>
    <!-- 查询mac数量 -->
    <select id="getMachineMacCount" resultType="java.lang.Integer">
        select count(1) from ftb_attendance_machine_manage
        where F_DeleteMark = 0 and F_Mac = #{mac}
        <if test="null != id and '' != id">
            and F_Id != #{id}
        </if>
    </select>
    <!-- 查询考勤机名称 -->
    <select id="getMachineName" resultType="java.lang.String">
        select F_Name from ftb_attendance_machine_manage
        where F_DeleteMark = 0 and F_Mac = #{mac}
    </select>
    <!-- 获取数据库时间 -->
    <select id="getDbTime" resultType="java.util.Date">
        select now()
    </select>
    <!-- 查询关联此考勤组的考勤机 -->
    <select id="getRelationMachineByGroupId" resultType="jnpf.entity.attendance.AttendanceMachineManage">
        select F_Id id, F_Name `name`, F_Mac mac, F_Type `type`, F_GroupIds groupIds
        from ftb_attendance_machine_manage
        where F_DeleteMark = 0 and instr(F_GroupIds, #{groupId}) > 0
    </select>
    <!-- 查询绑定了考勤机的考勤组 -->
    <select id="selectGroupBindMachine" resultType="java.lang.String">
        select GROUP_CONCAT(F_GroupIds) from ftb_attendance_machine_manage where F_DeleteMark = 0
    </select>
    <!-- 查询考勤机列表 -->
    <select id="getMachineList" resultType="jnpf.entity.attendance.AttendanceMachineManage">
        SELECT
          mm.F_Id as id,
          mm.F_Name AS `name`,
          mm.F_Mac AS `mac`,
          mm.F_Type AS `type`,
          mm.F_TypeName AS typeName,
          SUM(CASE WHEN rs.F_ScopeType = 2 THEN 1 ELSE 0 END) AS orgCount,
          SUM(CASE WHEN rs.F_ScopeType = 1 THEN 1 ELSE 0 END) AS userCount
        FROM ftb_attendance_machine_manage mm
        <if test="null != scopeType">
          INNER JOIN (
            SELECT DISTINCT rsFilter.F_RuleId
            FROM ftb_attendance_rule_scope rsFilter
            WHERE rsFilter.F_BizType = #{bizType}
            AND (
              rsFilter.F_ScopeType = #{scopeType} AND rsFilter.F_ScopeValue IN
              <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item}
              </foreach>
            )
          ) scope ON mm.F_Id = scope.F_RuleId
        </if>
        LEFT JOIN ftb_attendance_rule_scope rs
        ON mm.F_Id = rs.F_RuleId AND rs.F_BizType = #{bizType}
        WHERE mm.F_DeleteMark = 0
        <if test="null != machineKind and '' != machineKind">
            AND mm.F_Type = #{machineKind}
        </if>
        <if test="null != keywords and '' != keywords">
            AND INSTR(CONCAT(IFNULL(mm.F_Mac, ""), IFNULL(mm.F_Name, "")), #{keywords}) > 0
        </if>
        GROUP BY mm.F_Id, mm.F_Name, mm.F_Mac, mm.F_Type, mm.F_TypeName
        ORDER BY mm.F_CreatorTime desc
    </select>
</mapper>