Files
wb-java-nc-shop/target/classes/mapper/UserBehaviorMapper.xml
2026-01-11 18:14:42 +08:00

63 lines
2.6 KiB
XML

<?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.nlshop.mapper.UserBehaviorMapper">
<resultMap id="BaseResultMap" type="com.nlshop.entity.UserBehavior">
<id column="id" property="id"/>
<result column="user_id" property="userId"/>
<result column="product_id" property="productId"/>
<result column="behavior_type" property="behaviorType"/>
<result column="score" property="score"/>
<result column="create_time" property="createTime"/>
</resultMap>
<insert id="insert" parameterType="com.nlshop.entity.UserBehavior" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user_behavior (user_id, product_id, behavior_type, score)
VALUES (#{userId}, #{productId}, #{behaviorType}, #{score})
</insert>
<select id="selectByUserId" resultMap="BaseResultMap">
SELECT * FROM user_behavior WHERE user_id = #{userId} ORDER BY create_time DESC
</select>
<select id="selectByProductId" resultMap="BaseResultMap">
SELECT * FROM user_behavior WHERE product_id = #{productId} ORDER BY create_time DESC
</select>
<select id="selectByUserAndProduct" resultMap="BaseResultMap">
SELECT * FROM user_behavior
WHERE user_id = #{userId} AND product_id = #{productId} AND behavior_type = #{behaviorType}
LIMIT 1
</select>
<update id="update" parameterType="com.nlshop.entity.UserBehavior">
UPDATE user_behavior
SET score = #{score}, create_time = NOW()
WHERE id = #{id}
</update>
<select id="selectProductIdsByUserId" resultType="java.lang.Long">
SELECT DISTINCT product_id FROM user_behavior WHERE user_id = #{userId}
</select>
<select id="selectUserIdsByProductId" resultType="java.lang.Long">
SELECT DISTINCT user_id FROM user_behavior WHERE product_id = #{productId}
</select>
<select id="selectActiveUsers" resultType="java.util.HashMap">
SELECT
u.id as userId,
u.username,
u.name,
COUNT(ub.id) as viewCount,
MAX(ub.create_time) as lastViewTime
FROM user_behavior ub
INNER JOIN user u ON ub.user_id = u.id
INNER JOIN product p ON ub.product_id = p.id
WHERE ub.behavior_type = 'VIEW'
AND p.merchant_id = #{merchantId}
AND ub.create_time >= DATE_SUB(NOW(), INTERVAL #{days} DAY)
GROUP BY u.id, u.username, u.name
ORDER BY lastViewTime DESC
</select>
</mapper>