<?php

namespace App\Imports;

use App\Models\S2Customer;
use App\Models\S2CustomerVisitRecord;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Log;

class CustomerVisitImport implements \Maatwebsite\Excel\Concerns\ToArray
{

    /**
     * @inheritDoc
     */
    public function array(array $array)
    {
        $no_user_list = [];
        foreach($array as $key => $arr) {
            continue;
            if ($key == 0) continue;
            if (!$arr[0]) continue;
            $mobile = $arr[7];
            Log::info($mobile);
            $enMobile = aesEncryptCBC($mobile);
            $customer_name = $arr[5];
            $status = 3;
            if($arr[9] == "跟进中") {
                $status = 2;
            }
            $remark = $arr[10];
            $province = $arr[2];
            $city = $arr[3];
            $dist = $arr[4];
            $date = $arr[1];
            $principal = $arr[6];
            if ($date) {
                $date = $this->excelTime($date);
            }
            $user = User::where(function($sql) use($mobile,$enMobile){
                $sql->where("mobile", $mobile)->orWhere("mobile", $enMobile);
            })->whereIn("source", [User::SOURCE_H5, User::SOURCE_WORK, User::SOURCE_WORK])->first();
            if (empty($user)) {
                $no_user_list[] = $arr[6]."-".$mobile."-".$enMobile;
                continue;
            }

            if (!$customer_name) continue;
            $customer = S2Customer::where("user_id", $user->id)->where("customer_name", $customer_name)->first();
            if (!$customer) {
                $customer = S2Customer::create([
                    "user_id"=>$user->id,
                    "customer_name"=>$customer_name,
                    "customer_type"=>201,
                    "status"=>$status,
                    "remark"=>$remark,
                    "province"=>$province,
                    "city"=>$city,
                    "dist"=>$dist,
                    "principal"=>$principal,
                    "contacts_name"=>"",
                    "contacts_mobile"=>"",
                    "intention"=>0,
                ]);
            }
            $visit = S2CustomerVisitRecord::where("customer_id", $customer->id)->where("user_id", $user->id)->where("visit_time", $date)->first();
            if ($visit) continue;
            S2CustomerVisitRecord::create([
                "customer_id"=>$customer->id,
                "user_id"=>$user->id,
                "visit_type"=>1,
                "visit_time"=>$date,
                "visit_address"=>$province.$city.$dist,
                "visit_result"=>$remark?:"",
                "visit_theme"=>""
            ]);

        }

    }

    public function excelTime($excelTime, $time = false)
    {
        if (!empty($excelTime)) {
            // $excelTime = $excelTime;
            $timestamp = (($excelTime - 25569) * 86400);
            $formatDate = date("Y-m-d", $timestamp);
            if ($time) {
                $formatDate = date("Y-m-d H:i:s", $timestamp);
            }
            return $formatDate;
        }
        return $excelTime;

    }
}