UserCloseForm.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\Status;
  7. use common\models\Period;
  8. use common\models\User;
  9. use common\models\UserClose;
  10. use common\models\UserInfo;
  11. use yii\base\Exception;
  12. /**
  13. * Login form
  14. */
  15. class UserCloseForm extends Model {
  16. public $id;
  17. public $userName;
  18. public $type;
  19. public $isClose;
  20. public $remark;
  21. public $auditStatus;
  22. private $_userId;
  23. private $_userCloseModel;
  24. private $_periodNum;
  25. private $_closeInfo = [
  26. UserClose::TYPE_GT => '关停',
  27. UserClose::TYPE_TF => '停发',
  28. ];
  29. /**
  30. * @inheritdoc
  31. */
  32. public function rules() {
  33. return [
  34. [['id', 'userName', 'type', 'isClose', 'remark', 'auditStatus'], 'trim'],
  35. [['id', 'userName', 'type', 'isClose'], 'required'],
  36. [['userName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME'],
  37. [['id', 'userName'], 'initModel'],
  38. [['id'], 'exist', 'targetClass' => UserClose::class, 'targetAttribute' => 'ID'],
  39. [['type'], 'isType'],
  40. [['isClose'], 'checkClose'],
  41. [['auditStatus'], 'isAuditStatus'],
  42. ];
  43. }
  44. /**
  45. * 指定校验场景
  46. * @return array
  47. */
  48. public function scenarios() {
  49. $parentScenarios = parent::scenarios();
  50. $customScenarios = [
  51. // 手动申请
  52. 'add' => ['userName', 'type', 'isClose', 'remark'],
  53. // 自动关停
  54. 'auto' => ['userName', 'type', 'isClose', 'remark'],
  55. // 审核关停
  56. 'audit' => ['id', 'auditStatus']
  57. ];
  58. return array_merge($parentScenarios, $customScenarios);
  59. }
  60. public function attributeLabels() {
  61. return [
  62. 'id' => 'ID',
  63. 'userName' => '会员编号',
  64. 'type' => '类型',
  65. 'isClose' => '是否关停',
  66. 'remark' => '备注',
  67. ];
  68. }
  69. /**
  70. * 初始化model和userId
  71. * @param $attribute
  72. */
  73. public function initModel($attribute) {
  74. $period = Period::instance();
  75. $this->_periodNum = $period->getNowPeriodNum();
  76. if ($this->scenario == 'audit') {
  77. $this->_userCloseModel = UserClose::findOne(['ID' => $this->id]);
  78. $this->_userId = $this->_userCloseModel->USER_ID;
  79. } elseif ($this->scenario == 'add' || $this->scenario == 'auto') {
  80. $this->_userCloseModel = new UserClose();
  81. $oneUser = UserInfo::findOneAsArray(['USER_NAME' => $this->userName], [], 'USER_ID');
  82. $this->_userId = $oneUser['USER_ID'];
  83. // 查看数据库中是否已经存在了该会员的申请未审核
  84. $oneUserClose = UserClose::findOne(['USER_ID' => $this->_userId, 'AUDIT_STATUS' => \Yii::$app->params['auditStatus']['un']['value'], 'TYPE' => $this->type]);
  85. if ($oneUserClose && $this->scenario == 'add') {
  86. // 不允许重复申请
  87. $this->addError($attribute, '已经存在该会员的待审核数据,请等待审核后再申请');
  88. } elseif ($oneUserClose && $this->scenario == 'auto') {
  89. // 如果是自动关停时,如果遇上未审核的,直接审核拒绝,防止影响自动关停的进程
  90. $oneUserClose->AUDIT_STATUS = \Yii::$app->params['auditStatus']['reject']['value'];
  91. $oneUserClose->AUDITED_AT = Date::nowTime();
  92. $oneUserClose->save();
  93. }
  94. } else {
  95. $this->addError($attribute, '场景不正确');
  96. }
  97. }
  98. /**
  99. * 关停还是停发
  100. * @param $attribute
  101. */
  102. public function isType($attribute) {
  103. if (!in_array($this->type, [UserClose::TYPE_GT, UserClose::TYPE_TF])) {
  104. $this->addError($attribute, '类型不正确');
  105. }
  106. }
  107. /**
  108. * 校验该用户是否已经被关闭
  109. * @param $attribute
  110. */
  111. public function checkClose($attribute) {
  112. if ($this->type == UserClose::TYPE_GT) {
  113. if ($this->isClose == 1 && Status::isCloseAccount($this->_userId)) {
  114. $this->addError($attribute, '该会员已经被关停');
  115. } elseif ($this->isClose == 0 && !Status::isCloseAccount($this->_userId)) {
  116. $this->addError($attribute, '该会员没有被关停');
  117. }
  118. } elseif ($this->type == UserClose::TYPE_TF) {
  119. if ($this->isClose == 1 && Status::isCloseBonus($this->_userId)) {
  120. $this->addError($attribute, '该会员已经被停发');
  121. } elseif ($this->isClose == 0 && !Status::isCloseBonus($this->_userId)) {
  122. $this->addError($attribute, '该会员没有被停发');
  123. }
  124. }
  125. }
  126. /**
  127. * 审核状态是否正确
  128. * @param $attributes
  129. */
  130. public function isAuditStatus($attributes) {
  131. if (!array_key_exists($this->auditStatus, \Yii::$app->params['auditStatus'])) {
  132. $this->addError($attributes, '无效的审核状态');
  133. }
  134. }
  135. /**
  136. * 申请
  137. * @return null
  138. * @throws \yii\db\Exception
  139. */
  140. public function edit() {
  141. if (!$this->validate()) {
  142. return null;
  143. }
  144. $db = \Yii::$app->db;
  145. $transaction = $db->beginTransaction();
  146. try {
  147. $model = $this->_userCloseModel;
  148. $model->USER_ID = $this->_userId;
  149. $model->TYPE = $this->type;
  150. $model->PERIOD_NUM = $this->_periodNum;
  151. $model->IS_CLOSE = $this->isClose;
  152. $model->REMARK = $this->remark;
  153. if ($this->scenario == 'add') {
  154. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['un']['value'];
  155. $model->IS_MANUAL = 1;
  156. $model->ADMIN_ID = \Yii::$app->user->id;
  157. } elseif ($this->scenario == 'auto') {
  158. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  159. $model->AUDITED_AT = Date::nowTime();
  160. } else {
  161. $this->addError('setClose', '场景不正确');
  162. }
  163. $model->CREATED_AT = Date::nowTime();
  164. if (!$model->save()) {
  165. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  166. }
  167. // 把商城会员表的状态改为0
  168. $isCloseShopUser = false;
  169. $closeShopStatus = 0;
  170. if ($this->scenario == 'auto' && $this->type == UserClose::TYPE_GT && $this->isClose == 1) {
  171. $isCloseShopUser = true;
  172. $closeShopStatus = 0;
  173. } elseif ($this->scenario == 'auto' && $this->type == UserClose::TYPE_GT && $this->isClose == 0) {
  174. $isCloseShopUser = true;
  175. $closeShopStatus = 1;
  176. }
  177. if ($isCloseShopUser) {
  178. $oneUser = User::findOne(['ID' => $this->_userId]);
  179. $oneUser->STATUS = $closeShopStatus;
  180. if (!$oneUser->save()) {
  181. throw new Exception(Form::formatErrorsForApi($oneUser->getErrors()));
  182. }
  183. }
  184. $transaction->commit();
  185. } catch (Exception $e) {
  186. $transaction->rollBack();
  187. $this->addError('edit', $e->getMessage());
  188. return null;
  189. }
  190. return $model;
  191. }
  192. /**
  193. * 审核
  194. * @return null
  195. * @throws \yii\db\Exception
  196. */
  197. public function audit() {
  198. if (!$this->validate()) {
  199. return null;
  200. }
  201. $db = \Yii::$app->db;
  202. $transaction = $db->beginTransaction();
  203. try {
  204. $model = $this->_userCloseModel;
  205. if ($this->auditStatus == 'true') {
  206. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  207. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  208. $model->AUDITED_AT = Date::nowTime();
  209. if (!$model->save()) {
  210. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  211. }
  212. if ($model->IS_CLOSE) {
  213. $closedAt = Date::nowTime();
  214. } else {
  215. $closedAt = 0;
  216. }
  217. // 修改结算系统会员表的状态
  218. $closeField = UserClose::TYPE_GT ? 'CLOSE_ACCOUNT' : 'CLOSE_BONUS';
  219. UserInfo::updateAll([$closeField => $model->IS_CLOSE, 'CLOSE_ACCOUNT_AT' => $closedAt], 'USER_ID=:USER_ID', [':USER_ID' => $this->_userId]);
  220. // 把商城会员表的状态改为0
  221. $isCloseShopUser = false;
  222. $closeShopStatus = 0;
  223. if ($model->TYPE == UserClose::TYPE_GT && $model->IS_CLOSE == 1) {
  224. $isCloseShopUser = true;
  225. $closeShopStatus = 0;
  226. } elseif ($model->TYPE == UserClose::TYPE_GT && $model->IS_CLOSE == 0) {
  227. $isCloseShopUser = true;
  228. $closeShopStatus = 1;
  229. }
  230. if ($isCloseShopUser) {
  231. $oneUser = User::findOne(['ID' => $this->_userId]);
  232. $oneUser->STATUS = $closeShopStatus;
  233. if (!$oneUser->save()) {
  234. throw new Exception(Form::formatErrorsForApi($oneUser->getErrors()));
  235. }
  236. }
  237. } elseif ($this->auditStatus == 'reject') {
  238. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  239. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['reject']['value'];
  240. $model->AUDITED_AT = Date::nowTime();
  241. if (!$model->save()) {
  242. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  243. }
  244. }
  245. $transaction->commit();
  246. } catch (Exception $e) {
  247. $transaction->rollBack();
  248. $this->addError($e->getMessage());
  249. return null;
  250. }
  251. return $model;
  252. }
  253. }