You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.3 KiB

  1. <?php
  2. namespace api\logic;
  3. use backend\modules\goods\models\ars\Goods;
  4. use backend\modules\shop\models\ars\Collection;
  5. use Yii;
  6. use Throwable;
  7. use yii\base\Component;
  8. use yii\web\BadRequestHttpException;
  9. use yii\web\NotFoundHttpException;
  10. use yii\web\ServerErrorHttpException;
  11. /**
  12. * @author iron
  13. * @email weiriron@gmail.com
  14. * Class CartLogic
  15. * @package api\logic
  16. */
  17. class CollectionLogic extends Component
  18. {
  19. /**
  20. * @throws BadRequestHttpException
  21. * @throws NotFoundHttpException
  22. * @throws ServerErrorHttpException
  23. * @throws Throwable
  24. * @throws yii\db\StaleObjectException
  25. */
  26. public function delete()
  27. {
  28. $ids = Yii::$app->request->getBodyParam('ids');
  29. if (empty($ids) || !is_array($ids)) {
  30. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  31. }
  32. foreach ($ids as $id) {
  33. $collection = $this->findCollection($id);
  34. if (!$collection) {
  35. throw new NotFoundHttpException("未找到该收藏");
  36. }
  37. if (!$collection->delete()) {
  38. throw new ServerErrorHttpException('服务器取消收藏失败');
  39. }
  40. }
  41. Yii::$app->getResponse()->setStatusCode(204);
  42. }
  43. /**
  44. * @return bool
  45. * @throws ServerErrorHttpException
  46. * @throws Throwable
  47. * @throws yii\db\StaleObjectException
  48. */
  49. public function update()
  50. {
  51. $collection = $this->findCollectionByGoods();
  52. if (!$collection) {
  53. return $this->create();
  54. }
  55. if (!$collection->delete()) {
  56. throw new ServerErrorHttpException('服务器取消收藏失败');
  57. }
  58. return true;
  59. }
  60. /*------------------------------------------------------------------------------------------*/
  61. /**
  62. * @throws NotFoundHttpException
  63. * @throws ServerErrorHttpException
  64. */
  65. private function create()
  66. {
  67. $goods = $this->findGoods();
  68. $collection = new Collection();
  69. $collection->goods_id = $goods->id;
  70. $collection->goods_name = $goods->name;
  71. $collection->goods_img = $goods->image;
  72. $collection->goods_price = $goods->price;
  73. $collection->user_id = Yii::$app->user->getId();
  74. if (!$collection->save()) {
  75. throw new ServerErrorHttpException('服务器新建收藏失败');
  76. }
  77. return true;
  78. }
  79. /**
  80. * @param integer $id
  81. * @return array|Collection|null
  82. */
  83. private function findCollection($id)
  84. {
  85. return Collection::find()
  86. ->where(['id' => $id])
  87. ->andWhere(['user_id' => Yii::$app->user->getId()])
  88. ->one();
  89. }
  90. private function findCollectionByGoods()
  91. {
  92. $goodsId = Yii::$app->request->getQueryParam('id');
  93. return Collection::find()
  94. ->where(['goods_id' => $goodsId])
  95. ->andWhere(['user_id' => Yii::$app->user->getId()])
  96. ->one();
  97. }
  98. /**
  99. * @return Goods|null
  100. * @throws NotFoundHttpException
  101. */
  102. private function findGoods()
  103. {
  104. $goodsId = Yii::$app->request->getQueryParam('id');
  105. $goods = Goods::findOne(['id' => $goodsId, 'is_delete' => Goods::IS_DELETE_NO, 'is_sale' => Goods::IS_SALE_YES]);
  106. if (!$goods) {
  107. throw new NotFoundHttpException('商品未找到');
  108. }
  109. return $goods;
  110. }
  111. }