<?php
namespace app\model;

use support\Log;
use think\facade\Db;
use support\Redis;

class Orders extends base{
    
    //orderStatus  1,   未付款  2 已取消  3 ,  待使用   4,   已使用  5,   已退款
    const OrderStatus = [1=>'未付款',2=>'已取消',3=>'待使用',4=>'已使用',5=>'已退款'];
    const KuaishouStatus = [ 1=>'已取消', 2=> '待支付' , 3=> '订单确认中',4 => '待使用', 5=>'已完成', 6 => '已过期', 7=>'待分享'];

    const DouyinReservationStatus = [ 1=>'未预约', 2=> '待接单' , 3=> '已预约', 4 => '已取消', 5=>'已完成', 6 => '取消/退款申请'];
    const DouyinStatus = [ 1=>'未核销', 2=> '已核销' , 3=> '申请退款中', 4 => '已退款', 5=>'部分核销'];

    const StatusName = ['待跟进','跟进中','已核销','核销失败','放弃跟单'];
    const OSS = [1 => '美团', 2=> '快手', 3=> '抖音'];

    const timeType = ['create_time' => '添加记录时间', 'update_time' => '修改记录时间', 'last_follow' => '最后跟进时间', 'next_follow' => '下次跟进时间', 'travel_date' => '出行时间', 'create_at' => '下单时间'];

    protected $json = ['personnel'];

    public function getOrderStatusNameAttr($val) {
        if ($this->os == 1)
            return self::OrderStatus[$this->order_status] ?? '未知';
        elseif($this->os == 3)
            return self::DouyinStatus[$this->order_status] ?? '未知';
        else
            return self::KuaishouStatus[$this->order_status] ?? '未知';
    }

    public function getStatusNameAttr($val) {
        return self::StatusName[$this->status] ?? '未知';
    }

    public function getOsNameAttr($val) {
        return self::OSS[$this->os] ?? '未知';
    }

    public function getAdminNameAttr() {
        if($this->admin_id > 0) {
            return $this->admin->getData('name');
        }else{
            $aid = Redis::get('Travel:Order:'.$this->id);
            if($aid) {
                $a = Admins::where('id', $aid)->find();
                return $a->getData('name').'（电话中...）';
            }
        }
        return '';
    }

    public static function attimes($type, $times){
        $query = self::where([]);
        if(in_array($type, array_keys(self::timeType))) {
            if(is_string($times)) {
                $times = explode(',', $times);
            }else{
                if(empty($times[0])) $times[0] = '';
                if(empty($times[1])) $times[1] = '';
            }
        }

        switch ($type) {
            case 'create_time':  //添加记录时间
                $query->whereBetween('create_time', [strtotime($times[0]), strtotime($times[1])]);
                Log::warning("==Orders====:",['a'=>strtotime($times[0]),'b'=>strtotime($times[1])]);
                break;

            case 'update_time':  //修改记录时间
                $query->whereBetween('update_time', [strtotime($times[0]), strtotime($times[1])]);
                break;
            
            case 'last_follow':  //最后跟进时间
                $query->whereBetween('last_follow', [strtotime($times[0])*1000, strtotime($times[1])*1000+999]);
                break;
            
            case 'next_follow':  //下次跟进时间
                $query->whereBetween('next_follow', [strtotime($times[0])*1000, strtotime($times[1])*1000+999]);
                break;

            case 'travel_date':  //出行时间
                $query->whereBetween('travel_date', [date('Y-m-d H:i:s',strtotime($times[0])), date('Y-m-d H:i:s',strtotime($times[1]))]);
                break;
            
            case 'create_at':  //下单时间
                $query->whereBetween('create_at', [strtotime($times[0])*1000, strtotime($times[1])*1000+999]);
                break;
            
            default:
                # code...
                break;
        }
        return $query;
    }

    public function admin() {
        return $this->belongsTo(Admins::class, 'admin_id')->visible(['name','username','avatar']);
    }

    public function anchor() {
        return $this->belongsTo(Admins::class, 'zhubo')->visible(['name','username','avatar']);
    }

    public function backs() {
        return $this->hasOne(Backs::class, 'order_id')->where('status',0)->visible(['status','admin_id']);
    }

    public function follow() {
        return $this->hasMany(Follows::class, 'order_id')->order('id','desc');
    }

    public function finance() {
        return $this->hasMany(Finances::class, 'order_id')->order('id','desc');
    }

    public static function fish($id, $admin_id) {
        return Db::transaction(function () use($id, $admin_id) {
            $order = Orders::where('id', $id)->where('admin_id', 0)->lock(true)->find();
            if(empty($order)) return false;
            $order->admin_id = $admin_id;
            $order->give_time = time();
            $order->save();
            Logs::todo($id, $admin_id, 6);
            return true;
        });
    }

}