<?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="delay_reason" column="delayReason"/>
        <result property="remarks" column="remarks"/>
        <result property="project_status" column="projectStatus"/>
        <result property="send_letter" column="sendLetter"/>
        <result property="letter_str" column="letterStr"/>
        <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},
            purchase_services=#{purchaseServices},
            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="params!= null and params.beginTime!= null "><!-- 开始时间检索 -->
                and date_format(p.plan_demand_sub_time,'%y%m') &gt;= date_format(#{params.beginTime},'%y%m')
            </if>
            <if test="params!= null and params.endTime!= null "><!-- 结束时间检索 -->
                and date_format(p.plan_demand_sub_time,'%y%m') &lt;= date_format(#{params.endTime},'%y%m')
            </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="params!= null and params.beginTime!= null "><!-- 开始时间检索 -->
                and date_format(plan_demand_sub_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params!= null and params.endTime!= null "><!-- 结束时间检索 -->
                and date_format(plan_demand_sub_time,'%y%m%d') &lt;= date_format(#{params.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="getByPlanYearId" resultType="com.ozs.plan.doman.PlanQuarter" parameterType="java.lang.Long">
        select *
        from plan_practical
        where plan_year_id = #{planYearId}
          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>

    <select id="query" resultType="com.ozs.plan.doman.PlanQuarter"
            parameterType="com.ozs.plan.doman.vo.requestVo.PlanQuarterStandardVo">
        select p.* from
        plan_practical p left join pm_audit_dept_ref r
        on p.plan_practical_id = r.ref_id and r.ref_type = 1 and p.plan_type = 0
        <where>
            ( #{created} = 1 or r.dept_id = #{purchaseDeptId} )
            <if test="projectName != null and projectName != ''">
                AND p.project_name like concat('%', #{projectName}, '%')
            </if>
            <if test="purchaseServices != null and purchaseServices != ''">
                AND p.purchase_services = #{purchaseServices}
            </if>
            <if test="projectStatus == null or projectStatus ==''">
                AND p.project_status in (1,3)
            </if>
            <if test="projectStatus != null and projectStatus != ''">
                AND p.project_status = #{projectStatus}
            </if>
            <if test="params!= null and params.beginTime!= null "><!-- 开始时间检索 -->
                and date_format(p.plan_demand_sub_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params!= null and params.endTime!= null "><!-- 结束时间检索 -->
                and date_format(p.plan_demand_sub_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
            and p.del_flay=0
        </where>
        order by create_time desc

    </select>

    <!--	创建计划信息-->
    <insert id="insertPlanQuarter" parameterType="com.ozs.plan.doman.PlanQuarter" useGeneratedKeys="true"
            keyProperty="planPracticalId">
        insert into plan_practical (purchase_dept_id, plan_year_id, project_name, project_type, demand_overview,
                                    evaluation,
                                    purchase_services, organ_division, budget_account, 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}, #{planYearId}, #{projectName}, #{projectType}, #{demandOverview}, #{evaluation},
                #{purchaseServices}, #{organDivision}, #{budgetAccount}, #{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.vo.requestVo.PlanQuarterStandardVo">
        update plan_practical
        <set>
            <if test="refuseReason != null and refuseReason != ''">
                refuse_reason=#{refuseReason},
            </if>
            <if test="projectStatus != null and projectStatus != ''">
                project_status=#{projectStatus},
            </if>
            <if test="adjustReason != null and adjustReason != ''">
                adjust_reason=#{adjustReason}
            </if>
        </set>
        where plan_practical_id = #{planPracticalId}
    </update>

    <update id="sendLetter" parameterType="java.lang.Long">
        update plan_practical
        set send_letter=1,
        <if test="letterStr != null and letterStr != ''">
            letter_str=#{letterStr}
        </if>
        where plan_practical_id = #{planPracticalId}
    </update>


</mapper>
