<?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="cn.com.countrygarden.bi.main.dao.dup.LastTransferDetailMapper">
	
	<resultMap type="cn.com.countrygarden.bi.main.model.dup.LastTransferDetail" id="LastTransferDetailMap">
		<result column="id" property="id"/>
		<result column="organ_name" property="organName"/>
		<result column="organ_code" property="organCode"/>
		<result column="comm_name" property="commName"/>
		<result column="comm_id" property="commId"/>
		<result column="park_name" property="parkName"/>
		<result column="contract_address" property="contractAddress"/>
		<result column="room_code" property="roomCode"/>
		<result column="building_property" property="buildingProperty"/>
		<result column="building_propertys" property="buildingPropertys"/>
		<result column="decoration_standard" property="decorationStandard"/>
		<result column="whether_demonstration_unit" property="whetherDemonstrationUnit"/>
		<result column="temporary_measure_building" property="temporaryMeasureDuilding"/>
		<result column="temporary_measure_dwelling_space" property="temporaryMeasureDwellingSpace"/>
		<result column="temporary_measure_garden" property="temporaryMeasureGarden"/>
		<result column="actual_measurement_building" property="actualMeasurementDuilding"/>
		<result column="actual_measurement_dwelling_space" property="actualMeasurementDwellingSpace"/>
		<result column="actual_measurement_garden" property="actualMeasurementgarden"/>
		<result column="advance_date" property="advanceDate"/>
		<result column="completion_date" property="completionDate"/>
		<result column="contract_repossession_date" property="contractRepossessionDate"/>
		<result column="sales_status" property="salesStatus"/>
		<result column="cost" property="cost"/>
		<result column="headquarters_low_price_monitor" property="headquartersLowPriceMonitor"/>
		<result column="headquarters_low_price_rough_monitor" property="headquartersLowPriceRoughMonitor"/>
		<result column="whether_partake_light_disk" property="whetherPartakeLightDisk"/>
		<result column="fixed_price" property="fixedPrice"/>
		<result column="sales_promotion_discount" property="salesPromotionDiscount"/>
		<result column="discount_price" property="discountPrice"/>
		<result column="contract_version" property="contractVersion"/>
		<result column="whether_sell" property="whetherSell"/>
		<result column="no_sell_reason" property="noSellReason"/>
		<result column="review_status" property="reviewStatus"/>
		<result column="status" property="status"/>
		<result column="create_date" property="createDate"/>
	</resultMap>
	
	
  <!-- 查询尾盘移交数据的所有数据 -->		
  <select id="findLastTransferDetailLike" parameterType="cn.com.countrygarden.bi.main.model.dup.LastTransferDetail" resultMap="LastTransferDetailMap">
		SELECT
			 id ,
			 organ_name,
			 comm_name ,
			 comm_id ,
			 park_name ,
			 contract_address ,
			 room_code ,
			 building_property ,
			 building_propertys,
			 decoration_standard ,
			 whether_demonstration_unit ,
			 temporary_measure_building ,
			 temporary_measure_dwelling_space ,
			 temporary_measure_garden ,
			 actual_measurement_building ,
			 actual_measurement_dwelling_space ,
			 actual_measurement_garden ,
			 advance_date ,
			 completion_date ,
			 contract_repossession_date ,
			 cost ,
			 headquarters_low_price_monitor ,
			 headquarters_low_price_rough_monitor ,
			 whether_partake_light_disk ,
			 fixed_price ,
			 sales_promotion_discount ,
			 discount_price ,
			 contract_version ,
			 whether_sell ,
			 no_sell_reason ,
			 review_status ,
			 status,
			 create_date 
		FROM
			 tb_last_transfer_detail 
		<where>		
		<if test="organName!=null and organName!=''">and tb_last_transfer_detail.organ_name LIKE CONCAT('%',#{organName},'%')</if> 
		<if test="commName!=null and commName!=''">and tb_last_transfer_detail.comm_name LIKE CONCAT('%',#{commName},'%')</if> 
		<if test="reviewStatus!=null and reviewStatus!=''">and  tb_last_transfer_detail.review_status= #{reviewStatus}</if>
		<if test="createDateStart!=null and createDateStart!='' ">and DATE_FORMAT(tb_last_transfer_detail.create_date,'%Y-%m-%d') &gt;= DATE_FORMAT(#{createDateStart},'%Y-%m-%d')</if>
		<if test="createDateEnd!=null and createDateEnd!=''">     and DATE_FORMAT(tb_last_transfer_detail.create_date,'%Y-%m-%d') &lt;= DATE_FORMAT(#{createDateEnd},'%Y-%m-%d')</if>
		</where>	 
	ORDER BY tb_last_transfer_detail.create_date DESC	 
  </select>
  
  <!-- 更新尾盘移交数据 -->
  <update id="updataLastTransferDetail" parameterType="cn.com.countrygarden.bi.main.model.dup.LastTransferDetail">
  	UPDATE `tb_last_transfer_detail`
		<set>
		<if test="organName!=null and organName!=''">`organ_name`=#{organName},</if>
		<if test="commName!=null and commName!=''">`comm_name` = #{commName},</if>
		<!-- <if test="commId!=null and commId!=0">`comm_id` = #{commId},</if>  -->
		<if test="parkName!=null and parkName!=''">`park_name` = #{parkName},</if>
		<if test="contractAddress!=null and contractAddress!=''"> `contract_address` = #{contractAddress},</if>
		<if test="buildingProperty!=null and buildingProperty!=''">`building_property` = #{buildingProperty},</if>
		<if test="decorationStandard!=null and decorationStandard!=''">`decoration_standard` = #{decorationStandard},</if>
		<if test="whetherDemonstrationUnit !=null and whetherDemonstrationUnit!=''"> `whether_demonstration_unit` = #{whetherDemonstrationUnit},</if>
		<if test="temporaryMeasureDuilding!=null and temporaryMeasureDuilding!=''">`temporary_measure_building` = #{temporaryMeasureDuilding},</if>
		<if test="temporaryMeasureDwellingSpace!=null and temporaryMeasureDwellingSpace!=''">`temporary_measure_dwelling_space` = #{temporaryMeasureDwellingSpace},</if>
		<if test="temporaryMeasureGarden!=null and temporaryMeasureGarden!=''"> `temporary_measure_garden` = #{temporaryMeasureGarden},</if>
		<if test="actualMeasurementDuilding!=null and actualMeasurementDuilding!=''">`actual_measurement_building` = #{actualMeasurementDuilding},</if>
		<if test="actualMeasurementDwellingSpace!=null and actualMeasurementDwellingSpace!=''">`actual_measurement_dwelling_space` = #{actualMeasurementDwellingSpace},</if>
		<if test="actualMeasurementgarden!=null and actualMeasurementgarden!=''"> `actual_measurement_garden` = #{actualMeasurementgarden},</if>
		<if test="advanceDate!=null"> `advance_date` = #{advanceDate},</if>
		<if test="completionDate !=null"> `completion_date` = #{completionDate},</if>
		<if test="contractRepossessionDate!=null"> `contract_repossession_date` = #{contractRepossessionDate},</if>
		<if test="cost!=null and cost!=''"> `cost` = #{cost},</if>
		<if test="headquartersLowPriceMonitor!=null and headquartersLowPriceMonitor!=''">`headquarters_low_price_monitor` = #{headquartersLowPriceMonitor},</if>
		<if test="headquartersLowPriceRoughMonitor!=null and headquartersLowPriceRoughMonitor!=''"> `headquarters_low_price_rough_monitor` = #{headquartersLowPriceRoughMonitor},</if>
		<if test="whetherPartakeLightDisk!=null and whetherPartakeLightDisk!=''"> `whether_partake_light_disk` = #{whetherPartakeLightDisk},</if>
		<if test="fixedPrice!=null and fixedPrice!=0"> `fixed_price` = #{fixedPrice},</if>
		<if test="salesPromotionDiscount!=null and salesPromotionDiscount!=0">`sales_promotion_discount` = #{salesPromotionDiscount},</if> 
		<if test="discountPrice!=null and discountPrice!=''">`discount_price` = #{discountPrice},</if> 
		<if test="contractVersion!=null and contractVersion!=''"> `contract_version` = #{contractVersion},</if>
		<if test="whetherSell!=null and whetherSell!=''"> `whether_sell` = #{whetherSell},</if>
		<if test="noSellReason!=null and noSellReason!=''"> `no_sell_reason` = #{noSellReason},</if>
		<if test="reviewStatus!=null and reviewStatus!=''"> `review_status` = #{reviewStatus},</if>
		<if test="status!=null and status!=''">`status`= #{status},</if>
		<if test="createDate!=null"> `create_date` = #{createDate}</if>
	 </set>
		WHERE
			(`room_code` = #{roomCode})
  </update>
  
  <!-- 新增尾盘移交数据 -->
  <insert id="addLastTransferDetail" parameterType="cn.com.countrygarden.bi.main.model.dup.LastTransferDetail">
  	INSERT INTO `tb_last_transfer_detail` (
  		`organ_name`,
		`comm_name`,
		`park_name`,
		`contract_address`,
		`room_code`,
		`building_property`,
		`decoration_standard`,
		`whether_demonstration_unit`,
		`temporary_measure_building`,
		`temporary_measure_dwelling_space`,
		`temporary_measure_garden`,
		`actual_measurement_building`,
		`actual_measurement_dwelling_space`,
		`actual_measurement_garden`,
		`advance_date`,
		`completion_date`,
		`contract_repossession_date`,
		`cost`,
		`headquarters_low_price_monitor`,
		`headquarters_low_price_rough_monitor`,
		`whether_partake_light_disk`,
		`fixed_price`,
		`sales_promotion_discount`,
		`discount_price`,
		`contract_version`,
		`whether_sell`,
		`no_sell_reason`,
		`review_status`,
		`status`,
		`create_date`
	)
	VALUES
		(
		#{organName},
		#{commName},
		#{parkName},
		#{contractAddress},
		#{roomCode},
		#{buildingProperty},
	    #{decorationStandard},
	    #{whetherDemonstrationUnit},
		#{temporaryMeasureDuilding},
		#{temporaryMeasureDwellingSpace},
		#{temporaryMeasureGarden},
		#{actualMeasurementDuilding},
	    #{actualMeasurementDwellingSpace},
	    #{actualMeasurementgarden},
	    #{advanceDate},
	    #{completionDate},
	    #{contractRepossessionDate},
		#{cost},
		#{headquartersLowPriceMonitor},
		#{headquartersLowPriceRoughMonitor},
		#{whetherPartakeLightDisk},
		#{fixedPrice},
		#{salesPromotionDiscount},
		#{discountPrice}, 
		#{contractVersion},
	    #{whetherSell},
		#{noSellReason},
		#{reviewStatus},
		#{status},
		#{createDate}
		)
  </insert>
  
  <!-- 删除尾盘移交数据 -->
  <delete id="deleteLastTransferDetail" parameterType="cn.com.countrygarden.bi.main.model.dup.LastTransferDetail">
   DELETE
		FROM
			tb_last_transfer_detail
		WHERE
			tb_last_transfer_detail.id=#{id}
  </delete>
  
  <!-- 查询所有的房间代码 -->
  <select id="findAllRoomCode" resultType="string">
  	SELECT
		tb_last_transfer_detail.room_code
	FROM
		tb_last_transfer_detail
  </select>
  
  <resultMap type="cn.com.countrygarden.bi.main.model.dup.LastTransferDetailReports" id="lastTransferDetailReportsMap">
  	<result column="organ_name" property="organName"/>
  	<result column="comm_name" property="commName"/>
  	<result column="building_property" property="buildingProperty"/>
  	<result column="building_propertys" property="buildingPropertys"/>
  	<result column="status" property="status"/>
  	<result column="number" property="number"/>
  	<result column="fixed_price" property="fixedPrice"/>
  </resultMap>	
  	
  <!--  查询尾盘转移数据报表(支持模糊查询) -->
  <select id="findLastTransferDetailLikeReports" parameterType="cn.com.countrygarden.bi.main.model.dup.LastTransferDetailReports" resultMap="lastTransferDetailReportsMap">
	 SELECT DISTINCT
		tb_last_transfer_detail.organ_name,
		tb_last_transfer_detail.comm_name,
		tb_last_transfer_detail.building_property,
		tb_last_transfer_detail.building_propertys,
		tb_last_transfer_detail.status
	FROM
		tb_last_transfer_detail
	<where>
		<if test="organName!=null and organName!=''">and tb_last_transfer_detail.organ_name LIKE CONCAT('%',#{organName},'%')</if> 
		<if test="commName!=null and commName!=''">and tb_last_transfer_detail.comm_name LIKE CONCAT('%',#{commName},'%')</if> 
	</where>	
	ORDER  BY  tb_last_transfer_detail.organ_name DESC
  </select>
  
  <!-- 查询数量 -->
  <select id="findLastTransferDetailLikeReportsNumber" parameterType="cn.com.countrygarden.bi.main.model.dup.LastTransferDetailReports" resultMap="lastTransferDetailReportsMap">
  	SELECT
		COUNT(
			tb_last_transfer_detail.room_code
		) AS number,
		SUM(
			tb_last_transfer_detail.fixed_price
		) AS fixed_price
	FROM
		tb_last_transfer_detail
	<where>
	<if test="organName!=null and organName!=''"> AND  tb_last_transfer_detail.organ_name =#{organName}</if>
	<if test="commName!=null and commName!=''">AND tb_last_transfer_detail.comm_name =#{commName}</if>
	<if test="buildingProperty!=null  and buildingProperty!=''">AND tb_last_transfer_detail.building_property=#{buildingProperty}</if>
	<if test="buildingPropertys!=null  and buildingPropertys!=''">AND tb_last_transfer_detail.building_propertys=#{buildingPropertys}</if>
	<if test="status!=null">AND tb_last_transfer_detail.status=#{status}</if>
  </where>
  </select>
</mapper>