<?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.ParkingSpacesTransferDetailMapper">
	
	<resultMap type="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferDetail" id="ParkingSpacesTransferDetailMap">
		<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="parking_spaces_attributes" property="parkingSpacesAttributes"/>
		<result column="prediction_area" property="predictionArea"/>
		<result column="measured_area" property="measuredArea"/>
		<result column="advance_date" property="advanceDate"/>
		<result column="completion_date" property="completionDate"/>
		<result column="contract_repossession_date" property="contractRepossessionDate"/>
		<result column="fixed_price" property="fixedPrice"/>
		<result column="preferential_price" property="preferentialPrice"/>
		<result column="preferential_average_price" property="preferentialAveragePrice"/>
		<result column="average_start_cost" property="averageStartCost"/>
		<result column="average_end_cost" property="averageEndCost"/>
		<result column="transfer_price" property="transferPrice"/>
		<result column="suite_model" property="suiteModel"/>
		<result column="preferential" property="preferential"/>
		<result column="payment_method" property="paymentMethod"/>
		<result column="create_date" property="createDate"/>
	
	</resultMap>
	
	<!-- 查询车位移交数据 -->
	<select id="findParkingSpacesTransferDetailLike" parameterType="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferDetail" resultMap="ParkingSpacesTransferDetailMap">
		SELECT
			id,
			organ_name,
			organ_code,
			comm_name,
			comm_id,
			park_name,
			contract_address,
			room_code,
			parking_spaces_attributes,
			prediction_area,
			measured_area,
			advance_date,
			completion_date,
			contract_repossession_date,
			fixed_price,
			preferential_price,
			preferential_average_price,
			average_start_cost,
			average_end_cost,
			transfer_price,
			suite_model,
			preferential,
			payment_method,
			create_date
		FROM
			tb_parking_spaces_transfer_detail
		<where>
	<!-- 	<if test="organCode!=null and organCode!=''"> AND organ_code=#{organCode}</if>
		<if test="commId!=null and commId!=0">AND comm_id=#{commId}</if> -->
		<if test="commName!=null and commName!=''">AND comm_name LIKE CONCAT('%',#{commName},'%')</if>
		<if test="organName!=null and organName!=''">AND organ_name LIKE CONCAT('%',#{organName},'%')</if>
		<if test="createDateStart!=null and createDateStart!=''">AND create_date &gt; #{createDateStart}</if>
		<if test="createDateEnd!=null and createDateEnd!=''">AND create_date &lt; #{createDateEnd}</if>
		<if test="parkName!=null and parkName!=''">AND park_name LIKE CONCAT('%',#{parkName},'%')</if>
		<if test="contractAddress!=null and contractAddress!=''">AND contract_address LIKE CONCAT('%',#{contractAddress},'%')</if>
		</where>	
		ORDER BY create_date DESC
	</select>
	
	<!-- 更新车位移交数据 -->
	<update id="updateParkingSpacesTransferDetail" parameterType="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferDetail">
		UPDATE `tb_parking_spaces_transfer_detail`
		<set>
		 <if test="organName!=null and organName!=''"> `organ_name` = #{organName},</if>
		<!--<if test="organCode!=null and organCode!=''">`organ_code` = #{organCode},</if> -->
		<if test="commName!=null and !=''">`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="roomCode!=null and roomCode!=''"> `room_code` = #{roomCode},</if>
		<if test="parkingSpacesAttributes!=null and parkingSpacesAttributes!=''"> `parking_spaces_attributes` = #{parkingSpacesAttributes},</if>
		<if test="predictionArea!=null and predictionArea!=''"> `prediction_area` = #{predictionArea},</if>
		<if test="measuredArea!=null and measuredArea!=''">`measured_area` = #{measuredArea},</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="fixedPrice!=null and fixedPrice!=0"> `fixed_price` = #{fixedPrice},</if>
	    <if test="preferentialPrice!=null and preferentialPrice!=0">`preferential_price` = #{preferentialPrice},</if>
		<if test="preferentialAveragePrice!=null and preferentialAveragePrice!=0"> `preferential_average_price` = #{preferentialAveragePrice},</if>
		<if test="averageStartCost!=null and averageStartCost!=0">`average_start_cost` = #{averageStartCost},</if>
		<if test="averageEndCost!=null and averageEndCost!=0"> `average_end_cost` = #{averageEndCost},</if>
		<if test="transferPrice!=null and transferPrice!=0"> `transfer_price` = #{transferPrice},</if>
		<if test="suiteModel!=null and suiteModel!=''"> `suite_model` = #{suiteModel},</if>
		<if test="preferential!=null and preferential!=''">`preferential` = #{preferential},</if>
		<if test="paymentMethod!=null and paymentMethod!=''"> `payment_method` = #{paymentMethod},</if>
		<if test="createDate!=null"> `create_date` = #{createDate}</if>
		</set>
		<where>
			(`id` = #{id})
		</where>
	</update>
	
	<!-- 新增车位移交数据 -->
	<insert id="addParkingSpacesTransferDetail" parameterType="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferDetail">
		INSERT INTO `tb_parking_spaces_transfer_detail` (
			`organ_name`,
			`organ_code`,
			`comm_name`,
			`comm_id`,
			`park_name`,
			`contract_address`,
			`room_code`,
			`parking_spaces_attributes`,
			`prediction_area`,
			`measured_area`,
			`advance_date`,
			`completion_date`,
			`contract_repossession_date`,
			`fixed_price`,
			`preferential_price`,
			`preferential_average_price`,
			`average_start_cost`,
			`average_end_cost`,
			`transfer_price`,
			`suite_model`,
			`preferential`,
			`payment_method`,
			`create_date`
		)
		VALUES
			(
				#{organName},
				#{organCode},
				#{commName},
				#{commId},
				#{parkName},
				#{contractAddress},
				#{roomCode},
				#{parkingSpacesAttributes},
				#{predictionArea},
				#{measuredArea},
				#{advanceDate},
				#{completionDate},
				#{contractRepossessionDate},
				#{fixedPrice},
				#{preferentialPrice},
				#{preferentialAveragePrice},
				#{averageStartCost},
				#{averageEndCost},
				#{transferPrice},
				#{suiteModel},
				#{preferential},
				#{paymentMethod},
				#{createDate}
			)
	</insert>
	
	<!-- 删除车位移交数据 -->
	<delete id="deleteParkingSpacesTransferDetail" parameterType="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferDetail">
	DELETE FROM tb_parking_spaces_transfer_detail WHERE tb_parking_spaces_transfer_detail.id =#{id}
	</delete>
	
	<resultMap type="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferReportsDto" id="ParkingSpacesTransferReportsDtoMap">
		<result column="organ_name" property="organName"/>
		<result column="comm_name" property="commName"/>
		<result column="room_code" property="roomCode"/>
		<result column="measuredArea" property="measuredArea"/>
		<result column="predictionArea" property="predictionArea"/>
		<result column="number" property="number"/>
		<result column="preferentialPriceTotal" property="preferentialPriceTotal"/>
		<result column="differenceOfPrices" property="differenceOfPrices"/>
	</resultMap>
	
	<!-- 查询基础数据 -->
	<select id="findParkingSpacesTransferReports" parameterType="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferReportsDto" resultMap="ParkingSpacesTransferReportsDtoMap">
		SELECT DISTINCT
			a.organ_name,
			a.comm_name,
			a.room_code
		FROM 
			tb_parking_spaces_transfer_detail as a
		<where>
			<if test="roomCode!=null and roomCode!=''">AND a.room_code LIKE CONCAT('%',#{roomCode},'%') </if>
			<if test="organName!=null and organName!=''">AND  a.organ_name LIKE CONCAT('%',#{organName},'%')</if>
			<if test="commName!=null and commName!=''">AND a.comm_name LIKE CONCAT('%',#{commName},'%')</if>
		</where>
	</select>
	
	<!-- 查询对应的计数数据-->
	<select id="finParkingSpacesTransferReportsDto" parameterType="cn.com.countrygarden.bi.main.model.dup.ParkingSpacesTransferReportsDto" resultMap="ParkingSpacesTransferReportsDtoMap">
		SELECT
			SUM(a.measured_area) as measuredArea,
			SUM(a.prediction_area) as predictionArea,
			COUNT(a.parking_spaces_attributes) as number,
			SUM(a.preferential_price) as preferentialPriceTotal,
			SUM(a.preferential_price-a.transfer_price) as differenceOfPrices
			FROM 
			tb_parking_spaces_transfer_detail as a
			<where>
			<if test="roomCode!=null and roomCode!=''"> AND a.room_code=#{roomCode} </if>
			<if test="organName!=null and organName!=''">AND  a.organ_name=#{organName}</if>
			<if test="commName!=null and commName!=''">AND a.comm_name=#{commName}</if>
		</where>
	</select>
</mapper>