<?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.api.manage.dao.chargeManagement.SysDailyPaymentPlanDao">

    <select id="list" parameterType="com.api.model.chargeManagement.SearchDailyPaymentPlan" resultType="com.api.vo.chargeManagement.VoDailyPaymentPlan">
        select sdpp.*,CONCAT(cb.no,'-',cbu.no,'-',cbue.room_number) as room_name,sctd.id as charges_template_detail_id,sctd.name as charges_template_detail_name,su.actual_name as create_name
        from sys_daily_payment_plan as sdpp,cpm_building_unit_estate as cbue,
        cpm_building_unit as cbu,cpm_building as cb,sys_charges_template_detail as sctd,sys_user as su
        <where>
            sdpp.building_unit_estate_id = cbue.id
            and cbue.building_unit_id = cbu.id
            and cbu.building_id = cb.id
            and sdpp.charges_template_detail_id = sctd.id
            and sdpp.create_id = su.id
            <if test="buildingUnitEstateId != null">
                and sdpp.building_unit_estate_id = #{buildingUnitEstateId}
            </if>
            and sdpp.is_delete = 1
        </where>
        order by sdpp.create_date desc
    </select>

    <insert id="insert" parameterType="com.api.model.chargeManagement.DailyPaymentPlan">
        insert into sys_daily_payment_plan(
            building_unit_estate_id,
            charges_template_detail_id,
            begin_plan_date,
            end_plan_date,
            unit_price,
            type,
            num,
            cost_price,
            cycle,
            rate,
            payment_time,
            remake,
            create_id,
            create_date,
            is_delete
        )values (
            #{buildingUnitEstateId},
            #{chargesTemplateDetailId},
            #{beginPlanDate},
            #{endPlanDate},
            #{unitPrice},
            #{type},
            #{num},
            #{costPrice},
            #{cycle},
            #{rate},
            #{paymentTime},
            #{remake},
            #{createId},
            #{createDate},
            #{isDelete}
        )
    </insert>

    <update id="update" parameterType="com.api.model.chargeManagement.DailyPaymentPlan">
        update sys_daily_payment_plan
        <set>
            <if test="chargesTemplateDetailId != null">
                charges_template_detail_id = #{chargesTemplateDetailId},
            </if>
            <if test="beginPlanDate != null">
                begin_plan_date = #{beginPlanDate},
            </if>
            <if test="endPlanDate != null">
                end_plan_date = #{beginPlanDate},
            </if>
            <if test="unitPrice != null">
                unit_price = #{unitPrice},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="num != null">
                num = #{num},
            </if>
            <if test="costPrice != null">
                cost_price = #{costPrice},
            </if>
            <if test="cycle != null">
                cycle = #{cycle},
            </if>
            <if test="rate != null">
                rate = #{rate},
            </if>
            <if test="paymentTime != null">
                payment_time = #{paymentTime},
            </if>
            <if test="remake != null and remake != ''">
                remake = #{remake},
            </if>
            modify_id = #{modifyId},
            modify_date = #{modifyDate}
        </set>
        where id = #{id}
    </update>

    <update id="delete" parameterType="java.lang.Integer">
        update sys_daily_payment_plan set is_delete = 0 where id = #{id}
    </update>

    <select id="findEnableDPP" resultType="com.api.model.chargeManagement.DailyPaymentPlan">
        select * from sys_daily_payment_plan
        where is_delete = 1 and now() <![CDATA[ >= ]]> begin_plan_date
        and now() <![CDATA[ <= ]]> end_plan_date
    </select>

</mapper>