<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Widgets\Dropdown;
use Illuminate\Database\Eloquent\Model;

class Order extends BaseModel
{
	use HasDateTimeFormatter;
    

    protected $table = 'order';

    protected $primaryKey = 'order_id';

    protected $guarded = ['order_id'];  //设置字段黑名单

    public $timestamps = false;

    public static $STATUS_WAIT_AUDIT = 1;//待审核
    public static $STATUS_WAIT_PAY = 2;//待付款


    public static $STATUS_FORAMT=[
        "已取消"=>-1,
        "审核不通过"=>-2,
        "待审核"=>1,
        "待付款"=>2,
        "待付尾款"=>3,
        "待发货"=>4,
        "部分发货"=>7,
        "待收货"=>8,
        "交易成功"=>10,
    ];

    public static $ORDER_USER_PAY_TYPE=[
        "Paypal"=>1,
        "TT"=>2,
        ];

    //查看权限
    public static $ruleViewList = [
        "sem_order_viewAllList", //查看所有
        "sem_order_viewSubList",//查看下级
    ];


    public function scmUser()
    {
        return $this->belongsTo(ScmUser::class, 'user_id', 'id');
    }

    public function order_items()
    {
        return $this->hasMany(OrderItem::class, 'order_id', 'order_id');
    }

    public function order_price()
    {
        return $this->hasMany(OrderPrice::class, 'order_id', 'order_id');
    }

    public function order_address()
    {
        return $this->hasMany(OrderAddress::class, 'order_id', 'order_id');
    }


    //审核订单
    public static function auditOrder($orderIds=[]){
        return self::whereIn("order_id",$orderIds)->where("status",self::$STATUS_WAIT_AUDIT)->update(["status"=>self::$STATUS_WAIT_PAY]);
    }

    //反审核订单
    public static function reverseAuditOrder($orderIds=[]){
        return self::whereIn("order_id",$orderIds)->where("status",self::$STATUS_FORAMT["待付款"])->update(["status"=>self::$STATUS_FORAMT["待审核"]]);
    }


    //获取订单数据
    public static function getOrderList($orderId=0){
        if(empty($orderId)){
            return [];
        }
        $info =  self::where("order_id",$orderId)->whereHas("order_items",function ($q){
            $q->where("status",1);
        })->with("order_items")->with("order_price")->with("scmUser")->first();
        return $info ? $info->toArray() : [];
    }

    public static function getOne($orderId=0){
        $orderInfo = self::where("order_id",$orderId)->first();
        return  $orderInfo ? $orderInfo->toArray() : [];
    }

    public static function getOneAndRelationList($orderId=0){
        $orderInfo = self::where("order_id",$orderId)->with("scmUser")->with("order_price")->with("order_items")->with("order_address")->first();
        return  $orderInfo ? $orderInfo->toArray() : [];
    }

    //取消
    public static function canelOrder($orderId=0){
        return self::where("order_id",$orderId)->update([
            "status"=>self::$STATUS_FORAMT["已取消"],
        ]);
    }

    //修改订单
    public static function updateOrder($where,$update){
        $update["update_time"] = time();
        return self::where($where)->update($update);
    }

    //修改订单的销售
    public static function updateOrderSale($userids,$sale_id,$sale_name){
        return self::whereIn("user_id",$userids)
            ->whereRaw("status >= 1  and status < 10")
            ->update([
            "sale_id"=>$sale_id,
            "sale_name"=>$sale_name,
            "update_time"=>time(),
        ]);
    }

    //统计用户的下单数
    public static function getUserOrderStatistical($userIds,$type){
        $query = self::whereIn("user_id",$userIds)->selectRaw("user_id,count(order_id) as nums");
        if($type == "1"){
            $query->whereRaw("status >= 1 and status <= 8");
        }elseif($type == "2"){
            $query->whereRaw("status >= 1");
        }
        return $query->groupBy("user_id")->get()->toArray();
    }




}