-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($sp6fe8b9, $sp17d280, $spf46b4f, $sp59ae99, $spad1f18, $sp3afcf8) { DB::statement('call add_cards(?,?,?,?,?,?)', array($sp6fe8b9, $sp17d280, $spf46b4f, $sp59ae99, $spad1f18, (int) $sp3afcf8)); } public static function _trash($sp6c3275) { DB::transaction(function () use($sp6c3275) { $sp52c4e7 = clone $sp6c3275; $sp52c4e7->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp1150d0) { foreach ($sp1150d0 as $sp06865f) { $sp0a72f9 = \App\Product::where('id', $sp06865f->product_id)->lockForUpdate()->first(); if ($sp0a72f9) { $sp0a72f9->count_all -= $sp06865f->count_left; $sp0a72f9->saveOrFail(); } } }); $sp6c3275->delete(); return true; }); } public static function _restore($sp6c3275) { DB::transaction(function () use($sp6c3275) { $sp52c4e7 = clone $sp6c3275; $sp52c4e7->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp1150d0) { foreach ($sp1150d0 as $sp06865f) { $sp0a72f9 = \App\Product::where('id', $sp06865f->product_id)->lockForUpdate()->first(); if ($sp0a72f9) { $sp0a72f9->count_all += $sp06865f->count_left; $sp0a72f9->saveOrFail(); } } }); $sp6c3275->restore(); return true; }); } }
0 commit comments