<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.ParkingInfoDAO">
	<resultMap id="ParkingInfoMap" type="com.jpsoft.shinestar.modules.base.entity.ParkingInfo">
		<id property="id" column="id_" />
			<result property="parkId" column="park_id"/>
			<result property="createBy" column="create_by" />
			<result property="createTime" column="create_time" />
			<result property="updateBy" column="update_by" />
			<result property="updateTime" column="update_time" />
			<result property="delFlag" column="del_flag" />
			<result property="parkingName" column="parking_name" />
			<result property="parentId" column="parent_id" />
			<result property="parkingPrice" column="parking_price" />
			<result property="rentPrice" column="rent_price" />
			<result property="contractType" column="contract_type" />
			<result property="ownerId" column="owner_id" />
			<result property="effectiveTime" column="effective_time" />
			<result property="expirationTime" column="expiration_time" />
			<result property="companyId" column="company_id"/>
			<result property="companyName" column="company_name"/>
			<result property="picUrl" column="pic_url"/>
			<result property="code" column="code_"/>
			<result property="applyEndTime" column="apply_end_time"/>
			<result property="parkingFlag" column="parking_flag"/>
			<result property="parentName" column="parent_name"/>
			</resultMap>
	<insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.ParkingInfo">
	<!--
	<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
		select sys_guid() from dual
	</selectKey>
	-->
	<![CDATA[
		insert into base_parking_info
	    (id_,park_id,create_by,create_time,update_by,update_time,del_flag,parking_name,parking_price,rent_price,contract_type,owner_id,effective_time,expiration_time,company_id,parent_id,pic_url,code_,apply_end_time,parking_flag)
		values
		(
#{id,jdbcType=VARCHAR}
,#{parkId,jdbcType=INTEGER}
,#{createBy,jdbcType=VARCHAR}
,#{createTime,jdbcType= TIMESTAMP }
,#{updateBy,jdbcType=VARCHAR}
,#{updateTime,jdbcType= TIMESTAMP }
,#{delFlag,jdbcType= NUMERIC }
,#{parkingName,jdbcType=VARCHAR}
,#{parkingPrice,jdbcType= NUMERIC }
,#{rentPrice,jdbcType= NUMERIC }
,#{contractType,jdbcType=VARCHAR}
,#{ownerId,jdbcType=VARCHAR}
,#{effectiveTime,jdbcType= TIMESTAMP }
,#{expirationTime,jdbcType= TIMESTAMP }
,#{companyId,jdbcType=VARCHAR}
,#{parentId,jdbcType=VARCHAR}
,#{picUrl,jdbcType=VARCHAR}
,#{code,jdbcType=VARCHAR}
,#{applyEndTime,jdbcType=TIMESTAMP}
,#{parkingFlag,jdbcType=BOOLEAN}
		)
	]]>
	</insert>
	<delete id="delete" parameterType="string">
		delete from base_parking_info where id_=#{id,jdbcType=VARCHAR}
	</delete>
	<update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.ParkingInfo">
		update base_parking_info
		<set>
			<if test="parkId!=null">
				park_id=#{parkId,jdbcType=INTEGER},
			</if>
				<if test="createBy!=null">
		create_by=#{createBy,jdbcType=VARCHAR},
		</if>
				<if test="createTime!=null">
		create_time=#{createTime,jdbcType= TIMESTAMP },
		</if>
				<if test="updateBy!=null">
		update_by=#{updateBy,jdbcType=VARCHAR},
		</if>
				<if test="updateTime!=null">
		update_time=#{updateTime,jdbcType= TIMESTAMP },
		</if>
				<if test="delFlag!=null">
		del_flag=#{delFlag,jdbcType= NUMERIC },
		</if>
				<if test="parkingName!=null">
					parking_name=#{parkingName,jdbcType=VARCHAR},
		</if>
				<if test="parkingPrice!=null">
		parking_price=#{parkingPrice,jdbcType= NUMERIC },
		</if>
				<if test="rentPrice!=null">
		rent_price=#{rentPrice,jdbcType= NUMERIC },
		</if>
				<if test="contractType!=null">
		contract_type=#{contractType,jdbcType=VARCHAR},
		</if>
				<if test="ownerId!=null">
		owner_id=#{ownerId,jdbcType=VARCHAR},
		</if>
				<if test="effectiveTime!=null">
		effective_time=#{effectiveTime,jdbcType= TIMESTAMP },
		</if>
				<if test="expirationTime!=null">
		expiration_time=#{expirationTime,jdbcType= TIMESTAMP },
		</if>
			<if test="companyId!=null">
				company_id=#{companyId,jdbcType=VARCHAR},
			</if>
			<if test="parentId!=null">
				parent_id=#{parentId,jdbcType=VARCHAR},
			</if>
			<if test="picUrl!=null">
				pic_url=#{picUrl,jdbcType=VARCHAR},
			</if>
			<if test="code!=null">
				code_=#{code,jdbcType=VARCHAR},
			</if>
			<if test="applyEndTime!=null">
				apply_end_time=#{applyEndTime,jdbcType=TIMESTAMP},
			</if>
			<if test="parkingFlag!=null">
				parking_flag=#{parkingFlag,jdbcType=BOOLEAN},
			</if>
		</set>
	where id_=#{id}
	</update>
	<select id="get" parameterType="string" resultMap="ParkingInfoMap">
		select * from base_parking_info where id_=#{0} and del_flag = 0
	</select>
	<select id="exist" parameterType="string" resultType="int">
		select count(*) from base_parking_info where id_=#{0}
	</select>
	<select id="list" resultMap="ParkingInfoMap">
		select * from base_parking_info where del_flag=0
	</select>
	<select id="search" parameterType="hashmap" resultMap="ParkingInfoMap">
		<![CDATA[
			SELECT
				a.*,
				b.name_ AS company_name,
				c.parking_name AS parent_name
			FROM
				base_parking_info a
				LEFT JOIN base_company_info b ON a.company_id = b.id_
				LEFT JOIN base_parking_info c ON a.parent_id = c.id_ and c.del_flag = 0
		]]>
		<where>
			a.del_flag=0
			<if test="searchParams.parkingName != null">
				and a.parking_name like #{searchParams.parkingName}
			</if>
			<if test="searchParams.parentId != null">
				and a.parent_id = #{searchParams.parentId}
			</if>
			<if test="searchParams.companyId != null">
				and a.company_id = #{searchParams.companyId}
			</if>
			<if test="searchParams.parkingFlag != null">
				and a.parking_flag = #{searchParams.parkingFlag}
			</if>
			<if test="searchParams.code != null">
				and a.code_ like #{searchParams.code}
			</if>
			<if test="searchParams.applyEndTime != null">
				<![CDATA[
				and a.apply_end_time <= #{searchParams.applyEndTime}
				]]>
			</if>
			<if test="searchParams.companyIds != null">
				<foreach collection="searchParams.companyIds" index="index" item="item" open="and a.company_id in(" separator="," close=")">
					#{item.id}
				</foreach>
			</if>
		</where>
		<foreach item="sort" collection="sortList"  open="order by" separator=",">
	        ${sort.name} ${sort.order}
	 	</foreach>
	</select>

	<select id="findByCompanyIdNotParentId" resultMap="ParkingInfoMap">
		<![CDATA[
		select * from base_parking_info
		where del_flag = 0
		and parent_id is null
		and company_id = #{companyId}
		]]>
	</select>

	<select id="findByParentId" resultMap="ParkingInfoMap">
		<![CDATA[
		select * from base_parking_info
		where del_flag = 0
		and parent_id = #{parentId}
		]]>
	</select>

	<select id="findHasPicUrl" resultMap="ParkingInfoMap">
		<![CDATA[
		select * from base_parking_info
		where del_flag = 0
		and pic_url is not null
		and pic_url != ''
		]]>
	</select>

	<select id="findByParkId" resultMap="ParkingInfoMap">
		<![CDATA[
		select * from base_parking_info
		where del_flag = 0
		and park_id = #{parkId}
		]]>
	</select>
</mapper>