fix: 根据视频和视频脚本里面的uuid来判断每个视频脚本属于哪个一个增值套餐的里面,避免订单逾期未支付导致统计上传视频数量错误的问题

This commit is contained in:
cjy 2026-06-10 11:00:49 +08:00
parent d94afe2ab7
commit 9aa2bf6459

View File

@ -99,12 +99,12 @@ func GetArtistUploadStatsList(req *dto.TaskQueryRequest) ([]*dto.ArtistUploadSta
if req != nil {
bundleTaskType = req.BundleTaskType
}
taskType, billingType, orderFilter, latestOrderFilter := taskStatsQueryOptions(bundleTaskType)
taskType, _, orderFilter, latestOrderFilter := taskStatsQueryOptions(bundleTaskType)
cte := fmt.Sprintf(`WITH
-- 1. 获取当前套餐类型下参与统计的订单基础套餐取每个用户最新普通订单先用后付取所有待付款/已付款订单
target_orders AS (
SELECT bor.id, bor.uuid, bor.customer_id, bor.customer_num, bor.created_at
SELECT bor.id, bor.uuid, bor.order_no, bor.customer_id, bor.customer_num, bor.created_at
FROM bundle_order_records bor
WHERE bor.deleted_at IS NULL
AND bor.customer_id IS NOT NULL
@ -127,6 +127,7 @@ active_order_balances AS (
u.id AS user_id,
u.tel_num COLLATE utf8mb4_0900_ai_ci AS phone,
bor.uuid AS order_uuid,
bor.order_no,
bor.customer_num COLLATE utf8mb4_0900_ai_ci AS customer_num,
bb.start_at,
bb.expired_at,
@ -272,19 +273,18 @@ balance_sum AS (
GROUP BY bb2.user_id
),
-- 6. 作品统计只统计 origin_uuid 为空的作品
-- 6. 作品统计按订单归属统计避免重叠时间窗口重复计算
cw_agg AS (
SELECT
aw.user_id,
COUNT(CASE WHEN cw.work_category = 2 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aw.start_at AND aw.expired_at THEN 1 END) AS uploaded_video_count,
COUNT(CASE WHEN cw.work_category = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aw.start_at AND aw.expired_at THEN 1 END) AS uploaded_post_count,
COUNT(CASE WHEN cw.work_category = 2 AND cw.cost = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aw.start_at AND aw.expired_at THEN 1 END) AS released_video_consumed,
COUNT(CASE WHEN cw.work_category = 1 AND cw.cost = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aw.start_at AND aw.expired_at THEN 1 END) AS released_post_consumed
FROM active_windows aw
LEFT JOIN cast_work cw ON cw.artist_phone = aw.phone
LEFT JOIN cast_work_extra cwe ON cwe.work_uuid = cw.uuid AND cwe.deleted_at = 0
WHERE (cw.uuid IS NULL OR cw.work_category != 2 OR IFNULL(cwe.billing_type, 0) = ?)
GROUP BY aw.user_id
aob.user_id,
COUNT(DISTINCT CASE WHEN cw.work_category = 2 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aob.start_at AND aob.expired_at THEN cw.uuid END) AS uploaded_video_count,
COUNT(DISTINCT CASE WHEN cw.work_category = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aob.start_at AND aob.expired_at THEN cw.uuid END) AS uploaded_post_count,
COUNT(DISTINCT CASE WHEN cw.work_category = 2 AND cw.cost = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aob.start_at AND aob.expired_at THEN cw.uuid END) AS released_video_consumed,
COUNT(DISTINCT CASE WHEN cw.work_category = 1 AND cw.cost = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aob.start_at AND aob.expired_at THEN cw.uuid END) AS released_post_consumed
FROM active_order_balances aob
LEFT JOIN cast_work_extra cwe ON cwe.bundle_order_uuid = aob.order_uuid AND cwe.deleted_at = 0
LEFT JOIN cast_work cw ON cw.uuid = cwe.work_uuid
GROUP BY aob.user_id
),
-- 7. 数据分析作品统计排除 work_analysis_status = 1 的数据分析
@ -309,18 +309,17 @@ ccr_agg AS (
GROUP BY aw.user_id
),
-- 9. 视频脚本统计
-- 9. 视频脚本统计按订单归属统计缺失订单号的数据不纳入当前有效订单
cvs_agg AS (
SELECT
aw.user_id,
COUNT(cvs.artist_uuid) AS uploaded_video_script_count
FROM active_windows aw
LEFT JOIN cast_video_script cvs ON CAST(aw.user_id AS CHAR) = cvs.artist_uuid
AND cvs.deleted_at = 0
AND cvs.billing_type = ?
AND cvs.created_at BETWEEN UNIX_TIMESTAMP(CONVERT_TZ(aw.start_at, '+00:00', '+00:00'))
AND UNIX_TIMESTAMP(CONVERT_TZ(aw.expired_at, '+00:00', '+00:00'))
GROUP BY aw.user_id
aob.user_id,
COUNT(DISTINCT cvs.uuid) AS uploaded_video_script_count
FROM active_order_balances aob
LEFT JOIN cast_video_script cvs ON cvs.order_no = aob.order_no
AND cvs.deleted_at = 0
AND cvs.created_at BETWEEN UNIX_TIMESTAMP(CONVERT_TZ(aob.start_at, '+00:00', '+00:00'))
AND UNIX_TIMESTAMP(CONVERT_TZ(aob.expired_at, '+00:00', '+00:00'))
GROUP BY aob.user_id
),
-- 9. 任务统计直接使用active_windows的customer_num避免重复JOIN latest_bor
@ -402,8 +401,8 @@ LEFT JOIN tar_agg ta ON ta.user_id = aw.user_id
LEFT JOIN assigned_pending_agg apa ON apa.user_id = aw.user_id`
whereParts := make([]string, 0, 4)
args := make([]interface{}, 0, 8)
args = append(args, nowMonth, billingType, billingType, taskType, taskType, taskType, taskType)
args := make([]interface{}, 0, 6)
args = append(args, nowMonth, taskType, taskType, taskType, taskType)
if req != nil && req.Keyword != "" {
like := "%" + req.Keyword + "%"
whereParts = append(whereParts, "(aw.customer_num LIKE ? OR aw.phone LIKE ? OR aw.user_name LIKE ?)")
@ -550,12 +549,12 @@ func GetPendingAssignBySubNums(req *dto.PendingAssignQueryRequest) ([]*dto.Artis
taskSchema := bundleConfig.Data.TaskBenchDB.DbName
nowMonth := time.Now().Format("2006-01")
taskType, billingType, orderFilter, latestOrderFilter := taskStatsQueryOptions(req.BundleTaskType)
taskType, _, orderFilter, latestOrderFilter := taskStatsQueryOptions(req.BundleTaskType)
cte := fmt.Sprintf(`WITH
-- 1. 获取当前套餐类型下参与统计的订单基础套餐取每个用户最新普通订单先用后付取所有待付款/已付款订单
target_orders AS (
SELECT bor.id, bor.uuid, bor.customer_id, bor.customer_num, bor.created_at
SELECT bor.id, bor.uuid, bor.order_no, bor.customer_id, bor.customer_num, bor.created_at
FROM bundle_order_records bor
WHERE bor.deleted_at IS NULL
AND bor.customer_id IS NOT NULL
@ -579,6 +578,7 @@ active_order_balances AS (
u.id AS user_id,
u.tel_num COLLATE utf8mb4_0900_ai_ci AS phone,
bor.uuid AS order_uuid,
bor.order_no,
bor.customer_num COLLATE utf8mb4_0900_ai_ci AS customer_num,
bb.start_at,
bb.expired_at,
@ -694,17 +694,16 @@ balance_sum AS (
GROUP BY bb2.user_id
),
-- 6. 作品统计只统计 origin_uuid 为空的作品
-- 6. 作品统计按订单归属统计避免重叠时间窗口重复计算
cw_agg AS (
SELECT
aw.user_id,
COUNT(CASE WHEN cw.work_category = 2 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aw.start_at AND aw.expired_at THEN 1 END) AS uploaded_video_count,
COUNT(CASE WHEN cw.work_category = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aw.start_at AND aw.expired_at THEN 1 END) AS uploaded_post_count
FROM active_windows aw
LEFT JOIN cast_work cw ON cw.artist_phone = aw.phone
LEFT JOIN cast_work_extra cwe ON cwe.work_uuid = cw.uuid AND cwe.deleted_at = 0
WHERE (cw.uuid IS NULL OR cw.work_category != 2 OR IFNULL(cwe.billing_type, 0) = ?)
GROUP BY aw.user_id
aob.user_id,
COUNT(DISTINCT CASE WHEN cw.work_category = 2 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aob.start_at AND aob.expired_at THEN cw.uuid END) AS uploaded_video_count,
COUNT(DISTINCT CASE WHEN cw.work_category = 1 AND cw.deleted_at = 0 AND (cw.origin_uuid = '' OR cw.origin_uuid IS NULL) AND cw.submit_time BETWEEN aob.start_at AND aob.expired_at THEN cw.uuid END) AS uploaded_post_count
FROM active_order_balances aob
LEFT JOIN cast_work_extra cwe ON cwe.bundle_order_uuid = aob.order_uuid AND cwe.deleted_at = 0
LEFT JOIN cast_work cw ON cw.uuid = cwe.work_uuid
GROUP BY aob.user_id
),
-- 7. 数据分析作品统计排除 work_analysis_status = 1 的数据分析
@ -727,18 +726,17 @@ ccr_agg AS (
GROUP BY aw.user_id
),
-- 9. 视频脚本统计
-- 9. 视频脚本统计按订单归属统计缺失订单号的数据不纳入当前有效订单
cvs_agg AS (
SELECT
aw.user_id,
COUNT(cvs.artist_uuid) AS uploaded_video_script_count
FROM active_windows aw
LEFT JOIN cast_video_script cvs ON CAST(aw.user_id AS CHAR) = cvs.artist_uuid
AND cvs.deleted_at = 0
AND cvs.billing_type = ?
AND cvs.created_at BETWEEN UNIX_TIMESTAMP(CONVERT_TZ(aw.start_at, '+00:00', '+00:00'))
AND UNIX_TIMESTAMP(CONVERT_TZ(aw.expired_at, '+00:00', '+00:00'))
GROUP BY aw.user_id
aob.user_id,
COUNT(DISTINCT cvs.uuid) AS uploaded_video_script_count
FROM active_order_balances aob
LEFT JOIN cast_video_script cvs ON cvs.order_no = aob.order_no
AND cvs.deleted_at = 0
AND cvs.created_at BETWEEN UNIX_TIMESTAMP(CONVERT_TZ(aob.start_at, '+00:00', '+00:00'))
AND UNIX_TIMESTAMP(CONVERT_TZ(aob.expired_at, '+00:00', '+00:00'))
GROUP BY aob.user_id
),
-- 9. 已指派且未完成的数量统计actual_status=1未完成或actual_status=3已中止排除actual_status=2实际已完成的
@ -792,8 +790,8 @@ LEFT JOIN ccr_agg ccr ON ccr.user_id = aw.user_id
LEFT JOIN cvs_agg cvs ON cvs.user_id = aw.user_id
LEFT JOIN assigned_pending_agg apa ON apa.user_id = aw.user_id`
args := make([]interface{}, 0, 5)
args = append(args, req.SubNums, nowMonth, billingType, billingType, taskType)
args := make([]interface{}, 0, 3)
args = append(args, req.SubNums, nowMonth, taskType)
// 使用事务固定同一连接,仅对本次查询的 session 设置 collation不影响其他接口
tx2 := app.ModuleClients.BundleDB.Begin()