Region.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace addons\SmartForm\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\user\UserAddress;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%region}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城ID
  11. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  12. * @property int $created_at 创建时间
  13. * @property int $updated_at 修改时间
  14. */
  15. class Region extends \common\models\base\BaseActiveRecord
  16. {
  17. const ADDONS_NAME = '';
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public static function tableName()
  22. {
  23. return '{{%region}}';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function rules()
  29. {
  30. return [
  31. [['name', 'level'], 'required'],
  32. [['parent_id', 'level', 'created_at', 'updated_at'], 'integer'],
  33. [['name', 'short_title', 'areacode', 'zipcode', 'initial', 'lng', 'lat'], 'string', 'max' => 150],
  34. ];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function attributeLabels()
  40. {
  41. return [
  42. 'id' => '主键',
  43. 'name' => '地区名',
  44. 'parent_id' => '上级ID',
  45. 'level' => '层级',
  46. 'short_title' => '缩写',
  47. 'areacode' => '区域编码',
  48. 'zipcode' => '邮政编码',
  49. 'initial' => '拼音首字母',
  50. 'lng' => '经度',
  51. 'lat' => '纬度',
  52. 'created_at' => '创建时间',
  53. 'updated_at' => '更新时间',
  54. ];
  55. }
  56. /**
  57. * @param array $where
  58. * @param string $select
  59. * @param array $with
  60. * @param int $is_array
  61. * @return array|Region[]|\yii\db\ActiveRecord[]
  62. */
  63. public static function getRegionList(array $where, $select = '*', array $with = [], int $is_array = 0)
  64. {
  65. $offset = 1;
  66. $limit = 1000;
  67. $params['where'] = $where;
  68. $params['with'] = $with;
  69. $params['select'] = $select;
  70. $params['limit'] = $limit;
  71. $list = [];
  72. $is_flag = true;
  73. do {
  74. $params['offset'] = ($offset - 1) * $limit;
  75. $rows = self::lists($params);
  76. $list = array_merge($list, $rows);
  77. if (empty($rows)) $is_flag = false;
  78. $offset++;
  79. } while ($is_flag);
  80. return $list;
  81. }
  82. /**
  83. * @Description: 获取用户地址信息
  84. * @Author: qi
  85. * @DateTime 2022-11-02 17:39:28
  86. * @param integer $user_id
  87. * @param integer $address_id
  88. * @return mixed
  89. */
  90. public static function getUserAddress($user_id, $address_id = 0)
  91. {
  92. $default_cond = [['status' => StatusEnum::ENABLED, 'user_id' => $user_id]];
  93. if ($address_id > 0) {
  94. $cond = array_merge($default_cond, [['id' => $address_id]]);
  95. return UserAddress::getOne($cond, true, [], false, 'id,province_id,city_id,district_id,name,mobile,province,city,district,town,detail');
  96. } else {
  97. $result = [];
  98. //有默认地址取默认地址,没有取第一个
  99. $list = UserAddress::lists([
  100. 'where' => $default_cond,
  101. 'select' => 'id,name,mobile,province_id,city_id,district_id,province,city,district,town,detail,is_default',
  102. 'order' => 'is_default desc,id asc',
  103. 'offset' => 0,
  104. 'limit' => 2
  105. ], true);
  106. foreach ($list as $item) {
  107. if ($item['is_default']) {
  108. $result = $item;
  109. break;
  110. }
  111. }
  112. if (empty($result) && !empty($list)) $result = $list[0];
  113. return $result;
  114. }
  115. }
  116. }