You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
8.4 KiB

<?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="cc.hiver.mall.dao.mapper.WorkerRelaPriceMapper">
<resultMap id="BaseResultMap" type="cc.hiver.mall.entity.WorkerRelaPrice">
<id column="id" jdbcType="VARCHAR" property="id"/>
<result column="create_by" jdbcType="VARCHAR" property="createBy"/>
<result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
<result column="del_flag" jdbcType="INTEGER" property="delFlag"/>
<result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
<result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
<result column="worker_id" jdbcType="VARCHAR" property="workerId"/>
<result column="get_area_id" jdbcType="VARCHAR" property="getAreaId"/>
<result column="get_area_name" jdbcType="VARCHAR" property="getAreaName"/>
<result column="put_area_id" jdbcType="VARCHAR" property="putAreaId"/>
<result column="put_area_name" jdbcType="VARCHAR" property="putAreaName"/>
<result column="remark" jdbcType="VARCHAR" property="remark"/>
<result column="order_type" jdbcType="INTEGER" property="orderType"/>
<result column="order_bkge" jdbcType="DECIMAL" property="orderBkge"/>
<result column="get_push_order" jdbcType="INTEGER" property="getPushOrder"/>
</resultMap>
<sql id="Base_Column_List">
id, create_by, create_time, del_flag, update_by, update_time, worker_id, get_area_id,
get_area_name, put_area_id, put_area_name, remark, order_type, order_bkge, get_push_order
</sql>
<insert id="insert" parameterType="cc.hiver.mall.entity.ProductGroupBuyPrice">
insert into t_worker_rela_price (id, create_by, create_time,
del_flag, update_by, update_time,
worker_id, get_area_id, get_area_name, put_area_id, put_area_name, remark, order_type, order_bkge, get_push_order)
values (#{id,jdbcType=VARCHAR}, #{createBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
#{delFlag,jdbcType=INTEGER}, #{updateBy,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP},
#{workerId,jdbcType=VARCHAR}, #{getAreaId,jdbcType=VARCHAR}, #{getAreaName,jdbcType=VARCHAR},
#{putAreaId,jdbcType=VARCHAR}, #{putAreaName,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR},
#{orderType,jdbcType=INTEGER}, #{orderBkge,jdbcType=DECIMAL},
#{getPushOrder,jdbcType=INTEGER})
</insert>
<select id="selectByWorkerId" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from t_worker_rela_price
where worker_id = #{workerId,jdbcType=VARCHAR}
</select>
<delete id="deleteByWorkerId" parameterType="java.lang.String">
delete
from t_worker_rela_price
where worker_id = #{workerId,jdbcType=VARCHAR}
</delete>
<!-- =====================================================================
配送员匹配列表:联查 t_worker、t_worker_rela_price、mall_delivery_order
支持:状态过滤、交易保障、相同地址、关键词搜索、动态排序
===================================================================== -->
<resultMap id="WorkerMatchVOMap" type="cc.hiver.mall.pojo.vo.WorkerMatchVO">
<result column="worker_id" property="workerId"/>
<result column="worker_name" property="workerName"/>
<result column="mobile" property="mobile"/>
<result column="icon" property="icon"/>
<result column="score" property="score"/>
<result column="avg_time" property="avgTime"/>
<result column="rebate_amount" property="rebateAmount"/>
<result column="high_floor_fee" property="highFloorFee"/>
<result column="order_bkge" property="orderBkge"/>
<result column="order_wait_count" property="orderWaitCount"/>
<result column="order_get_count" property="orderGetCount"/>
<result column="order_put_count" property="orderPutCount"/>
</resultMap>
<select id="getMatchingWorkerList" resultMap="WorkerMatchVOMap">
SELECT
w.worker_id,
w.worker_name,
w.mobile,
w.icon,
w.score,
w.avg_time,
w.rebate_amount,
w.high_floor_fee,
p.order_bkge,
/* 待接单:status = 0 */
COUNT(CASE WHEN d.status = 0 THEN 1 END) AS order_wait_count,
/* 待取货:status = 1 */
COUNT(CASE WHEN d.status = 1 THEN 1 END) AS order_get_count,
/* 待送达:status = 2 */
COUNT(CASE WHEN d.status = 2 THEN 1 END) AS order_put_count
FROM t_worker w
/* 关联配送规则表:只取满足区域匹配和接单条件的规则 */
INNER JOIN t_worker_rela_price p
ON p.worker_id = w.worker_id
AND p.get_push_order = 1
<if test="shopAreaId != null and shopAreaId != ''">
AND p.get_area_id = #{shopAreaId}
</if>
<if test="putAreaId != null and putAreaId != ''">
AND p.put_area_id = #{putAreaId}
</if>
<if test="orderType != null">
AND p.order_type = #{orderType}
</if>
/* 关联配送订单表:LEFT JOIN 保留无任何订单的配送员 */
LEFT JOIN mall_delivery_order d
ON d.worker_id = w.worker_id
AND d.status IN (0, 1, 2)
WHERE
/* 配送员基础状态:启用 + 上线 + 接收指派 */
w.worker_status = 1
AND w.is_on_line = 1
AND w.get_push_order = 1
/* 交易保障:rebate_amount > 0 */
<if test="baozhang != null and baozhang == true">
AND w.rebate_amount &gt; 0
</if>
/* 相同地址:手上存在送往相同 put_area_id 的进行中订单 */
<if test="xiangtong != null and xiangtong == true">
AND EXISTS (
SELECT 1 FROM mall_delivery_order d2
WHERE d2.worker_id = w.worker_id
AND d2.put_area_id = #{putAreaId}
AND d2.status IN (0, 1, 2)
)
</if>
/* 关键词模糊搜索:姓名或手机号 */
<if test="keyword != null and keyword != ''">
AND (
w.worker_name LIKE CONCAT('%', #{keyword}, '%')
OR w.mobile LIKE CONCAT('%', #{keyword}, '%')
)
</if>
GROUP BY
w.worker_id, w.worker_name, w.mobile, w.icon,
w.score, w.avg_time, w.rebate_amount, w.high_floor_fee, p.order_bkge
ORDER BY
<choose>
<when test="sortField != null and sortField == 'score'">
w.score DESC
</when>
<when test="sortField != null and sortField == 'orderBkge'">
p.order_bkge ASC
</when>
<!-- 默认按 score 倒序 -->
<otherwise>
w.avg_time ASC
</otherwise>
</choose>
</select>
<resultMap id="WorkerAreaOnlineCountVOMap" type="cc.hiver.mall.pojo.vo.WorkerAreaOnlineCountVO">
<result column="area_id" property="areaId"/>
<result column="area_name" property="areaName"/>
<result column="online_worker_count" property="onlineWorkerCount"/>
</resultMap>
<select id="getOnlineWorkerCountByCanteenArea" resultMap="WorkerAreaOnlineCountVOMap">
SELECT
sa.id AS area_id,
sa.title AS area_name,
COUNT(DISTINCT w.worker_id) AS online_worker_count
FROM t_shop_area sa
LEFT JOIN t_worker_rela_price wrp
ON wrp.get_area_id = sa.id
AND wrp.get_push_order = 1
LEFT JOIN t_worker w
ON w.worker_id = wrp.worker_id
AND w.worker_status = 1
AND w.is_on_line = 1
AND w.get_push_order = 1
WHERE sa.is_canteen = 1 and sa.parent_id = #{parentId}
GROUP BY sa.id, sa.title, sa.sort_order, sa.create_time
ORDER BY sa.sort_order ASC, sa.create_time ASC
</select>
</mapper>