| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace addons\WechatVideoShop\common\service;
- use addons\WechatVideoShop\common\enums\WechatVideoShopGoodsUpdateStatusEnums;
- use addons\WechatVideoShop\common\models\WechatVideoShopGoods;
- class GoodsCallbackService
- {
- /**
- * 商品审核
- * @param array $event
- * @return void
- * @throws \Exception
- */
- public function auditResult(array $event)
- {
- $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
- if (empty($audit)) throw new \Exception("商品不存在");
- $audit->audit_status = $event['ProductSpuAudit']['status'];
- $audit->audit_reason = $event['ProductSpuAudit']['reason'] ?? '';
- if (!$audit->save()) {
- throw new \Exception($audit->getErrorMessage());
- }
- }
- /**
- * 上下架结果
- * @param array $event
- * @return void
- * @throws \Exception
- */
- public function listingResult(array $event)
- {
- $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
- if (empty($audit)) throw new \Exception("商品不存在");
- $audit->listing_status = $event['ProductSpuAudit']['status'];
- $audit->listing_reason = $event['ProductSpuAudit']['reason'] ?? '';
- if (!$audit->save()) {
- throw new \Exception($audit->getErrorMessage());
- }
- }
- /**
- * @param array $event
- * @return void
- * @throws \Exception
- */
- public function updateResult(array $event)
- {
- switch ($event['ProductSpuAudit']['status']) {
- case WechatVideoShopGoodsUpdateStatusEnums::GOODS_UPDATE_STATUS_CHANGE: // 更新
- $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
- if (empty($audit)) throw new \Exception("商品不存在");
- break;
- case WechatVideoShopGoodsUpdateStatusEnums::GOODS_UPDATE_STATUS_CREATE: // 新增
- break;
- case WechatVideoShopGoodsUpdateStatusEnums::GOODS_UPDATE_STATUS_DELETE: // 删除
- $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
- if (empty($audit)) throw new \Exception("商品不存在");
- break;
- }
- }
- /**
- * 通过productid获取本地商品
- * @param $product_id
- * @return mixed|null
- */
- protected function findByProductId($product_id)
- {
- return WechatVideoShopGoods::getOne([
- ['product_id' => $product_id]
- ]);
- }
- }
|