GoodsCollect.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace common\models\goods;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\user\UserWallet;
  6. use PHPUnit\Util\Exception;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_goods_collect".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城id
  13. * @property int $goods_id 商品id
  14. * @property int $user_id 会员id
  15. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  16. * @property int $created_at
  17. * @property int $updated_at
  18. */
  19. class GoodsCollect extends BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%goods_collect}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'goods_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
  35. ];
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function attributeLabels()
  41. {
  42. return [
  43. 'id' => 'ID',
  44. 'mall_id' => '商城id',
  45. 'goods_id' => '商品id',
  46. 'user_id' => '会员id',
  47. 'status' => '状态[-1:删除;0:禁用;1启用]',
  48. 'created_at' => 'Created At',
  49. 'updated_at' => 'Updated At',
  50. ];
  51. }
  52. /**
  53. * @desc:保存数据
  54. * @Author: hua
  55. * @Date: 2021/11/9
  56. * @Time: 9:08
  57. * @Copyright: copyright (c) 2021 广东七件事集团
  58. * @param array $params
  59. * @return GoodsCollect|false
  60. */
  61. public static function setData(array $params)
  62. {
  63. try {
  64. if($params['type']==1){
  65. if (!empty($params['goods_id'])) {
  66. $model = self::findOne(['mall_id' => $params['mall_id'],'goods_id' => $params['goods_id'], 'user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  67. if (empty($model)){
  68. $model = new self();
  69. $model->loadDefaultValues();
  70. }else{
  71. if($model->status)throw new Exception('已收藏');
  72. }
  73. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  74. return $model;
  75. }
  76. }
  77. if($params['type']==0){
  78. if (!empty($params['goods_id'])) {
  79. $model = self::findOne(['mall_id' => $params['mall_id'],'goods_id' => $params['goods_id'], 'user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED]]);
  80. if (empty($model)) throw new Exception('不存在');
  81. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  82. return $model;
  83. }
  84. }
  85. } catch (Exception $e) {
  86. self::$static_error = $e->getMessage();
  87. return false;
  88. }
  89. }
  90. }