<?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="cn.iocoder.yudao.module.system.dal.mysql.equipment.AttendanceMachineMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
    <select id="selectAttendancePage" resultType="cn.iocoder.yudao.module.system.controller.admin.equipment.vo.attendancemachine.AttendanceMachineRespVO">
        SELECT
            b.id,
            a.id AS assetsId,
            a.assets_no AS assetsNo,
            b.device_no AS deviceNo,
            c.dept_id AS deptId,
            b.device_name AS deviceName,
            b.status AS isOnLine
        FROM
            zc_assets a
        LEFT JOIN kq_attendance_machine b ON a.assets_no = b.assets_no
        LEFT JOIN zc_dept_assets c ON a.id = c.assets_id
        WHERE
            a.deleted = 0
            AND a.type_id = #{reqVO.typeId}
            <if test="reqVO.deptId != null">
                AND c.dept_id = #{reqVO.deptId}
            </if>
            <if test="reqVO.deviceName != null and reqVO.deviceName != ''">
                AND b.device_name LIKE CONCAT('%', #{reqVO.deviceName}, '%')
            </if>
            <if test="reqVO.isOnLine != null">
                AND b.status = #{reqVO.isOnLine}
            </if>
    </select>

    <select id="selectAttendanceByAssetsNo" resultType="cn.iocoder.yudao.module.system.controller.admin.equipment.vo.attendancemachine.AttendanceMachineRespVO">
        SELECT
            a.id AS id,
            a.assets_id AS assetsId,
            a.device_no AS deviceNo,
            b.dept_id AS deptId,
            a.device_name AS deviceName
        FROM
            kq_attendance_machine a,
            zc_dept_assets b
        WHERE
            a.assets_id = b.assets_id
            AND a.assets_no = #{assetsNo}
    </select>

    <select id="selectUserPage" resultType="cn.iocoder.yudao.module.system.controller.admin.equipment.vo.userExt.UsersExtRespVO">
        SELECT
            a.id AS id,
            a.user_id AS userId,
            d.nickname AS userName,
            b.dept_id AS deptId,
            a.face_img AS faceImg,
            c.create_time AS createTime
        FROM
            ( system_users_ext a, kq_attendance_machine b, system_users d )
        LEFT JOIN (
            SELECT
                user_id,
                max( create_time ) AS create_time
             FROM
                kq_distribute_record
            WHERE
                code = 0
                AND ( type = 1 OR type = 2 )
                AND device_no = #{reqVO.deviceNo}
            GROUP BY
                user_id
            ) c ON c.user_id = a.user_id
        WHERE
            a.attendance_machine_nos LIKE CONCAT( '%', b.device_no, '%' )
            AND b.device_no = #{reqVO.deviceNo}
            AND a.user_id = d.id
            <if test="reqVO.userName != null and reqVO.userName != ''">
                AND d.nickname LIKE CONCAT( '%', #{reqVO.userName}, '%' )
            </if>
    </select>
</mapper>