FavoriteAddForm.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. /*
  8. * @Author: your name
  9. * @Date: 2021-05-12 10:51:33
  10. * @LastEditTime: 2021-05-19 14:51:01
  11. * @LastEditors: Please set LastEditors
  12. * @Description: In User Settings Edit
  13. * @FilePath: \admin_php\modules\client\models\v1\FavoriteAddForm.php
  14. */
  15. namespace app\modules\alliance\models;
  16. use app\models\Favorite;
  17. use yii\base\Model;
  18. class FavoriteAddForm extends Model
  19. {
  20. public $store_id;
  21. public $user_id;
  22. public $saas_id;
  23. public $id;
  24. public $type;
  25. public function rules()
  26. {
  27. return [
  28. [['type'], 'default', 'value' => 0],
  29. ['type', 'in', 'range' => [Favorite::FAVORITE_GOODS, Favorite::FAVORITE_SHOP]],
  30. [['id'], 'default', 'value' => 0],
  31. //[['id'], 'required',],
  32. ];
  33. }
  34. public function save()
  35. {
  36. if (!$this->validate()) {
  37. return [
  38. 'code' => 1,
  39. 'msg' => $this->getErrorSummary(false)[0]
  40. ];
  41. }
  42. $existFavorite = Favorite::findOne([
  43. 'store_id' => $this->store_id,
  44. 'user_id' => 0,
  45. 'saas_id' => $this->saas_id,
  46. 'goods_id' => $this->id,
  47. 'type' => $this->type,
  48. ]);
  49. if ($existFavorite) {
  50. $existFavorite->is_delete = 0;
  51. $existFavorite->save();
  52. return [
  53. 'code' => 0,
  54. 'msg' => 'success',
  55. ];
  56. }
  57. $favorite = new Favorite();
  58. $favorite->store_id = $this->store_id;
  59. $favorite->user_id = 0;
  60. $favorite->saas_id = $this->saas_id;
  61. $favorite->goods_id = $this->id;
  62. $favorite->type = $this->type;
  63. $favorite->created_at = time();
  64. if ($favorite->save()) {
  65. return [
  66. 'code' => 0,
  67. 'msg' => 'success',
  68. ];
  69. } else {
  70. return [
  71. 'code' => 1,
  72. 'msg' => 'fail',
  73. ];
  74. }
  75. }
  76. }