ReceiveAddressForm.php 6.6 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 $address;
  26. public $zipCode;
  27. public $isDefault;
  28. /**
  29. * @var ReceiveAddress
  30. */
  31. private $_model;
  32. public function init() {
  33. parent::init();
  34. $this->adminOperateLogger = new UserOperate([
  35. 'fetchClass' => ReceiveAddress::class,
  36. ]);
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['id', 'consignee', 'mobile', 'province', 'city', 'county', 'address', 'zipCode', 'isDefault'], 'trim'],
  45. [['id', 'consignee', 'mobile', 'province', 'city', 'county', 'address'], 'required'],
  46. [['mobile'], 'mobile'],
  47. [['province', 'city', 'county'], 'exist', 'targetClass' => Region::class, 'targetAttribute' => 'REGION_CODE'],
  48. ];
  49. }
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'consignee' => '收货人',
  54. 'mobile' => '手机号',
  55. 'province' => '省/市',
  56. 'city' => '市/区',
  57. 'county' => '区/县',
  58. 'address' => '详细地址',
  59. 'zipCode' => '邮政编码',
  60. ];
  61. }
  62. /**
  63. * 指定校验场景
  64. * @return array
  65. */
  66. public function scenarios()
  67. {
  68. $parentScenarios = parent::scenarios();
  69. $customScenarios = [
  70. 'userAdd' => ['consignee', 'mobile', 'province', 'city', 'county', 'address', 'zipCode', 'isDefault'],
  71. 'userEdit' => ['id', 'consignee', 'mobile', 'province', 'city', 'county', 'address', 'zipCode', 'isDefault'],
  72. 'userIsDefault' => ['id', 'isDefault'],
  73. ];
  74. return array_merge($parentScenarios, $customScenarios);
  75. }
  76. /**
  77. * 校验之前
  78. * @return bool
  79. */
  80. public function beforeValidate()
  81. {
  82. $parentResult = parent::beforeValidate();
  83. if ($this->scenario == 'userAdd' || $this->scenario == 'userEdit' || $this->scenario == 'userIsDefault') {
  84. if ($this->scenario == 'userAdd') {
  85. $count = ReceiveAddress::find()->where('USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id])->count();
  86. if ($count > 10) {
  87. $this->addError('id', '最多只能添加10个收货地址');
  88. return $parentResult;
  89. }
  90. }
  91. if ($this->id) {
  92. $this->_model = ReceiveAddress::findOne(["ID"=>$this->id]);
  93. if (!$this->_model){
  94. $this->addError('id', '地址不存在');
  95. return $parentResult;
  96. }
  97. if ($this->_model['USER_ID'] != \Yii::$app->user->id){
  98. $this->addError('id', '无权修改此地址');
  99. return $parentResult;
  100. }
  101. } else {
  102. $this->_model = new ReceiveAddress();
  103. }
  104. }
  105. return $parentResult;
  106. }
  107. /**
  108. * 添加编辑
  109. * @return ReceiveAddress|null
  110. * @throws \yii\db\Exception
  111. */
  112. public function edit(){
  113. if(!$this->validate()){
  114. return null;
  115. }
  116. $db = \Yii::$app->db;
  117. $transaction = $db->beginTransaction();
  118. try {
  119. // 如果设置了此项为默认则清空所有默认
  120. if ($this->isDefault) {
  121. ReceiveAddress::updateAll(['IS_DEFAULT' => 0], 'USER_ID=:USER_ID', [':USER_ID'=>\Yii::$app->user->id]);
  122. }
  123. if($this->scenario == 'userAdd'){
  124. $this->_model->USER_ID = \Yii::$app->user->id;
  125. $this->_model->USER_NAME = Info::getUserNameByUserId(\Yii::$app->user->id);
  126. $this->_model->CONSIGNEE = $this->consignee;
  127. $this->_model->MOBILE = $this->mobile;
  128. $this->_model->PROVINCE = $this->province;
  129. $this->_model->CITY = $this->city;
  130. $this->_model->COUNTY = $this->county;
  131. $this->_model->ADDRESS = $this->address;
  132. $this->_model->ZIP_CODE = $this->zipCode;
  133. $this->_model->IS_DEFAULT = $this->isDefault ? 1 : 0;
  134. $this->_model->CREATED_AT = Date::nowTime();
  135. } elseif($this->scenario == 'userEdit') {
  136. $this->_model->CONSIGNEE = $this->consignee;
  137. $this->_model->MOBILE = $this->mobile;
  138. $this->_model->PROVINCE = $this->province;
  139. $this->_model->CITY = $this->city;
  140. $this->_model->COUNTY = $this->county;
  141. $this->_model->ADDRESS = $this->address;
  142. $this->_model->ZIP_CODE = $this->zipCode;
  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('提交场景不存在');
  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' => '添加收货地址',
  162. ]);
  163. } elseif($this->scenario == 'adminEdit') {
  164. $this->adminOperateLogger->afterUpdate($this->_model)->clean()->save([
  165. 'optType' => '编辑收货地址',
  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' => '删除收货地址',
  186. ]);
  187. }
  188. }