<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
	use HasDateTimeFormatter;
    

    protected $table = 'order_items';

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

    public $timestamps = false;

    public static $STATUS_FORMAT=[
        "已删除"=>-1,
        "正常"=>1,
    ];


    public static function getOrderItems($order_id=0){
        return self::where("order_id",$order_id)->get()->toArray();

    }


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

    //修改订单
    public static function updateOrderItem($where,$update){
        return self::where($where)->update($update);
    }

    public static function getItemsInfo($rec_id=[]){
        return self::whereIn("rec_id",$rec_id)->get()->toArray();
    }

    public static function getOrderTotalAmount($order_id){
        $info =  self::where("order_id",$order_id)->where("status",self::$STATUS_FORMAT["正常"])->selectRaw("sum(goods_price*goods_number) as total_amount")->first();
        return $info ? decimal_number_format($info->total_amount) : 0;
    }

    public static function getOrderItemids($order_id){
        return self::where("order_id",$order_id)->where("status",self::$STATUS_FORMAT["正常"])->pluck("rec_id")->toArray();
    }

}