-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($spa27b73, $sp52cac1, $sp4ad441, $sp415e3d, $spa504de, $sp24ed58) { DB::statement('call add_cards(?,?,?,?,?,?)', array($spa27b73, $sp52cac1, $sp4ad441, $sp415e3d, $spa504de, (int) $sp24ed58)); } public static function _trash($spd10097) { DB::transaction(function () use($spd10097) { $sp6161c4 = clone $spd10097; $sp6161c4->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp389a95) { foreach ($sp389a95 as $sp29413f) { $sp4aad76 = \App\Product::where('id', $sp29413f->product_id)->lockForUpdate()->first(); if ($sp4aad76) { $sp4aad76->count_all -= $sp29413f->count_left; $sp4aad76->saveOrFail(); } } }); $spd10097->delete(); return true; }); } public static function _restore($spd10097) { DB::transaction(function () use($spd10097) { $sp6161c4 = clone $spd10097; $sp6161c4->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp389a95) { foreach ($sp389a95 as $sp29413f) { $sp4aad76 = \App\Product::where('id', $sp29413f->product_id)->lockForUpdate()->first(); if ($sp4aad76) { $sp4aad76->count_all += $sp29413f->count_left; $sp4aad76->saveOrFail(); } } }); $spd10097->restore(); return true; }); } }
0 commit comments