<?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.yanzhu.jh.project.mapper.SurProjectCostOutputMapper">
    
    <resultMap type="SurProjectCostOutput" id="SurProjectCostOutputResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="money"    column="money"    />
        <result property="year"    column="year"    />
        <result property="month"    column="month"    />
        <result property="costType"    column="cost_type"    />
        <result property="remark"    column="remark"    />
        <result property="isDel"    column="is_del"    />
        <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="selectSurProjectCostOutputVo">
        select id, project_id, money, year, month, cost_type, remark, is_del, create_by, create_time, update_by, update_time from sur_project_cost_output
    </sql>

    <select id="selectSurProjectCostOutputList" parameterType="SurProjectCostOutput" resultMap="SurProjectCostOutputResult">
        <include refid="selectSurProjectCostOutputVo"/>
        <where>  
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="money != null "> and money = #{money}</if>
            <if test="year != null "> and year = #{year}</if>
            <if test="month != null "> and month = #{month}</if>
            <if test="costType != null "> and cost_type = #{costType}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectSurProjectCostOutputById" parameterType="Long" resultMap="SurProjectCostOutputResult">
        <include refid="selectSurProjectCostOutputVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertSurProjectCostOutput" parameterType="SurProjectCostOutput" useGeneratedKeys="true" keyProperty="id">
        insert into sur_project_cost_output
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="money != null">money,</if>
            <if test="year != null">year,</if>
            <if test="month != null">month,</if>
            <if test="costType != null">cost_type,</if>
            <if test="remark != null">remark,</if>
            <if test="isDel != null">is_del,</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="projectId != null">#{projectId},</if>
            <if test="money != null">#{money},</if>
            <if test="year != null">#{year},</if>
            <if test="month != null">#{month},</if>
            <if test="costType != null">#{costType},</if>
            <if test="remark != null">#{remark},</if>
            <if test="isDel != null">#{isDel},</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="updateSurProjectCostOutput" parameterType="SurProjectCostOutput">
        update sur_project_cost_output
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="money != null">money = #{money},</if>
            <if test="year != null">year = #{year},</if>
            <if test="month != null">month = #{month},</if>
            <if test="costType != null">cost_type = #{costType},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="isDel != null">is_del = #{isDel},</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 id = #{id}
    </update>

    <delete id="deleteSurProjectCostOutputById" parameterType="Long">
        delete from sur_project_cost_output where id = #{id}
    </delete>

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

    <select id="selectYearAndMonth"  parameterType="SurProjectCostOutput" resultMap="SurProjectCostOutputResult">
        SELECT *
        FROM sur_project_cost_output
        WHERE project_id = #{projectId}
          and (
                (YEAR=#{year} AND cost_type=2) OR cost_type NOT IN (2, 9)
                OR (cost_type = 9 AND YEAR = #{year} AND MONTH &lt;= #{month})
                OR (cost_type = 9 AND YEAR &lt; #{year})
            )
    </select>

    <select id="sumByDeptId"  parameterType="SurProjectCostOutput" resultMap="SurProjectCostOutputResult">
        SELECT 10 cost_type , SUM(money) money FROM sur_project_cost_output
        WHERE ( (cost_type=9 AND YEAR=#{year} AND MONTH &lt;= #{month}) OR (cost_type=9 AND YEAR &lt;#{year}))
        <if test="id!=null and id>0">
            and  project_id IN  (SELECT id FROM sur_project WHERE deptid=#{id})
        </if>
        <if test="projectId!=null and projectId>0">
            and project_id=#{projectId}
        </if>

        UNION

        SELECT 2 cost_type , SUM(money) money FROM sur_project_cost_output
        WHERE  (cost_type=2 AND YEAR=#{year} )
        <if test="id!=null and id>0">
            and  project_id IN  (SELECT id FROM sur_project WHERE deptid=#{id})
        </if>
        <if test="projectId!=null and projectId>0">
            and project_id=#{projectId}
        </if>
        UNION

        SELECT 9 cost_type , SUM(money) money FROM sur_project_cost_output
        WHERE  (cost_type=9 AND YEAR=#{year} AND MONTH= #{month} )
        <if test="id!=null and id>0">
           and  project_id IN  (SELECT id FROM sur_project WHERE deptid=#{id})
        </if>
        <if test="projectId!=null and projectId>0">
         and project_id=#{projectId}
        </if>


    </select>
</mapper>