CommunityHead.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace addons\Community\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\user\User;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_addons_community_head".
  9. *
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property int $user_id 绑定会员id
  13. * @property string $contacts 联系人姓名
  14. * @property string $mobile 联系人手机号
  15. * @property string $name 名称
  16. * @property string $desc 简介
  17. * @property string $logo logo
  18. * @property string $license_img 营业执照
  19. * @property string $idcard_img 身份证
  20. * @property int|null $province_id 省
  21. * @property int|null $city_id 市
  22. * @property int|null $district_id 区
  23. * @property string $address 详细地址
  24. * @property string $longitude 经度
  25. * @property string $latitude 维度
  26. * @property int $check_status 审核状态:0:待审核;1:已通过;2:未通过
  27. * @property string $check_remark 审核备注
  28. * @property int $status 状态[0禁用 1启用 -1删除]
  29. * @property int $created_at 创建时间
  30. * @property int $updated_at 修改时间
  31. * @property int $is_home_delivery 是否提供送货上门服务
  32. * @property User $user
  33. */
  34. class CommunityHead extends BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%addons_community_head}}';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [[
  50. 'mall_id', 'user_id', 'province_id', 'city_id', 'district_id', 'check_status',
  51. 'status', 'created_at', 'updated_at', 'is_home_delivery'
  52. ], 'integer'],
  53. [['contacts', 'name', 'desc', 'logo', 'address', 'longitude', 'latitude', 'check_remark', 'idcard_img', 'license_img'], 'string', 'max' => 255],
  54. [['setting'], 'string'],
  55. [['mobile'], 'string', 'max' => 11],
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => 'Mall ID',
  66. 'user_id' => '绑定会员id',
  67. 'contacts' => '联系人姓名',
  68. 'mobile' => '联系人手机号',
  69. 'name' => '名称',
  70. 'desc' => '简介',
  71. 'logo' => 'logo',
  72. 'license_img' => '营业执照',
  73. 'province_id' => '省',
  74. 'city_id' => '市',
  75. 'district_id' => '区',
  76. 'address' => '详细地址',
  77. 'longitude' => '经度',
  78. 'latitude' => '维度',
  79. 'check_status' => '审核状态:0:待审核;1:已通过;2:未通过',
  80. 'check_remark' => '审核备注',
  81. 'status' => '状态[0禁用 1启用 -1删除]',
  82. 'created_at' => '创建时间',
  83. 'updated_at' => '修改时间',
  84. ];
  85. }
  86. public function getUser()
  87. {
  88. return $this->hasOne(User::class, ['id' => 'user_id']);
  89. }
  90. public static function setData(array $params, int $admin_id = 0, bool $is_from_apply_check = false)
  91. {
  92. if (!$is_from_apply_check) {
  93. $transaction = Yii::$app->db->beginTransaction();
  94. }
  95. try {
  96. $is_new = true;
  97. if (!empty($params['id'])) {
  98. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  99. if (empty($model)) throw new \Exception('自提点不存在或已删除');
  100. // 判断是否需要修改用户
  101. if ($model->user_id != $params['user_id']) {
  102. CommunityHeadApply::updateAll(['user_id' => $params['user_id']],
  103. ['user_id' => $model->user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  104. }
  105. $is_new = false;
  106. } else {
  107. $model = new self();
  108. $model->loadDefaultValues();
  109. }
  110. if (!$model->load($params, '') || !$model->save()) {
  111. throw new \Exception($model->getErrorMessage());
  112. }
  113. // 查询是否有记录
  114. $has = CommunityHeadApply::find()
  115. ->select('id')
  116. ->where(['user_id' => $model->user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  117. ->one();
  118. if ($is_new || empty($has)) {
  119. CommunityGoods::setGoodsSetting($params['mall_id'], $model->id);
  120. if (!$is_from_apply_check) { // 如果不是从审核过来的,且是新的数据那么就需要同步至申请表
  121. // 需要将数据存储到申请表
  122. CommunityHeadApply::create($model, $admin_id);
  123. }
  124. }
  125. if (!empty($transaction)) $transaction->commit();
  126. return $model;
  127. } catch (\Exception $e) {
  128. if (!empty($transaction)) $transaction->rollBack();
  129. self::$static_error = $e->getMessage() . $e->getLine() . $e->getFile();
  130. return false;
  131. }
  132. }
  133. public static function getDataOne($params)
  134. {
  135. return self::find()->where($params)->asArray()->one();
  136. }
  137. }