<?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.dsk.cscec.mapper.CbCostMeasureActualMapper">
  <resultMap id="BaseResultMap" type="com.dsk.cscec.domain.CbCostMeasureActual">
    <!--@mbg.generated-->
    <!--@Table cb_cost_measure_actual-->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="plan_measure_id" jdbcType="BIGINT" property="planMeasureId" />
    <result column="month_cost_rate" jdbcType="DECIMAL" property="monthCostRate" />
    <result column="project_volume" jdbcType="DECIMAL" property="projectVolume" />
    <result column="cost_effective" jdbcType="DECIMAL" property="costEffective" />
    <result column="current_project_volume" jdbcType="DECIMAL" property="currentProjectVolume" />
    <result column="submit_project_volume" jdbcType="DECIMAL" property="submitProjectVolume" />
    <result column="month" jdbcType="VARCHAR" property="month" />
    <result column="push_time" jdbcType="TIMESTAMP" property="pushTime" />
    <result column="push_data_json" jdbcType="LONGVARCHAR" property="pushDataJson" />
    <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="update_user" jdbcType="VARCHAR" property="updateUser" />
    <result column="update_id" jdbcType="VARCHAR" property="updateId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, plan_measure_id, month_cost_rate, cost_effective,project_volume, current_project_volume, submit_project_volume,
    `month`, push_time, push_data_json, del_flag, update_user, update_id, create_time, 
    update_time
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.dsk.cscec.domain.CbCostMeasureActual" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into cb_cost_measure_actual
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="planMeasureId != null">
        plan_measure_id,
      </if>
      <if test="monthCostRate != null">
        month_cost_rate,
      </if>
      <if test="costEffective != null">
        cost_effective,
      </if>
      <if test="currentProjectVolume != null">
        current_project_volume,
      </if>
      <if test="submitProjectVolume != null">
        submit_project_volume,
      </if>
      <if test="month != null">
        `month`,
      </if>
      <if test="pushTime != null">
        push_time,
      </if>
      <if test="pushDataJson != null">
        push_data_json,
      </if>
      <if test="delFlag != null">
        del_flag,
      </if>
      <if test="updateUser != null">
        update_user,
      </if>
      <if test="updateId != null">
        update_id,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="planMeasureId != null">
        #{planMeasureId,jdbcType=BIGINT},
      </if>
      <if test="monthCostRate != null">
        #{monthCostRate,jdbcType=DECIMAL},
      </if>
      <if test="costEffective != null">
        #{costEffective,jdbcType=DECIMAL},
      </if>
      <if test="currentProjectVolume != null">
        #{currentProjectVolume,jdbcType=DECIMAL},
      </if>
      <if test="submitProjectVolume != null">
        #{submitProjectVolume,jdbcType=DECIMAL},
      </if>
      <if test="month != null">
        #{month,jdbcType=VARCHAR},
      </if>
      <if test="pushTime != null">
        #{pushTime,jdbcType=TIMESTAMP},
      </if>
      <if test="pushDataJson != null">
        #{pushDataJson,jdbcType=LONGVARCHAR},
      </if>
      <if test="delFlag != null">
        #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="updateUser != null">
        #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateId != null">
        #{updateId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.dsk.cscec.domain.CbCostMeasureActual">
    <!--@mbg.generated-->
    update cb_cost_measure_actual
    <set>
      <if test="planMeasureId != null">
        plan_measure_id = #{planMeasureId,jdbcType=BIGINT},
      </if>
      <if test="monthCostRate != null">
        month_cost_rate = #{monthCostRate,jdbcType=DECIMAL},
      </if>
      <if test="costEffective != null">
        cost_effective = #{costEffective,jdbcType=DECIMAL},
      </if>
      <if test="currentProjectVolume != null">
        current_project_volume = #{currentProjectVolume,jdbcType=DECIMAL},
      </if>
      <if test="submitProjectVolume != null">
        submit_project_volume = #{submitProjectVolume,jdbcType=DECIMAL},
      </if>
      <if test="month != null">
        `month` = #{month,jdbcType=VARCHAR},
      </if>
      <if test="pushTime != null">
        push_time = #{pushTime,jdbcType=TIMESTAMP},
      </if>
      <if test="pushDataJson != null">
        push_data_json = #{pushDataJson,jdbcType=LONGVARCHAR},
      </if>
      <if test="delFlag != null">
        del_flag = #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="updateUser != null">
        update_user = #{updateUser,jdbcType=VARCHAR},
      </if>
      <if test="updateId != null">
        update_id = #{updateId,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="selectDataList" resultType="com.dsk.cscec.domain.vo.CbCostMeasureActualVo">
    SELECT t1.plan_measure_id,
           t1.month_cost_rate,
           t1.cost_effective,
           t1.month_cost_rate,
           t1.current_project_volume,
           t1.submit_project_volume,
           t1.`month`,
           t1.push_time
    FROM cb_cost_measure t
    inner JOIN cb_cost_measure_actual t1 ON t1.plan_measure_id=t.id
    WHERE
      t.project_id=#{projectId}
      AND t.cb_stage=#{cbStage}
      AND t1.`month`=#{month}
  </select>

  <select id="getMonthActualCostList" resultType="com.dsk.cscec.domain.vo.CbCostMeasureActualVo">
    SELECT t1.item_content, t.project_volume
    FROM cb_cost_measure_actual t
    inner JOIN cb_cost_measure t1 ON t1.id=t.plan_measure_id
    WHERE
      t1.project_id=#{projectId}
      AND t1.parent_id=0
      AND t.`month`=#{month}
    </select>

  <select id="selectSumData" resultType="java.math.BigDecimal">
        SELECT SUM(t.project_volume) measureTotal FROM cb_cost_measure_actual t WHERE t.plan_measure_id=#{planMeasureId}
        and t.month &lt;#{month}
    </select>

  <select id="selectMonthProjectVolumeByLevel" resultType="java.math.BigDecimal">
    SELECT SUM(t.project_volume)
    FROM cb_cost_measure_actual t
    inner JOIN cb_cost_measure t1 ON t1.id=t.plan_measure_id
    WHERE
    t1.project_id=#{projectId}
    AND t1.level=#{level}
    AND t.`month`=#{month}
  </select>

  <select id="calculateSumDifference" resultType="java.math.BigDecimal">
      select ifnull(sum(ccma.difference), 0) sumDifference
      from cb_cost_measure_actual ccma
      where ccma.plan_measure_id = #{item.id}
        and ccma.month &lt;= #{item.month}
  </select>
</mapper>