<?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.ruoyi.system.mapper.project.BmConstructionPayMapper">
    
    <resultMap type="BmConstructionPay" id="BmConstructionPayResult">
        <result property="id"    column="id"    />
        <result property="bmProjectId"    column="bm_project_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="payForType"    column="pay_for_type"    />
        <result property="payForMoney"    column="pay_for_money"    />
        <result property="photo"    column="photo"    />
    </resultMap>

    <sql id="selectBmConstructionPayVo">
        select id, bm_project_id, create_by, create_time, update_by, update_time, is_del, pay_for_type, pay_for_money,  photo from bm_construction_pay
    </sql>

    <select id="selectBmConstructionPayList" parameterType="BmConstructionPay" resultMap="BmConstructionPayResult">
        select p.id, p.bm_project_id, p.create_by, p.create_time, p.update_by, p.update_time, p.is_del, p.pay_for_type, p.pay_for_money,  p.photo, d.dict_label as payForTypeName
        from bm_construction_pay p
        left join sys_dict_data d on p.pay_for_type = d.dict_value and d.dict_type = 'project_pay'
        <where>
            p.is_del = '0'
            <if test="bmProjectId != null "> and p.bm_project_id = #{bmProjectId}</if>
            <if test="payForType != null  and payForType != ''"> and p.pay_for_type = #{payForType}</if>
            <if test="payForMoney != null"> and p.pay_for_money = #{payForMoney}</if>
            <if test="photo != null  and photo != ''"> and p.photo = #{photo}</if>
        </where>
        order by p.create_time DESC
    </select>

    <select id="selectBmConstructionPayById" parameterType="Long" resultMap="BmConstructionPayResult">
        select p.id, p.bm_project_id, p.create_by, p.create_time, p.update_by, p.update_time, p.is_del, p.pay_for_type, p.pay_for_money,  p.photo, d.dict_label as payForTypeName
        from bm_construction_pay p
        left join sys_dict_data d on p.pay_for_type = d.dict_value and d.dict_type = 'project_pay'
        where p.id = #{id}
    </select>
    <select id="selectBmConstructionPayByProjectId" parameterType="Long" resultMap="BmConstructionPayResult">
        select p.id, p.bm_project_id, p.create_by, p.create_time, p.update_by, p.update_time, p.is_del, p.pay_for_type, p.pay_for_money,  p.photo, d.dict_label as payForTypeName
        from bm_construction_pay p
        left join sys_dict_data d on p.pay_for_type = d.dict_value and d.dict_type = 'project_pay' and p.is_del = '0'
        where p.bm_project_id = #{project}
    </select>
        
    <insert id="insertBmConstructionPay" parameterType="BmConstructionPay">
        insert into bm_construction_pay
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="bmProjectId != null">bm_project_id,</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>
            <if test="isDel != null">is_del,</if>
            <if test="payForType != null and payForType != ''">pay_for_type,</if>
            <if test="payForMoney != null">pay_for_money,</if>
            <if test="photo != null">photo,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="bmProjectId != null">#{bmProjectId},</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>
            <if test="isDel != null">#{isDel},</if>
            <if test="payForType != null and payForType != ''">#{payForType},</if>
            <if test="payForMoney != null">#{payForMoney},</if>
            <if test="photo != null">#{photo},</if>
         </trim>
    </insert>

    <update id="updateBmConstructionPay" parameterType="BmConstructionPay">
        update bm_construction_pay
        <trim prefix="SET" suffixOverrides=",">
            <if test="bmProjectId != null">bm_project_id = #{bmProjectId},</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>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="payForType != null">pay_for_type = #{payForType},</if>
            <if test="payForMoney != null">pay_for_money = #{payForMoney},</if>
            <if test="photo != null">photo = #{photo},</if>
        </trim>
        where id = #{id}
    </update>

<!--    <delete id="deleteBmConstructionPayById" parameterType="Long">
        delete from bm_construction_pay where id = #{id}
    </delete>-->

    <update id="deleteBmConstructionPayById" parameterType="Long">
        update bm_construction_pay set is_del = '1'
    </update>



    <select id="selectIndexEharts" parameterType="string" resultType="com.ruoyi.system.domain.vo.BmDeptPlanVo">
        SELECT
        d.dept_name as deptName,
        ifnull(sum(cp.pay_for_money),0) as payForMoney
        FROM
        sys_dept d
        LEFT JOIN bm_project p ON d.dept_id = p.dept_id
        AND d.del_flag = '0'
        LEFT JOIN bm_construction_pay cp ON p.id = cp.bm_project_id and p.is_del = '0' and year(cp.create_time) = #{year}
        GROUP BY d.dept_id

    </select>


    <!--    <delete id="deleteBmConstructionPayByIds" parameterType="String">
            delete from bm_construction_pay where id in
            <foreach item="id" collection="array" open="(" separator="," close=")">
                #{id}
            </foreach>
        </delete>-->

    <update id="deleteBmConstructionPayByIds" parameterType="String">
        update bm_construction_pay set is_del = '1' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>