<?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.kmall.common.dao.CusPayDataOpenDao">

    <resultMap type="com.kmall.common.entity.CusPayDataOpenEntity" id="cusPayDataOpenMap">
        <result property="id" column="id"/>
        <result property="merchOrderSn" column="merch_order_sn"/>
        <result property="orderNo" column="order_no"/>
        <result property="initalRequest" column="inital_request"/>
        <result property="initalResponse" column="inital_response"/>
        <result property="payTransactionId" column="pay_transaction_id"/>
        <result property="totalAmount" column="total_amount"/>
        <result property="currency" column="currency"/>
        <result property="verDept" column="ver_dept"/>
        <result property="payType" column="pay_type"/>
        <result property="tradingTime" column="trading_time"/>
        <result property="note" column="note"/>
        <result property="createrSn" column="creater_sn"/>
        <result property="createTime" column="create_time"/>
        <result property="moderSn" column="moder_sn"/>
        <result property="modTime" column="mod_time"/>
        <result property="tstm" column="tstm"/>
		<collection property="goodsInfoList" ofType="com.kmall.common.entity.pay.data.open.GoodsInfo">
			<result property="gname" column="gname"/>
			<result property="itemLink" column="item_link"/>
		</collection>
    </resultMap>

	<select id="queryObject" resultType="com.kmall.common.entity.CusPayDataOpenEntity">
		select
			`id`,
			`merch_order_sn`,
			`order_no`,
			`inital_request`,
			`inital_response`,
			`pay_transaction_id`,
			`total_amount`,
			`currency`,
			`ver_dept`,
			`pay_type`,
			`trading_time`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`
		from cus_pay_data_open
		where id = #{id}
	</select>

	<select id="queryObjectByOrderNo" resultMap="cusPayDataOpenMap">
		SELECT
			t.id,
			t.merch_order_sn,
			t.order_no,
			t.inital_request,
			t.inital_response,
			t.pay_transaction_id,
			t.total_amount,
			t.currency,
			t.ver_dept,
			t.pay_type,
			t.trading_time,
			t.note,
			t.creater_sn,
			t.create_time,
			t.moder_sn,
			t.mod_time,
			t.tstm,
			t2.goods_name AS gname,
			t2.goods_id AS item_link
		FROM
			cus_pay_data_open t
			LEFT JOIN mall_order t1 ON t.order_no = t1.order_sn
			LEFT JOIN mall_order_goods t2 ON t1.id = t2.order_id
		WHERE
			order_no = #{orderNo}
	</select>

	<select id="queryList" resultType="com.kmall.common.entity.CusPayDataOpenEntity">
		select
    		`id`,
    		`merch_order_sn`,
    		`order_no`,
    		`inital_request`,
    		`inital_response`,
    		`pay_transaction_id`,
    		`total_amount`,
    		`currency`,
    		`ver_dept`,
    		`pay_type`,
    		`trading_time`,
    		`note`,
    		`creater_sn`,
    		`create_time`,
    		`moder_sn`,
    		`mod_time`,
    		`tstm`
		from cus_pay_data_open
		WHERE 1=1
		<if test="name != null and name.trim() != ''">
			AND name LIKE concat('%',#{name},'%')
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by id desc
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from cus_pay_data_open
		WHERE 1=1
        <if test="name != null and name.trim() != ''">
            AND name LIKE concat('%',#{name},'%')
        </if>
	</select>
	 
	<insert id="save" parameterType="com.kmall.common.entity.CusPayDataOpenEntity" useGeneratedKeys="true" keyProperty="id">
		insert into cus_pay_data_open(
			`merch_order_sn`,
			`order_no`,
			`inital_request`,
			`inital_response`,
			`pay_transaction_id`,
			`total_amount`,
			`currency`,
			`ver_dept`,
			`pay_type`,
			`trading_time`,
			`note`,
			`creater_sn`,
			`create_time`,
			`moder_sn`,
			`mod_time`,
			`tstm`)
		values(
			#{merchOrderSn},
			#{orderNo},
			#{initalRequest},
			#{initalResponse},
			#{payTransactionId},
			#{totalAmount},
			#{currency},
			#{verDept},
			#{payType},
			#{tradingTime},
			#{note},
			#{createrSn},
			#{createTime},
			#{moderSn},
			#{modTime},
			#{tstm})
	</insert>
	 
	<update id="update" parameterType="com.kmall.common.entity.CusPayDataOpenEntity">
		update cus_pay_data_open 
		<set>
			<if test="merchOrderSn != null">`merch_order_sn` = #{merchOrderSn}, </if>
			<if test="orderNo != null">`order_no` = #{orderNo}, </if>
			<if test="initalRequest != null">`inital_request` = #{initalRequest}, </if>
			<if test="initalResponse != null">`inital_response` = #{initalResponse}, </if>
			<if test="payTransactionId != null">`pay_transaction_id` = #{payTransactionId}, </if>
			<if test="totalAmount != null">`total_amount` = #{totalAmount}, </if>
			<if test="currency != null">`currency` = #{currency}, </if>
			<if test="verDept != null">`ver_dept` = #{verDept}, </if>
			<if test="payType != null">`pay_type` = #{payType}, </if>
			<if test="tradingTime != null">`trading_time` = #{tradingTime}, </if>
			<if test="note != null">`note` = #{note}, </if>
			<if test="createrSn != null">`creater_sn` = #{createrSn}, </if>
			<if test="createTime != null">`create_time` = #{createTime}, </if>
			<if test="moderSn != null">`moder_sn` = #{moderSn}, </if>
			<if test="modTime != null">`mod_time` = #{modTime}, </if>
			<if test="tstm != null">`tstm` = #{tstm}</if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from cus_pay_data_open where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from cus_pay_data_open where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>