ReceiveAddressForm.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\components\Model;
  5. use common\helpers\Form;
  6. use common\helpers\user\Info;
  7. use common\libs\logging\operate\UserOperate;
  8. use common\models\Ad;
  9. use common\models\ReceiveAddress;
  10. use common\models\Region;
  11. use common\models\User;
  12. use common\models\UserInfo;
  13. use yii\base\Exception;
  14. /**
  15. * Login form
  16. */
  17. class ReceiveAddressForm extends Model
  18. {
  19. public $id;
  20. public $consignee;
  21. public $mobile;
  22. public $countryId;
  23. public $province;
  24. public $city;
  25. public $county;
  26. public $lgaName;
  27. public $cityName;
  28. public $address;
  29. public $isDefault;
  30. /**
  31. * @var ReceiveAddress
  32. */
  33. private $_model;
  34. public function init() {
  35. parent::init();
  36. $this->adminOperateLogger = new UserOperate([
  37. 'fetchClass' => ReceiveAddress::class,
  38. ]);
  39. }
  40. /**
  41. * @inheritdoc
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'], 'trim'],
  47. [['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address'], 'required'],
  48. [['mobile'], 'mobile'],
  49. [['province'], 'exist', 'targetClass' => Region::class, 'targetAttribute' => 'REGION_CODE'],
  50. ];
  51. }
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'consignee' => 'Consignee',
  56. 'mobile' => 'Phone',
  57. 'province' => 'State',
  58. // 'city' => '市/区',
  59. // 'county' => '区/县',
  60. 'lgaName' => 'Local Government Area',
  61. 'cityName' => 'City Name',
  62. 'address' => 'Address',
  63. ];
  64. }
  65. /**
  66. * 指定校验场景
  67. * @return array
  68. */
  69. public function scenarios()
  70. {
  71. $parentScenarios = parent::scenarios();
  72. $customScenarios = [
  73. 'userAdd' => ['consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'],
  74. 'userEdit' => ['id', 'consignee', 'mobile', 'province', 'lgaName', 'cityName', 'address', 'isDefault'],
  75. 'userIsDefault' => ['id', 'isDefault'],
  76. ];
  77. return array_merge($parentScenarios, $customScenarios);
  78. }
  79. /**
  80. * 校验之前
  81. * @return bool
  82. */
  83. public function beforeValidate()
  84. {
  85. $parentResult = parent::beforeValidate();
  86. if ($this->scenario == 'userAdd' || $this->scenario == 'userEdit' || $this->scenario == 'userIsDefault') {
  87. if ($this->scenario == 'userAdd') {
  88. $count = ReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->count();
  89. if ($count > 10) {
  90. $this->addError('id', 'A maximum of 10 shipping addresses can be added'); // 最多只能添加10个收货地址
  91. return $parentResult;
  92. }
  93. }
  94. if ($this->id) {
  95. $this->_model = ReceiveAddress::findOne(["ID"=>$this->id]);
  96. if (!$this->_model){
  97. $this->addError('id', 'Address does not exist'); // 地址不存在
  98. return $parentResult;
  99. }
  100. if ($this->_model['USER_ID'] != \Yii::$app->user->id){
  101. $this->addError('id', 'You have no right to modify this address'); // 无权修改此地址
  102. return $parentResult;
  103. }
  104. } else {
  105. $this->_model = new ReceiveAddress();
  106. }
  107. $this->countryId = Info::getUserCountryByUserId(\Yii::$app->user->id);
  108. }
  109. return $parentResult;
  110. }
  111. /**
  112. * 添加编辑
  113. * @return ReceiveAddress|null
  114. * @throws \yii\db\Exception
  115. */
  116. public function edit(){
  117. if(!$this->validate()){
  118. return null;
  119. }
  120. $db = \Yii::$app->db;
  121. $transaction = $db->beginTransaction();
  122. try {
  123. // 如果设置了此项为默认则清空所有默认
  124. if ($this->isDefault) {
  125. ReceiveAddress::updateAll(['IS_DEFAULT' => 0], 'USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id]);
  126. }
  127. if($this->scenario == 'userAdd'){
  128. $this->_model->USER_ID = \Yii::$app->user->id;
  129. $this->_model->USER_NAME = Info::getUserNameByUserId(\Yii::$app->user->id);
  130. $this->_model->CONSIGNEE = $this->consignee;
  131. $this->_model->MOBILE = $this->mobile;
  132. $this->_model->COUNTRY_ID = $this->countryId ?? Info::getUserCountryByUserId(\Yii::$app->user->id);
  133. $this->_model->PROVINCE = $this->province;
  134. $this->_model->LGA_NAME = $this->lgaName;
  135. $this->_model->CITY_NAME = $this->cityName;
  136. $this->_model->ADDRESS = $this->address;
  137. $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
  138. $this->_model->CREATED_AT = Date::nowTime();
  139. } elseif($this->scenario == 'userEdit') {
  140. $this->_model->CONSIGNEE = $this->consignee;
  141. $this->_model->MOBILE = $this->mobile;
  142. $this->_model->COUNTRY_ID = $this->countryId ?? Info::getUserCountryByUserId(\Yii::$app->user->id);
  143. $this->_model->PROVINCE = $this->province;
  144. $this->_model->LGA_NAME = $this->lgaName;
  145. $this->_model->CITY_NAME = $this->cityName;
  146. $this->_model->ADDRESS = $this->address;
  147. $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
  148. $this->_model->UPDATED_AT = Date::nowTime();
  149. } elseif($this->scenario == 'userIsDefault') {
  150. $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
  151. } else {
  152. throw new Exception('The scene does not exist'); // 场景不存在
  153. }
  154. if(!$this->_model->save()){
  155. throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
  156. }
  157. $transaction->commit();
  158. } catch (Exception $e) {
  159. $transaction->rollBack();
  160. $this->addError('edit', $e->getMessage());
  161. return null;
  162. }
  163. if($this->scenario == 'adminAdd'){
  164. $this->adminOperateLogger->afterInsert($this->_model)->clean()->save([
  165. 'optType' => 'Add shipping address', // 添加收货地址
  166. ]);
  167. } elseif($this->scenario == 'adminEdit') {
  168. $this->adminOperateLogger->afterUpdate($this->_model)->clean()->save([
  169. 'optType' => 'Edit shipping address', // 编辑收货地址
  170. ]);
  171. }
  172. return $this->_model;
  173. }
  174. /**
  175. * 删除前
  176. * @param $selected
  177. */
  178. public function beforeDelete($selected) {
  179. $this->adminOperateLogger->fetchClass = Ad::class;
  180. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  181. }
  182. /**
  183. * 删除
  184. * @param $selected
  185. * @throws Exception
  186. */
  187. public function delete($selected) {
  188. $this->adminOperateLogger->clean()->save([
  189. 'optType' => 'Delete shipping address', // 删除收货地址
  190. ]);
  191. }
  192. }