BaReceiveAddressForm.php 6.8 KB

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