cxfoot/models/common/CommonAddressActionForm.php
2023-10-24 14:54:18 +08:00

100 lines
2.7 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2021年6月1日
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\models\common;
use app\models\Address;
use app\models\Model;
class CommonAddressActionForm extends Model
{
public $address_id;
public $user_id;
public $cx_mch_id;
public function rules()
{
return [
[['user_id', 'cx_mch_id'], 'integer'],
[['address_id', ], 'safe'],
[['address_id', 'user_id', 'cx_mch_id'], 'required'],
];
}
public function delete()
{
if(!$this->validate()){
return $this->getModelError();
}
if(is_array($this->address_id)){
foreach ($this->address_id as $address_id){
$model = Address::findOne([
'id' => $address_id,
'is_delete' => 0,
'user_id' => $this->user_id,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null)
continue;
$model->is_delete = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
} else {
$model = Address::findOne([
'id' => $this->address_id,
'is_delete' => 0,
'user_id' => $this->user_id,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('地址不存在或已经删除');
}
$model->is_delete = 1;
if(!$model->save()){
return $this->getModelError($model);
}
}
return $this->apiReturnSuccess("删除成功");
}
public function set_default()
{
if(!$this->validate()){
return $this->getModelError();
}
$t = \Yii::$app->db->beginTransaction();
$model = Address::findOne([
'id' => $this->address_id,
'is_delete' => 0,
'user_id' => $this->user_id,
'cx_mch_id' => $this->cx_mch_id
]);
if($model == null){
return $this->apiReturnError('地址不存在或已经删除');
}
Address::updateAll(['is_default' => 0],[
'is_delete' => 0,
'user_id' => $this->user_id,
'cx_mch_id' => $this->cx_mch_id
]);
$model->is_default = 1;
if(!$model->save()){
$t->rollBack();
return $this->getModelError($model);
}
$t->commit();
return $this->apiReturnSuccess("设置成功");
}
}