<?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.dora.mapper.WorkflowCopyStatMapper">

    <select id="countCopiesByOwnerId" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM workflow_copy_stat 
        WHERE owner_id = #{ownerId} AND is_deleted = 0
    </select>

    <select id="countCopiesByWorkflowId" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM workflow_copy_stat 
        WHERE workflow_id = #{workflowId} AND is_deleted = 0
    </select>

    <!-- 统计今日工作流复制次数 -->
    <select id="countTodayCopies" resultType="long">
        SELECT COUNT(*) FROM workflow_copy_stat 
        WHERE DATE(copy_time) = DATE(#{today}) AND is_deleted = 0
    </select>

    <!-- 插入工作流复制记录 -->
    <insert id="insertCopyRecord">
        INSERT INTO workflow_copy_stat (workflow_id, owner_id, copy_user_id, copy_time, is_deleted)
        VALUES (#{workflowId}, #{ownerId}, #{copyUserId}, NOW(), 0)
    </insert>

    <!-- 检查用户是否已经使用过该工作流 -->
    <select id="countUserCopiesByWorkflowId" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM workflow_copy_stat 
        WHERE workflow_id = #{workflowId} AND copy_user_id = #{copyUserId} AND is_deleted = 0
    </select>

    <!-- 统计工作流的不重复用户使用数 -->
    <select id="countUniqueUsersByWorkflowId" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT copy_user_id) FROM workflow_copy_stat 
        WHERE workflow_id = #{workflowId} AND is_deleted = 0
    </select>

    <!-- 统计工作流的不重复用户使用数（排除创作者本人） -->
    <select id="countUniqueUsersByWorkflowIdExcludeOwner" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT copy_user_id) FROM workflow_copy_stat 
        WHERE workflow_id = #{workflowId} AND copy_user_id != #{ownerId} AND is_deleted = 0
    </select>

    <!-- 统计工作流复制次数（排除创作者本人） -->
    <select id="countCopiesByWorkflowIdExcludeOwner" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM workflow_copy_stat 
        WHERE workflow_id = #{workflowId} AND copy_user_id != #{ownerId} AND is_deleted = 0
    </select>

</mapper> 