-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($spc2138c, $spcaeba2, $sp797c94, $sp6002b5, $sp5aa60c, $sp864dfc) { DB::statement('call add_cards(?,?,?,?,?,?)', array($spc2138c, $spcaeba2, $sp797c94, $sp6002b5, $sp5aa60c, (int) $sp864dfc)); } public static function _trash($sp8e2ceb) { DB::transaction(function () use($sp8e2ceb) { $spb1c766 = clone $sp8e2ceb; $spb1c766->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($spdcf051) { foreach ($spdcf051 as $spf40cf3) { $sp863814 = \App\Product::where('id', $spf40cf3->product_id)->lockForUpdate()->first(); if ($sp863814) { $sp863814->count_all -= $spf40cf3->count_left; $sp863814->saveOrFail(); } } }); $sp8e2ceb->delete(); return true; }); } public static function _restore($sp8e2ceb) { DB::transaction(function () use($sp8e2ceb) { $spb1c766 = clone $sp8e2ceb; $spb1c766->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($spdcf051) { foreach ($spdcf051 as $spf40cf3) { $sp863814 = \App\Product::where('id', $spf40cf3->product_id)->lockForUpdate()->first(); if ($sp863814) { $sp863814->count_all += $spf40cf3->count_left; $sp863814->saveOrFail(); } } }); $sp8e2ceb->restore(); return true; }); } }
0 commit comments