<?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.ozs.pm.mapper.PmDemandMapper">
    
    <resultMap type="com.ozs.pm.doman.PmDemand" id="PmDemandResult">
        <result property="demandId"    column="demand_id"    />
        <result property="planId"    column="plan_id"    />
        <result property="purchaseDeptId"    column="purchase_dept_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="projectType"    column="project_type"    />
        <result property="demandOverview"    column="demand_overview"    />
        <result property="evaluation"    column="evaluation"    />
        <result property="isExcess"    column="is_excess"    />
        <result property="purchaseServices"    column="purchase_services"    />
        <result property="purchaseMode"    column="purchase_mode"    />
        <result property="planDemandSubTime"    column="plan_demand_sub_time"    />
        <result property="planPurchaseFinishTime"    column="plan_purchase_finish_time"    />
        <result property="planDeliverTime"    column="plan_deliver_time"    />
        <result property="projectAttr"    column="project_attr"    />
        <result property="adjustReason"    column="adjust_reason"    />
        <result property="remarks"    column="remarks"    />
        <result property="realDemandCommitTime"    column="real_demand_commit_time"    />
        <result property="realPurchaseFinishTime"    column="real_purchase_finish_time"    />
        <result property="realDeliverTime"    column="real_deliver_time"    />
        <result property="auditTime"    column="audit_time"    />
        <result property="warnStatus"    column="warn_status"    />
        <result property="projectStatus"    column="project_status"    />
        <result property="refuseReason"    column="refuse_reason"    />
        <result property="delayReason"    column="delay_reason"    />
        <result property="adjustAdvice"    column="adjust_advice"    />
        <result property="agencyId"    column="agency_id"    />
        <result property="extractAgencyTime"    column="extract_agency_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectPmDemandVo">
        select demand_id, plan_id, purchase_dept_id, project_name, project_type, demand_overview, evaluation, is_excess, purchase_services, purchase_mode, plan_demand_sub_time, plan_purchase_finish_time, plan_deliver_time, project_attr, adjust_reason, remarks, real_demand_commit_time, real_purchase_finish_time, real_deliver_time, audit_time, warn_status, project_status, refuse_reason, delay_reason, adjust_advice, agency_id, extract_agency_time, create_by, create_time, update_by, update_time from pm_demand
    </sql>

    <select id="selectPmDemandList" parameterType="com.ozs.pm.doman.PmDemand" resultMap="PmDemandResult">
        <include refid="selectPmDemandVo"/>
        <where>  
            <if test="planId != null "> and plan_id = #{planId}</if>
            <if test="purchaseDeptId != null "> and purchase_dept_id = #{purchaseDeptId}</if>
            <if test="projectName != null  and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if>
            <if test="projectType != null  and projectType != ''"> and project_type = #{projectType}</if>
            <if test="demandOverview != null  and demandOverview != ''"> and demand_overview = #{demandOverview}</if>
            <if test="evaluation != null "> and evaluation = #{evaluation}</if>
            <if test="isExcess != null "> and is_excess = #{isExcess}</if>
            <if test="purchaseServices != null  and purchaseServices != ''"> and purchase_services = #{purchaseServices}</if>
            <if test="purchaseMode != null  and purchaseMode != ''"> and purchase_mode = #{purchaseMode}</if>
            <if test="planDemandSubTime != null "> and plan_demand_sub_time = #{planDemandSubTime}</if>
            <if test="planPurchaseFinishTime != null "> and plan_purchase_finish_time = #{planPurchaseFinishTime}</if>
            <if test="planDeliverTime != null "> and plan_deliver_time = #{planDeliverTime}</if>
            <if test="projectAttr != null  and projectAttr != ''"> and project_attr = #{projectAttr}</if>
            <if test="adjustReason != null  and adjustReason != ''"> and adjust_reason = #{adjustReason}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="realDemandCommitTime != null "> and real_demand_commit_time = #{realDemandCommitTime}</if>
            <if test="realPurchaseFinishTime != null "> and real_purchase_finish_time = #{realPurchaseFinishTime}</if>
            <if test="realDeliverTime != null "> and real_deliver_time = #{realDeliverTime}</if>
            <if test="auditTime != null "> and audit_time = #{auditTime}</if>
            <if test="warnStatus != null  and warnStatus != ''"> and warn_status = #{warnStatus}</if>
            <if test="projectStatus != null "> and project_status = #{projectStatus}</if>
            <if test="refuseReason != null  and refuseReason != ''"> and refuse_reason = #{refuseReason}</if>
            <if test="delayReason != null  and delayReason != ''"> and delay_reason = #{delayReason}</if>
            <if test="adjustAdvice != null  and adjustAdvice != ''"> and adjust_advice = #{adjustAdvice}</if>
            <if test="params.beginDate != null  and params.beginDate != ''">
                and
                (plan_demand_sub_time >= #{params.beginDate} or plan_purchase_finish_time >= #{params.beginDate} or plan_deliver_time >= #{params.beginDate})
            </if>
            <if test="params.endDate != null  and params.endDate != ''">
                and
                (plan_demand_sub_time &lt;= #{params.endDate} or plan_purchase_finish_time &lt;= #{params.endDate} or plan_deliver_time &lt;= #{params.endDate})
            </if>
        </where>
    </select>
    
    <select id="selectPmDemandByDemandId" parameterType="Long" resultMap="PmDemandResult">
        <include refid="selectPmDemandVo"/>
        where demand_id = #{demandId}
    </select>
        
    <insert id="insertPmDemand" parameterType="com.ozs.pm.doman.PmDemand" useGeneratedKeys="true" keyProperty="demandId">
        insert into pm_demand
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="purchaseDeptId != null">purchase_dept_id,</if>
            <if test="projectName != null and projectName != ''">project_name,</if>
            <if test="projectType != null and projectType != ''">project_type,</if>
            <if test="demandOverview != null">demand_overview,</if>
            <if test="evaluation != null">evaluation,</if>
            <if test="isExcess != null">is_excess,</if>
            <if test="purchaseServices != null">purchase_services,</if>
            <if test="purchaseMode != null and purchaseMode != ''">purchase_mode,</if>
            <if test="planDemandSubTime != null">plan_demand_sub_time,</if>
            <if test="planPurchaseFinishTime != null">plan_purchase_finish_time,</if>
            <if test="planDeliverTime != null">plan_deliver_time,</if>
            <if test="projectAttr != null and projectAttr != ''">project_attr,</if>
            <if test="adjustReason != null">adjust_reason,</if>
            <if test="remarks != null">remarks,</if>
            <if test="realDemandCommitTime != null">real_demand_commit_time,</if>
            <if test="realPurchaseFinishTime != null">real_purchase_finish_time,</if>
            <if test="realDeliverTime != null">real_deliver_time,</if>
            <if test="auditTime != null">audit_time,</if>
            <if test="warnStatus != null">warn_status,</if>
            <if test="projectStatus != null">project_status,</if>
            <if test="refuseReason != null">refuse_reason,</if>
            <if test="delayReason != null">delay_reason,</if>
            <if test="adjustAdvice != null">adjust_advice,</if>
            <if test="agencyId != null">agency_id,</if>
            <if test="extractAgencyTime != null">extract_agency_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="purchaseDeptId != null">#{purchaseDeptId},</if>
            <if test="projectName != null and projectName != ''">#{projectName},</if>
            <if test="projectType != null and projectType != ''">#{projectType},</if>
            <if test="demandOverview != null">#{demandOverview},</if>
            <if test="evaluation != null">#{evaluation},</if>
            <if test="isExcess != null">#{isExcess},</if>
            <if test="purchaseServices != null">#{purchaseServices},</if>
            <if test="purchaseMode != null and purchaseMode != ''">#{purchaseMode},</if>
            <if test="planDemandSubTime != null">#{planDemandSubTime},</if>
            <if test="planPurchaseFinishTime != null">#{planPurchaseFinishTime},</if>
            <if test="planDeliverTime != null">#{planDeliverTime},</if>
            <if test="projectAttr != null and projectAttr != ''">#{projectAttr},</if>
            <if test="adjustReason != null">#{adjustReason},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="realDemandCommitTime != null">#{realDemandCommitTime},</if>
            <if test="realPurchaseFinishTime != null">#{realPurchaseFinishTime},</if>
            <if test="realDeliverTime != null">#{realDeliverTime},</if>
            <if test="auditTime != null">#{auditTime},</if>
            <if test="warnStatus != null">#{warnStatus},</if>
            <if test="projectStatus != null">#{projectStatus},</if>
            <if test="refuseReason != null">#{refuseReason},</if>
            <if test="delayReason != null">#{delayReason},</if>
            <if test="adjustAdvice != null">#{adjustAdvice},</if>
            <if test="agencyId != null">#{agencyId},</if>
            <if test="extractAgencyTime != null">#{extractAgencyTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updatePmDemand" parameterType="com.ozs.pm.doman.PmDemand">
        update pm_demand
        <trim prefix="SET" suffixOverrides=",">
            <if test="planId != null">plan_id = #{planId},</if>
            <if test="purchaseDeptId != null">purchase_dept_id = #{purchaseDeptId},</if>
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
            <if test="projectType != null and projectType != ''">project_type = #{projectType},</if>
            <if test="demandOverview != null">demand_overview = #{demandOverview},</if>
            <if test="evaluation != null">evaluation = #{evaluation},</if>
            <if test="isExcess != null">is_excess = #{isExcess},</if>
            <if test="purchaseServices != null">purchase_services = #{purchaseServices},</if>
            <if test="purchaseMode != null and purchaseMode != ''">purchase_mode = #{purchaseMode},</if>
            <if test="planDemandSubTime != null">plan_demand_sub_time = #{planDemandSubTime},</if>
            <if test="planPurchaseFinishTime != null">plan_purchase_finish_time = #{planPurchaseFinishTime},</if>
            <if test="planDeliverTime != null">plan_deliver_time = #{planDeliverTime},</if>
            <if test="projectAttr != null and projectAttr != ''">project_attr = #{projectAttr},</if>
            <if test="adjustReason != null">adjust_reason = #{adjustReason},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="realDemandCommitTime != null">real_demand_commit_time = #{realDemandCommitTime},</if>
            <if test="realPurchaseFinishTime != null">real_purchase_finish_time = #{realPurchaseFinishTime},</if>
            <if test="realDeliverTime != null">real_deliver_time = #{realDeliverTime},</if>
            <if test="auditTime != null">audit_time = #{auditTime},</if>
            <if test="warnStatus != null">warn_status = #{warnStatus},</if>
            <if test="projectStatus != null">project_status = #{projectStatus},</if>
            <if test="refuseReason != null">refuse_reason = #{refuseReason},</if>
            <if test="delayReason != null">delay_reason = #{delayReason},</if>
            <if test="adjustAdvice != null">adjust_advice = #{adjustAdvice},</if>
            <if test="agencyId != null">agency_id = #{agencyId},</if>
            <if test="extractAgencyTime != null">extract_agency_time = #{extractAgencyTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where demand_id = #{demandId}
    </update>

</mapper>