<?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.TenantOrderRecordDao">
    <resultMap id="BaseResultMap" type="com.ym.mec.biz.dal.entity.TenantOrderRecord">
        <id column="id_" jdbcType="INTEGER" property="id"/>
        <result column="tenant_id_" jdbcType="INTEGER" property="tenantId"/>
        <result column="order_no_" jdbcType="VARCHAR" property="orderNo"/>
        <result column="trans_no_" jdbcType="VARCHAR" property="transNo"/>
        <result column="order_type_" jdbcType="VARCHAR" property="orderType"/>
        <result column="payment_channel_" jdbcType="VARCHAR" property="paymentChannel"/>
        <result column="pay_channel_" jdbcType="VARCHAR" property="payChannel"/>
        <result column="expect_amount" jdbcType="VARCHAR" property="expectAmount"/>
        <result column="actual_amount" jdbcType="VARCHAR" property="actualAmount"/>
        <result column="mer_nos_" jdbcType="VARCHAR" property="merNos"/>
        <result column="order_state_" jdbcType="INTEGER" property="orderState"/>
        <result column="pay_date_" jdbcType="TIMESTAMP" property="payDate"/>
        <result column="pay_time_" jdbcType="TIMESTAMP" property="payTime"/>
        <result column="created_time_" jdbcType="TIMESTAMP" property="createdTime"/>
    </resultMap>

    <sql id="Base_Column_List">
        id_
        , tenant_id_, order_no_, trans_no_, order_type_, payment_channel_, pay_channel_, expect_amount, actual_amount, mer_nos_, order_state_, pay_date_, pay_time_, created_time_
    </sql>
    
    <select id="getLockedById" resultMap="BaseResultMap">
    	select * from tenant_order_record where id_ = #{id} for update
    </select>

    <insert id="insertBatch" keyColumn="id_" keyProperty="id" useGeneratedKeys="true"
            parameterType="com.ym.mec.biz.dal.entity.TenantOrderRecord">
        insert into tenant_order_record(tenant_id_, order_no_, trans_no_, order_type_, payment_channel_, pay_channel_,
        expect_amount, actual_amount, mer_nos_,order_state_, pay_date_, pay_time_, created_time_)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.tenantId}, #{entity.orderNo}, #{entity.transNo}, #{entity.orderType}, #{entity.paymentChannel},
            #{entity.payChannel}, #{entity.expectAmount}, #{entity.actualAmount}, #{entity.merNos},#{entity.orderState},
            #{entity.payDate}, #{entity.payTime}, #{entity.createdTime})
        </foreach>
    </insert>

    <select id="queryPage" parameterType="com.ym.mec.biz.dal.dto.TenantOrderRecordDto"
            resultType="com.ym.mec.biz.dal.vo.TenantOrderRecordVo">
        SELECT
            b.name_ as tenantName,
            a.tenant_id_ as tenantId,
            a.order_no_ as orderNo,
            a.trans_no_ as transNo,
            a.order_type_ as orderType,
            a.payment_channel_ as paymentChannel,
            a.pay_channel_ AS payChannel,
            a.expect_amount as expectAmount,
            a.actual_amount as actualAmount,
            a.order_state_ as orderState,
            a.pay_date_ as payDate,
            a.pay_time_ as payTime,
            a.created_time_ as createdTime
        FROM
            tenant_order_record AS a
                LEFT JOIN tenant_info AS b
                          ON a.tenant_id_ = b.id_
    <where>
        <if test="param.tenantId != null">
            a.tenant_id_ = #{param.tenantId}
        </if>
        <if test="param.orderNo != null ">
            AND a.`order_no_` = #{param.orderNo}
        </if>
        <if test="param.transNo != null ">
            AND a.`trans_no_` = #{param.transNo}
        </if>
        <if test="param.orderType != null ">
            AND a.`order_type_` = #{param.orderType}
        </if>
        <if test="param.orderState != null ">
            AND a.`order_state_` = #{param.orderState}
        </if>
        <if test="param.startDate != null">
            AND a.`created_time_` <![CDATA[ >= ]]>  #{param.startDate}
        </if>
        <if test="param.endDate != null ">
            AND a.`created_time_` <![CDATA[ <= ]]>  #{param.endDate}
        </if>
    </where>
    </select>
    
</mapper>
