<?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.ym.mec.biz.dal.dao.TenantPaymentOrderDao">

	<resultMap type="com.ym.mec.biz.dal.entity.TenantPaymentOrder"
		id="TenantPaymentOrder">
		<result column="id_" property="id" />
		<result column="activities_id_" property="activitiesId" />
		<result column="user_id_" property="userId" />
		<result column="real_name_" property="user.username" />
		<result column="phone_" property="user.phone" />
		<result column="organ_id_" property="organId" />
		<result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="trans_minutes_" property="transMinutes" />
		<result column="give_minutes_" property="giveMinutes" />
		<result column="expect_amount_" property="expectAmount" />
		<result column="actual_amount_" property="actualAmount" />
		<result column="balance_payment_amount_" property="balancePaymentAmount" />
		<result column="com_amount_" property="comAmount" />
		<result column="per_amount_" property="perAmount" />
		<result column="remit_fee_" property="remitFee" />
		<result column="trans_no_" property="transNo" />
		<result column="status_" property="status" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="memo_" property="memo" />
		<result column="payment_channel_" property="paymentChannel" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler" />
		<result column="payment_business_channel_" property="paymentBusinessChannel" />
		<result column="mer_nos_" property="merNos" />
		<result column="order_no_" property="orderNo" />
		<result column="pay_time_" property="payTime" />
		<result column="version_" property="version" />
		<result column="create_time_" property="createTime" />
		<result column="update_time_" property="updateTime" />
	</resultMap>
	
	<sql id="queryPaymentOrder">
        <where>
            <if test="organId != null">
                AND FIND_IN_SET(tpo.organ_id_,#{organId})
            </if>
            <if test="orderStartDate != null">
                AND DATE_FORMAT(tpo.create_time_,'%Y-%m-%d') &gt;= #{orderStartDate}
            </if>
            <if test="orderEndDate != null">
                AND DATE_FORMAT(tpo.create_time_,'%Y-%m-%d') &lt;= #{orderEndDate}
            </if>
            <if test="tenantPaymentType != null">
                AND tpo.type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="userId != null">
                AND tpo.user_id_ = #{userId}
            </if>
            <if test="phone != null and phone != ''">
                AND su.phone_ = #{phone}
            </if>
            <if test="paymentChannel != null">
                AND tpo.payment_channel_ NOT IN (#{paymentChannel})
            </if>
            <if test="status != null">
                AND tpo.status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

	<!-- 根据主键查询一条记录 -->
	<select id="get" resultMap="TenantPaymentOrder">
		SELECT * FROM tenant_payment_order WHERE id_ = #{id}
	</select>

	<!-- 全查询 -->
	<select id="findAll" resultMap="TenantPaymentOrder">
		SELECT * FROM tenant_payment_order ORDER BY id_
	</select>

	<!-- 向数据库增加一条记录 -->
	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.TenantPaymentOrder"
		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
		<!-- <selectKey resultClass="int" keyProperty="id" > SELECT SEQ_WSDEFINITION_ID.nextval 
			AS ID FROM DUAL </selectKey> -->
		INSERT INTO tenant_payment_order
		(id_,activities_id_,user_id_,organ_id_,type_,trans_minutes_,give_minutes_,expect_amount_,actual_amount_,balance_payment_amount_,com_amount_,per_amount_,remit_fee_,trans_no_,status_,memo_,payment_channel_,payment_business_channel_,mer_nos_,order_no_,pay_time_,version_,create_time_,update_time_)
		VALUES(#{id},#{activitiesId},#{userId},#{organId},#{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{transMinutes},#{giveMinutes},#{expectAmount},#{actualAmount},#{balancePaymentAmount},#{comAmount},#{perAmount},#{remitFee},#{transNo},#{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{memo},#{paymentChannel},#{paymentBusinessChannel},#{merNos},#{orderNo},#{payTime},#{version},#{createTime},#{updateTime})
	</insert>

	<!-- 根据主键查询一条记录 -->
	<update id="update" parameterType="com.ym.mec.biz.dal.entity.TenantPaymentOrder">
		UPDATE tenant_payment_order
		<set>
			<if test="status != null">
				status_ = #{status, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="transMinutes != null">
				trans_minutes_ = #{transMinutes},
			</if>
			<if test="giveMinutes != null">
				give_minutes_ = #{giveMinutes},
			</if>
			<if test="organId != null">
				organ_id_ = #{organId},
			</if>
			<if test="activitiesId != null">
				activities_id_ = #{activitiesId},
			</if>
			<if test="comAmount != null">
				com_amount_ = #{comAmount},
			</if>
			<if test="orderNo != null">
				order_no_ = #{orderNo},
			</if>
			<if test="id != null">
				id_ = #{id},
			</if>
			<if test="perAmount != null">
				per_amount_ = #{perAmount},
			</if>
			<if test="expectAmount != null">
				expect_amount_ = #{expectAmount},
			</if>
			<if test="balancePaymentAmount != null">
				balance_payment_amount_ = #{balancePaymentAmount},
			</if>
			<if test="version != null">
				version_ = #{version},
			</if>
			<if test="memo != null">
				memo_ = #{memo},
			</if>
			<if test="paymentChannel != null">
				payment_channel_ = #{paymentChannel},
			</if>
			<if test="remitFee != null">
				remit_fee_ = #{remitFee},
			</if>
			<if test="createTime != null">
				create_time_ = #{createTime},
			</if>
			<if test="userId != null">
				user_id_ = #{userId},
			</if>
			<if test="paymentBusinessChannel != null">
				payment_business_channel_ = #{paymentBusinessChannel},
			</if>
			<if test="updateTime != null">
				update_time_ = #{updateTime},
			</if>
			<if test="transNo != null">
				trans_no_ = #{transNo},
			</if>
			<if test="actualAmount != null">
				actual_amount_ = #{actualAmount},
			</if>
			<if test="type != null">
				type_ = #{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
			</if>
			<if test="merNos != null">
				mer_nos_ = #{merNos},
			</if>
			<if test="payTime != null">
				pay_time_ = #{payTime},
			</if>
		</set>
		WHERE id_ = #{id}
	</update>

	<!-- 根据主键删除一条记录 -->
	<delete id="delete">
		DELETE FROM tenant_payment_order WHERE id_ = #{id}
	</delete>

	<!-- 分页查询 -->
	<select id="queryPage" resultMap="TenantPaymentOrder"
		parameterType="map">
		SELECT tpo.*,su.real_name_,su.phone_ FROM tenant_payment_order tpo
		LEFT JOIN sys_user su ON su.id_ = tpo.user_id_
        <include refid="queryPaymentOrder"/>
		ORDER BY tpo.id_
		<include refid="global.limit" />
	</select>

	<!-- 查询当前表的总记录数 -->
	<select id="queryCount" resultType="int">
		SELECT COUNT(tpo.id_) FROM tenant_payment_order tpo
		LEFT JOIN sys_user su ON su.id_ = tpo.user_id_
        <include refid="queryPaymentOrder"/>
	</select>
	
	<select id="queryByUserId" resultMap="TenantPaymentOrder">
		SELECT * FROM tenant_payment_order where user_id_ = #{userId} ORDER BY id_
	</select>
	
	<select id="queryByUserIdAndActivitiesId" resultMap="TenantPaymentOrder" parameterType="map">
		SELECT * FROM tenant_payment_order where user_id_ = #{userId} and activities_id_ = #{activitiesId} ORDER BY id_
	</select>

    <!-- 根据订单号查询订单 -->
    <select id="findOrderByOrderNo" resultMap="TenantPaymentOrder">
        SELECT * FROM tenant_payment_order WHERE order_no_ = #{orderNo}
    </select>

    <select id="findOrdersByStatus" resultMap="TenantPaymentOrder">
        SELECT * FROM tenant_payment_order
        WHERE status_ = #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
          AND payment_channel_ = #{paymentChannel}
        LIMIT 100
    </select>
	<select id="getOrderMoneyAmount" resultMap="TenantPaymentOrder">
		SELECT SUM(expect_amount_) expect_amount_,SUM(actual_amount_) actual_amount_ FROM tenant_payment_order tpo
		LEFT JOIN sys_user su ON su.id_ = tpo.user_id_
		<include refid="queryPaymentOrder"/>
	</select>
</mapper>
