<?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.goafanti.common.dao.OrgActivityCostMapper" >
  <resultMap id="BaseResultMap" type="com.goafanti.common.model.OrgActivityCost" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="aid" property="aid" jdbcType="VARCHAR" />
    <result column="uid" property="uid" jdbcType="VARCHAR" />
    <result column="year" property="year" jdbcType="INTEGER" />
    <result column="activity_number" property="activityNumber" jdbcType="VARCHAR" />
    <result column="internal_labor_cost" property="internalLaborCost" jdbcType="DECIMAL" />
    <result column="internal_direct_cost" property="internalDirectCost" jdbcType="DECIMAL" />
    <result column="internal_depreciation_cost" property="internalDepreciationCost" jdbcType="DECIMAL" />
    <result column="internal_amortization_cost" property="internalAmortizationCost" jdbcType="DECIMAL" />
    <result column="internal_design_cost" property="internalDesignCost" jdbcType="DECIMAL" />
    <result column="internal_equipment_cost" property="internalEquipmentCost" jdbcType="DECIMAL" />
    <result column="internal_other_cost" property="internalOtherCost" jdbcType="DECIMAL" />
    <result column="external_total_cost" property="externalTotalCost" jdbcType="DECIMAL" />
    <result column="external_abroad_cost" property="externalAbroadCost" jdbcType="DECIMAL" />
    <result column="enterprise_filler" property="enterpriseFiller" jdbcType="VARCHAR" />
    <result column="sign_date" property="signDate" jdbcType="TIMESTAMP" />
    <result column="sort_number" property="sortNumber" jdbcType="VARCHAR" />
    <result column="start_date" property="startDate" jdbcType="TIMESTAMP" />
    <result column="end_date" property="endDate" jdbcType="TIMESTAMP" />
    <result column="account_url" property="accountUrl" jdbcType="VARCHAR" />
    <result column="deleted_sign" property="deletedSign" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, aid, uid, year, activity_number, internal_labor_cost, internal_direct_cost, internal_depreciation_cost, 
    internal_amortization_cost, internal_design_cost, internal_equipment_cost, internal_other_cost, 
    external_total_cost, external_abroad_cost, enterprise_filler, sign_date, sort_number, 
    start_date, end_date, account_url, deleted_sign
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select 
    <include refid="Base_Column_List" />
    from org_activity_cost
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
    delete from org_activity_cost
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.goafanti.common.model.OrgActivityCost" >
    insert into org_activity_cost (id, aid, uid, year,
      activity_number, internal_labor_cost, internal_direct_cost, 
      internal_depreciation_cost, internal_amortization_cost, 
      internal_design_cost, internal_equipment_cost, 
      internal_other_cost, external_total_cost, external_abroad_cost, 
      enterprise_filler, sign_date, sort_number, 
      start_date, end_date, account_url, deleted_sign
      )
    values (#{id,jdbcType=VARCHAR}, #{aid,jdbcType=VARCHAR}, #{uid,jdbcType=VARCHAR}, #{year,jdbcType=INTEGER},
      #{activityNumber,jdbcType=VARCHAR}, #{internalLaborCost,jdbcType=DECIMAL}, #{internalDirectCost,jdbcType=DECIMAL}, 
      #{internalDepreciationCost,jdbcType=DECIMAL}, #{internalAmortizationCost,jdbcType=DECIMAL}, 
      #{internalDesignCost,jdbcType=DECIMAL}, #{internalEquipmentCost,jdbcType=DECIMAL}, 
      #{internalOtherCost,jdbcType=DECIMAL}, #{externalTotalCost,jdbcType=DECIMAL}, #{externalAbroadCost,jdbcType=DECIMAL}, 
      #{enterpriseFiller,jdbcType=VARCHAR}, #{signDate,jdbcType=TIMESTAMP}, #{sortNumber,jdbcType=VARCHAR}, 
      #{startDate,jdbcType=TIMESTAMP}, #{endDate,jdbcType=TIMESTAMP}, #{accountUrl,jdbcType=VARCHAR}, #{deletedSign,jdbcType=INTEGER}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.goafanti.common.model.OrgActivityCost" >
    insert into org_activity_cost
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="aid != null" >
        aid,
      </if>
      <if test="uid != null" >
        uid,
      </if>
      <if test="year != null" >
        year,
      </if>
      <if test="activityNumber != null" >
        activity_number,
      </if>
      <if test="internalLaborCost != null" >
        internal_labor_cost,
      </if>
      <if test="internalDirectCost != null" >
        internal_direct_cost,
      </if>
      <if test="internalDepreciationCost != null" >
        internal_depreciation_cost,
      </if>
      <if test="internalAmortizationCost != null" >
        internal_amortization_cost,
      </if>
      <if test="internalDesignCost != null" >
        internal_design_cost,
      </if>
      <if test="internalEquipmentCost != null" >
        internal_equipment_cost,
      </if>
      <if test="internalOtherCost != null" >
        internal_other_cost,
      </if>
      <if test="externalTotalCost != null" >
        external_total_cost,
      </if>
      <if test="externalAbroadCost != null" >
        external_abroad_cost,
      </if>
      <if test="enterpriseFiller != null" >
        enterprise_filler,
      </if>
      <if test="signDate != null" >
        sign_date,
      </if>
      <if test="sortNumber != null" >
        sort_number,
      </if>
      <if test="startDate != null" >
        start_date,
      </if>
      <if test="endDate != null" >
        end_date,
      </if>
       <if test="accountUrl != null" >
        account_url,
      </if>
      <if test="deletedSign != null" >
        deleted_sign,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="aid != null" >
        #{aid,jdbcType=VARCHAR},
      </if>
      <if test="uid != null" >
        #{uid,jdbcType=VARCHAR},
      </if>
      <if test="year != null" >
        #{year,jdbcType=INTEGER},
      </if>
       <if test="activityNumber != null" >
        #{activityNumber,jdbcType=VARCHAR},
      </if>
      <if test="internalLaborCost != null" >
        #{internalLaborCost,jdbcType=DECIMAL},
      </if>
      <if test="internalDirectCost != null" >
        #{internalDirectCost,jdbcType=DECIMAL},
      </if>
      <if test="internalDepreciationCost != null" >
        #{internalDepreciationCost,jdbcType=DECIMAL},
      </if>
      <if test="internalAmortizationCost != null" >
        #{internalAmortizationCost,jdbcType=DECIMAL},
      </if>
      <if test="internalDesignCost != null" >
        #{internalDesignCost,jdbcType=DECIMAL},
      </if>
      <if test="internalEquipmentCost != null" >
        #{internalEquipmentCost,jdbcType=DECIMAL},
      </if>
      <if test="internalOtherCost != null" >
        #{internalOtherCost,jdbcType=DECIMAL},
      </if>
      <if test="externalTotalCost != null" >
        #{externalTotalCost,jdbcType=DECIMAL},
      </if>
      <if test="externalAbroadCost != null" >
        #{externalAbroadCost,jdbcType=DECIMAL},
      </if>
      <if test="enterpriseFiller != null" >
        #{enterpriseFiller,jdbcType=VARCHAR},
      </if>
      <if test="signDate != null" >
        #{signDate,jdbcType=TIMESTAMP},
      </if>
      <if test="sortNumber != null" >
        #{sortNumber,jdbcType=VARCHAR},
      </if>
      <if test="startDate != null" >
        #{startDate,jdbcType=TIMESTAMP},
      </if>
      <if test="endDate != null" >
        #{endDate,jdbcType=TIMESTAMP},
      </if>
      <if test="accountUrl != null" >
        #{accountUrl,jdbcType=VARCHAR},
      </if>
      <if test="deletedSign != null" >
        #{deletedSign,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.goafanti.common.model.OrgActivityCost" >
    update org_activity_cost
    <set >
      <if test="aid != null" >
        aid = #{aid,jdbcType=VARCHAR},
      </if>
      <if test="uid != null" >
        uid = #{uid,jdbcType=VARCHAR},
      </if>
      <if test="year != null" >
        year = #{year,jdbcType=INTEGER},
      </if>
      <if test="activityNumber != null" >
        activity_number = #{activityNumber,jdbcType=VARCHAR},
      </if>
      <if test="internalLaborCost != null" >
        internal_labor_cost = #{internalLaborCost,jdbcType=DECIMAL},
      </if>
      <if test="internalDirectCost != null" >
        internal_direct_cost = #{internalDirectCost,jdbcType=DECIMAL},
      </if>
      <if test="internalDepreciationCost != null" >
        internal_depreciation_cost = #{internalDepreciationCost,jdbcType=DECIMAL},
      </if>
      <if test="internalAmortizationCost != null" >
        internal_amortization_cost = #{internalAmortizationCost,jdbcType=DECIMAL},
      </if>
      <if test="internalDesignCost != null" >
        internal_design_cost = #{internalDesignCost,jdbcType=DECIMAL},
      </if>
      <if test="internalEquipmentCost != null" >
        internal_equipment_cost = #{internalEquipmentCost,jdbcType=DECIMAL},
      </if>
      <if test="internalOtherCost != null" >
        internal_other_cost = #{internalOtherCost,jdbcType=DECIMAL},
      </if>
      <if test="externalTotalCost != null" >
        external_total_cost = #{externalTotalCost,jdbcType=DECIMAL},
      </if>
      <if test="externalAbroadCost != null" >
        external_abroad_cost = #{externalAbroadCost,jdbcType=DECIMAL},
      </if>
      <if test="enterpriseFiller != null" >
        enterprise_filler = #{enterpriseFiller,jdbcType=VARCHAR},
      </if>
      <if test="signDate != null" >
        sign_date = #{signDate,jdbcType=TIMESTAMP},
      </if>
      <if test="sortNumber != null" >
        sort_number = #{sortNumber,jdbcType=VARCHAR},
      </if>
      <if test="startDate != null" >
        start_date = #{startDate,jdbcType=TIMESTAMP},
      </if>
      <if test="endDate != null" >
        end_date = #{endDate,jdbcType=TIMESTAMP},
      </if>
      <if test="accountUrl != null" >
        account_url = #{accountUrl,jdbcType=VARCHAR},
      </if>
      <if test="deletedSign != null" >
        deleted_sign = #{deletedSign,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.goafanti.common.model.OrgActivityCost" >
    update org_activity_cost
    set aid = #{aid,jdbcType=VARCHAR},
      uid = #{uid,jdbcType=VARCHAR},
      year = #{year,jdbcType=INTEGER},
      activity_number = #{activityNumber,jdbcType=VARCHAR},
      internal_labor_cost = #{internalLaborCost,jdbcType=DECIMAL},
      internal_direct_cost = #{internalDirectCost,jdbcType=DECIMAL},
      internal_depreciation_cost = #{internalDepreciationCost,jdbcType=DECIMAL},
      internal_amortization_cost = #{internalAmortizationCost,jdbcType=DECIMAL},
      internal_design_cost = #{internalDesignCost,jdbcType=DECIMAL},
      internal_equipment_cost = #{internalEquipmentCost,jdbcType=DECIMAL},
      internal_other_cost = #{internalOtherCost,jdbcType=DECIMAL},
      external_total_cost = #{externalTotalCost,jdbcType=DECIMAL},
      external_abroad_cost = #{externalAbroadCost,jdbcType=DECIMAL},
      enterprise_filler = #{enterpriseFiller,jdbcType=VARCHAR},
      sign_date = #{signDate,jdbcType=TIMESTAMP},
      sort_number = #{sortNumber,jdbcType=VARCHAR},
      start_date = #{startDate,jdbcType=TIMESTAMP},
      end_date = #{endDate,jdbcType=TIMESTAMP},
      account_url = #{accountUrl,jdbcType=VARCHAR},
      deleted_sign = #{deletedSign,jdbcType=INTEGER}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <select id="findOrgActivityCostListByPage" parameterType="java.lang.String" resultMap="BaseResultMap">
  	 select 
    <include refid="Base_Column_List" />
    from org_activity_cost
    where deleted_sign = 0
    <if test="uid != null">
	    and  uid = #{uid,jdbcType=VARCHAR}
	</if>
	<if test="year != null">
		and  year = #{year,jdbcType=INTEGER}
	</if>
    <if test="activityNumber != null">
    	and  activity_number = #{activityNumber,jdbcType=VARCHAR}
    </if>
    <if test="activityName != null">
    	and  activity_name= #{activityName,jdbcType=VARCHAR}
    </if>
    <if test="startDate != null">
    	and  start_date <![CDATA[ >= ]]> #{startDate,jdbcType=TIMESTAMP}
    </if>
    <if test="endDate != null">
    	and  end_date <![CDATA[ < ]]> #{endDate,jdbcType=TIMESTAMP}
    </if>
    order by start_date
    <if test="page_sql != null">
    	${page_sql}
    </if>
  </select>
  
   <select id="findOrgActivityCostCount" resultType="java.lang.Integer" parameterType="String">
  	 select count(1) 
    from org_activity_cost
    where deleted_sign = 0
    <if test="uid != null">
	    and  uid = #{uid,jdbcType=VARCHAR}
	</if>
	<if test="year != null">
		and year = #{year,jdbcType=INTEGER}
	</if>
    <if test="activityNumber != null">
    	and  activity_number = #{activityNumber,jdbcType=VARCHAR}
    </if>
    <if test="activityName != null">
    	and  activity_name= #{activityName,jdbcType=VARCHAR}
    </if>
    <if test="startDate != null">
    	and  start_date <![CDATA[ >= ]]> #{startDate,jdbcType=TIMESTAMP}
    </if>
    <if test="endDate != null">
    	and  end_date <![CDATA[ < ]]> #{endDate,jdbcType=TIMESTAMP}
    </if>
  </select>
  
  <update id="batchDeleteByPrimaryKey" parameterType="java.util.List">
  	update org_activity_cost set deleted_sign = 1
  	where  id in 
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
		#{item}
	</foreach>
  </update>
  
  <select id="selectOrgActivityCostByAid" parameterType="java.lang.String" resultMap="BaseResultMap">
  	 select 
    <include refid="Base_Column_List" />
    from org_activity_cost
    where aid = #{aid,jdbcType=VARCHAR}
    and deleted_sign = 0
  </select>
  
  <update id="batchDeleteByAid" parameterType="java.util.List">
  	update org_activity_cost set deleted_sign = 1
  	where  aid in 
	<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
		#{item}
	</foreach>
  </update>
  
  <select id="selectOrgActivityCostByUidAndAidAndYear" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from org_activity_cost
    where 
    	aid = #{aid,jdbcType=VARCHAR} 
    and uid = #{uid,jdbcType=VARCHAR} 
    and year = #{year,jdbcType=INTEGER} 
    and deleted_sign = 0
  </select>
  
   <select id="selectOrgActivityCostByAidAndYear" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from org_activity_cost
    where aid = #{0} and year = #{1} and deleted_sign = 0
  </select>
  
  <select id="selectDetailCostByIdAndYear" resultType="com.goafanti.cognizance.bo.OrgActivityDetailCostBo">
  	select oa.id,
	(oac1.internal_labor_cost + oac1.internal_direct_cost + oac1.internal_depreciation_cost + oac1.internal_amortization_cost 
	+ oac1.internal_design_cost + oac1.internal_equipment_cost + oac1.internal_other_cost + oac1.external_total_cost) as firstYearCost,
  (oac2.internal_labor_cost + oac2.internal_direct_cost + oac2.internal_depreciation_cost + oac2.internal_amortization_cost 
	+ oac2.internal_design_cost + oac2.internal_equipment_cost + oac2.internal_other_cost + oac2.external_total_cost) as secondYearCost,
  (oac3.internal_labor_cost + oac3.internal_direct_cost + oac3.internal_depreciation_cost + oac3.internal_amortization_cost 
	+ oac3.internal_design_cost + oac3.internal_equipment_cost + oac3.internal_other_cost + oac3.external_total_cost) as thirdYearCost
	from org_activity oa 
	LEFT JOIN org_activity_cost oac1 on oa.id = oac1.aid and oac1.year = #{1} and oa.deleted_sign = 0 and oac1.deleted_sign = 0
	LEFT JOIN org_activity_cost oac2 on oa.id = oac2.aid and oac2.year = #{1}-1 and oa.deleted_sign = 0 and oac2.deleted_sign = 0
	LEFT JOIN org_activity_cost oac3 on oa.id = oac3.aid and oac3.year = #{1}-2 and oa.deleted_sign = 0 and oac3.deleted_sign = 0
   where oa.id = #{0}
  </select>
</mapper>