<?php

namespace App\Modules\Trade\Pay;

use App\Modules\Book\Services\BookConfigService;
use App\Modules\Subscribe\Models\Order;
use DB;

/**
 * 
 */
class OrderPaySuccess
{
    /**
     * 支付成功回调处理订单
     * @param string $trade_no 订单号
     * @param string $transaction_id 微信商户号
     * @return bool
     */
    public static function handle(string $trade_no, string $transaction_id)
    {
        $order = Order::where('trade_no', $trade_no)->first();
        //订单跨天
        $is_change = self::orderAcrossDay($order);
        $order->transaction_id = $transaction_id;
        if ($order) {
            if ($order->status == 'PAID') {
                myLog('pay_notify')->info('has_pay:' . $trade_no);
                return true;
            }
            if ($order->order_type == 'YEAR') {
                $app = new YearOrderPaySuccess($order);
            } elseif ($order->order_type == 'BOOK') {
                $app = new BookOrderPaySuccess($order);
            } elseif ($order->order_type == 'QUARTER') {
                $app = new QuarterOrderPaySuccess($order);
            } elseif ($order->order_type == 'RECHARGE') {
                $app = new RechargeOrderPaySuccess($order);
            }
            $status =  $app->success();
            if ($is_change) {
                DB::table('orders')->where('id', $order->id)->update([
                    'pay_end_at' => date('Y-m-d H:i:s', time() + 5),
                    'updated_at' => date('Y-m-d H:i:s', time() + 5),
                    'created_at' => date('Y-m-d H:i:s')
                ]);
            }
            self::freeBookStats($order->from_bid,$order->price,$order->uid);
            return $status;
        } else {
            return false;
        }
    }


    private static function freeBookStats($bid,$price,$uid){
        if(!$bid) return ;
        $free_book = BookConfigService::getByBidNoFilter($bid);
        if(!$free_book) return ;
        if(strtotime($free_book->end_time)+7*86400 < time())   return ;
        BookConfigService::chargeStats($free_book->id,$price,$uid);
    }

    private static function orderAcrossDay($order_info)
    {
        if (date('Y-m-d', strtotime($order_info->created_at)) == date('Y-m-d')) return false;
        $created_at = date('Y-m-d H:i:s', strtotime($order_info->created_at));
        $trade_no = $order_info->trade_no;
        $init_order = [
            'distribution_channel_id' => $order_info->distribution_channel_id,
            'uid' => $order_info->uid,
            'product_id' => $order_info->product_id,
            'price' => $order_info->price,
            'pay_type' => $order_info->pay_type,
            'trade_no' => 'cd-' . $trade_no,
            'pay_merchant_source' => $order_info->pay_merchant_source,
            'pay_merchant_id' => $order_info->pay_merchant_id,
            'create_ip' => $order_info->create_ip,
            'send_order_id' => $order_info->send_order_id,
            'send_order_name' => $order_info->send_order_name,
            'order_type' => $order_info->order_type,
            'from_bid' => $order_info->from_bid,
            'from_type' => $order_info->from_type,
            'activity_id' => $order_info->activity_id,
            'inner_send_order_id' => $order_info->inner_send_order_id,
            'status' => 'UNPAID',
            'transaction_id' => '',
            'pay_end_at' => '0000-00-00 00:00:00',
            'created_at' => $created_at,
            'updated_at' => $created_at
        ];
        try {
            DB::table('orders')->insert($init_order);
        } catch (\Exception $e) {
            return false;
        }
        return true;
    }

}
