<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DmaProcess extends BaseModel
{
    use HasFactory;
    protected $table = 'dma_process';

    public static function addUserProcess($order_id=0,$user_id=0,$step){
        $new_user_id = Order::query()->where("id",$order_id)->value("user_id");
        if(!empty($new_user_id)){
            $user_id = $new_user_id;
        }
        if($order_id > 0){
            $process = self::query()->where("order_id",$order_id)->where("user_id",$user_id)->first();
            $step_content = [];
            $step_content[$step] = 1;
            if(!empty($process)){
                $content = json_decode($process->content,true);
                $new_content = array_merge($content,$step_content);
                self::query()->where("order_id",$order_id)->where("user_id",$user_id)->update(['content'=>json_encode($new_content)]);
            }else{
                $map = [];
                $map["user_id"] = $user_id;
                $map["order_id"] = $order_id;
                $map["content"] = json_encode($step_content);
                self::query()->create($map);
            }
        }
    }
}
