UserAddress.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use PHPUnit\Util\Exception;
  6. use Yii;
  7. /**
  8. * This is the model class for table "qimall_user_address".
  9. *
  10. *
  11. * @property int $id
  12. * @property int $user_id
  13. * @property string $name 收货人
  14. * @property int $province_id
  15. * @property string $province 省份名称
  16. * @property int $city_id
  17. * @property string $city 城市名称
  18. * @property int $district_id
  19. * @property string $district 县区名称
  20. * @property int $town_id 镇ID
  21. * @property string|null $town 镇
  22. * @property int $community_id 小区ID
  23. * @property string $community 小区名称
  24. * @property string $mobile 联系电话
  25. * @property string $detail 详细地址
  26. * @property int $is_default 是否默认
  27. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  28. * @property int $created_at 创建时间
  29. * @property int $updated_at
  30. * @property string $latitude 经度
  31. * @property string $longitude 纬度
  32. * @property string $location 位置
  33. */
  34. class UserAddress extends BaseActiveRecord
  35. {
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%user_address}}';
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['user_id', 'name', 'province_id', 'province', 'city_id', 'city', 'mobile', 'detail'], 'required'],
  50. [['user_id', 'province_id', 'city_id', 'district_id', 'town_id', 'is_default', 'status', 'created_at', 'updated_at', 'community_id'], 'integer'],
  51. [['name', 'province', 'city', 'district', 'town', 'latitude', 'longitude', 'location', 'community'], 'string', 'max' => 255],
  52. [['detail'], 'string', 'max' => 1000],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'user_id' => 'User ID',
  63. 'name' => '收货人',
  64. 'province_id' => 'Province ID',
  65. 'province' => '省份名称',
  66. 'city_id' => 'City ID',
  67. 'city' => '城市名称',
  68. 'district_id' => 'District ID',
  69. 'district' => '县区名称',
  70. 'town_id' => '镇ID',
  71. 'town' => '镇',
  72. 'mobile' => '联系电话',
  73. 'detail' => '详细地址',
  74. 'is_default' => '是否默认',
  75. 'status' => '状态[-1:删除;0:禁用;1启用]',
  76. 'created_at' => '创建时间',
  77. 'updated_at' => 'Updated At',
  78. 'latitude' => '经度',
  79. 'longitude' => '纬度',
  80. 'location' => '位置',
  81. ];
  82. }
  83. /**
  84. * 保存数据
  85. * @param array $params
  86. * @return bool|UserAddress
  87. * @throws \yii\db\Exception
  88. */
  89. public static function setData(array $params){
  90. $t = \Yii::$app->db->beginTransaction();
  91. try{
  92. if(!empty($params['id'])){
  93. $model = self::findOne(['id' => $params['id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  94. if(empty($model)) throw new Exception('服务不存在或已删除');
  95. }else{
  96. $model = new self();
  97. $model->loadDefaultValues();
  98. }
  99. if(!empty($params['is_default'])){
  100. $old = self::getOne([['user_id'=>$params['user_id']],['is_default'=>1]]);
  101. if($old&&$old->id!=$params['id']){
  102. $old->is_default = 0;
  103. $old->save();
  104. }
  105. }
  106. $params['town'] = $params['town']??'';
  107. $params['town_id'] = !empty($params['town_id'])?$params['town_id']:'0';
  108. $params['community_id'] = !empty($params['community_id'])?$params['community_id']:'0';
  109. $params['community'] = !empty($params['community'])?$params['community']:'';
  110. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  111. $t->commit();
  112. return $model;
  113. }catch(Exception $e){
  114. self::$static_error = $e->getMessage();
  115. $t->rollBack();
  116. return false;
  117. }
  118. }
  119. /**
  120. * 获取用户收货地址
  121. * @Author bing
  122. * @DateTime 2021-10-21 17:37:10
  123. * @copyright: Copyright (c) 2020 广东七件事集团
  124. * @param [type] $user_id
  125. * @return void
  126. */
  127. public static function getUserAddress($user_id,$address_id=0){
  128. $default_cond = [['status'=>StatusEnum::ENABLED,'user_id'=>$user_id]];
  129. if($address_id > 0){
  130. $cond = array_merge($default_cond,[['id'=>$address_id]]);
  131. return self::getOne($cond,true,[],false,'id,name,mobile,province,city,district,town,detail,community,province_id,city_id,district_id,latitude,longitude');
  132. }else{
  133. $result = [];
  134. //有默认地址取默认地址,没有取第一个
  135. $list = self::lists([
  136. 'where'=>$default_cond,
  137. 'select'=>'id,name,mobile,province,city,district,town,detail,is_default,community,province_id,city_id,district_id,latitude,longitude',
  138. 'order'=>'is_default desc,id asc',
  139. 'offset'=>0,
  140. 'limit'=>2
  141. ],true);
  142. foreach ($list as $item){
  143. if($item['is_default']){
  144. $result=$item;
  145. break;
  146. }
  147. }
  148. if(empty($result)&&!empty($list)) $result=$list[0];
  149. return $result;
  150. }
  151. }
  152. }