validate()) { return $this->getModelError(); } if (is_array($this->goods_id)) { foreach ($this->goods_id as $goods_id) { $t = \Yii::$app->db->beginTransaction(); $model = Goods::findOne([ 'plugin_sign' => $this->plugin_sign, 'id' => $goods_id, 'is_delete' => 0, 'status' => Goods::STATUS_OFFLINE, 'cx_mch_id' => $this->cx_mch_id ]); if ($model == null) continue; $model->is_delete = 1; //删除之后 $this->doDeleteAfter($model->id, $model->plugin_sign, $model->cx_mch_id); if (!$model->save()) { $t->rollBack(); return $this->getModelError($model); } $t->commit(); } } else { $t = \Yii::$app->db->beginTransaction(); $model = Goods::findOne([ 'plugin_sign' => $this->plugin_sign, 'id' => $this->goods_id, 'is_delete' => 0, 'status' => Goods::STATUS_OFFLINE, 'cx_mch_id' => $this->cx_mch_id ]); if ($model == null) { return $this->apiReturnError('商品不存在或已经删除'); } $model->is_delete = 1; //删除之后 $this->doDeleteAfter($model->id, $model->plugin_sign, $model->cx_mch_id); if (!$model->save()) { $t->rollBack(); return $this->getModelError($model); } $t->commit(); } return $this->apiReturnSuccess("删除成功"); } public function online() { if (!$this->validate()) { return $this->getModelError(); } if (is_array($this->goods_id)) { foreach ($this->goods_id as $goods_id) { $t = \Yii::$app->db->beginTransaction(); $model = Goods::findOne([ // 'plugin_sign' => $this->plugin_sign, 'id' => $goods_id, 'is_delete' => 0, 'status' => Goods::STATUS_OFFLINE, 'cx_mch_id' => $this->cx_mch_id ]); if ($model == null) continue; $model->status = Goods::STATUS_ONLINE; //检查商品库存是否充足 // $res = $this->checkGoodsStock($model->id, $model->cx_mch_id); // if ($res['code'] != 0) { // $t->rollBack(); // return $res; // } if (!$model->save()) { $t->rollBack(); return $this->getModelError($model); } $t->commit(); } } else { $t = \Yii::$app->db->beginTransaction(); $model = Goods::findOne([ // 'plugin_sign' => $this->plugin_sign, 'id' => $this->goods_id, 'is_delete' => 0, 'status' => Goods::STATUS_OFFLINE, 'cx_mch_id' => $this->cx_mch_id ]); if ($model == null) { return $this->apiReturnError('商品不存在或已经上架'); } $model->status = Goods::STATUS_ONLINE; //检查商品库存是否充足 // $res = $this->checkGoodsStock($model->id, $model->cx_mch_id); // if ($res['code'] != 0) { // $t->rollBack(); // return $res; // } if (!$model->save()) { $t->rollBack(); return $this->getModelError($model); } $t->commit(); } return $this->apiReturnSuccess("操作成功"); } public function offline() { if (!$this->validate()) { return $this->getModelError(); } if (is_array($this->goods_id)) { foreach ($this->goods_id as $goods_id) { $t = \Yii::$app->db->beginTransaction(); $model = Goods::findOne([ // 'plugin_sign' => $this->plugin_sign, 'id' => $goods_id, 'is_delete' => 0, 'status' => Goods::STATUS_ONLINE, 'cx_mch_id' => $this->cx_mch_id ]); if ($model == null) continue; $model->status = Goods::STATUS_OFFLINE; if (!$model->save()) { $t->rollBack(); return $this->getModelError($model); } $t->commit(); } } else { $t = \Yii::$app->db->beginTransaction(); $model = Goods::findOne([ // 'plugin_sign' => $this->plugin_sign, 'id' => $this->goods_id, 'is_delete' => 0, 'status' => Goods::STATUS_ONLINE, 'cx_mch_id' => $this->cx_mch_id ]); if ($model == null) { return $this->apiReturnError('商品不存在或已经下架'); } $model->status = Goods::STATUS_OFFLINE; if (!$model->save()) { $t->rollBack(); return $this->getModelError($model); } $t->commit(); } return $this->apiReturnSuccess("操作成功"); } //商品删除之后处理其他 private function doDeleteAfter($goods_id, $plugin_sign, $cx_mch_id) { if ($plugin_sign == SysConst::$cxPluginSceneIntegralMall) { //积分商品 IntegralMallGoods::updateAll(['is_delete' => 1], [ 'is_delete' => 0, 'goods_id' => $goods_id, 'cx_mch_id' => $cx_mch_id ]); } } //检查商品库存是否充足 private function checkGoodsStock($goods_id, $cx_mch_id) { $goods = Goods::findOne(['id' => $goods_id, 'is_delete' => 0, 'cx_mch_id' => $cx_mch_id]); if ($goods == null) { return $this->apiReturnError('商品不存在或已经删除'); } if ($goods->use_attr != 1) { if ($goods->goods_stock == 0) { return $this->apiReturnError('商品库存不足'); } } else { $stock = GoodsAttr::find() ->where([ 'goods_id' => $goods_id, 'cx_mch_id' => $cx_mch_id, 'is_delete' => 0, ]) ->sum('stock'); if ($stock == 0) { return $this->apiReturnError('商品库存不足'); } } return $this->apiReturnSuccess(); } }