<?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.api.dao.ApiOrderMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.OrderVo" id="orderMap">
        <result property="id" column="id"/>
        <result property="order_sn" column="order_sn"/>
        <result property="user_id" column="user_id"/>
        <result property="order_status" column="order_status"/>
        <result property="shipping_status" column="shipping_status"/>
        <result property="pay_status" column="pay_status"/>
        <result property="consignee" column="consignee"/>
        <result property="country" column="country"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property="district" column="district"/>
        <result property="address" column="address"/>
        <result property="address_id" column="address_id"/>
        <result property="mobile" column="mobile"/>
        <result property="postscript" column="postscript"/>
        <result property="shipping_id" column="shipping_id"/>
        <result property="shipping_name" column="shipping_name"/>
        <result property="shipping_mobile" column="shipping_mobile"/>
        <result property="shipping_fee" column="shipping_fee"/>
        <result property="shipping_no" column="shipping_no"/>
        <result property="shipping_code" column="shipping_code"/>
        <result property="pay_id" column="pay_id"/>
        <result property="pay_name" column="pay_name"/>
        <result property="actual_price" column="actual_price"/>
        <result property="integral" column="integral"/>
        <result property="integral_money" column="integral_money"/>
        <result property="order_price" column="order_price"/>
        <result property="goods_price" column="goods_price"/>
        <result property="add_time" column="add_time"/>
        <result property="confirm_time" column="confirm_time"/>
        <result property="pay_time" column="pay_time"/>
        <result property="freight_price" column="freight_price"/>
        <result property="coupon_id" column="coupon_id"/>
        <result property="coupon_name" column="coupon_name"/>
        <result property="coupon_price" column="coupon_price"/>
        <result property="full_cut_price" column="full_cut_price"/>
        <result property="order_type" column="order_type"/>
        <result property="activity_id" column="activity_id"/>
        <result property="store_id" column="store_id"/>
        <result property="delivery_date" column="delivery_date"/>
        <result property="delivery_remark" column="delivery_remark"/>
        <result property="predict_time" column="predict_time"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
        <result property="comment_count" column="comment_count"/>

        <result column="merch_sn" property="merchSn" jdbcType="VARCHAR" />
        <result column="order_biz_type" property="orderBizType" jdbcType="CHAR" />
        <result column="pay_transaction_id" property="payTransactionId" jdbcType="VARCHAR" />
        <result column="pay_mobile" property="payMobile" jdbcType="VARCHAR" />
        <result column="buyer_pay_check" property="buyerPayCheck" jdbcType="VARCHAR" />
        <result column="pay_flag" property="payFlag" jdbcType="VARCHAR" />
        <result column="creater_sn" property="createrSn" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="moder_sn" property="moderSn" jdbcType="VARCHAR" />
        <result column="mod_time" property="modTime" jdbcType="TIMESTAMP" />
        <result column="tstm" property="tstm" jdbcType="TIMESTAMP" />
        <result column="merch_order_sn" property="merchOrderSn" jdbcType="VARCHAR" />
        <result column="is_scan" property="isScan" jdbcType="VARCHAR" />
        <result column="store_name" property="storeName" jdbcType="VARCHAR" />
        <result property="isRefundStatus" column="isRefundStatus"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="approvalRemark" column="approval_remark"/>
        <result property="isMergePay" column="is_merge_pay"/>
        <result property="isOnfflineOrder" column="is_onffline_order"/>
    </resultMap>

    <select id="queryObject" resultMap="orderMap">
		select a.*,b.latitude,b.longitude
		from mall_order a
		left join mall_address b on a.address_id = b.id
		where a.id = #{value}
	</select>

    <select id="queryObjectByOrderSn" resultMap="orderMap">
        select a.*,b.latitude,b.longitude
        from mall_order a
        left join mall_address b on a.address_id = b.id
        where a.order_sn = #{order_sn}
    </select>

    <select id="queryList" resultMap="orderMap">
        select a.*,b.latitude,b.longitude,s.store_name,f.refund_status,f.approval_remark
        from mall_order a
        left join mall_address b on a.address_id = b.id
        left join mall_store s on a.store_id = s.id
        left join mall_order_refund f on a.id = f.order_id
        <where>
            <if test="user_id != null">
                and a.user_id = #{user_id}
            </if>
            <if test="pay_status != null">
                and a.pay_status = #{pay_status}
            </if>
            <if test="shipping_status != null">
                and a.shipping_status = #{shipping_status}
            </if>
            <if test="order_status != null and order_status != 0">
                and a.order_status = #{order_status}
            </if>
            <if test="order_status == 0 ">
                and a.order_status in (0,100)
            </if>
            <if test="evaluate_status != null and evaluate_status != 0 ">
                and a.comment_count <![CDATA[ >= ]]>  1
            </if>
            <if test="evaluate_status != null and evaluate_status == 0 ">
                and a.comment_count <![CDATA[ < ]]>  1
            </if>
            <if test="startDate!=null  and startDate!=''">
                AND a.add_time >= #{startDate}
            </if>
            <if test="endDate!=null  and endDate!=''">
                AND a.add_time  <![CDATA[ < ]]>   #{endDate}
            </if>
            <if test="shippingId!=null  and shippingId!=''">
                AND a.shipping_id = #{shippingId}
            </if>
        </where>

        <if test="order_status != 401">
            <choose>
                <when test="sidx != null and sidx.trim() != ''">
                    order by ${sidx} ${order}
                </when>
                <otherwise>
                    order by a.id desc
                </otherwise>
            </choose>
        </if>
        <if test="order_status == 401">
            order by f.refund_time desc
        </if>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(id) from mall_order a
        <where>
            <if test="user_id != null">
                and a.user_id = #{user_id}
            </if>
            <if test="pay_status != null">
                and a.pay_status = #{pay_status}
            </if>
            <if test="shipping_status != null">
                and a.shipping_status = #{shipping_status}
            </if>
            <if test="evaluate_status != null and evaluate_status != 0 ">
                and a.comment_count <![CDATA[ >= ]]>  1
            </if>
            <if test="evaluate_status != null and evaluate_status == 0 ">
                and a.comment_count <![CDATA[ < ]]>  1
            </if>
            <if test="startDate!=null  and startDate!=''">
                AND a.add_time >= #{startDate}
            </if>
            <if test="endDate!=null  and endDate!=''">
                AND a.add_time  <![CDATA[ < ]]>   #{endDate}
            </if>
            <if test="shippingId!=null  and shippingId!=''">
                AND a.shipping_id = #{shippingId}
            </if>
            <if test="order_status != null">
                and a.order_status = #{order_status}
            </if>
        </where>
    </select>

    <insert id="save" parameterType="com.kmall.api.entity.OrderVo" useGeneratedKeys="true" keyProperty="id">
		insert into mall_order
		(
			`order_sn`,
			`user_id`,
			`order_status`,
			`shipping_status`,
			`shipping_code`,
			`pay_status`,
			`consignee`,
			`country`,
			`province`,
			`city`,
			`district`,
			`address`,
			`address_id`,
			`mobile`,
			`postscript`,
			`pay_id`,
			`pay_name`,
			`shipping_fee`,
			`actual_price`,
			`integral`,
			`integral_money`,
			`order_price`,
			`goods_price`,
			`add_time`,
			`confirm_time`,
			`pay_time`,
			`freight_price`,
			`coupon_id`,
			`coupon_price`,
			`full_cut_price`,
			`order_type`,
			`activity_id`,
			`store_id`,
			`delivery_date`,
			`delivery_remark`,
			`predict_time`,
			`coupon_name`,
			`comment_count`,

        <if test="merchSn != null" >
            merch_sn,
        </if>
        <if test="merchOrderSn != null" >
            merch_order_sn,
        </if>
        <if test="isScan != null" >
            is_scan,
        </if>
        <if test="isMergePay != null" >
            is_merge_pay,
        </if>
        <if test="orderBizType != null" >
            order_biz_type,
        </if>
        <if test="payTransactionId != null" >
            pay_transaction_id,
        </if>
        <if test="payMobile != null" >
            pay_mobile,
        </if>
        <if test="buyerPayCheck != null" >
            buyer_pay_check,
        </if>
        <if test="isOnfflineOrder != null" >
            `is_onffline_order`,
        </if>
        <if test="payFlag != null" >
            pay_flag,
        </if>
        <if test="createrSn != null" >
            creater_sn,
        </if>
        <if test="createTime != null" >
            create_time,
        </if>
        <if test="moderSn != null" >
            moder_sn,
        </if>
        <if test="modTime != null" >
            mod_time
        </if>
		)
		values
		(
			#{order_sn},
			#{user_id},
			#{order_status},
			#{shipping_status},
			#{shipping_code},
			#{pay_status},
			#{consignee},
			#{country},
			#{province},
			#{city},
			#{district},
			#{address},
			#{address_id},
			#{mobile},
			#{postscript},
			#{pay_id},
			#{pay_name},
			#{shipping_fee},
			#{actual_price},
			#{integral},
			#{integral_money},
			#{order_price},
			#{goods_price},
			now(),
			#{confirm_time},
			#{pay_time},
			#{freight_price},
			#{coupon_id},
			#{coupon_price},
			#{full_cut_price},
			#{order_type},
			#{activity_id},
			#{store_id},
			#{delivery_date},
			#{delivery_remark},
			#{predict_time},
			#{coupon_name},
			#{comment_count},
        <if test="merchSn != null" >
            #{merchSn,jdbcType=VARCHAR},
        </if>
        <if test="merchOrderSn != null" >
            #{merchOrderSn,jdbcType=VARCHAR},
        </if>
        <if test="isScan != null" >
            #{isScan,jdbcType=VARCHAR},
        </if>
        <if test="isMergePay != null" >
            #{isMergePay},
        </if>
        <if test="orderBizType != null" >
            #{orderBizType,jdbcType=CHAR},
        </if>
        <if test="payTransactionId != null" >
            #{payTransactionId,jdbcType=VARCHAR},
        </if>
        <if test="payMobile != null" >
            #{payMobile,jdbcType=VARCHAR},
        </if>
        <if test="buyerPayCheck != null" >
            #{buyerPayCheck,jdbcType=VARCHAR},
        </if>
        <if test="isOnfflineOrder != null" >
            #{isOnfflineOrder,jdbcType=VARCHAR},
        </if>
        <if test="payFlag != null" >
            #{payFlag,jdbcType=VARCHAR},
        </if>
        <if test="createrSn != null" >
            #{createrSn,jdbcType=VARCHAR},
        </if>
        <if test="createTime != null" >
            #{createTime,jdbcType=TIMESTAMP},
        </if>
        <if test="moderSn != null" >
            #{moderSn,jdbcType=VARCHAR},
        </if>
        <if test="modTime != null" >
            #{modTime,jdbcType=TIMESTAMP}
        </if>
		)
	</insert>

    <select id="queryObjectByIdList" resultMap="orderMap">
        select a.*
        from mall_order a
        where a.id in
        <foreach collection="orderIdList" item="orderId" open="(" close=")" separator=",">
            #{orderId}
        </foreach>
    </select>

    <insert id="saveBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        insert into mall_order
        (
        `order_sn`,
        `user_id`,
        `order_status`,
        `shipping_status`,
        `shipping_code`,
        `pay_status`,
        `consignee`,
        `country`,
        `province`,
        `city`,
        `district`,
        `address`,
        `address_id`,
        `mobile`,
        `postscript`,
        `pay_id`,
        `pay_name`,
        `shipping_fee`,
        `actual_price`,
        `integral`,
        `integral_money`,
        `order_price`,
        `goods_price`,
        `add_time`,
        `confirm_time`,
        `pay_time`,
        `freight_price`,
        `coupon_id`,
        `coupon_price`,
        `full_cut_price`,
        `order_type`,
        `activity_id`,
        `store_id`,
        `delivery_date`,
        `delivery_remark`,
        `predict_time`,
        `coupon_name`,
        `comment_count`,
         merch_sn,
         merch_order_sn,
         is_scan,
         is_merge_pay,
         order_biz_type,
        `is_onffline_order`,
         pay_transaction_id,
         pay_mobile,
         create_time,
         mod_time
        )
        values
        <foreach collection="list" index="index" item="orderInfo" separator=",">
            (
            #{orderInfo.order_sn},
            #{orderInfo.user_id},
            #{orderInfo.order_status},
            #{orderInfo.shipping_status},
            #{orderInfo.shipping_code},
            #{orderInfo.pay_status},
            #{orderInfo.consignee},
            #{orderInfo.country},
            #{orderInfo.province},
            #{orderInfo.city},
            #{orderInfo.district},
            #{orderInfo.address},
            #{orderInfo.address_id},
            #{orderInfo.mobile},
            #{orderInfo.postscript},
            #{orderInfo.pay_id},
            #{orderInfo.pay_name},
            #{orderInfo.shipping_fee},
            #{orderInfo.actual_price},
            #{orderInfo.integral},
            #{orderInfo.integral_money},
            #{orderInfo.order_price},
            #{orderInfo.goods_price},
            now(),
            #{orderInfo.confirm_time},
            #{orderInfo.pay_time},
            #{orderInfo.freight_price},
            #{orderInfo.coupon_id},
            #{orderInfo.coupon_price},
            #{orderInfo.full_cut_price},
            #{orderInfo.order_type},
            #{orderInfo.activity_id},
            #{orderInfo.store_id},
            #{orderInfo.delivery_date},
            #{orderInfo.delivery_remark},
            #{orderInfo.predict_time},
            #{orderInfo.coupon_name},
            #{orderInfo.comment_count},
            #{orderInfo.merchSn,jdbcType=VARCHAR},
            #{orderInfo.merchOrderSn,jdbcType=VARCHAR},
            #{orderInfo.isScan,jdbcType=VARCHAR},
            #{orderInfo.isMergePay},
            #{orderInfo.orderBizType,jdbcType=CHAR},
            #{orderInfo.isOnfflineOrder},
            #{orderInfo.payTransactionId,jdbcType=VARCHAR},
            #{orderInfo.payMobile,jdbcType=VARCHAR},
            #{orderInfo.createTime,jdbcType=TIMESTAMP},
            #{orderInfo.modTime,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>
    <update id="update" parameterType="com.kmall.api.entity.OrderVo">
        update mall_order
        <set>
            <if test="order_sn != null">`order_sn` = #{order_sn},</if>
            <if test="user_id != null">`user_id` = #{user_id},</if>
            <if test="order_status != null">`order_status` = #{order_status},</if>
            <if test="shipping_status != null">`shipping_status` = #{shipping_status},</if>
            <if test="shipping_code != null">`shipping_code` = #{shipping_code},</if>
            <if test="pay_status != null">`pay_status` = #{pay_status},</if>
            <if test="consignee != null">`consignee` = #{consignee},</if>
            <if test="country != null">`country` = #{country},</if>
            <if test="province != null">`province` = #{province},</if>
            <if test="city != null">`city` = #{city},</if>
            <if test="district != null">`district` = #{district},</if>
            <if test="address != null">`address` = #{address},</if>
            <if test="address_id != null">`address_id` = #{address_id},</if>
            <if test="mobile != null">`mobile` = #{mobile},</if>
            <if test="postscript != null">`postscript` = #{postscript},</if>
            <if test="shipping_id != null">`shipping_id` = #{shipping_id},</if>
            <if test="shipping_name != null">`shipping_name` = #{shipping_name},</if>
            <if test="shipping_fee != null">`shipping_fee` = #{shipping_fee},</if>
            <if test="shipping_no != null">`shipping_no` = #{shipping_no},</if>
            <if test="pay_id != null">`pay_id` = #{pay_id},</if>
            <if test="pay_name != null">`pay_name` = #{pay_name},</if>
            <if test="pay_time != null">`pay_time` = #{pay_time},</if>
            <if test="actual_price != null">`actual_price` = #{actual_price},</if>
            <if test="integral != null">`integral` = #{integral},</if>
            <if test="integral_money != null">`integral_money` = #{integral_money},</if>
            <if test="order_price != null">`order_price` = #{order_price},</if>
            <if test="goods_price != null">`goods_price` = #{goods_price},</if>
            <if test="confirm_time != null">`confirm_time` = #{confirm_time},</if>
            <if test="freight_price != null">`freight_price` = #{freight_price},</if>
            <if test="coupon_id != null">`coupon_id` = #{coupon_id},</if>
            <if test="coupon_price != null">`coupon_price` = #{coupon_price},</if>
            <if test="full_cut_price != null">`full_cut_price` = #{full_cut_price},</if>
            <if test="order_type != null">`order_type` = #{order_type},</if>
            <if test="activity_id != null">`activity_id` = #{activity_id},</if>
            <if test="store_id != null">`store_id` = #{store_id},</if>
            <if test="shipping_mobile != null">`shipping_mobile` = #{shipping_mobile},</if>
            <if test="delivery_date != null">`delivery_date` = #{delivery_date},</if>
            <if test="delivery_remark != null">`delivery_remark` = #{delivery_remark},</if>
            <if test="predict_time != null">`predict_time` = #{predict_time},</if>
            <if test="coupon_name != null">`coupon_name` = #{coupon_name},</if>
            <if test="comment_count != null">`comment_count` = #{comment_count},</if>

            <if test="merchSn != null" >
                merch_sn = #{merchSn,jdbcType=VARCHAR},
            </if>
            <if test="merchOrderSn != null" >
                merch_order_sn = #{merchOrderSn,jdbcType=VARCHAR},
            </if>
            <if test="isScan != null" >
                is_scan = #{isScan,jdbcType=VARCHAR},
            </if>
            <if test="isMergePay != null" >
                is_merge_pay = #{isMergePay},
            </if>
            <if test="orderBizType != null" >
                order_biz_type = #{orderBizType,jdbcType=CHAR},
            </if>
            <if test="payTransactionId != null" >
                pay_transaction_id = #{payTransactionId,jdbcType=VARCHAR},
            </if>
            <if test="payMobile != null" >
                pay_mobile = #{payMobile,jdbcType=VARCHAR},
            </if>
            <if test="buyerPayCheck != null" >
                buyer_pay_check = #{buyerPayCheck,jdbcType=VARCHAR},
            </if>
            <if test="payFlag != null" >
                pay_flag = #{payFlag,jdbcType=VARCHAR},
            </if>
            <if test="createrSn != null" >
                creater_sn = #{createrSn,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null" >
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="moderSn != null" >
                moder_sn = #{moderSn,jdbcType=VARCHAR},
            </if>
            <if test="modTime != null" >
                mod_time = #{modTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id}
    </update>

    <update id="riderOrderUpdate" parameterType="com.kmall.api.entity.OrderVo">
        update mall_order
        <set>
            `order_status` = #{order_status},
            `shipping_status` = #{shipping_status},
            `shipping_code` = #{shipping_code},
            `shipping_id` = #{shipping_id},
            `shipping_name` = #{shipping_name},
            `shipping_fee` = #{shipping_fee},
            `shipping_no` = #{shipping_no},
            `shipping_mobile` = #{shipping_mobile}
        </set>
        where id = #{id}
    </update>

    <update id="updateOrderByMerchOrderSn" parameterType="com.kmall.api.entity.OrderVo">
        update mall_order
        <set>
            `is_merge_pay` = #{isMergePay}
        </set>
        where merch_order_sn = #{merchOrderSn}
    </update>

    <select id="queryUcenterMap" resultType="map">
        select count(a.id) as orders, sum(case a.order_status when 0 then 1 else 0 end ) as unPayNum, sum(case a.order_status when 300 then 1 else 0 end ) as unTakeNum
        ,sum(case a.order_status when 201 then 1 else 0 end ) as unPaymentNum,
        sum(case a.order_status when 301 then 1 else 0 end and case a.comment_count when 0 then 1 else 0 end ) as unEvalNum
        from mall_order a
        left join mall_comment b on a.id = b.order_id
        where 1 = 1
        <if test="user_id != null">
            and a.user_id = #{user_id}
        </if>
        <if test="pay_status != null">
            and a.pay_status = #{pay_status}
        </if>
        <if test="shipping_status != null">
            and a.shipping_status = #{shipping_status}
        </if>
        <if test="evaluate_status != null and evaluate_status == 0 ">
            and not exists (select 1 from mall_comment m where a.id = m.order_id )
        </if>
        <if test="startDate!=null  and startDate!=''">
            AND a.add_time >= #{startDate}
        </if>
        <if test="endDate!=null  and endDate!=''">
            AND a.add_time  <![CDATA[ < ]]>   #{endDate}
        </if>
        <if test="shippingId!=null  and shippingId!=''">
            AND a.shipping_id  <![CDATA[ < ]]>   #{shippingId}
        </if>
    </select>

    <select id="queryObjectByMerchOrderSn" resultMap="orderMap">
        select a.*
        from mall_order a
        where a.merch_order_sn = #{merchOrderSn}
    </select>

    <select id="queryCountByMerchOrderSn" resultType="java.lang.Integer">
        select count(*)
        from mall_order a
        where a.merch_order_sn = #{merchOrderSn}
    </select>

    <select id="queryOrderByMerchOrderSnAndRefundStatus" resultMap="orderMap">
        select * from mall_order o inner join mall_order_refund r on r.order_id = o.id where o.merch_order_sn = #{merchOrderSn}
         and r.refund_status = 2
    </select>
</mapper>