<?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.PmsProjectMapper">

    <resultMap type="com.microservices.pms.project.domain.PmsProject" id="PmsProjectResult">
        <result property="id" column="id"/>
        <result property="projectIdentifier" column="project_identifier"/>
        <result property="projectName" column="project_name"/>
        <result property="projectAssigneeId" column="project_assignee_id"/>
        <result property="pmsEnterpriseId" column="pms_enterprise_id"/>
        <result property="pmsProductId" column="pms_product_id"/>
        <result property="projectDescription" column="project_description"/>
        <result property="status" column="status"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="isTop" column="is_top"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="isDeleted" column="is_deleted"/>
        <result property="noticeId" column="notice_id"/>
        <result property="reservedField2" column="reserved_field_2"/>
        <result property="deptId" column="dept_id"/>
    </resultMap>

    <sql id="selectPmsProjectVo">
        select id,
               project_identifier,
               project_name,
               project_assignee_id,
               pms_enterprise_id,
               pms_product_id,
               project_description,
               status,
               start_time,
               end_time,
               is_top,
               create_time,
               update_time,
               is_deleted,
               notice_id,
               reserved_field_2,
               dept_id
        from pms_project
    </sql>

    <sql id="selectPmsProjectDataScopeVo">
        select ppj.id,
        ppj.project_identifier,
        ppj.project_name,
        ppj.project_assignee_id,
        ppj.pms_enterprise_id,
        ppj.pms_product_id,
        ppj.project_description,
        ppj.status,
        ppj.start_time,
        ppj.end_time,
        ppj.is_top,
        ppj.create_time,
        ppj.update_time,
        ppj.is_deleted,
        ppj.notice_id,
        ppj.reserved_field_2,
        ppj.dept_id
        from pms_project ppj
        <if test="params.dataScopeJoin">
            left join pms_project_members ppm on ppj.id = ppm.pms_project_id
        </if>

    </sql>

    <select id="selectPmsProjectList" parameterType="PmsProject" resultMap="PmsProjectResult">
        <include refid="selectPmsProjectDataScopeVo"/>
        <where>
            <if test="projectIdentifier != null  and projectIdentifier != ''">and ppj.project_identifier =
                #{projectIdentifier}
            </if>
            <if test="projectName != null  and projectName != ''">and ppj.project_name like concat('%', #{projectName},
                '%')
            </if>
            <if test="projectAssigneeId != null ">and ppj.project_assignee_id = #{projectAssigneeId}</if>
            <if test="id != null ">and ppj.id = #{id}</if>
            <if test="pmsEnterpriseId != null ">and ppj.pms_enterprise_id = #{pmsEnterpriseId}</if>
            <if test="pmsProductId != null ">and ppj.pms_product_id = #{pmsProductId}</if>
            <if test="projectDescription != null  and projectDescription != ''">and ppj.project_description =
                #{projectDescription}
            </if>
            <if test="status != null  and status != ''">and ppj.status = #{status}</if>
            <if test="startTime != null ">and ppj.start_time = #{startTime}</if>
            <if test="endTime != null ">and ppj.end_time = #{endTime}</if>
            <if test="isTop != null ">and ppj.is_top = #{isTop}</if>
            <if test="createTime != null ">and ppj.create_time = #{createTime}</if>
            <if test="updateTime != null ">and ppj.update_time = #{updateTime}</if>
            <if test="isDeleted != null ">and ppj.is_deleted = #{isDeleted}</if>
            <if test="noticeId != null  and noticeId != ''">and ppj.notice_id = #{noticeId}
            </if>
            <if test="reservedField2 != null  and reservedField2 != ''">and ppj.reserved_field_2 = #{reservedField2}
            </if>
            <if test="deptId != null  and deptId != ''">and ppj.dept_id = #{deptId}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </select>

    <select id="selectPmsProjectById" resultMap="PmsProjectResult">
        <include refid="selectPmsProjectVo"/>
        where id = #{id}
    </select>


    <select id="selectPmsProjectListByNoDataScope" resultType="com.microservices.pms.project.domain.PmsProject">
        <include refid="selectPmsProjectVo"/>
        <where>
            <if test="projectIdentifier != null  and projectIdentifier != ''">and project_identifier =
                #{projectIdentifier}
            </if>
            <if test="projectName != null  and projectName != ''">and project_name like concat('%', #{projectName},
                '%')
            </if>
            <if test="projectAssigneeId != null ">and project_assignee_id = #{projectAssigneeId}</if>
            <if test="id != null ">and id = #{id}</if>
            <if test="pmsEnterpriseId != null ">and pms_enterprise_id = #{pmsEnterpriseId}</if>
            <if test="pmsProductId != null ">and pms_product_id = #{pmsProductId}</if>
            <if test="status != null  and status != ''">and `status` = #{status}</if>
            <if test="isDeleted != null ">and is_deleted = #{isDeleted}</if>
            <if test="noticeId != null  and noticeId != ''">and notice_id = #{noticeId}</if>
            <if test="deptId != null  and deptId != ''">and dept_id = #{deptId}</if>
        </where>
    </select>

    <sql id="selectWhereBySearchVo">
        <where>
            <if test="projectName != null  and projectName != ''">and ppj.project_name like concat('%', #{projectName},
                '%')
            </if>
            <if test="projectAssigneeId != null ">and ppj.project_assignee_id = #{projectAssigneeId}</if>
            <if test="pmsProductId != null ">and ppj.pms_product_id = #{pmsProductId}</if>
            <if test="status != null  and status != ''">and ppj.status = #{status}</if>
            <!-- 数据范围过滤 -->
            ${params.dataScope}
        </where>
    </sql>
    <select id="selectPmsProjectListBySearchInput"
            parameterType="com.microservices.pms.project.domain.vo.PmsProjectSearchVo"
            resultMap="PmsProjectResult">
        <include refid="selectPmsProjectDataScopeVo"/>
        <include refid="selectWhereBySearchVo"/>
        order by ppj.is_top desc
        <choose>
            <when test="isDescByUpdateTime">
                ,
                CASE
                WHEN ppj.status = '1' THEN 1
                WHEN ppj.status = '2' THEN 2
                ELSE 3
                END,
                ppj.update_time desc
            </when>
            <otherwise>
                ,
                CASE
                WHEN ppj.status = '1' THEN 1
                WHEN ppj.status = '2' THEN 2
                ELSE 3
                END,
                ppj.update_time asc
            </otherwise>
        </choose>
    </select>
    <select id="selectPmsProjectCount" resultType="java.lang.Integer">
        select count(*)
        from pms_project
        <include refid="selectWhereBySearchVo"/>
    </select>
    <select id="selectProjectIdsByAssigneeUserId" resultType="java.lang.Long">
        select pp.id
        from pms_project pp
        left join pms_enterprise pe on pe.id = pp.pms_enterprise_id
        where pp.project_assignee_id = #{gitlinkUserId}
        and pe.enterprise_identifier = #{enterpriseIdentifier}
    </select>
    <select id="selectEnterpriseIdentifierByProjectId" resultType="java.lang.String">
        select pe.enterprise_identifier
        from pms_project pp
        left join pms_enterprise pe on pe.id = pp.pms_enterprise_id
        where pp.id = #{pmProjectId}
    </select>

    <insert id="insertPmsProject" parameterType="PmsProject" useGeneratedKeys="true" keyProperty="id">
        insert into pms_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectIdentifier != null">project_identifier,</if>
            <if test="projectName != null and projectName != ''">project_name,</if>
            <if test="projectAssigneeId != null">project_assignee_id,</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id,</if>
            <if test="pmsProductId != null">pms_product_id,</if>
            <if test="projectDescription != null and projectDescription != ''">project_description,</if>
            <if test="status != null">status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="isTop != null">is_top,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDeleted != null">is_deleted,</if>
            <if test="noticeId != null">notice_id,</if>
            <if test="reservedField2 != null">reserved_field_2,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectIdentifier != null">#{projectIdentifier},</if>
            <if test="projectName != null and projectName != ''">#{projectName},</if>
            <if test="projectAssigneeId != null">#{projectAssigneeId},</if>
            <if test="pmsEnterpriseId != null">#{pmsEnterpriseId},</if>
            <if test="pmsProductId != null">#{pmsProductId},</if>
            <if test="projectDescription != null and projectDescription != ''">#{projectDescription},</if>
            <if test="status != null">#{status},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="isTop != null">#{isTop},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDeleted != null">#{isDeleted},</if>
            <if test="noticeId != null">#{noticeId},</if>
            <if test="reservedField2 != null">#{reservedField2},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updatePmsProject" parameterType="PmsProject">
        update pms_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectIdentifier != null">project_identifier = #{projectIdentifier},</if>
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
            <if test="projectAssigneeId != null">project_assignee_id = #{projectAssigneeId},</if>
            <if test="pmsEnterpriseId != null">pms_enterprise_id = #{pmsEnterpriseId},</if>
            <if test="pmsProductId != null">pms_product_id = #{pmsProductId},</if>
            <if test="projectDescription != null and projectDescription != ''">project_description =
                #{projectDescription},
            </if>
            <if test="status != null">status = #{status},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDeleted != null">is_deleted = #{isDeleted},</if>
            <if test="noticeId != null">notice_id = #{noticeId},</if>
            <if test="reservedField2 != null">reserved_field_2 = #{reservedField2},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="updatePmsProjectNoProduct">
        update pms_project
        SET pms_product_id=NULL
        where id in
        <foreach item="id" collection="projectIdList" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <update id="updatePmsProjectByUpdateInput"
            parameterType="com.microservices.pms.project.domain.vo.PmsProjectUpdateVo">
        update pms_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
            <if test="projectAssigneeId != null">project_assignee_id = #{projectAssigneeId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isTop != null">is_top = #{isTop},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <choose>
                <when test="isResetDescriptionToNull">
                    project_description = NULL,
                </when>
                <otherwise>
                    <if test="projectDescription != null and projectDescription != ''">
                        project_description = #{projectDescription},
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="isResetPmsProductIdToNull">
                    pms_product_id = NULL,
                </when>
                <otherwise>
                    <if test="pmsProductId != null">
                    pms_product_id = #{pmsProductId},
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="isResetStartTimeToNull">
                    start_time = NULL,
                </when>
                <otherwise>
                    <if test="startTime != null">
                        start_time = #{startTime},
                    </if>
                </otherwise>
            </choose>
            <choose>
                <when test="isResetEndTimeToNull">
                    end_time = NULL,
                </when>
                <otherwise>
                    <if test="endTime != null">
                        end_time = #{endTime},
                    </if>
                </otherwise>
            </choose>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProjectById" parameterType="Long">
        delete
        from pms_project
        where id = #{id}
    </delete>

    <delete id="deletePmsProjectByIds" parameterType="String">
        delete from pms_project where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>