| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace addons\Community\common\models;
- use common\enums\StatusEnum;
- use Yii;
- /**
- * This is the model class for table "{{%addons_community_user}}".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property int $user_id 用户ID
- * @property int $community_id 自提点ID
- * @property int $is_checked 是否选中[0未选中 1选中]
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- * @property CommunityHead $community
- */
- class CommunityUser extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_community_user}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'community_id', 'status', 'created_at', 'updated_at', 'is_checked'], 'integer'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户ID',
- 'community_id' => '自提点ID',
- 'is_checked' => '是否选中[0未选中 1选中]',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- public function getCommunity()
- {
- return $this->hasOne(CommunityHead::class, ['id' => 'community_id']);
- }
- public static function setData(array $params)
- {
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'community_id' => $params['community_id'], 'status' => StatusEnum::ENABLED]);
- if (!$model) {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($params, '') || !$model->save()) {
- throw new \Exception($model->getErrorMessage());
- }
- if (isset($params['is_checked']) && $params['is_checked'] == StatusEnum::ENABLED) {
- $old_data = self::findAll(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'is_checked' => StatusEnum::ENABLED]);
- foreach ($old_data as $data) {
- if ($data->id == $model->id) continue;
- $data->is_checked = StatusEnum::DISABLED;
- $data->save();
- }
- }
- $transaction->commit();
- return $model;
- } catch (\Exception $e) {
- $transaction->rollBack();
- self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
- return false;
- }
- }
- }
|