BaReceiveAddressForm.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. $parentResult = parent::beforeValidate();
  81. if ($this->scenario == 'userAdd' || $this->scenario == 'userEdit' || $this->scenario == 'userIsDefault') {
  82. if ($this->scenario == 'userAdd') {
  83. $count = BaReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id])->count();
  84. if ($count > 10) {
  85. $this->addError('id', '最多只能添加10个收货地址');
  86. return $parentResult;
  87. }
  88. }
  89. if ($this->id) {
  90. $this->_model = BaReceiveAddress::findOne(["ID" => $this->id]);
  91. if (!$this->_model){
  92. $this->addError('id', '地址不存在');
  93. return $parentResult;
  94. }
  95. if ($this->_model['USER_ID'] != \Yii::$app->user->id){
  96. $this->addError('id', '无权修改此地址');
  97. return $parentResult;
  98. }
  99. } else {
  100. $this->_model = new BaReceiveAddress();
  101. }
  102. }
  103. return $parentResult;
  104. }
  105. /**
  106. * 添加编辑
  107. * @return BaReceiveAddress|null
  108. * @throws \yii\db\Exception
  109. */
  110. public function edit(){
  111. if(!$this->validate()){
  112. return null;
  113. }
  114. $db = \Yii::$app->db;
  115. $transaction = $db->beginTransaction();
  116. try {
  117. // 如果设置了此项为默认则清空所有默认
  118. if ($this->isDefault) {
  119. BaReceiveAddress::updateAll(['IS_DEFAULT' => 0], 'USER_ID=:USER_ID', [':USER_ID' => \Yii::$app->user->id]);
  120. }
  121. if($this->scenario == 'userAdd'){
  122. $this->_model->USER_ID = \Yii::$app->user->id;
  123. $this->_model->USER_NAME = Info::getUserNameByUserId(\Yii::$app->user->id); // TODO:
  124. $this->_model->CONSIGNEE = $this->consignee;
  125. $this->_model->MOBILE = $this->mobile;
  126. $this->_model->PROVINCE = $this->province;
  127. $this->_model->LGA_NAME = $this->lgaName;
  128. $this->_model->CITY_NAME = $this->cityName;
  129. $this->_model->ADDRESS = $this->address;
  130. $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
  131. $this->_model->CREATED_AT = Date::nowTime();
  132. } elseif($this->scenario == 'userEdit') {
  133. $this->_model->CONSIGNEE = $this->consignee;
  134. $this->_model->MOBILE = $this->mobile;
  135. $this->_model->PROVINCE = $this->province;
  136. $this->_model->LGA_NAME = $this->lgaName;
  137. $this->_model->CITY_NAME = $this->cityName;
  138. $this->_model->ADDRESS = $this->address;
  139. $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
  140. $this->_model->UPDATED_AT = Date::nowTime();
  141. } elseif($this->scenario == 'userIsDefault') {
  142. $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
  143. } else {
  144. throw new Exception('提交场景不存在');
  145. }
  146. if(!$this->_model->save()){
  147. throw new Exception(Form::formatErrorsForApi($this->_model->getErrors()));
  148. }
  149. $transaction->commit();
  150. } catch (Exception $e) {
  151. $transaction->rollBack();
  152. $this->addError('edit', $e->getMessage());
  153. return null;
  154. }
  155. if($this->scenario == 'adminAdd'){
  156. $this->adminOperateLogger->afterInsert($this->_model)->clean()->save([
  157. 'optType' => '添加收货地址',
  158. ]);
  159. } elseif($this->scenario == 'adminEdit') {
  160. $this->adminOperateLogger->afterUpdate($this->_model)->clean()->save([
  161. 'optType' => '编辑收货地址',
  162. ]);
  163. }
  164. return $this->_model;
  165. }
  166. /**
  167. * 删除前
  168. * @param $selected
  169. */
  170. public function beforeDelete($selected) {
  171. $this->adminOperateLogger->fetchClass = Ad::class;
  172. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  173. }
  174. /**
  175. * 删除
  176. * @param $selected
  177. * @throws Exception
  178. */
  179. public function delete($selected) {
  180. $this->adminOperateLogger->clean()->save([
  181. 'optType' => '删除收货地址',
  182. ]);
  183. }
  184. }