<?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.ParkingCouponDAO">
	<resultMap id="ParkingCouponMap" type="com.jpsoft.shinestar.modules.base.entity.ParkingCoupon">
		<id property="id" column="id_" />
			<result property="merchantId" column="merchant_id" />
			<result property="type" column="type_" />
			<result property="reduce" column="reduce_" />
			<result property="parkingRecordId" column="parking_record_id" />
			<result property="useStatus" column="use_status" />
			<result property="useQuote" column="use_quote" />
			<result property="useTime" column="use_time" />
			<result property="invalidTime" column="invalid_time" />
			<result property="verifyTime" column="verify_time" />
			<result property="delFlag" column="del_flag" />
			<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="merchantName" column="merchant_name" />
			<result property="companyId" column="company_id" />
			<result property="companyName" column="company_name" />
			<result property="carNum" column="car_num" />
			<result property="inParkingTime" column="in_parking_time" />
			<result property="outParkingTime" column="out_parking_time" />
			</resultMap>
	<insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.ParkingCoupon">
	<!--
	<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
		select sys_guid() from dual
	</selectKey>
	-->
	<![CDATA[
		insert into base_parking_coupon
	    (id_,merchant_id,type_,reduce_,parking_record_id,use_status,use_quote,use_time,invalid_time,verify_time,del_flag,create_by,create_time,update_by,update_time)
		values
		(
#{id,jdbcType=VARCHAR}
,#{merchantId,jdbcType=VARCHAR}
,#{type,jdbcType= VARCHAR }
,#{reduce,jdbcType= NUMERIC }
,#{parkingRecordId,jdbcType=VARCHAR}
,#{useStatus,jdbcType= NUMERIC }
,#{useQuote,jdbcType= NUMERIC }
,#{useTime,jdbcType= TIMESTAMP }
,#{invalidTime,jdbcType= TIMESTAMP }
,#{verifyTime,jdbcType= TIMESTAMP }
,#{delFlag,jdbcType= NUMERIC }
,#{createBy,jdbcType=VARCHAR}
,#{createTime,jdbcType= TIMESTAMP }
,#{updateBy,jdbcType=VARCHAR}
,#{updateTime,jdbcType= TIMESTAMP }
		)
	]]>
	</insert>
	<delete id="delete" parameterType="string">
		delete from base_parking_coupon where id_=#{id,jdbcType=VARCHAR}
	</delete>
	<update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.ParkingCoupon">
		update base_parking_coupon
		<set>
				<if test="merchantId!=null">
		merchant_id=#{merchantId,jdbcType=VARCHAR},
		</if>
				<if test="type!=null">
		type_=#{type,jdbcType= VARCHAR },
		</if>
				<if test="reduce!=null">
		reduce_=#{reduce,jdbcType= NUMERIC },
		</if>
				<if test="parkingRecordId!=null">
		parking_record_id=#{parkingRecordId,jdbcType=VARCHAR},
		</if>
				<if test="useStatus!=null">
		use_status=#{useStatus,jdbcType= NUMERIC },
		</if>
				<if test="useQuote!=null">
		use_quote=#{useQuote,jdbcType= NUMERIC },
		</if>
				<if test="useTime!=null">
		use_time=#{useTime,jdbcType= TIMESTAMP },
		</if>
				<if test="invalidTime!=null">
		invalid_time=#{invalidTime,jdbcType= TIMESTAMP },
		</if>
				<if test="verifyTime!=null">
		verify_time=#{verifyTime,jdbcType= TIMESTAMP },
		</if>
				<if test="delFlag!=null">
		del_flag=#{delFlag,jdbcType= NUMERIC },
		</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>
		</set>
	where id_=#{id}
	</update>
	<select id="get" parameterType="string" resultMap="ParkingCouponMap">
		select * from base_parking_coupon where id_=#{0}
	</select>
	<select id="exist" parameterType="string" resultType="int">
		select count(*) from base_parking_coupon where id_=#{0}
	</select>
	<select id="list" resultMap="ParkingCouponMap">
		select * from base_parking_coupon
	</select>
	<select id="search" parameterType="hashmap" resultMap="ParkingCouponMap">
		<![CDATA[
			SELECT
				a.*,
				b.merchant_name AS merchant_name ,
				c.id_ as company_id,
				c.name_ as company_name,
				d.car_num as car_num,
				d.in_parking_time as in_parking_time,
				d.out_parking_time as out_parking_time
			FROM
				base_parking_coupon a
				LEFT JOIN base_parking_merchant b ON a.merchant_id = b.id_
				LEFT JOIN base_company_info c ON b.company_id = c.id_
				LEFT JOIN base_parking_record d ON a.parking_record_id = d.id_
		]]>
		<where>
			a.del_flag = 0
			and b.del_flag = 0
			and c.del_flag = 0
			and d.del_flag = 0
			<if test="searchParams.id != null">
				and a.ID_ like #{searchParams.id}
			</if>
			<if test="searchParams.merchantName != null">
				and b.merchant_name like #{searchParams.merchantName}
			</if>
			<if test="searchParams.merchantId != null">
				and a.merchant_id = #{searchParams.merchantId}
			</if>
			<if test="searchParams.startTime != null">
				and a.create_time >= #{searchParams.startTime}
			</if>
			<if test="searchParams.endTime != null">
				<![CDATA[
					and a.create_time <= #{searchParams.endTime}
				]]>

			</if>
			<if test="searchParams.carNum != null">
				and d.car_num like #{searchParams.carNum}
			</if>
			<if test="searchParams.companyId != null">
				and c.id_ = #{searchParams.companyId}
			</if>
			<if test="searchParams.useStatus != null">
				and a.use_status = #{searchParams.useStatus}
			</if>
			<if test="searchParams.verifyStartTime != null">
				<![CDATA[
					and a.verify_time >= #{searchParams.verifyStartTime}
				]]>
			</if>
			<if test="searchParams.verifyEndTime != null">
				<![CDATA[
					and a.verify_time <= #{searchParams.verifyEndTime}
				]]>
			</if>
			<if test="searchParams.parentCompanyCode != null">
				and c.code_ like #{searchParams.parentCompanyCode}
			</if>
		</where>
		<foreach item="sort" collection="sortList"  open="order by" separator=",">
	        ${sort.name} ${sort.order}
	 	</foreach>
	</select>

	<select id="findByParkingRecordIdAndStatus" resultMap="ParkingCouponMap">
		<![CDATA[
		select * from base_parking_coupon
		where del_flag = 0
		and parking_record_id = #{parkingRecordId}
		and use_status = #{status}
		limit 1
		]]>
	</select>
</mapper>