-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($sp699450, $sp517398, $spa0789d, $sp0f5dbe, $sp107491, $sp736e02) { DB::statement('call add_cards(?,?,?,?,?,?)', array($sp699450, $sp517398, $spa0789d, $sp0f5dbe, $sp107491, (int) $sp736e02)); } public static function _trash($spcfdf85) { DB::transaction(function () use($spcfdf85) { $spe137eb = clone $spcfdf85; $spe137eb->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp06abef) { foreach ($sp06abef as $sp66d858) { $sp222f58 = \App\Product::where('id', $sp66d858->product_id)->lockForUpdate()->first(); if ($sp222f58) { $sp222f58->count_all -= $sp66d858->count_left; $sp222f58->saveOrFail(); } } }); $spcfdf85->delete(); return true; }); } public static function _restore($spcfdf85) { DB::transaction(function () use($spcfdf85) { $spe137eb = clone $spcfdf85; $spe137eb->selectRaw('`product_id`,SUM(`count_all`-`count_sold`) as `count_left`')->groupBy('product_id')->orderByRaw('`product_id`')->chunk(100, function ($sp06abef) { foreach ($sp06abef as $sp66d858) { $sp222f58 = \App\Product::where('id', $sp66d858->product_id)->lockForUpdate()->first(); if ($sp222f58) { $sp222f58->count_all += $sp66d858->count_left; $sp222f58->saveOrFail(); } } }); $spcfdf85->restore(); return true; }); } }
0 commit comments