<?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.iformall.mapper.WxCarPayRecordMapper">
    <resultMap id="BaseResultMap" type="com.iformall.domain.po.WxCarPayRecord">
        <id column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="vendor_type" property="vendorType"/>
        <result column="syn_id" property="synId"/>
        <result column="park_id" property="parkId"/>
        <result column="park_name" property="parkName"/>
        <result column="user_type" property="userType"/>
        <result column="plate_number" property="plateNumber"/>
        <result column="entrance_time" property="entranceTime"/>
        <result column="exit_time" property="exitTime"/>
        <result column="fee" property="fee"/>
        <result column="fee_time" property="feeTime"/>
        <result column="coupon" property="coupon"/>
        <result column="coupon_code" property="couponCode"/>
        <result column="fix_parking_id" property="fixParkingId"/>
        <result column="remaining_days" property="remainingDays"/>
        <result column="discount_amount" property="discountAmount"/>
        <result column="paid_service_fee" property="paidServiceFee"/>
        <result column="phone" property="phone"/>
        <result column="create_date" property="createDate"/>
        <result column="update_date" property="updateDate"/>
        <result column="stayed_time" property="stayedTime"/>
    </resultMap>

    <sql id="allColumns">
	    `id`,`tenant_id`,`vendor_type`,`create_date`,`update_date`,syn_id,park_id,park_name,user_type,
	    plate_number,entrance_time,exit_time,fee,fee_time,coupon,coupon_code,fix_parking_id,remaining_days,discount_amount,phone
	    ,paid_service_fee,create_date,update_date,floor(stayed_time/60) stayed_time
	</sql>

    <sql id="dynamicWhereConditions">
        where 1 = 1

        <if test=" null != id ">
            and `id` = #{id}
        </if>
        <if test=" null != synId ">
            and `syn_id` = #{synId}
        </if>
        <if test=" null != feeTimeStr ">
            and `fee_time` = #{feeTimeStr}
        </if>
        <if test=" null != fee ">
            and `fee` = #{fee}
        </if>
        <if test=" null != feeTime ">
            and TIMESTAMPDIFF(MINUTE, `fee_time`, #{feeTime}) &lt;= 15
        </if>
        <if test=" null != tenantId ">
            and `tenant_id` = #{tenantId}
        </if>

        <if test=" null != vendorType ">
            and `vendor_type` = #{vendorType}
        </if>

        <choose>
            <when test="1==isPay">
                <if test=" null != startDate">
                    and fee_time >= #{startDate}
                </if>
                <if test=" null != endDate">
                    and fee_time &lt;= #{endDate}
                </if>
            </when>
            <otherwise>
                <if test=" null != startDate">
                    and create_date >= #{startDate}
                </if>
                <if test=" null != endDate">
                    and create_date &lt;= #{endDate}
                </if>
            </otherwise>
        </choose>

        <if test=" 1 == isPay">
            and fee is not null
            and fee != ''
        </if>

        <if test=" null != sortColumns">order by ${sortColumns}</if>
        <if test=" null == sortColumns">order by create_date</if>
    </sql>

    <select id="findCarPayListByPage" parameterType="com.iformall.domain.po.WxCarPayRecord" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_car_pay_record
        <include refid="dynamicWhereConditions"/>
    </select>

    <select id="findCarPayList" parameterType="com.iformall.domain.po.WxCarPayRecord" resultMap="BaseResultMap">
        select
        <include refid="allColumns"/>
        from wx_car_pay_record
        <include refid="dynamicWhereConditions"/>
    </select>

    <update id="updateBySynId" parameterType="com.iformall.domain.po.WxCarPayRecord">
        update wx_car_pay_record set update_date = now()
        <if test="null != exitTime">
            ,exit_time = #{exitTime}
        </if>
        <if test="null != stayedTime">
            ,stayed_time = #{stayedTime}
        </if>
        <if test="null != fee">
            ,fee = #{fee}
        </if>
        <if test="null != feeTime">
            ,fee_time = #{feeTime}
        </if>
        <if test="null != paidServiceFee">
            ,paid_service_fee = #{paidServiceFee}
        </if>

        where syn_id = #{synId}
    </update>

    <select id="findCarPayCount" parameterType="com.iformall.domain.po.WxCarPayRecord" resultType="Integer">
        select
        count(*)
        from wx_car_pay_record
        <include refid="dynamicWhereConditions"/>
    </select>


</mapper>
