| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace common\models\user;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use PHPUnit\Util\Exception;
- use Yii;
- /**
- * This is the model class for table "qimall_user_address".
- *
- *
- * @property int $id
- * @property int $user_id
- * @property string $name 收货人
- * @property int $province_id
- * @property string $province 省份名称
- * @property int $city_id
- * @property string $city 城市名称
- * @property int $district_id
- * @property string $district 县区名称
- * @property int $town_id 镇ID
- * @property string|null $town 镇
- * @property int $community_id 小区ID
- * @property string $community 小区名称
- * @property string $mobile 联系电话
- * @property string $detail 详细地址
- * @property int $is_default 是否默认
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at
- * @property string $latitude 经度
- * @property string $longitude 纬度
- * @property string $location 位置
- */
- class UserAddress extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_address}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['user_id', 'name', 'province_id', 'province', 'city_id', 'city', 'mobile', 'detail'], 'required'],
- [['user_id', 'province_id', 'city_id', 'district_id', 'town_id', 'is_default', 'status', 'created_at', 'updated_at', 'community_id'], 'integer'],
- [['name', 'province', 'city', 'district', 'town', 'latitude', 'longitude', 'location', 'community'], 'string', 'max' => 255],
- [['detail'], 'string', 'max' => 1000],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'user_id' => 'User ID',
- 'name' => '收货人',
- 'province_id' => 'Province ID',
- 'province' => '省份名称',
- 'city_id' => 'City ID',
- 'city' => '城市名称',
- 'district_id' => 'District ID',
- 'district' => '县区名称',
- 'town_id' => '镇ID',
- 'town' => '镇',
- 'mobile' => '联系电话',
- 'detail' => '详细地址',
- 'is_default' => '是否默认',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => 'Updated At',
- 'latitude' => '经度',
- 'longitude' => '纬度',
- 'location' => '位置',
- ];
- }
- /**
- * 保存数据
- * @param array $params
- * @return bool|UserAddress
- * @throws \yii\db\Exception
- */
- public static function setData(array $params){
- $t = \Yii::$app->db->beginTransaction();
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['id' => $params['id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if(empty($model)) throw new Exception('服务不存在或已删除');
- }else{
- $model = new self();
- $model->loadDefaultValues();
- }
- if(!empty($params['is_default'])){
- $old = self::getOne([['user_id'=>$params['user_id']],['is_default'=>1]]);
- if($old&&$old->id!=$params['id']){
- $old->is_default = 0;
- $old->save();
- }
- }
- $params['town'] = $params['town']??'';
- $params['town_id'] = !empty($params['town_id'])?$params['town_id']:'0';
- $params['community_id'] = !empty($params['community_id'])?$params['community_id']:'0';
- $params['community'] = !empty($params['community'])?$params['community']:'';
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- $t->commit();
- return $model;
- }catch(Exception $e){
- self::$static_error = $e->getMessage();
- $t->rollBack();
- return false;
- }
- }
- /**
- * 获取用户收货地址
- * @Author bing
- * @DateTime 2021-10-21 17:37:10
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $user_id
- * @return void
- */
- 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 self::getOne($cond,true,[],false,'id,name,mobile,province,city,district,town,detail,community,province_id,city_id,district_id,latitude,longitude');
- }else{
- $result = [];
- //有默认地址取默认地址,没有取第一个
- $list = self::lists([
- 'where'=>$default_cond,
- 'select'=>'id,name,mobile,province,city,district,town,detail,is_default,community,province_id,city_id,district_id,latitude,longitude',
- '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;
- }
- }
- }
|