<?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.yk.YkElderlyDeliveryMapper">


    <select id="selectDeliveryStatsByStaffMap" resultType="java.util.Map">
        SELECT
        ed.delivery_person_id AS staffId,
        ed.delivery_person_name AS staffName,
        COUNT(ed.id) AS deliveryCount,
        SUM(ed.meal_standard) AS deliveryAmount,
        COUNT(DISTINCT ed.delivery_date) AS workDays
        FROM yk_elderly_delivery ed
        WHERE ed.delivery_date BETWEEN #{startDate} AND #{endDate}
        <if test="staffId != null">
            AND ed.delivery_person_id = #{staffId}
        </if>
        GROUP BY ed.delivery_person_id, ed.delivery_person_name
        ORDER BY deliveryCount DESC
    </select>
</mapper>