| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace addons\SmartForm\common\models;
- use common\enums\StatusEnum;
- use common\models\user\UserAddress;
- use Yii;
- /**
- * This is the model class for table "{{%region}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class Region extends \common\models\base\BaseActiveRecord
- {
- const ADDONS_NAME = '';
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%region}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['name', 'level'], 'required'],
- [['parent_id', 'level', 'created_at', 'updated_at'], 'integer'],
- [['name', 'short_title', 'areacode', 'zipcode', 'initial', 'lng', 'lat'], 'string', 'max' => 150],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- 'name' => '地区名',
- 'parent_id' => '上级ID',
- 'level' => '层级',
- 'short_title' => '缩写',
- 'areacode' => '区域编码',
- 'zipcode' => '邮政编码',
- 'initial' => '拼音首字母',
- 'lng' => '经度',
- 'lat' => '纬度',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * @param array $where
- * @param string $select
- * @param array $with
- * @param int $is_array
- * @return array|Region[]|\yii\db\ActiveRecord[]
- */
- public static function getRegionList(array $where, $select = '*', array $with = [], int $is_array = 0)
- {
- $offset = 1;
- $limit = 1000;
- $params['where'] = $where;
- $params['with'] = $with;
- $params['select'] = $select;
- $params['limit'] = $limit;
- $list = [];
- $is_flag = true;
- do {
- $params['offset'] = ($offset - 1) * $limit;
- $rows = self::lists($params);
- $list = array_merge($list, $rows);
- if (empty($rows)) $is_flag = false;
- $offset++;
- } while ($is_flag);
- return $list;
- }
- /**
- * @Description: 获取用户地址信息
- * @Author: qi
- * @DateTime 2022-11-02 17:39:28
- * @param integer $user_id
- * @param integer $address_id
- * @return mixed
- */
- public static function getUserAddress($user_id, $address_id = 0)
- {
- $default_cond = [['status' => StatusEnum::ENABLED, 'user_id' => $user_id]];
- if ($address_id > 0) {
- $cond = array_merge($default_cond, [['id' => $address_id]]);
- return UserAddress::getOne($cond, true, [], false, 'id,province_id,city_id,district_id,name,mobile,province,city,district,town,detail');
- } else {
- $result = [];
- //有默认地址取默认地址,没有取第一个
- $list = UserAddress::lists([
- 'where' => $default_cond,
- 'select' => 'id,name,mobile,province_id,city_id,district_id,province,city,district,town,detail,is_default',
- 'order' => 'is_default desc,id asc',
- 'offset' => 0,
- 'limit' => 2
- ], true);
- foreach ($list as $item) {
- if ($item['is_default']) {
- $result = $item;
- break;
- }
- }
- if (empty($result) && !empty($list)) $result = $list[0];
- return $result;
- }
- }
- }
|