<?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.product.mapper.PmsProductPlanMapper">
    
    <resultMap type="PmsProductPlan" id="PmsProductPlanResult">
        <result property="id"    column="id"    />
        <result property="planTitle"    column="plan_title"    />
        <result property="pmsProductIdentifier"    column="pms_product_identifier"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="planDescription"    column="plan_description"    />
        <result property="statusId"    column="status_id"    />
        <result property="priorityId"    column="priority_id"    />
        <result property="workload"    column="workload"    />
        <result property="sort"    column="sort"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="reservedField1"    column="reserved_field_1"    />
        <result property="reservedField2"    column="reserved_field_2"    />
        <result property="productRequirementCount"    column="product_requirement_count"    />
    </resultMap>

    <sql id="selectPmsProductPlanVo">
        select id, plan_title, pms_product_identifier, start_time, end_time, plan_description, status_id, priority_id, workload, sort, update_time, update_by, create_time, create_by, del_flag, reserved_field_1, reserved_field_2 from pms_product_plan
    </sql>

    <sql id="selectPmsProductPlanAliasVo">
        SELECT
            ppp.id,
            ppp.plan_title,
            ppp.pms_product_identifier,
            ppp.start_time,
            ppp.end_time,
            ppp.plan_description,
            ppp.status_id,
            ppp.priority_id,
            ppp.workload,
            ppp.sort,
            ppp.update_time,
            ppp.update_by,
            ppp.create_time,
            ppp.create_by,
            ppp.del_flag,
            ppp.reserved_field_1,
            ppp.reserved_field_2,
            (select count(0) from pms_product_requirement ppr where ppp.id=ppr.pms_product_plan_id) as product_requirement_count
        FROM
            pms_product_plan ppp
    </sql>

    <select id="selectPmsProductPlanList" parameterType="PmsProductPlan" resultMap="PmsProductPlanResult">
        <include refid="selectPmsProductPlanAliasVo"/>
        <include refid="selectWhere"/>
    </select>
    <sql id="selectWhere">
        <where>
            <if test="planTitle != null  and planTitle != ''"> and ppp.plan_title like concat('%', #{planTitle}, '%')</if>
            <if test="pmsProductIdentifier != null "> and ppp.pms_product_identifier = #{pmsProductIdentifier}</if>
            <if test="startTime != null "> and ppp.start_time = #{startTime}</if>
            <if test="endTime != null "> and ppp.end_time = #{endTime}</if>
            <if test="planDescription != null  and planDescription != ''"> and ppp.plan_description = #{planDescription}</if>
            <if test="statusId != null"> and ppp.status_id = #{statusId}</if>
            <if test="priorityId != null"> and ppp.priority_id = #{priorityId}</if>
            <if test="workload != null  and workload != ''"> and workload = #{workload}</if>
            <if test="sort != null "> and ppp.sort = #{sort}</if>
            <if test="reservedField1 != null  and reservedField1 != ''"> and ppp.reserved_field_1 = #{reservedField1}</if>
            <if test="reservedField2 != null  and reservedField2 != ''"> and ppp.reserved_field_2 = #{reservedField2}</if>
        </where>
        order by ppp.update_time desc
    </sql>
    
    <select id="selectPmsProductPlanById" parameterType="Long" resultMap="PmsProductPlanResult">
        <include refid="selectPmsProductPlanAliasVo"/>
        where id = #{id}
    </select>
    <select id="selectPmsProductPlanCount" resultType="java.lang.Integer">
        select count(0) from pms_product_plan as ppp
        <include refid="selectWhere"/>
    </select>

    <insert id="insertPmsProductPlan" parameterType="PmsProductPlan" useGeneratedKeys="true" keyProperty="id">
        insert into pms_product_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planTitle != null and planTitle != ''">plan_title,</if>
            <if test="pmsProductIdentifier != null">pms_product_identifier,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="planDescription != null and planDescription != ''">plan_description,</if>
            <if test="statusId != null">status_id,</if>
            <if test="priorityId != null">priority_id,</if>
            <if test="workload != null">workload,</if>
            <if test="sort != null">sort,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="reservedField1 != null">reserved_field_1,</if>
            <if test="reservedField2 != null">reserved_field_2,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planTitle != null and planTitle != ''">#{planTitle},</if>
            <if test="pmsProductIdentifier != null">#{pmsProductIdentifier},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="planDescription != null and planDescription != ''">#{planDescription},</if>
            <if test="statusId != null">#{statusId},</if>
            <if test="priorityId != null">#{priorityId},</if>
            <if test="workload != null">#{workload},</if>
            <if test="sort != null">#{sort},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="reservedField1 != null">#{reservedField1},</if>
            <if test="reservedField2 != null">#{reservedField2},</if>
         </trim>
    </insert>

    <update id="updatePmsProductPlan" parameterType="PmsProductPlan">
        update pms_product_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="planTitle != null and planTitle != ''">plan_title = #{planTitle},</if>
            <if test="pmsProductIdentifier != null">pms_product_identifier = #{pmsProductIdentifier},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="planDescription != null and planDescription != ''">plan_description = #{planDescription},</if>
            <if test="statusId != null">status_id = #{statusId},</if>
            <if test="priorityId != null">priority_id = #{priorityId},</if>
            <if test="workload != null">workload = #{workload},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="reservedField1 != null">reserved_field_1 = #{reservedField1},</if>
            <if test="reservedField2 != null">reserved_field_2 = #{reservedField2},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePmsProductPlanById" parameterType="Long">
        delete from pms_product_plan where id = #{id}
    </delete>

    <delete id="deletePmsProductPlanByIds" parameterType="String">
        delete from pms_product_plan where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deletePmsProductPlanByProductIdentifier">
        delete
        from pms_product_plan
        where pms_product_identifier = #{productIdentifier}
    </delete>
</mapper>