NetMoveForm.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\helpers\Form;
  6. use common\helpers\NetPoint;
  7. use common\helpers\user\Info;
  8. use common\libs\logging\operate\AdminOperate;
  9. use common\models\Period;
  10. use common\models\UserBind;
  11. use common\models\UserInfo;
  12. use common\models\UserMove;
  13. use common\models\UserNetwork;
  14. use common\models\UserRelation;
  15. use yii\db\Exception;
  16. /**
  17. * Login form
  18. */
  19. class NetMoveForm extends Model {
  20. public $moveId;
  21. public $selected;
  22. public $moveUserName;
  23. public $toConUserName;
  24. public $toConRealName;
  25. public $location;
  26. public $type;
  27. public $remark;
  28. public $auditStatus;
  29. private $_model;
  30. private $_moveUserId;
  31. private $_toConUserId;
  32. private $_moveUserInfo;
  33. private $_fromConUserId;
  34. private $_fromConUserInfo;
  35. private $_fromLocation;
  36. public function init() {
  37. parent::init();
  38. $this->adminOperateLogger = new AdminOperate([
  39. 'fetchClass' => UserMove::class,
  40. ]);
  41. }
  42. /**
  43. * @inheritdoc
  44. */
  45. public function rules() {
  46. return [
  47. [['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark', 'auditStatus'], 'trim'],
  48. [['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark', 'auditStatus'], 'required'],
  49. [['moveId'], 'exist', 'targetClass' => UserMove::class, 'targetAttribute' => 'ID', 'message' => 'The application to move the net does not exist'], // 移网申请不存在
  50. [['moveId'], 'initModel'],
  51. [['moveUserName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME', 'message' => 'The member to move does not exist'],// 要移动的会员不存在
  52. [['location'], 'integer'],
  53. [['location'], 'isLocation'],
  54. [['type'], 'isType'],
  55. [['moveUserName'], 'isMoveUser'],
  56. [['toConUserName'], 'isToUser'],
  57. [['auditStatus'], 'isStatus'],
  58. ];
  59. }
  60. /**
  61. * 指定场景
  62. * @return array
  63. */
  64. public function scenarios() {
  65. $parentScenarios = parent::scenarios();
  66. $customScenarios = [
  67. 'add' => ['moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark'],
  68. 'changeAudit' => ['selected', 'remark', 'auditStatus'],
  69. 'edit' => ['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark'],
  70. 'pass' => ['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark'],
  71. 'async' => ['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type'],
  72. ];
  73. return array_merge($parentScenarios, $customScenarios);
  74. }
  75. /**
  76. * @return array
  77. */
  78. public function attributeLabels() {
  79. return [
  80. 'moveId' => 'Id of the network transfer application',// 移网申请Id
  81. 'moveUserName' => 'The member code to be moved',// 要移动的会员编号
  82. 'toConUserName' => 'The member code to which to move',//移动到的会员编号
  83. 'toConRealName' => 'Member name to move to',//移动到的会员姓名
  84. 'location' => 'Location',// 位置
  85. 'type' => 'Moving network type',// 移网类型
  86. 'remark' => 'Note',// 备注
  87. 'auditStatus' => 'State',// 状态
  88. ];
  89. }
  90. /**
  91. * 初始化model类
  92. * @param $attribute
  93. */
  94. public function initModel($attribute) {
  95. $model = $this->_model = UserMove::findOne(['ID' => $this->moveId]);
  96. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) {
  97. $this->addError($attribute, 'The application has been reviewed and cannot be reviewed again');// 该申请已经被审核,不能重复审核
  98. }
  99. }
  100. /**
  101. * 校验区位是否正确
  102. * @param $attribute
  103. */
  104. public function isLocation($attribute) {
  105. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  106. if (!in_array($this->location, [1, 2])) {
  107. $this->addError($attribute, 'Incorrect location');//区位不正确
  108. }
  109. }
  110. }
  111. /**
  112. * 移网类型校验
  113. * @param $attribute
  114. */
  115. public function isType($attribute) {
  116. if (!in_array($this->type, [NetPoint::NET_TYPE_NETWORK, NetPoint::NET_TYPE_RELATION])) {
  117. $this->addError($attribute, 'The type of moving net is incorrect');// 移网类型不正确
  118. }
  119. }
  120. /**
  121. * 校验移点位的一些细节要求
  122. * @param $attribute
  123. * @param $params
  124. * @throws Exception
  125. * @throws \yii\base\Exception
  126. */
  127. public function isMoveUser($attribute, $params) {
  128. if ($this->moveUserName == $this->toConUserName) {
  129. $this->addError($attribute, 'Cannot move to its own point');//不能移动到自己点位下
  130. }
  131. $this->_moveUserId = Info::getUserIdByUserName($this->moveUserName);
  132. $this->_moveUserInfo = Info::baseInfo($this->_moveUserId);
  133. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  134. $this->_fromConUserId = $this->_moveUserInfo['CON_UID'];
  135. } else {
  136. $this->_fromConUserId = $this->_moveUserInfo['REC_UID'];
  137. }
  138. $this->_fromConUserInfo = Info::baseInfo($this->_fromConUserId);
  139. $this->_toConUserId = Info::getUserIdByUserName($this->toConUserName);
  140. if (!$this->_fromConUserInfo) {
  141. $this->addError($attribute, 'The original node member does not exist. The member may be a top-level member and cannot be moved.');// 原节点会员不存在,可能该会员是顶层会员不可移动
  142. }
  143. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  144. $this->_fromLocation = UserNetwork::getLocation($this->_moveUserId, $this->_fromConUserId);
  145. }
  146. // 原接点和新接点是否同一个
  147. if ($this->_fromConUserId == $this->_toConUserId) {
  148. if($this->type == NetPoint::NET_TYPE_RELATION){
  149. $this->addError($attribute, 'The node position does not change');// 节点位置没有发生变化
  150. }
  151. elseif($this->type == NetPoint::NET_TYPE_NETWORK){
  152. // 查看原区位
  153. if($this->_fromLocation == $this->location){
  154. $this->addError($attribute, 'The node position does not change');// 节点位置没有发生变化
  155. }
  156. }
  157. }
  158. // 要求开拓网新接点人必须是自己的上级(从自己的父级线中能找到新接点人)
  159. // if ($this->type == NetPoint::NET_TYPE_RELATION) {
  160. // if(!UserNetwork::isParentUser($this->_moveUserId, $this->_toConUserId)){
  161. // $this->addError($attribute, '修改后的开拓者编号不在移动会员安置网所有上级编号中');
  162. // }
  163. // //此会员的上级必须在新开拓者的安置网下
  164. // if(($this->_moveUserInfo['CON_UID']!=$this->_toConUserId)&&!UserNetwork::isParentUser($this->_moveUserInfo['CON_UID'],$this->_toConUserId)){
  165. // $this->addError($attribute, '会员的上级必须在新开拓者的安置网下');
  166. // }
  167. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  168. // 查看新节点的下面人数是否已经达到上限
  169. $allChildUser = UserNetwork::getFirstFloorChildren($this->_toConUserId);
  170. if ($allChildUser) {
  171. $isLocation = [1 => false, 2 => false, 3 => false];
  172. foreach ($allChildUser as $child) {
  173. $isLocation[$child['RELATIVE_LOCATION']] = true;
  174. if ($child['RELATIVE_LOCATION'] == $this->location) {
  175. $this->addError($attribute, 'The new node already has a member for this location');// 新接点人的此区位已经存在会员
  176. }
  177. }
  178. // 第二区不能空点,必须要有会员才能注册到第三区
  179. /*if($this->location == 3 && $isLocation[2] === false){
  180. $this->addError($attribute, '新接点人第二区没有安置会员,无法安置到第三区');
  181. }*/
  182. } else {
  183. /*if($this->location != 1){
  184. $this->addError($attribute, '新接点人左区没有点位,必须放在其左区');
  185. }*/
  186. }
  187. }
  188. // 新节点不能是自己节点的下级
  189. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  190. $modelClass = UserNetwork::class;
  191. } else {
  192. $modelClass = UserRelation::class;
  193. }
  194. if ($modelClass::isParentUser($this->_toConUserId, $this->_moveUserId)) {
  195. $this->addError($attribute, 'Cannot be moved to the sub-member point of the member');// 不能移动到该会员的子会员点位下
  196. }
  197. }
  198. /**
  199. * 移动到的会员账号姓名判断
  200. * @param $attribute
  201. * @throws \yii\base\Exception
  202. */
  203. public function isToUser($attribute) {
  204. //移动到的节点会员不存在
  205. $toUser = Info::baseInfo($this->_toConUserId);
  206. if ($toUser['REAL_NAME'] != $this->toConRealName) {
  207. $this->addError($attribute, 'The code and name of the member you moved to do not match');// 移动到的会员账号和姓名不匹配
  208. }
  209. }
  210. /**
  211. * 校验状态
  212. * @param $attribute
  213. */
  214. public function isStatus($attribute) {
  215. // 获取当前提现单的状态
  216. $oneData = UserMove::findOneAsArray(['ID' => $this->moveId]);
  217. switch ($this->auditStatus) {
  218. case 'un':
  219. $this->addError($attribute, 'Cannot be set to pending review' . $this->auditStatus); // 不能设置为待审核状态
  220. break;
  221. case 'true':
  222. if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) {
  223. $this->addError($attribute, 'The application has been passed and cannot be reviewed again'); // 已经审核通过不能重复审核
  224. } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) {
  225. $this->addError($attribute, 'The application has been rejected and cannot be reviewed again'); // 已经审核拒绝不能重复审核
  226. }
  227. break;
  228. case 'reject':
  229. if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) {
  230. $this->addError($attribute, 'The application has been passed and cannot be reviewed again');// 已经审核通过不能重复审核
  231. } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) {
  232. $this->addError($attribute, 'The application has been rejected and cannot be reviewed again');// 已经审核拒绝不能重复审核
  233. }
  234. break;
  235. default:
  236. $this->addError($attribute, 'Status parameter error'); // 状态参数有误
  237. }
  238. }
  239. /**
  240. * 添加移网申请记录
  241. * @return UserMove|null
  242. * @throws Exception
  243. */
  244. public function add() {
  245. if (!$this->validate()) {
  246. return null;
  247. }
  248. $db = \Yii::$app->db;
  249. $transaction = $db->beginTransaction();
  250. try {
  251. $period = Period::instance();
  252. $model = new UserMove();
  253. $model->USER_ID = $this->_moveUserId;
  254. $model->FROM_UID = $this->_fromConUserId;
  255. $model->TO_UID = $this->_toConUserId;
  256. $model->NET_TABLE_NAME = strtoupper($this->type) . '_' . \date('YmdHis', Date::nowTime()) . rand(100, 999);
  257. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  258. $model->TYPE = UserMove::TYPE_NETWORK;
  259. $model->LOCATION = $this->location;
  260. } elseif ($this->type == NetPoint::NET_TYPE_RELATION) {
  261. $model->TYPE = UserMove::TYPE_RELATION;
  262. } else {
  263. throw new Exception('The type of moving net is incorrect'); // 错误的移网类型
  264. }
  265. $model->PERIOD_NUM = $period->getNowPeriodNum();
  266. $model->MOVE_ADMIN_ID = \Yii::$app->user->id;
  267. $model->CREATE_REMARK = $this->remark;
  268. $model->CREATED_AT = Date::nowTime();
  269. if (!$model->save()) {
  270. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  271. }
  272. $transaction->commit();
  273. } catch (Exception $e) {
  274. $transaction->rollBack();
  275. $this->addError('add', $e->getMessage());
  276. return null;
  277. }
  278. $this->adminOperateLogger->afterInsert($model)->clean()->save([
  279. 'optType' => 'Apply for moving network', // 申请移网
  280. 'userId' => $model->USER_ID,
  281. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  282. 'remark' => $this->remark
  283. ]);
  284. return $model;
  285. }
  286. /**
  287. * 修改信息
  288. * @return null
  289. * @throws \yii\db\Exception
  290. */
  291. public function edit() {
  292. //不再可以单独修改
  293. return null;
  294. if (!$this->validate()) {
  295. return null;
  296. }
  297. if( !$this->_checkMovingData() ) {
  298. return null;
  299. }
  300. $db = \Yii::$app->db;
  301. $transaction = $db->beginTransaction();
  302. try {
  303. $model = $this->_model;
  304. if ($model->IS_MOVING == 1) {
  305. throw new Exception('This application is being transferred to the net and cannot be edited repeatedly');// 该申请正在移网操作,不能重复编辑
  306. }
  307. $model->USER_ID = $this->_moveUserId;
  308. $model->FROM_UID = $this->_fromConUserId;
  309. $model->TO_UID = $this->_toConUserId;
  310. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  311. $model->TYPE = UserMove::TYPE_NETWORK;
  312. $model->LOCATION = $this->location;
  313. } elseif ($this->type == NetPoint::NET_TYPE_RELATION) {
  314. $model->TYPE = UserMove::TYPE_RELATION;
  315. } else {
  316. throw new Exception('The type of moving net is incorrect'); // 类型不存在
  317. }
  318. $model->CREATE_REMARK = $this->remark;
  319. if (!$model->save()) {
  320. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  321. }
  322. $transaction->commit();
  323. } catch (Exception $e) {
  324. $transaction->rollBack();
  325. $this->addError('edit', $e->getMessage());
  326. return null;
  327. }
  328. return $model;
  329. }
  330. /**
  331. * 审核通过
  332. * @return null
  333. * @throws \yii\db\Exception
  334. */
  335. public function pass() {
  336. if (!$this->validate()) {
  337. return null;
  338. }
  339. if( !$this->_checkMovingData() ) {
  340. return null;
  341. }
  342. $this->adminOperateLogger->beforeUpdate($this->_model);
  343. $db = \Yii::$app->db;
  344. $transaction = $db->beginTransaction();
  345. try {
  346. $model = $this->_model;
  347. if ($model->IS_MOVING == 1) {
  348. throw new Exception('This application is being transferred to the net and cannot be reviewed again'); // 该申请正在移网操作,不能重复审核
  349. }
  350. $model->USER_ID = $this->_moveUserId;
  351. $model->FROM_UID = $this->_fromConUserId;
  352. $model->TO_UID = $this->_toConUserId;
  353. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  354. $model->TYPE = UserMove::TYPE_NETWORK;
  355. $model->LOCATION = $this->location;
  356. } elseif ($this->type == NetPoint::NET_TYPE_RELATION) {
  357. $model->TYPE = UserMove::TYPE_RELATION;
  358. } else {
  359. throw new Exception('The type of moving net is incorrect'); // 类型不存在
  360. }
  361. if (!$model->save()) {
  362. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  363. }
  364. $transaction->commit();
  365. } catch (Exception $e) {
  366. $transaction->rollBack();
  367. $this->addError('edit', $e->getMessage());
  368. return null;
  369. }
  370. $this->adminOperateLogger->afterUpdate($model)->clean()->save([
  371. 'optType' => 'Transfer net approved', // 审核通过移网
  372. 'userId' => $model->USER_ID,
  373. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  374. 'remark' => $this->remark??''
  375. ]);
  376. $oneMove = UserMove::find()->select('UM.*, UMI.USER_NAME AS MOVE_USER_NAME, UTI.USER_NAME AS TO_USER_NAME')->from(UserMove::tableName() . ' AS UM')->join('LEFT JOIN', UserInfo::tableName() . ' AS UMI', 'UM.USER_ID=UMI.USER_ID')->join('LEFT JOIN', UserInfo::tableName() . ' AS UTI', 'UM.TO_UID=UTI.USER_ID')->where('UM.ID=:ID', [':ID' => $this->moveId])->asArray()->one();
  377. if (!$oneMove) {
  378. $this->addError('audit', 'The application to move the net does not exist'); // 移网申请不存在
  379. return null;
  380. }
  381. if ($oneMove['AUDIT_STATUS'] > 0) {
  382. $this->addError('audit', 'The application has been reviewed and cannot be reviewed again');// 已审核过,不能审核
  383. return null;
  384. }
  385. $this->moveUserName = $oneMove['MOVE_USER_NAME'];
  386. $this->toConUserName = $oneMove['TO_USER_NAME'];
  387. if ($oneMove['TYPE'] === UserMove::TYPE_NETWORK) {
  388. $this->type = NetPoint::NET_TYPE_NETWORK;
  389. } elseif ($oneMove['TYPE'] === UserMove::TYPE_RELATION) {
  390. $this->type = NetPoint::NET_TYPE_RELATION;
  391. }
  392. $this->location = $oneMove['LOCATION'];
  393. return $this->moveWebToAsync();
  394. }
  395. protected function _checkMovingData()
  396. {
  397. $oneData = UserMove::find()->where('IS_MOVING=:IS_MOVING AND AUDIT_STATUS=:AUDIT_STATUS', [
  398. 'IS_MOVING' => 1,
  399. 'AUDIT_STATUS' => \Yii::$app->params['auditStatus']['un']['value']
  400. ])->asArray()->one();
  401. if( $oneData ) {
  402. $this->addError('edit', 'There is data being moved, please operate later.'); // 有正在移网的数据,请稍后再操作。
  403. return false;
  404. }
  405. return true;
  406. }
  407. /**
  408. * 审核移网
  409. * @return null
  410. * @throws \yii\db\Exception
  411. */
  412. public function changeAudit() {
  413. if (!$this->validate()) {
  414. return null;
  415. }
  416. $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->selected,'ID');
  417. $period = Period::instance();
  418. //拒绝审核
  419. if ($this->auditStatus == 'reject') {
  420. $db = \Yii::$app->db;
  421. $transaction = $db->beginTransaction();
  422. try {
  423. foreach ($this->selected as $select) {
  424. $model = UserMove::findOne(['ID' => $select]);
  425. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) continue;
  426. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  427. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  428. $model->CREATE_REMARK = $this->remark;
  429. $model->AUDIT_PERIOD_NUM = $period->getNowPeriodNum();
  430. $model->AUDITED_AT = Date::nowTime();
  431. if (!$model->save()) {
  432. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  433. }
  434. }
  435. $transaction->commit();
  436. } catch (Exception $e) {
  437. $transaction->rollBack();
  438. $this->addError('changeAudit', $e->getMessage());
  439. return null;
  440. }
  441. } //审核通过
  442. elseif ($this->auditStatus == 'true') {
  443. foreach ($this->selected as $select) {
  444. $model = UserMove::findOne(['ID' => $select]);
  445. if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) continue;
  446. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value'];
  447. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  448. $model->CREATE_REMARK = $this->remark;
  449. $model->AUDIT_PERIOD_NUM = $period->getNowPeriodNum();
  450. $model->AUDITED_AT = Date::nowTime();
  451. if (!$model->save()) {
  452. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  453. }
  454. $oneMove = UserMove::find()->select('UM.*, UMI.USER_NAME AS MOVE_USER_NAME, UTI.USER_NAME AS TO_USER_NAME')->from(UserMove::tableName() . ' AS UM')->join('LEFT JOIN', UserInfo::tableName() . ' AS UMI', 'UM.USER_ID=UMI.USER_ID')->join('LEFT JOIN', UserInfo::tableName() . ' AS UTI', 'UM.TO_UID=UTI.USER_ID')->where('UM.ID=:ID', [':ID' => $this->moveId])->asArray()->one();
  455. if (!$oneMove) continue;
  456. if ($oneMove['AUDIT_STATUS'] > 0) continue;
  457. $this->moveUserName = $oneMove['MOVE_USER_NAME'];
  458. $this->toConUserName = $oneMove['TO_USER_NAME'];
  459. if ($oneMove['TYPE'] === UserMove::TYPE_NETWORK) {
  460. $this->type = NetPoint::NET_TYPE_NETWORK;
  461. } elseif ($oneMove['TYPE'] === UserMove::TYPE_RELATION) {
  462. $this->type = NetPoint::NET_TYPE_RELATION;
  463. }
  464. $this->location = $oneMove['LOCATION'];
  465. $this->moveWebToAsync();
  466. }
  467. } else {
  468. $this->addError('audit', 'The wrong way to audit' . $this->auditStatus); // 错误的审核方式
  469. }
  470. $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->selected,'ID')->clean()->save([
  471. 'optType' => '审核移网',
  472. 'remark' => $this->remark??'',
  473. ]);
  474. return true;
  475. }
  476. /**
  477. * 拒绝审核
  478. * @return null|static
  479. * @throws \yii\db\Exception
  480. */
  481. /*public function reject() {
  482. if (!$this->validate()) {
  483. return null;
  484. }
  485. $db = \Yii::$app->db;
  486. $transaction = $db->beginTransaction();
  487. try {
  488. //todo 是否这么判断未审核的记录
  489. if (!$model = UserMove::findOne(['ID' => $this->moveId, 'AUDIT_STATUS' => \Yii::$app->params['auditStatus']['un']['value']])) {
  490. throw new Exception('移网记录不存在或已处理');
  491. }
  492. $model->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  493. $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['reject']['value'];
  494. $model->AUDIT_REMARK = $this->remark;
  495. $model->AUDITED_AT = Date::nowTime();
  496. if (!$model->save()) {
  497. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  498. }
  499. $transaction->commit();
  500. } catch (Exception $e) {
  501. $transaction->rollBack();
  502. $this->addError($e->getMessage());
  503. return null;
  504. }
  505. return $model;
  506. }*/
  507. /**
  508. * 审核移网
  509. * @return null
  510. */
  511. /*public function moveAudit() {
  512. $oneMove = UserMove::find()->select('UM.*, UMI.USER_NAME AS MOVE_USER_NAME, UTI.USER_NAME AS TO_USER_NAME')->from(UserMove::tableName() . ' AS UM')->join('LEFT JOIN', UserInfo::tableName() . ' AS UMI', 'UM.USER_ID=UMI.USER_ID')->join('LEFT JOIN', UserInfo::tableName() . ' AS UTI', 'UM.TO_UID=UTI.USER_ID')->where('UM.ID=:ID', [':ID' => $this->moveId])->asArray()->one();
  513. if (!$oneMove) {
  514. $this->addError('audit', '移网申请不存在');
  515. return null;
  516. }
  517. if ($oneMove['AUDIT_STATUS'] > 0) {
  518. $this->addError('audit', '已审核过,不能审核');
  519. return null;
  520. }
  521. $this->moveUserName = $oneMove['MOVE_USER_NAME'];
  522. $this->toConUserName = $oneMove['TO_USER_NAME'];
  523. if ($oneMove['TYPE'] === UserMove::TYPE_NETWORK) {
  524. $this->type = NetPoint::NET_TYPE_NETWORK;
  525. } elseif ($oneMove['TYPE'] === UserMove::TYPE_RELATION) {
  526. $this->type = NetPoint::NET_TYPE_RELATION;
  527. }
  528. $this->location = $oneMove['LOCATION'];
  529. return $this->moveWebToAsync();
  530. }*/
  531. /**
  532. * 页面端到异步的移网
  533. * @return null
  534. */
  535. public function moveWebToAsync() {
  536. if (!$this->validate()) {
  537. return null;
  538. }
  539. $period = Period::instance();
  540. $moveModel = UserMove::findOne(['ID' => $this->moveId]);
  541. if ($moveModel->IS_MOVING == 1) {
  542. $this->addError('moveWebToAsync', 'Please wait while it is moving'); // 正在移动中请稍候
  543. return null;
  544. }
  545. $moveModel->IS_MOVING = 1;
  546. $moveModel->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  547. $moveModel->CREATE_REMARK = $this->remark;
  548. $moveModel->AUDIT_PERIOD_NUM = $period->getNowPeriodNum();
  549. $moveModel->STARTED_AT = Date::nowTime();
  550. $moveModel->AUDITED_AT = Date::nowTime();
  551. if (!$moveModel->save()) {
  552. $this->addError('scenario', Form::formatErrorsForApi($moveModel->getErrors()));
  553. return null;
  554. }
  555. if ($moveModel->TYPE === NetPoint::NET_TYPE_NETWORK) {
  556. $route = 'user/move-network';
  557. } elseif ($moveModel->TYPE === NetPoint::NET_TYPE_RELATION) {
  558. $route = 'user/move-relation';
  559. } else {
  560. $this->addError('scenario', 'The scene does not exist'); // 场景不存在
  561. return null;
  562. }
  563. // 异步处理添加任务
  564. $params = [
  565. 'moveId' => $this->moveId,
  566. 'moveUserName' => $this->moveUserName,
  567. 'toConUserName' => $this->toConUserName,
  568. 'toConRealName' => $this->toConRealName,
  569. 'location' => $this->location,
  570. 'type' => $this->type,
  571. 'remark' => $this->remark,
  572. ];
  573. $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle($route, $params);
  574. if ($taskKey === false) {
  575. $this->errorHandle();
  576. $this->addError('send', 'The request failed'); // 请求失败
  577. return null;
  578. }
  579. return $moveModel;
  580. }
  581. /**
  582. * 异步移网
  583. * @return bool|null
  584. * @throws \Exception
  585. */
  586. public function moveAsync() {
  587. if (!$this->validate()) {
  588. $this->errorHandle();
  589. return false;
  590. }
  591. return $this->moveNet();
  592. }
  593. /**
  594. * 移网
  595. * @return bool
  596. * @throws \Exception
  597. */
  598. public function moveNet() {
  599. $netPoint = NetPoint::factory($this->moveId);
  600. if ($this->type == NetPoint::NET_TYPE_NETWORK) {
  601. $moveResult = $netPoint->moveNetworkPoint($this->_moveUserId, $this->_toConUserId, $this->location, $this->_fromLocation);
  602. } elseif ($this->type == NetPoint::NET_TYPE_RELATION) {
  603. $moveResult = $netPoint->moveRelationPoint($this->_moveUserId, $this->_toConUserId);
  604. } else {
  605. $this->addError('netType', 'The type of moving net is incorrect'); // 移网的网络类型错误
  606. unset($netPoint);
  607. return null;
  608. }
  609. if ($moveResult) {
  610. unset($netPoint, $moveResult);
  611. return true;
  612. } else {
  613. $this->errorHandle();
  614. $this->addErrors($netPoint->getErrors());
  615. unset($netPoint, $moveResult);
  616. return false;
  617. }
  618. }
  619. public function errorHandle(){
  620. // 把移网的审核状态回归
  621. $oneMove = UserMove::findOne(['ID'=>$this->moveId]);
  622. $oneMove->IS_MOVING = 0;
  623. $oneMove->AUDIT_ADMIN_ID = null;
  624. $oneMove->AUDITED_AT = 0;
  625. $oneMove->AUDIT_STATUS = 0;
  626. $oneMove->save();
  627. }
  628. /**
  629. * 删除移网
  630. * @param $selected
  631. */
  632. public function beforeDelete($selected) {
  633. $this->adminOperateLogger->setIsBatch(true)->setOptObjField('USER_ID')->beforeDelete($selected, 'ID');
  634. }
  635. /**
  636. * 删除
  637. * @param $selected
  638. * @throws Exception
  639. */
  640. public function delete($selected) {
  641. $this->adminOperateLogger->clean()->save([
  642. 'optType' => 'Delete move network', // 删除移网
  643. ]);
  644. }
  645. }