GoodsCallbackService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace addons\WechatVideoShop\common\service;
  3. use addons\WechatVideoShop\common\enums\WechatVideoShopGoodsUpdateStatusEnums;
  4. use addons\WechatVideoShop\common\models\WechatVideoShopGoods;
  5. class GoodsCallbackService
  6. {
  7. /**
  8. * 商品审核
  9. * @param array $event
  10. * @return void
  11. * @throws \Exception
  12. */
  13. public function auditResult(array $event)
  14. {
  15. $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
  16. if (empty($audit)) throw new \Exception("商品不存在");
  17. $audit->audit_status = $event['ProductSpuAudit']['status'];
  18. $audit->audit_reason = $event['ProductSpuAudit']['reason'] ?? '';
  19. if (!$audit->save()) {
  20. throw new \Exception($audit->getErrorMessage());
  21. }
  22. }
  23. /**
  24. * 上下架结果
  25. * @param array $event
  26. * @return void
  27. * @throws \Exception
  28. */
  29. public function listingResult(array $event)
  30. {
  31. $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
  32. if (empty($audit)) throw new \Exception("商品不存在");
  33. $audit->listing_status = $event['ProductSpuAudit']['status'];
  34. $audit->listing_reason = $event['ProductSpuAudit']['reason'] ?? '';
  35. if (!$audit->save()) {
  36. throw new \Exception($audit->getErrorMessage());
  37. }
  38. }
  39. /**
  40. * @param array $event
  41. * @return void
  42. * @throws \Exception
  43. */
  44. public function updateResult(array $event)
  45. {
  46. switch ($event['ProductSpuAudit']['status']) {
  47. case WechatVideoShopGoodsUpdateStatusEnums::GOODS_UPDATE_STATUS_CHANGE: // 更新
  48. $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
  49. if (empty($audit)) throw new \Exception("商品不存在");
  50. break;
  51. case WechatVideoShopGoodsUpdateStatusEnums::GOODS_UPDATE_STATUS_CREATE: // 新增
  52. break;
  53. case WechatVideoShopGoodsUpdateStatusEnums::GOODS_UPDATE_STATUS_DELETE: // 删除
  54. $audit = $this->findByProductId($event['ProductSpuAudit']['product_id']);
  55. if (empty($audit)) throw new \Exception("商品不存在");
  56. break;
  57. }
  58. }
  59. /**
  60. * 通过productid获取本地商品
  61. * @param $product_id
  62. * @return mixed|null
  63. */
  64. protected function findByProductId($product_id)
  65. {
  66. return WechatVideoShopGoods::getOne([
  67. ['product_id' => $product_id]
  68. ]);
  69. }
  70. }