<?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.backendsys.mapper.B2c.B2cOrderMapper">

    <sql id="includeOrder">
        bo.id id,
        bo.id order_id,
        bo.order_code order_code,
        bo.member_open_id,
        bo.final_total_price,
        bo.pay_method,
        bo.pay_time,
        bo.pay_status,
        bo.prepay_id,
        bo.pay_refund_time,
        bo.pay_refund_status,
        bo.cancel_order_way,
        bo.cancel_order_reason,
        bo.status,
        bo.is_readed,
        bo.notes,
        bo.create_time,
        bo.update_time
    </sql>

    <resultMap id="resultMapOrder" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="order_id" column="order_id" javaType="java.lang.Long"/>
        <result property="order_code" column="order_code" />
        <result property="member_open_id" column="member_open_id" />
        <result property="final_total_price" column="final_total_price" javaType="java.math.BigDecimal"/>
        <result property="pay_method" column="pay_method" />
        <result property="pay_time" column="pay_time" />
        <result property="pay_status" column="pay_status" javaType="java.lang.Integer"/>
        <result property="prepay_id" column="prepay_id" />
        <result property="pay_refund_time" column="pay_refund_time" />
        <result property="pay_refund_status" column="pay_refund_status" javaType="java.lang.Integer" />
        <result property="cancel_order_way" column="cancel_order_way" javaType="java.lang.Integer" />
        <result property="cancel_order_reason" column="cancel_order_reason" />
        <result property="status" column="status" javaType="java.lang.Integer" />
        <result property="is_readed" column="is_readed" javaType="java.lang.Integer"/>
        <result property="notes" column="notes" />
        <result property="create_time" column="create_time" />
        <result property="update_time" column="update_time" />
        <collection select="queryOrderMemberById" property="snapshoot_member" javaType="java.util.LinkedHashMap" ofType="java.util.LinkedHashMap" column="order_id">
            <id property="id" column="id" />
            <result property="order_id" column="order_id" />
            <result property="member_id" column="member_id" />
            <result property="member_nickname" column="member_nickname" />
            <result property="member_phone" column="member_phone" />
            <result property="recipient_name" column="recipient_name" />
            <result property="recipient_phone" column="recipient_phone" />
            <result property="recipient_address" column="recipient_address" />
            <result property="recipient_point_longitude" column="recipient_point_longitude"/>
            <result property="recipient_point_latitude" column="recipient_point_latitude"/>
            <result property="recipient_price" column="recipient_price" />
        </collection>
        <collection select="queryOrderGoodById" property="snapshoot_good" javaType="java.util.List" ofType="java.util.LinkedHashMap" column="order_id">
            <id property="id" column="id" />
            <result property="order_id" column="order_id" />
            <result property="order_code" column="order_code" />
            <result property="good_id" column="good_id" />
            <result property="good_name" column="good_name" />
            <result property="good_description" column="good_description" />
            <result property="good_thumb" column="good_thumb" />
            <result property="spec_ids" column="spec_ids" />
            <result property="spec_groups" column="spec_groups" />
            <result property="unit_price" column="unit_price" />
            <result property="quantity" column="quantity" />
        </collection>
    </resultMap>

    <!-- insert -->
    <!--ST_GEOMFROMTEXT(#{item.geom, typeHandler=com.threestone.common.mybatisTypeHandler.LineStringTypeHandler})-->

    <!-- 子查询 (订单-会员信息) -->
    <select id="queryOrderMemberById" resultType="java.util.LinkedHashMap">
        SELECT bosm.id,bosm.order_id, bosm.member_id, bosm.member_nickname, bosm.member_phone,
               bosm.recipient_name, bosm.recipient_phone,  bosm.recipient_address,
               bosm.recipient_point_longitude, bosm.recipient_point_latitude, bosm.recipient_price
        FROM b2c_order_snapshoot_member bosm
        WHERE order_id = #{order_id}
    </select>
    <!-- 子查询 (订单-商品信息) -->
    <select id="queryOrderGoodById" resultType="java.util.LinkedHashMap">
        SELECT bosg.id, bosg.order_id, bosg.order_code, bosg.good_id, bosg.good_name, bosg.good_description,
               bosg.good_thumb, bosg.spec_ids, bosg.spec_groups, bosg.unit_price, bosg.quantity
        FROM b2c_order_snapshoot_good bosg
        WHERE order_id = #{order_id}
    </select>

<!-- FROM b2c_order gb-->
<!-- LEFT JOIN b2c_order gbp ON gb.parent_id = gbp.id-->
<!-- AND order_code LIKE CONCAT('%', #{order_code}, '%')-->
    <select id="queryOrderList" resultMap="resultMapOrder">
        SELECT
        <include refid="includeOrder" />
        FROM b2c_order bo
        <where>
            <if test="order_code != null and order_code != ''">
                AND bo.order_code LIKE CONCAT('%', #{order_code}, '%')
            </if>
            <if test="member_id != null and member_id != ''">
                AND bo.member_id = #{member_id}
            </if>
            <if test="pay_method != null and pay_method != ''">
                AND bo.pay_method = #{pay_method}
            </if>
            <if test="pay_status != null and pay_status != ''">
                AND bo.pay_status = #{pay_status}
            </if>
            <if test="prepay_id != null and prepay_id != ''">
                AND bo.prepay_id = #{prepay_id}
            </if>
            <if test="pay_refund_status != null and pay_refund_status != ''">
                AND bo.pay_refund_status = #{pay_refund_status}
            </if>
            <if test="cancel_order_way != null and cancel_order_way != ''">
                AND bo.cancel_order_way = #{cancel_order_way}
            </if>
            <if test="status != null and status != ''">
                AND bo.status = #{status}
            </if>
            <if test="is_readed != null and is_readed != ''">
                AND is_readed = #{is_readed}
            </if>
            <if test="create_time_begin != null and create_time_begin != '' and create_time_end != null and create_time_end != ''">
                AND create_time BETWEEN #{create_time_begin} AND #{create_time_end}
            </if>
        </where>
        ORDER BY create_time DESC
    </select>

    <!--FROM b2c_order gb-->
    <!--LEFT JOIN b2c_order gbp ON gb.parent_id = gbp.id-->
    <select id="queryOrderDetail" resultMap="resultMapOrder">
        SELECT
        <include refid="includeOrder" />
        FROM b2c_order bo
        WHERE id = #{order_id}
    </select>

    <insert id="insertOrder" parameterType="com.backendsys.entity.B2c.B2cOrderDTO"
        useGeneratedKeys="true" keyProperty="order_id">
        INSERT INTO b2c_order (order_code, order_source, final_total_price, pay_method, member_open_id
        <if test="pay_time != null and pay_time != ''">, pay_time</if>
        <if test="pay_status != null and pay_status != ''">, pay_status</if>
        <if test="prepay_id != null and prepay_id != ''">, prepay_id</if>
        <if test="pay_refund_time != null and pay_refund_time != ''">, pay_refund_time</if>
        <if test="pay_refund_status != null and pay_refund_status != ''">, pay_refund_status</if>
        <if test="cancel_order_way != null and cancel_order_way != ''">, cancel_order_way</if>
        <if test="cancel_order_reason != null and cancel_order_reason != ''">, cancel_order_reason</if>
        <if test="status != null and status != ''">, status</if>
        <if test="is_readed != null and is_readed != ''">, is_readed</if>
        <if test="notes != null and notes != ''">, notes</if>
        )
        VALUES (#{order_code}, #{order_source}, #{final_total_price}, #{pay_method}, #{member_open_id}
        <if test="pay_time != null and pay_time != ''">, #{pay_time}</if>
        <if test="pay_status != null and pay_status != ''">, #{pay_status}</if>
        <if test="prepay_id != null and prepay_id != ''">, #{prepay_id}</if>
        <if test="pay_refund_time != null and pay_refund_time != ''">, #{pay_refund_time}</if>
        <if test="pay_refund_status != null and pay_refund_status != ''">, #{pay_refund_status}</if>
        <if test="cancel_order_way != null and cancel_order_way != ''">, #{cancel_order_way}</if>
        <if test="cancel_order_reason != null and cancel_order_reason != ''">, #{cancel_order_reason}</if>
        <if test="status != null and status != ''">, #{status}</if>
        <if test="is_readed != null and is_readed != ''">, #{is_readed}</if>
        <if test="notes != null and notes != ''">, #{notes}</if>
        )
    </insert>

    <update id="updateOrder" parameterType="com.backendsys.entity.B2c.B2cOrderDTO"
            useGeneratedKeys="true" keyProperty="order_id">
        UPDATE b2c_order
        SET
        <if test="final_total_price != null and final_total_price != ''">, final_total_price = #{final_total_price}</if>
        <if test="pay_method != null and pay_method != ''">, pay_method = #{pay_method}</if>
        <if test="pay_time != null and pay_time != ''">, pay_time = #{pay_time}</if>
        <if test="pay_status != null and pay_status != ''">, pay_status = #{pay_status}</if>
        <if test="prepay_id != null and prepay_id != ''">, prepay_id = #{prepay_id}</if>
        <if test="pay_refund_time != null and pay_refund_time != ''">, pay_refund_time = #{pay_refund_time}</if>
        <if test="pay_refund_status != null and pay_refund_status != ''">, pay_refund_status = #{pay_refund_status}</if>
        <if test="cancel_order_way != null and cancel_order_way != ''">, cancel_order_way = #{cancel_order_way}</if>
        <if test="cancel_order_reason != null and cancel_order_reason != ''">, cancel_order_reason = #{cancel_order_reason}</if>
        <if test="status != null and status != ''">, status = #{status}</if>
        <if test="is_readed != null and is_readed != ''">, is_readed = #{is_readed}</if>
        <if test="notes != null and notes != ''">, notes = #{notes}</if>
        WHERE id = #{order_id}
    </update>

</mapper>