| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace common\models\forms;
- use common\helpers\Date;
- use common\components\Model;
- use common\helpers\Form;
- use common\helpers\user\Info;
- use common\libs\logging\operate\UserOperate;
- use common\models\Ad;
- use common\models\BaReceiveAddress;
- use common\models\ReceiveAddress;
- use common\models\Region;
- use common\models\User;
- use common\models\UserInfo;
- use yii\base\Exception;
- class BaReceiveAddressForm extends Model
- {
- public $id;
- public $consignee;
- public $mobile;
- public $province;
- public $city;
- public $county;
- public $lgaName;
- public $cityName;
- public $address;
- public $isDefault;
- /**
- * @var BaReceiveAddress
- */
- private $_model;
- public function init() {
- parent::init();
- // $this->adminOperateLogger = new UserOperate([
- // 'fetchClass' => BaReceiveAddress::class,
- // ]);
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'], 'trim'],
- [['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address'], 'required'],
- [['mobile'], 'mobile'],
- [['province'], 'exist', 'targetClass' => Region::class, 'targetAttribute' => 'REGION_CODE'],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'consignee' => 'Consignee',
- 'mobile' => 'Phone',
- 'province' => 'State',
- 'lgaName' => 'Local Government Area',
- 'cityName' => 'City Name',
- 'address' => 'Address',
- ];
- }
- /**
- * 指定校验场景
- * @return array
- */
- public function scenarios()
- {
- $parentScenarios = parent::scenarios();
- $customScenarios = [
- 'userAdd' => ['consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'],
- 'userEdit' => ['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'],
- 'userIsDefault' => ['id', 'isDefault'],
- ];
- return array_merge($parentScenarios, $customScenarios);
- }
- /**
- * 校验之前
- * @return bool
- */
- public function beforeValidate()
- {
- $userId = \Yii::$app->getUser()->getId();
- $parentResult = parent::beforeValidate();
- if ($this->scenario == 'userAdd' || $this->scenario == 'userEdit' || $this->scenario == 'userIsDefault') {
- if ($this->scenario == 'userAdd') {
- $count = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->count();
- if ($count > 10) {
- $this->addError('id', 'A maximum of 10 shipping addresses can be added'); // 最多只能添加10个收货地址
- return $parentResult;
- }
- }
- if ($this->id) {
- $this->_model = BaReceiveAddress::findOne(["ID" => $this->id]);
- if (!$this->_model){
- $this->addError('id', 'Address does not exist'); // 地址不存在
- return $parentResult;
- }
- if ($this->_model['USER_ID'] != $userId){
- $this->addError('id', 'You have no right to modify this address'); // 无权修改此地址
- return $parentResult;
- }
- } else {
- $this->_model = new BaReceiveAddress();
- }
- }
- return $parentResult;
- }
- /**
- * 添加编辑
- * @return BaReceiveAddress|null
- * @throws \yii\db\Exception
- */
- public function edit(){
- if(!$this->validate()){
- return null;
- }
- $userId = \Yii::$app->getUser()->getId();
- $db = \Yii::$app->db;
- $transaction = $db->beginTransaction();
- try {
- // 如果设置了此项为默认则清空所有默认
- if ($this->isDefault) {
- BaReceiveAddress::updateAll(['IS_DEFAULT' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
- }
- if($this->scenario == 'userAdd'){
- $this->_model->USER_ID = $userId;
- $this->_model->USER_NAME = Info::getBaUserNameByUserId($userId); // TODO:
- $this->_model->CONSIGNEE = $this->consignee;
- $this->_model->MOBILE = $this->mobile;
- $this->_model->PROVINCE = $this->province;
- $this->_model->LGA_NAME = $this->lgaName;
- $this->_model->CITY_NAME = $this->cityName;
- $this->_model->ADDRESS = $this->address;
- $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
- $this->_model->CREATED_AT = Date::nowTime();
- } elseif($this->scenario == 'userEdit') {
- $this->_model->CONSIGNEE = $this->consignee;
- $this->_model->MOBILE = $this->mobile;
- $this->_model->PROVINCE = $this->province;
- $this->_model->LGA_NAME = $this->lgaName;
- $this->_model->CITY_NAME = $this->cityName;
- $this->_model->ADDRESS = $this->address;
- $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
- $this->_model->UPDATED_AT = Date::nowTime();
- } elseif($this->scenario == 'userIsDefault') {
- $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
- } else {
- throw new Exception('The scene does not exist'); // 场景不存在
- }
- if(!$this->_model->save()){
- throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
- }
- $transaction->commit();
- } catch (Exception $e) {
- $transaction->rollBack();
- $this->addError('edit', $e->getMessage());
- return null;
- }
- if($this->scenario == 'adminAdd'){
- $this->adminOperateLogger->afterInsert($this->_model)->clean()->save([
- 'optType' => 'Add shipping address', // 添加收货地址
- ]);
- } elseif($this->scenario == 'adminEdit') {
- $this->adminOperateLogger->afterUpdate($this->_model)->clean()->save([
- 'optType' => 'Edit shipping address', // 编辑收货地址
- ]);
- }
- return $this->_model;
- }
- /**
- * 删除前
- * @param $selected
- */
- public function beforeDelete($selected) {
- $this->adminOperateLogger->fetchClass = Ad::class;
- $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
- }
- /**
- * 删除
- * @param $selected
- * @throws Exception
- */
- public function delete($selected) {
- $this->adminOperateLogger->clean()->save([
- 'optType' => 'Delete shipping address', // 删除收货地址
- ]);
- }
- }
|