<?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.plan.mapper.PlanQuarterMapper">

    <resultMap type="com.ozs.plan.doman.PlanQuarter" id="PlanQuarterResult">
        <id property="plan_practical_id" column="planPracticalId"/>
        <result property="plan_year_id" column="planYearId"/>
        <result property="purchase_dept_id" column="purchaseDeptId"/>
        <result property="project_name" column="projectName"/>
        <result property="project_type" column="projectType"/>
        <result property="demand_overview" column="demandOverview"/>
        <result property="evaluation" column="evaluation"/>
        <result property="is_excess" column="isExcess"/>
        <result property="purchase_services" column="purchaseServices"/>
        <result property="purchase_mode" column="purchaseMode"/>
        <result property="plan_demand_sub_time" column="planDemandSubTime"/>
        <result property="plan_purchase_finish_time" column="planPurchaseFinishTime"/>
        <result property="plan_deliver_time" column="planDeliverTime"/>
        <result property="project_attr" column="projectAttr"/>
        <result property="adjust_reason" column="adjustReason"/>
        <result property="remarks" column="remarks"/>
        <result property="project_status" column="projectStatus"/>
        <result property="send_letter" column="sendLetter"/>
        <result property="created" column="created"/>
        <result property="create_time" column="createTime"/>
        <result property="updated" column="updated"/>
        <result property="update_time" column="updateTime"/>
        <result property="refuse_reason" column="refuseReason"/>
        <result property="del_flay" column="delFlay"/>
        <result property="plan_type" column="planType"/>
    </resultMap>

    <update id="updateInfoById" parameterType="com.ozs.plan.doman.PlanQuarter">
        update plan_practical
        set project_name=#{projectName},
            project_type=#{projectType},
            demand_overview=#{demandOverview},
            evaluation=#{evaluation},
            is_excess=#{isExcess},
            purchase_services=#{purchaseServices},
            purchase_mode=#{purchaseMode},
            plan_demand_sub_time=#{planDemandSubTime},
            plan_purchase_finish_time=#{planPurchaseFinishTime},
            plan_deliver_time=#{planDeliverTime},
            project_attr=#{projectAttr},
            adjust_reason=#{adjustReason},
            remarks=#{remarks},
            updated=#{updated},
            update_time=#{updateTime}
        where plan_practical_id = #{planPracticalId}
    </update>

    <select id="selectPlanQuarterList" parameterType="com.ozs.plan.doman.PlanQuarter" resultMap="PlanQuarterResult">
        select * from plan_practical
        <where>
            <if test="projectName != null and projectName != ''">
                AND project_name like concat('%', #{projectName}, '%')
            </if>
            <if test="purchaseServices != null and purchaseServices != ''">
                AND purchase_services = #{purchaseServices}
            </if>
            <if test="isExcess != null and isExcess != ''">
                AND is_excess = #{isExcess}
            </if>
            <if test="projectStatus != null and projectStatus != ''">
                AND project_status = #{projectStatus}
            </if>
            <if test="beginTime != null  "><!-- 开始时间检索 -->
                and date_format(plan_demand_sub_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null  "><!-- 结束时间检索 -->
                and date_format(plan_demand_sub_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            and plan_type=0
            and del_flay=0
        </where>
        order by plan_demand_sub_time desc
    </select>

    <select id="selectPlanQuarterExamineList" parameterType="com.ozs.plan.doman.PlanQuarter"
            resultMap="PlanQuarterResult">
        select * from plan_practical
        <where>
            <if test="projectName != null and projectName != ''">
                AND project_name like concat('%', #{projectName}, '%')
            </if>
            <if test="purchaseServices != null and purchaseServices != ''">
                AND purchase_services = #{purchaseServices}
            </if>
            <if test="isExcess != null and isExcess != ''">
                AND is_excess = #{isExcess}
            </if>
            <if test="projectStatus == null or projectStatus ==''">
                AND project_status in (1,3)
            </if>
            <if test="projectStatus != null and projectStatus != ''">
                AND project_status = #{projectStatus}
            </if>
            <if test="beginTime != null "><!-- 开始时间检索 -->
                and date_format(plan_demand_sub_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
            </if>
            <if test="endTime != null "><!-- 结束时间检索 -->
                and date_format(plan_demand_sub_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
            </if>
            and plan_type=0
            and del_flay=0
        </where>
        order by plan_demand_sub_time desc
    </select>

    <select id="countProjectName" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(plan_practical.plan_practical_id)
        from plan_practical
        where project_name = #{projectName}
          and plan_type = 0
          and del_flay = 0
    </select>

    <select id="getById" resultType="com.ozs.plan.doman.PlanQuarter" parameterType="java.lang.Long">
        select *
        from plan_practical
        where plan_practical_id = #{planPracticalId}
          and del_flay = 0
    </select>

    <select id="countProjectNameOther" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(plan_practical.plan_practical_id)
        from plan_practical
        where project_name = #{projectName}
          and plan_practical_id != #{planPracticalId}
          and plan_type = 0
          and del_flay = 0
    </select>

    <!--	创建计划信息-->
    <insert id="insertPlanQuarter" parameterType="com.ozs.plan.doman.PlanQuarter" useGeneratedKeys="true"
            keyProperty="planYearId">
        insert into plan_practical (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, project_status,
                                    created, create_time, del_flay, plan_type, send_letter)
        values (#{purchaseDeptId}, #{projectName}, #{projectType}, #{demandOverview}, #{evaluation}, #{isExcess},
                #{purchaseServices}, #{purchaseMode}, #{planDemandSubTime}, #{planPurchaseFinishTime},
                #{planDeliverTime},
                #{projectAttr}, #{adjustReason}, #{remarks}, #{projectStatus}, #{created}, #{createTime}, 0, 0, 0)
    </insert>

    <!--	计划删除-->
    <update id="deletePlanQuarterByIds" parameterType="java.lang.Long">
        update plan_practical set del_flay=1 where plan_practical_id in
        <foreach collection="array" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <update id="deletePlanQuarterById" parameterType="java.lang.Long">
        update plan_practical
        set del_flay=1
        where plan_practical_id = #{planPracticalId}
    </update>

    <!--	计划提交-->
    <update id="commit" parameterType="java.lang.Long">
        update plan_practical
        set project_status=1
        where plan_practical_id = #{planPracticalId}
    </update>

    <!--	计划审核-->
    <update id="review" parameterType="com.ozs.plan.doman.PlanQuarter">
        update plan_practical
        <set>
            <if test="refuseReason != null and refuseReason != ''">
                refuse_reason=#{refuseReason},
            </if>
            <if test="projectStatus != null and projectStatus != ''">
                project_status=#{projectStatus}
            </if>
        </set>
        where plan_practical_id = #{planPracticalId}
    </update>

    <update id="sendLetter" parameterType="java.lang.Long">
        update plan_practical
        set send_letter= 1
        where plan_practical_id = #{planPracticalId}
    </update>


</mapper>
