-- ============================================
-- 更新补录数据的领取时间为2025-11-16
-- 适用场景：2026-01-16补录的数据，需要将领取时间改为2025-11-16
-- ============================================

-- ============================================
-- 第一步：查询今天补录的数据（请先执行此查询确认数据）
-- ============================================

-- 查询今天创建的所有申请记录
SELECT 
    F_Id,
    F_UsageBatchId,
    F_ApplicationStoreId,
    F_ApplicationTime,
    F_ReceiveTime as CurrentReceiveTime,
    F_IsReceived,
    F_ApprovalStatus,
    F_UpdateTime
FROM lq_inventory_usage_application
WHERE DATE(F_ApplicationTime) = '2026-01-16'
    AND F_IsEffective = 1
ORDER BY F_ApplicationTime DESC;

-- 查询今天标记为已领取的记录
SELECT 
    F_Id,
    F_UsageBatchId,
    F_ApplicationStoreId,
    F_ApplicationTime,
    F_ReceiveTime as CurrentReceiveTime,
    F_IsReceived,
    F_ApprovalStatus,
    F_UpdateTime
FROM lq_inventory_usage_application
WHERE DATE(F_ReceiveTime) = '2026-01-16'
    AND F_IsEffective = 1
    AND F_IsReceived = 1
ORDER BY F_ReceiveTime DESC;

-- 查询今天更新的所有记录
SELECT 
    F_Id,
    F_UsageBatchId,
    F_ApplicationStoreId,
    F_ApplicationTime,
    F_ReceiveTime as CurrentReceiveTime,
    F_IsReceived,
    F_ApprovalStatus,
    F_UpdateTime
FROM lq_inventory_usage_application
WHERE DATE(F_UpdateTime) = '2026-01-16'
    AND F_IsEffective = 1
ORDER BY F_UpdateTime DESC;

-- ============================================
-- 第二步：执行更新（请根据第一步的查询结果选择对应的方案）
-- ============================================

-- 方案1：更新今天创建的申请记录的领取时间（如果已领取）
-- 适用场景：今天创建的申请，今天标记为已领取，需要将领取时间改为2025-11-16
UPDATE lq_inventory_usage_application
SET F_ReceiveTime = '2025-11-16 00:00:00',
    F_UpdateTime = NOW(),
    F_UpdateUser = 'admin'
WHERE DATE(F_ApplicationTime) = '2026-01-16'
    AND F_IsEffective = 1
    AND F_IsReceived = 1
    AND F_ReceiveTime IS NOT NULL;

-- 方案2：更新今天标记为已领取的记录的领取时间
-- 适用场景：今天标记为已领取的记录（可能是之前创建的申请）
UPDATE lq_inventory_usage_application
SET F_ReceiveTime = '2025-11-16 00:00:00',
    F_UpdateTime = NOW(),
    F_UpdateUser = 'admin'
WHERE DATE(F_ReceiveTime) = '2026-01-16'
    AND F_IsEffective = 1
    AND F_IsReceived = 1;

-- 方案3：更新今天更新的记录的领取时间（如果已领取）
-- 适用场景：今天更新的记录（可能是补录或修改的数据）
UPDATE lq_inventory_usage_application
SET F_ReceiveTime = '2025-11-16 00:00:00',
    F_UpdateTime = NOW(),
    F_UpdateUser = 'admin'
WHERE DATE(F_UpdateTime) = '2026-01-16'
    AND F_IsEffective = 1
    AND F_IsReceived = 1
    AND F_ReceiveTime IS NOT NULL;

-- 方案4：根据批次ID更新（如果知道具体的批次ID）
-- 适用场景：知道具体的批次ID，需要更新特定批次的领取时间
-- UPDATE lq_inventory_usage_application
-- SET F_ReceiveTime = '2025-11-16 00:00:00',
--     F_UpdateTime = NOW(),
--     F_UpdateUser = 'admin'
-- WHERE F_UsageBatchId IN ('批次ID1', '批次ID2', '批次ID3')
--     AND F_IsEffective = 1
--     AND F_IsReceived = 1;

-- ============================================
-- 第三步：验证更新结果
-- ============================================

-- 验证：查询更新后的数据
SELECT 
    F_Id,
    F_UsageBatchId,
    F_ApplicationStoreId,
    F_ApplicationTime,
    F_ReceiveTime,
    F_IsReceived,
    F_ApprovalStatus,
    F_UpdateTime
FROM lq_inventory_usage_application
WHERE (DATE(F_ApplicationTime) = '2026-01-16' 
    OR DATE(F_ReceiveTime) = '2026-01-16'
    OR DATE(F_UpdateTime) = '2026-01-16')
    AND F_IsEffective = 1
    AND F_IsReceived = 1
    AND DATE(F_ReceiveTime) = '2025-11-16'
ORDER BY F_UpdateTime DESC;

-- 统计更新后的数据
SELECT 
    DATE(F_ReceiveTime) as ReceiveDate,
    COUNT(*) as Count,
    COUNT(DISTINCT F_UsageBatchId) as BatchCount
FROM lq_inventory_usage_application
WHERE (DATE(F_ApplicationTime) = '2026-01-16' 
    OR DATE(F_UpdateTime) = '2026-01-16')
    AND F_IsEffective = 1
    AND F_IsReceived = 1
    AND F_ReceiveTime IS NOT NULL
GROUP BY DATE(F_ReceiveTime)
ORDER BY ReceiveDate DESC;

-- ============================================
-- 第四步：更新后需要重新计算的数据
-- ============================================

-- 注意：更新领取时间为2025-11-16后，需要重新计算以下数据：

-- 1. 2025年11月的工资（因为领取时间改为11月，会影响11月的产品物料计算）
--    店长工资：
--    POST /api/Extend/lqstoremanagersalary/calculate/store-manager?year=2025&month=11
--    
--    主任工资：
--    POST /api/Extend/lqdirectorsalary/calculate/director?year=2025&month=11
--    
--    事业部总经理/经理工资：
--    POST /api/Extend/lqbusinessunitmanagersalary/calculate/business-unit-manager?year=2025&month=11

-- 2. 2025年12月的工资（因为计算12月工资时使用11月数据）
--    店长工资：
--    POST /api/Extend/lqstoremanagersalary/calculate/store-manager?year=2025&month=12
--    
--    主任工资：
--    POST /api/Extend/lqdirectorsalary/calculate/director?year=2025&month=12
--    
--    事业部总经理/经理工资：
--    POST /api/Extend/lqbusinessunitmanagersalary/calculate/business-unit-manager?year=2025&month=12

-- 3. 2025年11月的股份统计（因为产品成本基于领取时间）
--    POST /api/Extend/lqsharestatisticsstore/generate
--    Body: {"statisticsMonth":"202511"}

-- ============================================
-- 执行说明：
-- 1. 先执行第一步的查询语句，确认要更新的数据
-- 2. 根据查询结果，选择对应的更新方案（方案1/2/3/4）
-- 3. 执行更新语句
-- 4. 执行第三步的验证查询，确认更新成功
-- 5. 根据实际情况，重新计算相关的工资和股份统计数据
-- ============================================
