ReceiveAddressForm.php 6.8 KB

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