<?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.microservices.pms.project.mapper.ForgeIssuesMapper">
    <resultMap id="BaseResultMap" type="com.microservices.pms.project.domain.ForgeIssues">
        <id column="id" property="id" jdbcType="INTEGER" />
        <result column="tracker_id" property="trackerId" jdbcType="INTEGER" />
        <result column="project_id" property="projectId" jdbcType="INTEGER" />
        <result column="subject" property="subject" jdbcType="VARCHAR" />
        <result column="due_date" property="dueDate" jdbcType="DATE" />
        <result column="category_id" property="categoryId" jdbcType="INTEGER" />
        <result column="status_id" property="statusId" jdbcType="INTEGER" />
        <result column="assigned_to_id" property="assignedToId" jdbcType="INTEGER" />
        <result column="priority_id" property="priorityId" jdbcType="INTEGER" />
        <result column="fixed_version_id" property="fixedVersionId" jdbcType="INTEGER" />
        <result column="author_id" property="authorId" jdbcType="INTEGER" />
        <result column="created_on" property="createdOn" jdbcType="TIMESTAMP" />
        <result column="updated_on" property="updatedOn" jdbcType="TIMESTAMP" />
        <result column="start_date" property="startDate" jdbcType="DATE" />
        <result column="done_ratio" property="doneRatio" jdbcType="INTEGER" />
        <result column="estimated_hours" property="estimatedHours" jdbcType="REAL" />
        <result column="parent_id" property="parentId" jdbcType="INTEGER" />
        <result column="root_id" property="rootId" jdbcType="INTEGER" />
        <result column="lft" property="lft" jdbcType="INTEGER" />
        <result column="rgt" property="rgt" jdbcType="INTEGER" />
        <result column="is_private" property="isPrivate" jdbcType="BIT" />
        <result column="closed_on" property="closedOn" jdbcType="TIMESTAMP" />
        <result column="project_issues_index" property="projectIssuesIndex" jdbcType="INTEGER" />
        <result column="issue_type" property="issueType" jdbcType="VARCHAR" />
        <result column="token" property="token" jdbcType="INTEGER" />
        <result column="issue_tags_value" property="issueTagsValue" jdbcType="VARCHAR" />
        <result column="is_lock" property="isLock" jdbcType="BIT" />
        <result column="issue_classify" property="issueClassify" jdbcType="VARCHAR" />
        <result column="ref_name" property="refName" jdbcType="VARCHAR" />
        <result column="branch_name" property="branchName" jdbcType="VARCHAR" />
        <result column="description" property="description" jdbcType="VARCHAR" />
        <result column="blockchain_token_num" property="blockchainTokenNum" jdbcType="INTEGER" />
        <result column="pm_project_id" property="pmProjectId" jdbcType="BIGINT" />
        <result column="pm_sprint_id" property="pmSprintId" jdbcType="BIGINT" />
        <result column="pm_issue_type" property="pmIssueType" jdbcType="INTEGER" />
        <result column="time_scale" property="timeScale" jdbcType="DECIMAL" />
        <result column="child_count" property="childCount" jdbcType="INTEGER" />
        <result column="changer_id" property="changerId" jdbcType="BIGINT" />
        <result column="fake_id" property="fakeId" jdbcType="INTEGER" />
        <result column="enterprise_identifier" property="enterpriseIdentifier" jdbcType="VARCHAR" />
    </resultMap>
    <update id="updateForIssues">
        UPDATE issues
        set enterprise_identifier = #{enterpriseIdentifier}
        where id = #{id}
    </update>

    <select id="getBriefIssues" resultMap="BaseResultMap">
        SELECT
            id, pm_project_id, enterprise_identifier
        FROM
            issues
        WHERE pm_project_id IS NOT NULL AND (enterprise_identifier IS NULL OR enterprise_identifier = '');
    </select>
    <!-- 查询指定linkableId直接关联的所有工作项 -->
    <select id="selectDirectLinkedIssues" resultType="java.lang.Long">
        SELECT be_linkable_id
        FROM pm_links
        WHERE linkable_id = #{linkableId}
          AND linkable_type = #{linkableType}
          AND be_linkable_type = #{beLinkableType}
    </select>

    <!-- 批量查询多个linkableId关联的所有工作项 -->
    <select id="selectLinkedIssueIdsByLinkableIds" resultType="java.lang.Long">
        SELECT DISTINCT be_linkable_id AS linkedIssueIds
        FROM pm_links
        WHERE linkable_type = #{linkableType}
          AND be_linkable_type = #{beLinkableType}
        AND (linkable_id IN
        <foreach item="item" index="index" collection="linkableIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        OR be_linkable_id IN
        <foreach item="item" index="index" collection="linkableIds" open="(" separator="," close=")">
            #{item}
        </foreach>)
        UNION
        SELECT DISTINCT linkable_id AS linkedIssueIds
        FROM pm_links
        WHERE linkable_type = #{linkableType}
        AND be_linkable_type = #{beLinkableType}
        AND (linkable_id IN
        <foreach item="item" index="index" collection="linkableIds" open="(" separator="," close=")">
            #{item}
        </foreach>
        OR be_linkable_id IN
        <foreach item="item" index="index" collection="linkableIds" open="(" separator="," close=")">
            #{item}
        </foreach>)
    </select>

    <select id="getIssuesLinkedCount" resultType="com.microservices.pms.project.domain.PmsIssuesLinkedCount">
        SELECT
        COUNT(CASE WHEN pm_issue_type = 1 THEN 1 END) AS linkedRequirementsCount,
        COUNT(CASE WHEN pm_issue_type = 2 THEN 1 END) AS linkedTasksCount,
        COUNT(CASE WHEN pm_issue_type = 3 THEN 1 END) AS linkedBugsCount
        FROM issues
        WHERE id IN
        <foreach item="item" index="index" collection="linkIssueIdSet" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>


</mapper>