AreaApply.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace addons\Area\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use common\models\common\Region;
  6. use common\models\user\Town;
  7. use common\models\user\User;
  8. use Yii;
  9. /**
  10. * This is the model class for table "qimall_addons_area_apply".
  11. *
  12. * @property int $id
  13. * @property int $mall_id 商城id
  14. * @property int $user_id 会员id
  15. * @property string $address 地址
  16. * @property string $realname 真实姓名
  17. * @property string $mobile 手机号码
  18. * @property int $level 区域等级;1镇级;2区级;3市级;4省级;
  19. * @property int $province_id 省id
  20. * @property int $city_id 市id
  21. * @property int $district_id 区id
  22. * @property int $town_id 镇id
  23. * @property int $community_id 小区id
  24. * @property string $marks 备注
  25. * @property int $check_status 0未审核;1审核通过;2不通过
  26. * @property int $status 状态[-1:删除;0:禁用;1启用]
  27. * @property int $created_at 创建时间
  28. * @property int $updated_at 修改时间
  29. * @property int $agent_id 修改时间
  30. */
  31. class AreaApply extends BaseActiveRecord
  32. {
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return '{{%addons_area_apply}}';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['mall_id', 'user_id', 'level', 'province_id', 'city_id', 'district_id', 'town_id', 'check_status', 'status',
  47. 'created_at', 'updated_at', 'community_id', 'agent_id', 'is_refund', 'refund_time', 'order_id'], 'integer'],
  48. [['level'], 'required'],
  49. [['address', 'marks'], 'string', 'max' => 255],
  50. [['realname'], 'string', 'max' => 45],
  51. [['mobile'], 'string', 'max' => 11],
  52. [['pay_price'], 'number'],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'mall_id' => '商城id',
  63. 'user_id' => '会员id',
  64. 'address' => '地址',
  65. 'realname' => '真实姓名',
  66. 'mobile' => '手机号码',
  67. 'level' => '区域等级;1镇级;2区级;3市级;4省级;',
  68. 'province_id' => '省id',
  69. 'city_id' => '市id',
  70. 'district_id' => '区id',
  71. 'town_id' => '镇id',
  72. 'marks' => '备注',
  73. 'check_status' => '0未审核;1审核通过;2不通过',
  74. 'status' => '状态[-1:删除;0:禁用;1启用]',
  75. 'created_at' => '创建时间',
  76. 'updated_at' => '修改时间',
  77. ];
  78. }
  79. public function getUser()
  80. {
  81. return $this->hasOne(User::class, ['id' => 'user_id']);
  82. }
  83. public static function setData(array $params)
  84. {
  85. try {
  86. if (!empty($params['id'])) {
  87. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  88. if (empty($model)) throw new \Exception('服务不存在或已删除');
  89. } else {
  90. $model = new self();
  91. $model->loadDefaultValues();
  92. }
  93. $region_id_arr = [];
  94. if (!empty($params['province_id'])) $region_id_arr[] = $params['province_id'];
  95. if (!empty($params['city_id'])) $region_id_arr[] = $params['city_id'];
  96. if (!empty($params['district_id'])) $region_id_arr[] = $params['district_id'];
  97. $address = '';
  98. $region_list = Region::lists(['where' => [['id' => $region_id_arr]], 'select' => 'id,name', 'order' => 'id asc']);
  99. foreach ($region_list as $item) {
  100. $address .= $item['name'];
  101. }
  102. if (!empty($params['town_id'])) {
  103. $town = Town::getOne([['id' => $params['town_id']]], true, [], false, 'id,name');
  104. if (!empty($town['name'])) $address .= $town['name'];
  105. }
  106. if (!empty($params['community_id'])) {
  107. $community_name = AreaCommunity::find()->where(['id' => $params['community_id']])
  108. ->andWhere(['status' => StatusEnum::ENABLED])->select('community_name')->scalar();
  109. if (!empty($community_name)) $address .= $community_name;
  110. }
  111. $params['address'] = $address;
  112. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  113. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  114. return $model;
  115. } catch (\Exception $e) {
  116. self::$static_error = $e->getMessage();
  117. return false;
  118. }
  119. }
  120. /**
  121. * @desc:审核
  122. * @Author: hua
  123. * @Date: 2022/1/21
  124. * @Time: 14:40
  125. * @Copyright: copyright (c) 2021 广东七件事集团
  126. * @param $params
  127. * @return bool
  128. */
  129. public static function apply($params)
  130. {
  131. $t = Yii::$app->db->beginTransaction();
  132. try {
  133. $apply_model = AreaApply::findOne(['id' => $params['id']]);
  134. if ($params['check_status'] == 1) {
  135. $apply_arr = $apply_model->toArray();
  136. unset($apply_arr['id']);
  137. unset($params['id']);
  138. $new_params = array_merge($params, $apply_arr);
  139. $new_params['status'] = StatusEnum::ENABLED;
  140. $res = AreaAgent::setData($new_params);
  141. if ($res == false) throw new \Exception(AreaAgent::getStaticError());
  142. }
  143. $apply_model->check_status = $params['check_status'];
  144. $apply_model->marks = $params['marks'];
  145. $res = $apply_model->save();
  146. if ($res == false) throw new \Exception($apply_model::getStaticError());
  147. $t->commit();
  148. return true;
  149. } catch (\Exception $e) {
  150. $t->rollBack();
  151. self::$static_error = $e->getMessage();
  152. return false;
  153. }
  154. }
  155. }