<?php

namespace App\Imports;

use App\Facades\CommonService;
use App\Models\ActivityIntroduceLog;
use App\Models\ActivityOrder;
use App\Models\ActivityShareRefund;
use App\Models\RecommendUser;
use App\Models\User;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToCollection;

class ActivityOrdersImport implements ToCollection
{
    /**
    * @param Collection $collection
    */
    public function collection(Collection $collection)
    {
//        foreach ($collection as $order)
//        {
//            if ($order[0] == "订单ID") continue;
//            if ($order[8] == "友福伙伴") continue;
//            $balance = $order[10];
//            $share_refund = $order[11];
//            $admin_refund = $order[12];
//            $sign_value = $order[13];
//            $name = $order[2];
//            $mobile = $order[5];
//            $from_name = $order[6];
//            $activity_order = ActivityOrder::where("activity_id",17)->where("mobile", $mobile)->first();
//            //生成扣款记录
//            if ($balance != 2200) {
//                if ($share_refund) {
//                    Log::info($order);
//                    $target_order_id = $this->introduceOrder($name, $collection);
//                    $order_refund = ActivityShareRefund::where([
//                        'user_id'=>$activity_order->user_id,
//                        'order_id'=>$activity_order->id,
//                        'type'=>ActivityShareRefund::SHARETYPE
//                    ])->first();
//                    if (empty($order_refund)) {
//                        ActivityShareRefund::create([
//                            'user_id'=>$activity_order->user_id,
//                            'order_id'=>$activity_order->id,
//                            'target_order_id'=>$target_order_id,
//                            'trade_no'=>$activity_order->trade_no,
//                            'refund_trade_no'=>CommonService::getTradeNO(),
//                            'total_amount'=>$activity_order->amount,
//                            'amount'=>$share_refund,
//                            'status'=>1,
//                            'is_hook'=>1,
//                            'sub_mch_id'=>null,
//                            'type'=>ActivityShareRefund::SHARETYPE,
//                            'admin_id'=>0,
//                        ]);
//                        ActivityIntroduceLog::create(['order_id'=>$activity_order->id, 'amount'=>$share_refund, 'introduce_order_id'=>$activity_order->id, 'type'=>ActivityIntroduceLog::REFUNDTYPE]);
//                        $activity_order->decrement('residue_amount', $share_refund);
//                    }
//
//                }
//            }
//            if ($admin_refund) {
//                $order_refund = ActivityShareRefund::where([
//                    'user_id'=>$activity_order->user_id,
//                    'order_id'=>$activity_order->id,
//                    'type'=>ActivityShareRefund::ADMINTYPE
//                ])->first();
//                if (empty($order_refund)) {
//                    ActivityShareRefund::create([
//                        'user_id'=>$activity_order->user_id,
//                        'order_id'=>$activity_order->id,
//                        'target_order_id'=>$activity_order->id,
//                        'trade_no'=>$activity_order->trade_no,
//                        'refund_trade_no'=>CommonService::getTradeNO(),
//                        'total_amount'=>$activity_order->amount,
//                        'amount'=>$admin_refund,
//                        'status'=>1,
//                        'is_hook'=>1,
//                        'sub_mch_id'=>null,
//                        'type'=>ActivityShareRefund::ADMINTYPE,
//                        'admin_id'=>0,
//                    ]);
//                    ActivityIntroduceLog::create(['order_id'=>$activity_order->id, 'amount'=>$admin_refund, 'introduce_order_id'=>$activity_order->id, 'type'=>ActivityIntroduceLog::REFUNDTYPE]);
//                    $activity_order->decrement('residue_amount', $admin_refund);
//                }
//            }
//
//            if ($sign_value) {
//                $log = ActivityIntroduceLog::where(['order_id'=>$activity_order->id, 'amount'=>$sign_value, 'introduce_order_id'=>$activity_order->id, 'type'=>ActivityIntroduceLog::SIGNINTYPE])->first();
//                if (empty($log)) {
//                    ActivityIntroduceLog::create(['order_id'=>$activity_order->id, 'amount'=>$sign_value, 'introduce_order_id'=>$activity_order->id, 'type'=>ActivityIntroduceLog::SIGNINTYPE]);
//                    $activity_order->decrement('residue_amount', $sign_value);
//                }
//            }
//        }
        $this->addIntroduce($collection);
    }
    public function addIntroduce($orders)
    {
        foreach ($orders as $order) {
            if ($order[0] == "订单ID") continue;
            if ($order[8] == "友福伙伴") continue;
            $balance = $order[10];
            $share_refund = $order[11];
            $admin_refund = $order[12];
            $sign_value = $order[13];
            $name = $order[2];
            $mobile = $order[5];
            $from_name = $order[6];
            //活动订单
            $activity_order = ActivityOrder::where("activity_id", 17)->where("mobile", $mobile)->first();
            if($activity_order) {
                $from_order = $this->introduceOrderV2($from_name, $orders);
                if ($from_order) {
                    ActivityOrder::where("id", $activity_order->id)->update(['introduce_user_id'=>$from_order->user_id]);
                    $user = User::find($activity_order->user_id);
                    if ($user->recommendUserLog && $user->recommendUserLog->recommend_user_id != $from_order->user_id) {
                        Log::info("推荐人不统一  用户id:{$user->id}  记录推荐人：{$user->recommendUserLog->recommend_user_id}  线下推荐人：{$from_order->user_id}");
                    }
                    RecommendUser::updateOrCreate(['user_id'=>$user->id],[ 'recommend_user_id'=>$from_order->user_id]);
                }
            }
        }
    }


    public function introduceOrder($from_name, $orders) {
//        $mobile = '';
//        foreach ($orders as $order) {
//            if ($order[6] == $from_name) {
//                $mobile = $order[5];
//            }
//        }
//        if ($mobile) {
//            $target_order = ActivityOrder::where("activity_id",17)->where("mobile", $mobile)->first();
//            if ($target_order) return $target_order->id;
            //友福伙伴的 绑定陈彬订单
            $target_order = ActivityOrder::find(779);
            return $target_order->id;
//        }
//        return null;
    }

    public function introduceOrderV2($from_name, $orders)
    {
        $mobile = '';
        foreach ($orders as $order) {
            if ($order[2] == $from_name) {
                $mobile = $order[3];
            }
        }
        if ($mobile) {
            $target_order = ActivityOrder::where("activity_id",17)->where("mobile", $mobile)->first();
            if ($target_order) return $target_order;
//        //友福伙伴的 绑定陈彬订单
//        $target_order = ActivityOrder::find(779);
//        return $target_order->id;
        }
        return null;
    }
}
