<?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.haha.mapper.ReplenisherMapper">

    <!-- 补货员基础结果映射 -->
    <resultMap id="BaseResultMap" type="com.haha.entity.Replenisher">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="phone" property="phone"/>
        <result column="employee_id" property="employeeId"/>
        <result column="wechat_openid" property="wechatOpenid"/>
        <result column="avatar" property="avatar"/>
        <result column="status" property="status"/>
        <result column="total_tasks" property="totalTasks"/>
        <result column="last_task_time" property="lastTaskTime"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 带设备绑定数量的补货员列表查询 -->
    <select id="selectWithDeviceCount" resultMap="BaseResultMap">
        SELECT r.*,
               (SELECT COUNT(*) FROM t_replenisher_device rd WHERE rd.replenisher_id = r.id) AS bound_device_count
        FROM t_replenisher r
        <where>
            <if test="keyword != null and keyword != ''">
                AND (r.name LIKE CONCAT('%', #{keyword}, '%')
                     OR r.phone LIKE CONCAT('%', #{keyword}, '%')
                     OR r.employee_id LIKE CONCAT('%', #{keyword}, '%'))
            </if>
            <if test="status != null">
                AND r.status = #{status}
            </if>
        </where>
        ORDER BY r.create_time DESC
    </select>

</mapper>
