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.
 
 
 

707 lines
23 KiB

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.web.mapper.SupplyUsersMapper">
<resultMap id="userMap" type="com.example.web.entity.Users">
<id column="id" property="id"/>
<result column="openid" property="openid"/>
<result column="userId" property="userId"/>
<result column="nickName" property="nickName"/>
<result column="avatarUrl" property="avatarUrl"/>
<result column="phoneNumber" property="phoneNumber"/>
<result column="type" property="type"/>
<result column="gender" property="gender"/>
<result column="country" property="country"/>
<result column="province" property="province"/>
<result column="city" property="city"/>
<result column="language" property="language"/>
<result column="session_key" property="session_key"/>
<result column="created_at" property="created_at"/>
<result column="updated_at" property="updated_at"/>
<result property="company" column="company"/>
<result property="region" column="region"/>
<result property="level" column="level"/>
<result property="demand" column="demand" jdbcType="VARCHAR"/>
<result property="spec" column="spec" jdbcType="VARCHAR"/>
<result property="notice" column="notice" jdbcType="VARCHAR"/>
</resultMap>
<!-- 用户基本信息映射 -->
<resultMap id="userBasicInfoMap" type="com.example.web.dto.UserProductCartDTO">
<result column="userId" property="userId"/>
<result column="nickName" property="nickName"/>
<result column="phoneNumber" property="phoneNumber"/>
<result column="type" property="type"/>
<result column="created_at" property="created_at"/>
<result column="updated_at" property="updated_at"/>
<result property="company" column="company"/>
<result property="region" column="region"/>
<result property="level" column="level"/>
<result property="demand" column="demand" jdbcType="VARCHAR"/>
<result property="spec" column="spec" jdbcType="VARCHAR"/>
<result property="notice" column="notice" jdbcType="VARCHAR"/>
</resultMap>
<!-- 产品信息映射 -->
<resultMap id="productInfoMap" type="com.example.web.dto.UserProductCartDTO$ProductInfo">
<result column="quantity" property="quantity"/>
<result column="variety" property="variety"/>
<result column="grossWeight" property="grossWeight"/>
<result column="specification" property="specification"/>
<result column="yolk" property="yolk"/>
<result column="productId" property="productId"/>
<result column="productName" property="productName"/>
<result column="price" property="price"/>
<result column="sellerId" property="sellerId"/>
<result column="sellerPhone" property="sellerPhone"/>
<result column="sellerNickName" property="sellerNickName"/>
</resultMap>
<!-- 购物车信息映射 -->
<resultMap id="cartItemMap" type="com.example.web.dto.UserProductCartDTO$CartItem">
<result column="specification" property="specification"/>
<result column="quantity" property="quantity"/>
<result column="grossWeight" property="grossWeight"/>
<result column="yolk" property="yolk"/>
<result column="productId" property="productId"/>
<result column="productName" property="productName"/>
<result column="price" property="price"/>
<result column="selected" property="selected"/>
</resultMap>
<!-- 🚀 新增:采购端获取授权客户列表(数据库层面过滤) -->
<select id="getAuthorizedCustomers" resultMap="userBasicInfoMap" parameterType="map">
SELECT DISTINCT
u.userId,
u.phoneNumber,
u.type,
u.nickName,
u.company,
u.region,
u.level,
u.demand,
u.spec,
u.created_at,
u.updated_at,
u.notice
FROM wechat_app.users u
LEFT JOIN usermanagements um ON u.userId = um.userId
WHERE u.phoneNumber IS NOT NULL
AND u.phoneNumber != ''
AND (u.type = 'seller' OR u.type = 'both') <!-- 采购端:关注seller和both类型 -->
AND (
<!-- 公海池客户权限规则 -->
(u.level IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND (
<!-- 公司公海池:无负责人信息 -->
(u.level IN ('company-sea-pools', '公海池') AND (um.userId IS NULL OR um.managerId IS NULL OR um.managerId = ''))
OR
<!-- 组织公海池:组织匹配 -->
(u.level = 'organization-sea-pools' AND um.organization = #{authInfo.organization})
OR
<!-- 部门公海池:部门匹配 -->
(u.level = 'department-sea-pools' AND um.managerdepartment = #{authInfo.managerdepartment})
))
OR
<!-- 非公海池客户:完全匹配负责人信息 -->
(u.level NOT IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND um.managercompany = #{authInfo.managercompany}
AND um.managerdepartment = #{authInfo.managerdepartment}
AND um.organization = #{authInfo.organization}
AND um.userName = #{authInfo.userName}
)
)
ORDER BY u.created_at DESC
<if test="limit != null and offset != null">
LIMIT #{limit} OFFSET #{offset}
</if>
</select>
<!-- 🚀 新增:采购端获取授权客户总数 -->
<select id="getAuthorizedCustomersCount" resultType="int" parameterType="map">
SELECT COUNT(DISTINCT u.userId)
FROM wechat_app.users u
LEFT JOIN usermanagements um ON u.userId = um.userId
WHERE u.phoneNumber IS NOT NULL
AND u.phoneNumber != ''
AND (u.type = 'seller' OR u.type = 'both') <!-- 采购端:关注seller和both类型 -->
AND (
<!-- 公海池客户权限规则 -->
(u.level IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND (
<!-- 公司公海池:无负责人信息 -->
(u.level IN ('company-sea-pools', '公海池') AND (um.userId IS NULL OR um.managerId IS NULL OR um.managerId = ''))
OR
<!-- 组织公海池:组织匹配 -->
(u.level = 'organization-sea-pools' AND um.organization = #{authInfo.organization})
OR
<!-- 部门公海池:部门匹配 -->
(u.level = 'department-sea-pools' AND um.managerdepartment = #{authInfo.managerdepartment})
))
OR
<!-- 非公海池客户:完全匹配负责人信息 -->
(u.level NOT IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND um.managercompany = #{authInfo.managercompany}
AND um.managerdepartment = #{authInfo.managerdepartment}
AND um.organization = #{authInfo.organization}
AND um.userName = #{authInfo.userName}
)
)
</select>
<!-- 🚀 新增:采购端根据认证信息获取有权限的用户ID列表 -->
<select id="getAuthorizedUserIds" resultType="string" parameterType="map">
SELECT DISTINCT u.userId
FROM users u
LEFT JOIN usermanagements um ON u.userId = um.userId
WHERE u.phoneNumber IS NOT NULL
AND u.phoneNumber != ''
AND (u.type = 'seller' OR u.type = 'both') <!-- 采购端:关注seller和both类型 -->
AND (
<!-- 公海池客户权限规则 -->
(u.level IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND (
(u.level IN ('company-sea-pools', '公海池') AND (um.userId IS NULL OR um.managerId IS NULL OR um.managerId = ''))
OR
(u.level = 'organization-sea-pools' AND um.organization = #{authInfo.organization})
OR
(u.level = 'department-sea-pools' AND um.managerdepartment = #{authInfo.managerdepartment})
))
OR
<!-- 非公海池客户权限规则 -->
(u.level NOT IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND um.managercompany = #{authInfo.managercompany}
AND um.managerdepartment = #{authInfo.managerdepartment}
AND um.organization = #{authInfo.organization}
AND um.userName = #{authInfo.userName}
)
)
</select>
<!-- 🚀 新增:采购端根据手机号和权限查询用户 -->
<select id="selectByPhoneWithAuth" parameterType="map" resultMap="userBasicInfoMap">
SELECT
u.userId,
u.phoneNumber,
u.type,
u.nickName,
u.company,
u.region,
u.level,
u.demand,
u.spec,
u.created_at,
u.updated_at,
u.notice
FROM users u
LEFT JOIN usermanagements um ON u.userId = um.userId
WHERE u.phoneNumber = #{phoneNumber}
AND (u.type = 'seller' OR u.type = 'both') <!-- 采购端:关注seller和both类型 -->
AND (
<!-- 公海池客户权限规则 -->
(u.level IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND (
(u.level IN ('company-sea-pools', '公海池') AND (um.userId IS NULL OR um.managerId IS NULL OR um.managerId = ''))
OR
(u.level = 'organization-sea-pools' AND um.organization = #{authInfo.organization})
OR
(u.level = 'department-sea-pools' AND um.managerdepartment = #{authInfo.managerdepartment})
))
OR
<!-- 非公海池客户权限规则 -->
(u.level NOT IN ('company-sea-pools', 'organization-sea-pools', 'department-sea-pools', '公海池')
AND um.managercompany = #{authInfo.managercompany}
AND um.managerdepartment = #{authInfo.managerdepartment}
AND um.organization = #{authInfo.organization}
AND um.userName = #{authInfo.userName}
)
)
LIMIT 1
</select>
<!-- 获取用户基本信息 -->
<select id="getUserBasicInfo" resultMap="userBasicInfoMap" parameterType="string">
SELECT
userId,
phoneNumber,
type,
nickName,
company,
region,
level,
demand,
spec,
created_at,
updated_at,
notice
FROM wechat_app.users
WHERE userId = #{userId}
</select>
<!-- 采购端:获取卖家的产品信息(公海需求) -->
<select id="getSellerProducts" resultMap="productInfoMap" parameterType="string">
SELECT
productName,
variety,
specification,
quantity,
grossWeight,
yolk,
productId,
price,
sellerId,
created_at,
updated_at
FROM products
WHERE sellerId = #{sellerId}
ORDER BY created_at DESC
LIMIT 10
</select>
<!-- 🎯 修改:保留原方法,但建议使用新的授权查询 -->
<select id="getAllUserBasicInfo" resultMap="userBasicInfoMap">
SELECT
userId,
phoneNumber,
type,
nickName,
company,
region,
level,
demand,
spec,
notice,
created_at,
updated_at
FROM wechat_app.users
WHERE phoneNumber IS NOT NULL
AND phoneNumber != ''
AND (type = 'seller' OR type = 'both')
ORDER BY created_at DESC
</select>
<!-- 🎯 修改:保留原方法,但建议使用新的授权查询 -->
<select id="selectByPhone" parameterType="string" resultMap="userBasicInfoMap">
SELECT
userId,
phoneNumber,
type,
nickName,
company,
region,
level,
demand,
spec,
created_at,
updated_at,
notice
FROM wechat_app.users
WHERE phoneNumber = #{phoneNumber}
AND (type = 'seller' OR type = 'both')
</select>
<!-- 新增:根据用户ID精确查询用户 -->
<select id="selectByUserId" parameterType="string" resultMap="userBasicInfoMap">
SELECT
userId,
phoneNumber,
type,
nickName,
company,
region,
level,
demand,
spec,
created_at,
updated_at,
notice
FROM wechat_app.users
WHERE userId = #{userId}
AND (type = 'seller' OR type = 'both')
</select>
<!-- 采购端:更新公海需求信息 -->
<update id="updateProduct">
UPDATE products
SET
productName = #{product.productName},
specification = #{product.specification},
yolk = #{product.yolk},
quantity = #{product.quantity},
variety = #{product.variety},
grossWeight = #{product.grossWeight},
price = #{product.price},
updated_at = NOW()
WHERE
sellerId = #{sellerId}
AND productId = #{productId}
</update>
<!-- 根据手机号更新用户信息 -->
<update id="updateByPhone" parameterType="com.example.web.dto.UserProductCartDTO">
UPDATE wechat_app.users
SET
<if test="nickName != null and nickName != ''">
nickName = #{nickName},
</if>
<if test="type != null and type != ''">
type = #{type},
</if>
<if test="company != null">
company = #{company},
</if>
<if test="region != null">
region = #{region},
</if>
<if test="level != null and level != ''">
level = #{level},
</if>
<if test="demand != null">
demand = #{demand},
</if>
<if test="spec != null">
spec = #{spec},
</if>
updated_at = #{updated_at}
WHERE phoneNumber = #{phoneNumber}
</update>
<!-- 采购端:更新卖家产品信息 -->
<update id="updateProductBySellerId">
UPDATE products
SET
<if test="productName != null and productName != ''">
productName = #{productName},
</if>
<if test="variety != null and variety != ''">
variety = #{variety},
</if>
<if test="specification != null and specification != ''">
specification = #{specification},
</if>
<if test="quantity != null">
quantity = #{quantity},
</if>
<if test="grossWeight != null">
grossWeight = #{grossWeight},
</if>
<if test="yolk != null and yolk != ''">
yolk = #{yolk},
</if>
updated_at = NOW()
WHERE sellerId = #{sellerId}
</update>
<!-- 采购端:更新或插入产品信息 -->
<update id="updateOrInsertProduct">
INSERT INTO products (
productId,
sellerId,
productName,
variety,
specification,
quantity,
grossWeight,
yolk,
price,
created_at,
updated_at
) VALUES (
COALESCE(#{productId}, UUID()),
#{sellerId},
#{productName},
#{variety},
#{specification},
#{quantity},
#{grossWeight},
#{yolk},
#{price},
NOW(),
NOW()
)
ON DUPLICATE KEY UPDATE
productName = VALUES(productName),
variety = VALUES(variety),
specification = VALUES(specification),
quantity = VALUES(quantity),
grossWeight = VALUES(grossWeight),
yolk = VALUES(yolk),
price = VALUES(price),
updated_at = NOW()
</update>
<!-- 获取用户联系人信息 -->
<select id="getUserContacts" resultType="com.example.web.dto.UserProductCartDTO$UsersContacts">
SELECT
id as contactId,
wechat,
account,
accountNumber,
bank,
address
FROM contacts
WHERE userId = #{userId}
</select>
<!-- 更新用户联系人信息 -->
<update id="updateContactsByUserId">
UPDATE contacts
SET
<if test="wechat != null and wechat != ''">
wechat = #{wechat},
</if>
<if test="account != null and account != ''">
account = #{account},
</if>
<if test="accountNumber != null and accountNumber != ''">
accountNumber = #{accountNumber},
</if>
<if test="bank != null and bank != ''">
bank = #{bank},
</if>
<if test="address != null and address != ''">
address = #{address},
</if>
updated_at = NOW()
WHERE userId = #{userId}
LIMIT 1
</update>
<!-- 更新或插入联系人信息 -->
<update id="updateOrInsertContact">
INSERT INTO contacts (id, userId, wechat, account, accountNumber, bank, address, updated_at)
VALUES (
COALESCE(#{contactId}, UUID()),
#{userId},
#{wechat},
#{account},
#{accountNumber},
#{bank},
#{address},
NOW()
)
ON DUPLICATE KEY UPDATE
wechat = VALUES(wechat),
account = VALUES(account),
accountNumber = VALUES(accountNumber),
bank = VALUES(bank),
address = VALUES(address),
updated_at = NOW()
</update>
<!-- 更新特定联系人信息 -->
<update id="updateSpecificContact">
UPDATE contacts
SET
<if test="wechat != null">
wechat = #{wechat},
</if>
<if test="account != null">
account = #{account},
</if>
<if test="accountNumber != null">
accountNumber = #{accountNumber},
</if>
<if test="bank != null">
bank = #{bank},
</if>
<if test="address != null">
address = #{address},
</if>
updated_at = NOW()
WHERE userId = #{userId}
AND id = #{contactId}
</update>
<!-- 采购端:更新特定产品信息 -->
<update id="updateSpecificProduct">
UPDATE products
SET
<if test="productName != null">
productName = #{productName},
</if>
<if test="variety != null">
variety = #{variety},
</if>
<if test="specification != null">
specification = #{specification},
</if>
<if test="quantity != null">
quantity = #{quantity},
</if>
<if test="grossWeight != null">
grossWeight = #{grossWeight},
</if>
<if test="yolk != null">
yolk = #{yolk},
</if>
updated_at = NOW()
WHERE sellerId = #{sellerId}
AND productId = #{productId}
</update>
<!-- 获取用户所有联系人 -->
<select id="getUserAllContacts" resultType="com.example.web.mapper.SupplyUsersMapper$ContactInfo">
SELECT
id as contactId,
wechat,
account,
accountNumber,
bank,
address
FROM contacts
WHERE userId = #{userId}
</select>
<!-- 采购端:获取用户所有产品信息 -->
<select id="getUserAllProducts" resultType="com.example.web.mapper.SupplyUsersMapper$ProductInfo">
SELECT
productId,
productName,
variety,
specification,
quantity,
grossWeight,
yolk
FROM products
WHERE sellerId = #{sellerId}
</select>
<!-- 🔥 新增:批量查询用户联系人信息 -->
<select id="getUserContactsByUserIds" resultType="com.example.web.mapper.UsersMapper$ContactInfo">
SELECT
id as contactId,
userId as userId, <!-- 关键:添加userId字段用于分组 -->
wechat,
account,
accountNumber as accountNumber,
bank,
address
FROM contacts
WHERE userId IN
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
ORDER BY userId, id
</select>
<!-- 🔥 新增:批量查询卖家产品信息(采购端专用) -->
<select id="getSellerProductsByUserIds" resultType="com.example.web.dto.UserProductCartDTO$ProductInfo">
SELECT
productId as productId,
sellerId as sellerId,
productName as productName,
variety,
specification,
quantity,
grossWeight as grossWeight,
yolk
FROM products
WHERE sellerId IN
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
ORDER BY sellerId, productId
</select>
<!-- 🔥 新增:批量查询用户基本信息 -->
<select id="getUserBasicInfoByUserIds" resultMap="userBasicInfoMap">
SELECT
userId,
phoneNumber,
type,
nickName,
company,
region,
level,
demand,
spec,
created_at,
updated_at,
notice
FROM wechat_app.users
WHERE userId IN
<foreach collection="userIds" item="userId" open="(" separator="," close=")">
#{userId}
</foreach>
AND phoneNumber IS NOT NULL
AND phoneNumber != ''
AND (type = 'seller' OR type = 'both')
ORDER BY userId
</select>
<!-- 根据产品ID精确更新产品信息 -->
<update id="updateProductByProductId">
UPDATE products
SET
productName = #{productName},
variety = #{variety},
specification = #{specification},
quantity = #{quantity},
grossWeight = #{grossWeight},
yolk = #{yolk},
updated_at = NOW()
WHERE productId = #{productId}
</update>
<!-- 查询超过指定时间的未分级客户 -->
<select id="findUnclassifiedCustomersOlderThan" resultType="com.example.web.dto.UserProductCartDTO">
SELECT
userId,
phoneNumber,
type,
nickName,
company,
region,
level,
demand,
spec,
created_at,
updated_at
FROM wechat_app.users
WHERE level = 'unclassified'
AND updated_at &lt;= #{thresholdTime}
</select>
<!-- 查询超过指定时间的组织公海池客户 -->
<select id="findOrganizationSeaPoolsCustomersOlderThan" resultType="com.example.web.dto.UserProductCartDTO">
SELECT
userId,
phoneNumber,
type,
nickName,
company,
region,
level,
demand,
spec,
created_at,
updated_at
FROM wechat_app.users
WHERE level = 'organization-sea-pools'
AND updated_at &lt;= #{thresholdTime}
</select>
<select id="findRecentlyRecycledCustomers" resultType="com.example.web.dto.UserProductCartDTO">
SELECT
u.userId,
u.phoneNumber,
u.type,
u.nickName,
u.company,
u.region,
u.level,
u.demand,
u.spec,
u.created_at,
u.updated_at
FROM users u
WHERE u.updated_at >= #{sinceTime}
AND u.level IN ('organization-sea-pools', 'department-sea-pools')
ORDER BY u.updated_at DESC
</select>
</mapper>