-namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\DB; class Card extends Model { protected $guarded = array(); use SoftDeletes; protected $dates = array('deleted_at'); const STATUS_NORMAL = 0; const STATUS_SOLD = 1; const STATUS_USED = 2; const TYPE_ONETIME = 0; const TYPE_REPEAT = 1; function orders() { return $this->hasMany(Order::class); } function product() { return $this->belongsTo(Product::class); } function getCountAttribute() { return $this->count_all - $this->count_sold; } public static function add_cards($sp3546ff, $sp2f9632, $spb2a0bb, $sp671ce9, $sp114fae, $sp4b22b4) { DB::statement('call add_cards(?,?,?,?,?,?)', array($sp3546ff, $sp2f9632, $spb2a0bb, $sp671ce9, $sp114fae, (int) $sp4b22b4)); } public static function _trash($sp90af04) { DB::transaction(function () use($sp90af04) { $sp31ddba = clone $sp90af04; $sp31ddba->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp4f3a9b) { foreach ($sp4f3a9b as $sp9f8d97) { $spd39704 = \App\Product::where('id', $sp9f8d97->product_id)->lockForUpdate()->first(); if ($spd39704) { $spd39704->count_all -= $sp9f8d97->count_left; $spd39704->saveOrFail(); } } }); $sp90af04->delete(); return true; }); } public static function _restore($sp90af04) { DB::transaction(function () use($sp90af04) { $sp31ddba = clone $sp90af04; $sp31ddba->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp4f3a9b) { foreach ($sp4f3a9b as $sp9f8d97) { $spd39704 = \App\Product::where('id', $sp9f8d97->product_id)->lockForUpdate()->first(); if ($spd39704) { $spd39704->count_all += $sp9f8d97->count_left; $spd39704->saveOrFail(); } } }); $sp90af04->restore(); return true; }); } }
0 commit comments