package com.dora.mapper;

import com.dora.entity.WorkflowLike;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 工作流点赞Mapper接口
 */
@Mapper
public interface WorkflowLikeMapper {

    /**
     * 根据用户ID和工作流ID查询点赞记录
     * @param userId 用户ID
     * @param workflowId 工作流ID
     * @return 点赞记录
     */
    @Select("SELECT * FROM workflow_like WHERE user_id = #{userId} AND workflow_id = #{workflowId} AND is_deleted = 0")
    WorkflowLike selectByUserAndWorkflow(@Param("userId") Long userId, @Param("workflowId") Long workflowId);

    /**
     * 根据用户ID和工作流ID查询点赞记录（包含已删除的记录）
     * @param userId 用户ID
     * @param workflowId 工作流ID
     * @return 点赞记录
     */
    @Select("SELECT * FROM workflow_like WHERE user_id = #{userId} AND workflow_id = #{workflowId}")
    WorkflowLike selectByUserAndWorkflowIncludeDeleted(@Param("userId") Long userId, @Param("workflowId") Long workflowId);

    /**
     * 插入点赞记录
     * @param workflowLike 点赞记录
     * @return 插入结果
     */
    @Insert("INSERT IGNORE INTO workflow_like (user_id, workflow_id, create_time, is_deleted) VALUES (#{userId}, #{workflowId}, NOW(), 0)")
    int insert(WorkflowLike workflowLike);

    /**
     * 逻辑删除点赞记录
     * @param userId 用户ID
     * @param workflowId 工作流ID
     * @return 删除结果
     */
    @Update("UPDATE workflow_like SET is_deleted = 1 WHERE user_id = #{userId} AND workflow_id = #{workflowId}")
    int deleteByUserAndWorkflow(@Param("userId") Long userId, @Param("workflowId") Long workflowId);

    /**
     * 恢复已删除的点赞记录
     * @param userId 用户ID
     * @param workflowId 工作流ID
     * @return 恢复结果
     */
    @Update("UPDATE workflow_like SET is_deleted = 0 WHERE user_id = #{userId} AND workflow_id = #{workflowId}")
    int restoreByUserAndWorkflow(@Param("userId") Long userId, @Param("workflowId") Long workflowId);

    /**
     * 统计工作流的点赞数量
     * @param workflowId 工作流ID
     * @return 点赞数量
     */
    @Select("SELECT COUNT(*) FROM workflow_like WHERE workflow_id = #{workflowId} AND is_deleted = 0")
    int countByWorkflowId(@Param("workflowId") Long workflowId);

    /**
     * 获取用户点赞的工作流列表（包含工作流信息）
     * @param userId 用户ID
     * @param offset 偏移量
     * @param size 每页大小
     * @return 点赞记录列表（包含工作流信息）
     */
    @Select("SELECT wl.id, wl.user_id, wl.workflow_id, wl.create_time, " +
            "w.name as workflow_name, w.description as workflow_description, w.cover_url as workflow_cover " +
            "FROM workflow_like wl " +
            "LEFT JOIN workflow w ON wl.workflow_id = w.id " +
            "WHERE wl.user_id = #{userId} AND wl.is_deleted = 0 " +
            "GROUP BY wl.id " +
            "ORDER BY wl.create_time DESC LIMIT #{offset}, #{size}")
    List<WorkflowLikeWithInfo> selectByUserIdWithWorkflowInfo(@Param("userId") Long userId, @Param("offset") int offset, @Param("size") int size);

    /**
     * 获取用户点赞的工作流列表（原始方法，保持兼容性）
     * @param userId 用户ID
     * @param offset 偏移量
     * @param size 每页大小
     * @return 点赞记录列表
     */
    @Select("SELECT * FROM workflow_like WHERE user_id = #{userId} AND is_deleted = 0 ORDER BY create_time DESC LIMIT #{offset}, #{size}")
    List<WorkflowLike> selectByUserId(@Param("userId") Long userId, @Param("offset") int offset, @Param("size") int size);

    /**
     * 统计用户点赞的工作流数量
     * @param userId 用户ID
     * @return 点赞数量
     */
    @Select("SELECT COUNT(*) FROM workflow_like WHERE user_id = #{userId} AND is_deleted = 0")
    int countByUserId(@Param("userId") Long userId);

    /**
     * 检查是否存在重复的点赞记录
     * @param userId 用户ID
     * @param workflowId 工作流ID
     * @return 重复记录数量
     */
    @Select("SELECT COUNT(*) FROM workflow_like WHERE user_id = #{userId} AND workflow_id = #{workflowId} AND is_deleted = 0")
    int countDuplicateLikes(@Param("userId") Long userId, @Param("workflowId") Long workflowId);

    /**
     * 删除重复的点赞记录（保留最早的一条）
     * @param userId 用户ID
     * @param workflowId 工作流ID
     * @return 删除的记录数
     */
    @Update("DELETE wl1 FROM workflow_like wl1 " +
            "INNER JOIN workflow_like wl2 ON wl1.user_id = wl2.user_id AND wl1.workflow_id = wl2.workflow_id " +
            "WHERE wl1.user_id = #{userId} AND wl1.workflow_id = #{workflowId} " +
            "AND wl1.is_deleted = 0 AND wl2.is_deleted = 0 " +
            "AND wl1.id > wl2.id")
    int deleteDuplicateLikes(@Param("userId") Long userId, @Param("workflowId") Long workflowId);

    /**
     * 工作流点赞记录（包含工作流信息）
     */
    class WorkflowLikeWithInfo {
        private Long id;
        private Long userId;
        private Long workflowId;
        private String workflowName;
        private String workflowDescription;
        private String workflowCover;
        private java.time.LocalDateTime createTime;

        // Getters and Setters
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }
        
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        
        public Long getWorkflowId() { return workflowId; }
        public void setWorkflowId(Long workflowId) { this.workflowId = workflowId; }
        
        public String getWorkflowName() { return workflowName; }
        public void setWorkflowName(String workflowName) { this.workflowName = workflowName; }
        
        public String getWorkflowDescription() { return workflowDescription; }
        public void setWorkflowDescription(String workflowDescription) { this.workflowDescription = workflowDescription; }
        
        public String getWorkflowCover() { return workflowCover; }
        public void setWorkflowCover(String workflowCover) { this.workflowCover = workflowCover; }
        
        public java.time.LocalDateTime getCreateTime() { return createTime; }
        public void setCreateTime(java.time.LocalDateTime createTime) { this.createTime = createTime; }
    }
} 