<?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.sv.mapper.OrderMapper">

	<resultMap id="OrderMap" type="com.sv.entity.Order">

		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="pay_status" property="payStatus" />
		<result column="member_id" property="memberId" />
		<result column="order_sn" property="orderSn" />
		<result column="parent_order_id" property="parentOrderId" />
		<result column="trade_sn" property="tradeSn" />
		<result column="pay_type" property="payType" />
		<result column="price" property="price" />
		<result column="prepay_id" property="prepayId" />
		<result column="pay_time" property="payTime" />
		<result column="to_money" property="toMoney" />
		<result column="type" property="type" />
		<result column="platform_id" property="platformId" />
		<result column="created_id" property="createdId" />
		<result column="modified_id" property="modifiedId" />
		<result column="created_time" property="createdTime" />
		<result column="modified_time" property="modifiedTime" />
		<result column="deleted" property="deleted" />
	</resultMap>

	<resultMap id="OrderDtoMap" type="com.sv.dto.oms.OrderDTO">

		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="pay_status" property="payStatus" />
		<result column="member_id" property="memberId" />
		<result column="order_sn" property="orderSn" />
		<result column="parent_order_id" property="parentOrderId" />
		<result column="trade_sn" property="tradeSn" />
		<result column="pay_type" property="payType" />
		<result column="price" property="price" />
		<result column="prepay_id" property="prepayId" />
		<result column="pay_time" property="payTime" />
		<result column="type" property="type" />
		<result column="platform_id" property="platformId" />
		<result column="created_id" property="createdId" />
		<result column="modified_id" property="modifiedId" />
		<result column="created_time" property="createdTime" />
		<result column="modified_time" property="modifiedTime" />
		<result column="deleted" property="deleted" />
		<result column="nickname" property="nickname" />
		<result column="mobile" property="mobile" />
	</resultMap>

	<resultMap id="OrderDtoOmsMap" type="com.sv.dto.oms.OrderCheatDTO">
		<result column="price" property="price" />
		<result column="payTime" property="payTime" />
		<result column="days" property="days" />
	</resultMap>
	
	<!--表名 -->
	<sql id="tableName">
		sv_order
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		pay_status,
		member_id,
		order_sn,
		parent_order_id,
		trade_sn,
		pay_type,
		price,
		prepay_id,
		pay_time,
		type,
		platform_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted,
		to_money
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id, jdbcType=INTEGER},
		#{payStatus, jdbcType=TINYINT},
		#{memberId, jdbcType=INTEGER},
		#{orderSn, jdbcType=VARCHAR},
		#{parentOrderId, jdbcType=INTEGER},
		#{tradeSn, jdbcType=VARCHAR},
		#{payType, jdbcType=TINYINT},
		#{price, jdbcType=DECIMAL},
		#{prepayId, jdbcType=VARCHAR},
		#{payTime, jdbcType=TIMESTAMP},
		#{type, jdbcType=TINYINT},
		#{platformId, jdbcType=INTEGER},
		#{createdId, jdbcType=INTEGER},
		#{modifiedId, jdbcType=INTEGER},
		#{createdTime, jdbcType=TIMESTAMP},
		#{modifiedTime, jdbcType=TIMESTAMP},
		#{deleted, jdbcType=TINYINT},
		#{deleted, jdbcType=TINYINT},
		#{toMoney, jdbcType=DECIMAL}

	</sql>

	<!-- 查询全部记录 -->
	<select id="findAll" resultMap="OrderMap">
		SELECT
			<include refid="Field"></include>
		FROM
			<include refid="tableName"></include>
		WHERE
			deleted = 0
	</select>

	<!-- 查询全部记录oms -->
	<select id="findAlls" resultMap="OrderDtoMap">
		SELECT
		so.id,
		so.pay_status,
		so.member_id,
		so.order_sn,
		so.parent_order_id,
		so.trade_sn,
		so.pay_type,
		so.price,
		so.prepay_id,
		so.pay_time,
		so.type,
		so.platform_id,
		so.created_id,
		so.modified_id,
		so.created_time,
		so.modified_time,
		so.deleted,
		sm.nickname,
		sm.mobile
		FROM
		sv_order AS so
		LEFT JOIN sv_member AS sm ON so.member_id = sm.id
		WHERE
		so.deleted = 0
		AND sm.deleted = 0
		<if test="starTime != ''">
			AND so.created_time  &gt;= #{starTime}
		</if>
		<if test="endTime != ''">
			AND so.created_time &lt;= #{endTime}
		</if>
	</select>

	<!-- 查询全部记录oms -->
	<select id="findMoney" resultMap="OrderDtoOmsMap">
		SELECT
			SUM(price) AS price,
			DATE(created_time) as payTime
		FROM
			sv_order
		WHERE
			(type = 2 OR type = 4)
			AND pay_status = 1
			AND pay_type = 1
			AND deleted = 0
			AND DATE_FORMAT(created_time, '%Y%m') = #{time}
			AND deleted = 0
		GROUP BY DATE(created_time)
	</select>
	
	<!-- 新增-->
<insert id="insert" parameterType="com.sv.entity.Order" useGeneratedKeys="true" keyProperty="id">

        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="payStatus != null">
				pay_status,
            </if>
            <if test="memberId != null">
				member_id,
            </if>
            <if test="orderSn != null">
				order_sn,
            </if>
            <if test="parentOrderId != null">
				parent_order_id,
            </if>
            <if test="tradeSn != null">
				trade_sn,
            </if>
            <if test="payType != null">
				pay_type,
            </if>
            <if test="price != null">
				price,
            </if>
            <if test="prepayId != null">
				prepay_id,
            </if>
            <if test="payTime != null">
				pay_time,
            </if>
            <if test="toMoney != null">
                to_money,
            </if>
            <if test="type != null">
				type,
            </if>
            <if test="platformId != null">
				platform_id,
            </if>
            <if test="createdId != null">
				created_id,
            </if>
            <if test="modifiedId != null">
				modified_id,
            </if>
            <if test="createdTime != null">
				created_time,
            </if>
            <if test="modifiedTime != null">
				modified_time,
            </if>
            <if test="deleted != null">
				deleted,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
				#{id},
            </if>
            <if test="payStatus != null">
				#{payStatus},
            </if>
            <if test="memberId != null">
				#{memberId},
            </if>
            <if test="orderSn != null">
				#{orderSn},
            </if>
            <if test="parentOrderId != null">
				#{parentOrderId},
            </if>
            <if test="tradeSn != null">
				#{tradeSn},
            </if>
            <if test="payType != null">
				#{payType},
            </if>
            <if test="price != null">
				#{price},
            </if>
            <if test="prepayId != null">
				#{prepayId},
            </if>
            <if test="payTime != null">
				#{payTime},
            </if>
            <if test="toMoney != null">
				#{toMoney},
            </if>
            <if test="type != null">
				#{type},
            </if>
            <if test="platformId != null">
				#{platformId},
            </if>
            <if test="createdId != null">
				#{createdId},
            </if>
            <if test="modifiedId != null">
				#{modifiedId},
            </if>
            <if test="createdTime != null">
				#{createdTime},
            </if>
            <if test="modifiedTime != null">
				#{modifiedTime},
            </if>
            <if test="deleted != null">
				#{deleted},
            </if>
        </trim>
	</insert>
	
	<!-- 删除-->
	<delete id="delete" parameterType="java.lang.Integer">
		UPDATE
			<include refid="tableName"></include>
		SET
			deleted = 1
		WHERE
			id = #{id}
	</delete>

    <!-- 批量删除 -->
    <delete id="deleteByIds">
        UPDATE
        	<include refid="tableName"></include>
		SET
			deleted = 1
        WHERE
			id IN
			<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
				#{item}
			</foreach>
    </delete>
	
	<!-- 更新记录 -->
<update id="update" parameterType="com.sv.entity.Order">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="payStatus != null">
				pay_status = #{payStatus},
			</if>
			<if test="memberId != null">
				member_id = #{memberId},
			</if>
			<if test="orderSn != null">
				order_sn = #{orderSn},
			</if>
			<if test="parentOrderId != null">
				parent_order_id = #{parentOrderId},
			</if>
			<if test="tradeSn != null">
				trade_sn = #{tradeSn},
			</if>
			<if test="payType != null">
				pay_type = #{payType},
			</if>
			<if test="price != null">
				price = #{price},
			</if>
			<if test="prepayId != null">
				prepay_id = #{prepayId},
			</if>
			<if test="payTime != null">
				pay_time = #{payTime},
			</if>
			<if test="type != null">
				type = #{type},
			</if>
			<if test="platformId != null">
				platform_id = #{platformId},
			</if>
			<if test="createdId != null">
				created_id = #{createdId},
			</if>
			<if test="modifiedId != null">
				modified_id = #{modifiedId},
			</if>
			<if test="createdTime != null">
				created_time = #{createdTime},
			</if>
			<if test="modifiedTime != null">
				modified_time = #{modifiedTime},
			</if>
			<if test="deleted != null">
				deleted = #{deleted},
			</if>
			<if test="toMoney != null">
                to_money = #{toMoney},
			</if>
        </set>
        WHERE id = #{id}
	</update>
	
	<!-- 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="OrderDtoMap">
		SELECT
			so.id,
			so.pay_status,
			so.member_id,
			so.order_sn,
			so.parent_order_id,
			so.trade_sn,
			so.pay_type,
			so.price,
			so.prepay_id,
			so.pay_time,
			so.type,
			so.platform_id,
			so.created_id,
			so.modified_id,
			so.created_time,
			so.modified_time,
			so.deleted,
			sm.nickname
		FROM
			sv_order AS so
		LEFT JOIN sv_member AS sm ON so.member_id = sm.id
		WHERE so.id = #{id}
	</select>

	<!-- api 根据用户Id，订单号查询订单 -->
	<select id="findOrderSn" resultMap="OrderMap">
		SELECT
			id,
			member_id,
			order_sn,
			price,
			`type`,
			platform_id,
			to_money,
            pay_status,
			trade_sn
		FROM
			<include refid="tableName"></include>
		WHERE order_sn = #{orderSn}
			<if test="memberId != null">
				and member_id = #{memberId}
			</if>
		and deleted = 0
	</select>

	<!-- api -->
	<update id="updatePrepayId">
		UPDATE
		<include refid="tableName"></include>
		SET
		prepay_id = #{prepayId}
		where order_sn= #{orderSn}
	</update>

	<!-- api 根据订单编号查询总价钱 -->
	<select id="findByOrderSn" resultMap="OrderMap">
		SELECT
			price
		FROM
			sv_order
		WHERE
			order_sn = #{orderSn}
	</select>
</mapper>