CommunityUser.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace addons\Community\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_community_user}}".
  7. *
  8. * @property int $id ID
  9. * @property int $mall_id 商城ID
  10. * @property int $user_id 用户ID
  11. * @property int $community_id 自提点ID
  12. * @property int $is_checked 是否选中[0未选中 1选中]
  13. * @property int $status 状态[-1:删除;0:禁用;1启用]
  14. * @property int $created_at 创建时间
  15. * @property int $updated_at 更新时间
  16. * @property CommunityHead $community
  17. */
  18. class CommunityUser extends \common\models\base\BaseActiveRecord
  19. {
  20. /**
  21. * {@inheritdoc}
  22. */
  23. public static function tableName()
  24. {
  25. return '{{%addons_community_user}}';
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function rules()
  31. {
  32. return [
  33. [['mall_id', 'user_id', 'community_id', 'status', 'created_at', 'updated_at', 'is_checked'], 'integer'],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => 'ID',
  43. 'mall_id' => '商城ID',
  44. 'user_id' => '用户ID',
  45. 'community_id' => '自提点ID',
  46. 'is_checked' => '是否选中[0未选中 1选中]',
  47. 'status' => '状态[-1:删除;0:禁用;1启用]',
  48. 'created_at' => '创建时间',
  49. 'updated_at' => '更新时间',
  50. ];
  51. }
  52. public function getCommunity()
  53. {
  54. return $this->hasOne(CommunityHead::class, ['id' => 'community_id']);
  55. }
  56. public static function setData(array $params)
  57. {
  58. $connection = Yii::$app->db;
  59. $transaction = $connection->beginTransaction();
  60. try {
  61. $model = self::findOne(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'community_id' => $params['community_id'], 'status' => StatusEnum::ENABLED]);
  62. if (!$model) {
  63. $model = new self();
  64. $model->loadDefaultValues();
  65. }
  66. if (!$model->load($params, '') || !$model->save()) {
  67. throw new \Exception($model->getErrorMessage());
  68. }
  69. if (isset($params['is_checked']) && $params['is_checked'] == StatusEnum::ENABLED) {
  70. $old_data = self::findAll(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'is_checked' => StatusEnum::ENABLED]);
  71. foreach ($old_data as $data) {
  72. if ($data->id == $model->id) continue;
  73. $data->is_checked = StatusEnum::DISABLED;
  74. $data->save();
  75. }
  76. }
  77. $transaction->commit();
  78. return $model;
  79. } catch (\Exception $e) {
  80. $transaction->rollBack();
  81. self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
  82. return false;
  83. }
  84. }
  85. }