<?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.schedule.dao.QzOrderMapper">
    <select id="queryrderUnEvaluateList" resultType="map">
       SELECT
            a.id AS order_id,
            a.order_sn,
            c.goods_name,
	        a.form_id,
	        u.weixin_openid AS open_id
        FROM
            mall_order a
        LEFT JOIN mall_comment b ON a.id = b.order_id
        LEFT JOIN mall_order_goods c ON c.order_id = a.id
        LEFT JOIN mall_user u ON a.user_id = u.id
        WHERE
            b.id IS NULL
        AND a.confirm_time > date_add(now(), INTERVAL - 1 DAY)
        AND a.confirm_time IS NOT NULL
    </select>

    <select id="queryPastOrderList" resultType="map">
        SELECT b.product_id,b.number,c.stock_num,c.id as storeRelaId
        FROM mall_order a
        LEFT JOIN mall_order_goods b ON a.id = b.order_id
        LEFT JOIN mall_product_store_rela c ON b.product_id = c.product_id
        AND c.store_id = a.store_id
        WHERE 1 = 1
        AND a.add_time <![CDATA[ < ]]> DATE_ADD(now(),INTERVAL -15 MINUTE)
        AND a.order_status in (0,100) and a.order_type = 1 AND a.is_onffline_order = 0
    </select>
    <update id="unpayPastUpdate">
        UPDATE mall_order a
        SET order_status=101,shipping_status=0,pay_status=0
        WHERE 1 = 1
        AND a.add_time <![CDATA[ <  ]]> DATE_ADD(now(),INTERVAL -15 MINUTE)
        AND a.order_status = 0  and a.order_type = 1  AND a.is_onffline_order = 0
    </update>

    <update id="updateStockNum" parameterType="map">
        update mall_product_store_rela a
        <set>
            <if test="stock_num != null">a.`stock_num` = #{stock_num},</if>
        </set>
        where a.id = #{id}
    </update>


    <select id="queryRefundOrderList" resultType="map">
        select a.pay_transaction_id,a.merch_order_sn,a.order_sn,a.order_status,a.pay_status,f.out_refund_no,f.refund_id,f.order_id,f.id 'orderRefundId',a.is_onffline_order,
        a.order_sn_wx,a.pay_flag
        from mall_order a
        inner join mall_order_refund f on a.id = f.order_id	where (f.out_refund_no is null or f.refund_id is null
        or f.refund_time is null or f.wechat_refund_status_des is null) and a.order_status = '401' and f.refund_status
        in (2,5)
    </select>

    <select id="queryPayingOrderList" resultType="map">
		select a.pay_transaction_id,a.order_sn,a.order_status,a.pay_status,a.id 'order_id',a.merch_order_sn,date_format(a.add_time,'%Y-%m-%d %H:%i:%s') as add_time,
        b.product_id,b.number,c.stock_num,c.id as storeRelaId,a.pay_flag
        from mall_order a
        LEFT JOIN mall_order_goods b ON a.id = b.order_id
        LEFT JOIN mall_product_store_rela c ON b.product_id = c.product_id
        AND c.store_id = a.store_id  where (a.order_status in ('0','100','201') and a.pay_status in (0,1,2)) and pay_id is not null
        and (a.pay_transaction_id = '' or a.pay_transaction_id is null or a.pay_time is null or a.pay_time = '')
        AND a.is_onffline_order = 0 and a.pay_flag = 'weixin'
    </select>

    <select id="queryPinganPayingOrderList" resultType="map">
		select a.pay_transaction_id,a.order_sn,a.order_status,a.pay_status,a.id 'order_id',a.merch_order_sn,date_format(a.add_time,'%Y-%m-%d %H:%i:%s') as add_time,
        b.product_id,b.number,c.stock_num,c.id as storeRelaId,a.pay_flag
        from mall_order a
        LEFT JOIN mall_order_goods b ON a.id = b.order_id
        LEFT JOIN mall_product_store_rela c ON b.product_id = c.product_id
        AND c.store_id = a.store_id  where (a.order_status in ('0','100','201') and a.pay_status in (0,1,2)) and pay_id is not null
        and (a.pay_transaction_id = '' or a.pay_transaction_id is null or a.pay_time is null or a.pay_time = '')
        AND a.is_onffline_order = 0 and a.pay_flag = 'pingan'
    </select>

    <update id="updateOrderInfo" parameterType="map">
        UPDATE mall_order a
        <set>
            <if test="orderStatus != null">a.order_status = #{orderStatus},</if>
            <if test="payStatus != null">a.pay_status = #{payStatus},</if>
            <if test="payFlag != null">a.pay_flag = #{payFlag},</if>
            <if test="orderSnWx != null">a.order_sn_wx = #{orderSnWx},</if>
            <if test="payTransactionId != null">a.pay_transaction_id = #{payTransactionId},</if>
            <if test="payTime != null">a.pay_time = #{payTime},</if>

            <if test="totalFee != null">a.total_fee = #{totalFee},</if>
            <if test="feeType != null">a.fee_type = #{feeType},</if>
            <if test="cashFee != null">a.cash_fee = #{cashFee},</if>
            <if test="cashFeeType != null">a.cash_fee_type = #{cashFeeType},</if>
            <if test="rate != null">a.rate = #{rate},</if>
        </set>
        WHERE 1 = 1
        AND a.id = #{orderId}
    </update>

    <update id="updateOrderProcessRecord" parameterType="map">
        UPDATE mall_order_process_record a
        <set>
            <if test="payTime != null">a.pay_succ_time = #{payTime}</if>
        </set>
        WHERE 1 = 1
        AND a.order_sn = #{orderSn}
    </update>

    <update id="updateOrderRefund" parameterType="map">
        UPDATE mall_order_refund a
        <set>
            <if test="refundId != null">a.refund_id = #{refundId},</if>
            <if test="refundMoney != null">a.refund_money = #{refundMoney},</if>
            <if test="refundStatus != null">a.refund_status = #{refundStatus},</if>
            <if test="refundRecvAccout != null">a.refund_recv_accout = #{refundRecvAccout},</if>
            <if test="refundTime != null">a.refund_time = #{refundTime},</if>
            <if test="wechat_refund_status_des != null">a.wechat_refund_status_des = #{wechat_refund_status_des},</if>

            <if test="totalFee != null">a.total_fee = #{totalFee},</if>
            <if test="feeType != null">a.fee_type = #{feeType},</if>
            <if test="cashFee != null">a.cash_fee = #{cashFee},</if>
            <if test="cashFeeType != null">a.cash_fee_type = #{cashFeeType},</if>
            <if test="rate != null">a.rate = #{rate},</if>
        </set>
        WHERE 1 = 1
        AND a.out_refund_no = #{orderRefundId}
    </update>

    <select id="queryShipmentOrderList" resultType="map">
        SELECT a.order_sn,a.order_status,a.pay_status,a.id 'orderId',a.merch_order_sn,date_format(r.customs_succ_time,'%Y-%m-%d %H:%i:%s') as customs_succ_time
        FROM `mall_order` a inner join mall_order_process_record r on a.order_sn=r.order_sn where order_status='300'
        and shipping_status ='1' and a.order_biz_type in(10,02) AND r.customs_succ_time <![CDATA[ <  ]]>  DATE_ADD(now(),INTERVAL -7 DAY)  AND a.is_onffline_order = 0
    </select>
    <select id="queryShipmentBondedOrderList" resultType="map">
        SELECT a.order_sn,a.order_status,a.pay_status,a.id 'orderId',a.merch_order_sn,date_format(r.shipment_start_time,'%Y-%m-%d %H:%i:%s') as shipment_start_time
        FROM `mall_order` a inner join mall_order_process_record r on a.order_sn=r.order_sn where order_status='300' and shipping_status ='1'
        and a.order_biz_type in(00,11) AND r.shipment_start_time <![CDATA[ <  ]]>  DATE_ADD(now(),INTERVAL -7 DAY)  AND a.is_onffline_order = 0
    </select>

    <update id="updateShipmentOrder" parameterType="map">
        update mall_order set order_status ='301', shipping_status='2',confirm_time=now() where id=#{orderId}
    </update>

    <select id="queryReceiptOrderList" resultType="map">
        select o.user_id 'userId',o.id 'orderId',g.product_id'productId',g.goods_specification_name_value 'goodsSpecificationNameValue', g.goods_id 'goodsId',
        g.goods_name 'goodsName',confirm_time FROM `mall_order` o inner join mall_order_goods g on o.id = g.order_id
        where order_status='301' and shipping_status ='2' and comment_count=0 AND confirm_time <![CDATA[ < ]]> DATE_ADD(now(),INTERVAL -30 DAY)  AND o.is_onffline_order = 0
    </select>

    <update id="updateReceiptOrderByCommentCount">
        update mall_order set comment_count = 1 where
        order_status='301' and shipping_status ='2' and comment_count=0 AND confirm_time <![CDATA[ < ]]> DATE_ADD(now(),INTERVAL -30 DAY)
    </update>

    <insert id="insertComment" parameterType="map">
        insert into mall_comment(
        `type_id`,
        `value_id`,
        `user_id`,
        `order_id`,
        `product_id`,
        `goods_specification_name_value`,
        `status`,
        `content`,
        `eval_level`,
        `value_name`,
        `goods_level`,
        `create_time`,
        add_time,
        `mod_time`)
        values(
        #{typeId},
        #{valueId},
        #{userId},
        #{orderId},
        #{productId},
        #{goodsSpecificationNameValue},
        #{status},
        #{content},
        #{evalLevel},
        #{valueName},
        #{goodsLevel},
        #{createTime},
        #{addTime},
        #{modTime})
    </insert>

    <insert id="insertPinganResponseDto" parameterType="map">
        insert into pingan_response(
			`id`,
			`out_no`,
			`req_interface`,
			`errcode`,
			`msg`,
			`data`,
			`sign`,
			`datajson`,
			`timestamp`,
			`create_time`,
			`tstm`)
		values(
			#{id},
			#{outNo},
			#{reqInterface},
			#{errcode},
			#{msg},
			#{data},
			#{sign},
			#{datajson},
			#{timestamp},
			#{createTime},
			#{tstm})
    </insert>

    <select id="queryOrder" resultType="map">
        select id,order_status
        from mall_order
        WHERE order_sn = #{orderSn}
    </select>

    <select id="queryWXPaySuccessRecords" resultType="map">
        select id,out_trade_no_wx
        from mall_order_wxpay_record
        WHERE out_trade_no = #{orderSn} AND trade_state='SUCCESS'
    </select>

    <update id="updatePayOrderByOutNo" parameterType="map">
        update pingan_pay_order
        <set>
            <if test="pmtName != null">`pmt_name` = #{pmtName}, </if>
            <if test="pmtTag != null">`pmt_tag` = #{pmtTag}, </if>
            <if test="ordMctId != null">`ord_mct_id` = #{ordMctId}, </if>
            <if test="ordShopId != null">`ord_currency` = #{ordShopId}, </if>
            <if test="ordCurrency != null">`ord_shop_id` = #{ordCurrency}, </if>
            <if test="currencySign != null">`currency_sign` = #{currencySign}, </if>
            <if test="ordNo != null">`ord_no` = #{ordNo}, </if>
            <if test="ordType != null">`ord_type` = #{ordType}, </if>
            <if test="originalAmount != null">`original_amount` = #{originalAmount}, </if>
            <if test="discountAmount != null">`discount_amount` = #{discountAmount}, </if>
            <if test="ignoreAmount != null">`ignore_amount` = #{ignoreAmount}, </if>
            <if test="tradeAccount != null">`trade_account` = #{tradeAccount}, </if>
            <if test="tradeNo != null">`trade_no` = #{tradeNo}, </if>
            <if test="tradeAmount != null">`trade_amount` = #{tradeAmount}, </if>
            <if test="tradeQrcode != null">`trade_qrcode` = #{tradeQrcode}, </if>
            <if test="amount != null">`amount` = #{amount}, </if>
            <if test="tradeTime != null">`trade_time` = #{tradeTime}, </if>
            <if test="tradePayTime != null">`trade_pay_time` = #{tradePayTime}, </if>
            <if test="payTime != null">`pay_time` = #{payTime}, </if>
            <if test="status != null">`status` = #{status}, </if>
            <if test="tradeResult != null">`trade_result` = #{tradeResult}, </if>
            <if test="jsapiPayUrl != null">`jsapi_pay_url` = #{jsapiPayUrl}, </if>
            <if test="randStr != null">`rand_str` = #{randStr}, </if>
            <if test="appid != null">`appId` = #{appid}, </if>
            <if test="noncestr != null">`nonceStr` = #{noncestr}, </if>
            <if test="signtype != null">`signType` = #{signtype}, </if>
            <if test="prepayId != null">`prepay_id` = #{prepayId}, </if>
            <if test="paysign != null">`paySign` = #{paysign}, </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 `out_no` = #{outNo}
    </update>
</mapper>