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.

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