StoreCollect.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace addons\Store\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "qimall_addons_store_collect".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城id
  11. * @property int $store_id 商品id
  12. * @property int $user_id 会员id
  13. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  14. * @property int $created_at
  15. * @property int $updated_at
  16. */
  17. class StoreCollect extends BaseActiveRecord
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public static function tableName()
  23. {
  24. return '{{%addons_store_collect}}';
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function rules()
  30. {
  31. return [
  32. [['mall_id', 'store_id', 'user_id', 'status', 'created_at', 'updated_at'], 'integer'],
  33. ];
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function attributeLabels()
  39. {
  40. return [
  41. 'id' => 'ID',
  42. 'mall_id' => '商城id',
  43. 'store_id' => '商品id',
  44. 'user_id' => '会员id',
  45. 'status' => '状态[-1:删除;0:禁用;1启用]',
  46. 'created_at' => 'Created At',
  47. 'updated_at' => 'Updated At',
  48. ];
  49. }
  50. public static function setData(array $params)
  51. {
  52. try {
  53. if ($params['type'] == 1) {
  54. if (!empty($params['store_id'])) {
  55. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  56. if (empty($model)) {
  57. $model = new self();
  58. $model->loadDefaultValues();
  59. } else {
  60. if ($model->status) throw new \Exception('已收藏');
  61. }
  62. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  63. return $model;
  64. }
  65. }
  66. if ($params['type'] == 0) {
  67. if (!empty($params['store_id'])) {
  68. $model = self::findOne(['mall_id' => $params['mall_id'], 'store_id' => $params['store_id'], 'user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED]]);
  69. if (empty($model)) throw new \Exception('不存在');
  70. if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
  71. return $model;
  72. }
  73. }
  74. } catch (\Exception $e) {
  75. self::$static_error = $e->getMessage();
  76. return false;
  77. }
  78. }
  79. }