where('order_sn', $order_sn)->select(); // 启动事务 Db::startTrans(); try { foreach ($list as $item) { $goods = $item->goods; $sku = $item->sku; if ($goods) { $goods->setInc('sales', $item->nums); } if ($sku) { $sku->setInc('sales', $item->nums); } } // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } return true; } //销量减 public static function setGoodsSalesDec($order_sn) { $list = (new self)->where('order_sn', $order_sn)->select(); // 启动事务 Db::startTrans(); try { foreach ($list as $item) { $goods = $item->goods; $sku = $item->sku; if ($goods) { $goods->setDec('sales', $item->nums); } if ($sku) { $sku->setDec('sales', $item->nums); } } // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } return true; } //库存增 public static function setGoodsStocksInc($order_sn) { $list = (new self)->where('order_sn', $order_sn)->select(); // 启动事务 Db::startTrans(); try { foreach ($list as $item) { $goods = $item->goods; $sku = $item->sku; if ($sku) { $sku->setInc('stocks', $item->nums); } if ($goods) { $goods->setInc('stocks', $item->nums); } } // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } return true; } //库存减 public static function setGoodsStocksDec($order_sn) { $list = (new self)->where('order_sn', $order_sn)->select(); // 启动事务 Db::startTrans(); try { foreach ($list as $item) { $goods = $item->goods; $sku = $item->sku; if ($sku) { $sku->setDec('stocks', $item->nums); } if ($goods) { $goods->setDec('stocks', $item->nums); } } // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); } return true; } public function getGoodsSkuAttrAttr($value, $data) { return json_decode($value, true); } public function goods() { return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(1); } public function Sku() { return $this->belongsTo('Sku', 'goods_sku_id', 'id', [], 'LEFT'); } public function Order() { return $this->hasOne('Order', 'order_sn', 'order_sn', [], 'LEFT'); } }