adminOperateLogger = new AdminOperate([ 'fetchClass' => UserMove::class, ]); } /** * @inheritdoc */ public function rules() { return [ [['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark', 'auditStatus'], 'trim'], [['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark', 'auditStatus'], 'required'], [['moveId'], 'exist', 'targetClass' => UserMove::class, 'targetAttribute' => 'ID', 'message' => 'The application to move the net does not exist'], // 移网申请不存在 [['moveId'], 'initModel'], [['moveUserName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME', 'message' => 'The member to move does not exist'],// 要移动的会员不存在 [['location'], 'integer'], [['location'], 'isLocation'], [['type'], 'isType'], [['moveUserName'], 'isMoveUser'], [['toConUserName'], 'isToUser'], [['auditStatus'], 'isStatus'], ]; } /** * 指定场景 * @return array */ public function scenarios() { $parentScenarios = parent::scenarios(); $customScenarios = [ 'add' => ['moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark'], 'changeAudit' => ['selected', 'remark', 'auditStatus'], 'edit' => ['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark'], 'pass' => ['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type', 'remark'], 'async' => ['moveId', 'moveUserName', 'toConUserName', 'toConRealName', 'location', 'type'], ]; return array_merge($parentScenarios, $customScenarios); } /** * @return array */ public function attributeLabels() { return [ 'moveId' => 'Id of the network transfer application',// 移网申请Id 'moveUserName' => 'The member code to be moved',// 要移动的会员编号 'toConUserName' => 'The member code to which to move',//移动到的会员编号 'toConRealName' => 'Member name to move to',//移动到的会员姓名 'location' => 'Location',// 位置 'type' => 'Moving network type',// 移网类型 'remark' => 'Note',// 备注 'auditStatus' => 'State',// 状态 ]; } /** * 初始化model类 * @param $attribute */ public function initModel($attribute) { $model = $this->_model = UserMove::findOne(['ID' => $this->moveId]); if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) { $this->addError($attribute, 'The application has been reviewed and cannot be reviewed again');// 该申请已经被审核,不能重复审核 } } /** * 校验区位是否正确 * @param $attribute */ public function isLocation($attribute) { if ($this->type == NetPoint::NET_TYPE_NETWORK) { if (!in_array($this->location, [1, 2])) { $this->addError($attribute, 'Incorrect location');//区位不正确 } } } /** * 移网类型校验 * @param $attribute */ public function isType($attribute) { if (!in_array($this->type, [NetPoint::NET_TYPE_NETWORK, NetPoint::NET_TYPE_RELATION])) { $this->addError($attribute, 'The type of moving net is incorrect');// 移网类型不正确 } } /** * 校验移点位的一些细节要求 * @param $attribute * @param $params * @throws Exception * @throws \yii\base\Exception */ public function isMoveUser($attribute, $params) { if ($this->moveUserName == $this->toConUserName) { $this->addError($attribute, 'Cannot move to its own point');//不能移动到自己点位下 } $this->_moveUserId = Info::getUserIdByUserName($this->moveUserName); $this->_moveUserInfo = Info::baseInfo($this->_moveUserId); if ($this->type == NetPoint::NET_TYPE_NETWORK) { $this->_fromConUserId = $this->_moveUserInfo['CON_UID']; } else { $this->_fromConUserId = $this->_moveUserInfo['REC_UID']; } $this->_fromConUserInfo = Info::baseInfo($this->_fromConUserId); $this->_toConUserId = Info::getUserIdByUserName($this->toConUserName); if (!$this->_fromConUserInfo) { $this->addError($attribute, 'The original node member does not exist. The member may be a top-level member and cannot be moved.');// 原节点会员不存在,可能该会员是顶层会员不可移动 } if ($this->type == NetPoint::NET_TYPE_NETWORK) { $this->_fromLocation = UserNetwork::getLocation($this->_moveUserId, $this->_fromConUserId); } // 原接点和新接点是否同一个 if ($this->_fromConUserId == $this->_toConUserId) { if($this->type == NetPoint::NET_TYPE_RELATION){ $this->addError($attribute, 'The node position does not change');// 节点位置没有发生变化 } elseif($this->type == NetPoint::NET_TYPE_NETWORK){ // 查看原区位 if($this->_fromLocation == $this->location){ $this->addError($attribute, 'The node position does not change');// 节点位置没有发生变化 } } } // 要求开拓网新接点人必须是自己的上级(从自己的父级线中能找到新接点人) // if ($this->type == NetPoint::NET_TYPE_RELATION) { // if(!UserNetwork::isParentUser($this->_moveUserId, $this->_toConUserId)){ // $this->addError($attribute, '修改后的开拓者编号不在移动会员安置网所有上级编号中'); // } // //此会员的上级必须在新开拓者的安置网下 // if(($this->_moveUserInfo['CON_UID']!=$this->_toConUserId)&&!UserNetwork::isParentUser($this->_moveUserInfo['CON_UID'],$this->_toConUserId)){ // $this->addError($attribute, '会员的上级必须在新开拓者的安置网下'); // } if ($this->type == NetPoint::NET_TYPE_NETWORK) { // 查看新节点的下面人数是否已经达到上限 $allChildUser = UserNetwork::getFirstFloorChildren($this->_toConUserId); if ($allChildUser) { $isLocation = [1 => false, 2 => false, 3 => false]; foreach ($allChildUser as $child) { $isLocation[$child['RELATIVE_LOCATION']] = true; if ($child['RELATIVE_LOCATION'] == $this->location) { $this->addError($attribute, 'The new node already has a member for this location');// 新接点人的此区位已经存在会员 } } // 第二区不能空点,必须要有会员才能注册到第三区 /*if($this->location == 3 && $isLocation[2] === false){ $this->addError($attribute, '新接点人第二区没有安置会员,无法安置到第三区'); }*/ } else { /*if($this->location != 1){ $this->addError($attribute, '新接点人左区没有点位,必须放在其左区'); }*/ } } // 新节点不能是自己节点的下级 if ($this->type == NetPoint::NET_TYPE_NETWORK) { $modelClass = UserNetwork::class; } else { $modelClass = UserRelation::class; } if ($modelClass::isParentUser($this->_toConUserId, $this->_moveUserId)) { $this->addError($attribute, 'Cannot be moved to the sub-member point of the member');// 不能移动到该会员的子会员点位下 } } /** * 移动到的会员账号姓名判断 * @param $attribute * @throws \yii\base\Exception */ public function isToUser($attribute) { //移动到的节点会员不存在 $toUser = Info::baseInfo($this->_toConUserId); if ($toUser['REAL_NAME'] != $this->toConRealName) { $this->addError($attribute, 'The code and name of the member you moved to do not match');// 移动到的会员账号和姓名不匹配 } } /** * 校验状态 * @param $attribute */ public function isStatus($attribute) { // 获取当前提现单的状态 $oneData = UserMove::findOneAsArray(['ID' => $this->moveId]); switch ($this->auditStatus) { case 'un': $this->addError($attribute, 'Cannot be set to pending review' . $this->auditStatus); // 不能设置为待审核状态 break; case 'true': if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) { $this->addError($attribute, 'The application has been passed and cannot be reviewed again'); // 已经审核通过不能重复审核 } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) { $this->addError($attribute, 'The application has been rejected and cannot be reviewed again'); // 已经审核拒绝不能重复审核 } break; case 'reject': if ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['true']['value']) { $this->addError($attribute, 'The application has been passed and cannot be reviewed again');// 已经审核通过不能重复审核 } elseif ($oneData['AUDIT_STATUS'] == \Yii::$app->params['auditStatus']['reject']['value']) { $this->addError($attribute, 'The application has been rejected and cannot be reviewed again');// 已经审核拒绝不能重复审核 } break; default: $this->addError($attribute, 'Status parameter error'); // 状态参数有误 } } /** * 添加移网申请记录 * @return UserMove|null * @throws Exception */ public function add() { if (!$this->validate()) { return null; } $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $period = Period::instance(); $model = new UserMove(); $model->USER_ID = $this->_moveUserId; $model->FROM_UID = $this->_fromConUserId; $model->TO_UID = $this->_toConUserId; $model->NET_TABLE_NAME = strtoupper($this->type) . '_' . \date('YmdHis', Date::nowTime()) . rand(100, 999); if ($this->type == NetPoint::NET_TYPE_NETWORK) { $model->TYPE = UserMove::TYPE_NETWORK; $model->LOCATION = $this->location; } elseif ($this->type == NetPoint::NET_TYPE_RELATION) { $model->TYPE = UserMove::TYPE_RELATION; } else { throw new Exception('The type of moving net is incorrect'); // 错误的移网类型 } $model->PERIOD_NUM = $period->getNowPeriodNum(); $model->MOVE_ADMIN_ID = \Yii::$app->user->id; $model->CREATE_REMARK = $this->remark; $model->CREATED_AT = Date::nowTime(); if (!$model->save()) { throw new Exception(Form::formatErrorsForApi($model->getErrors())); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); $this->addError('add', $e->getMessage()); return null; } $this->adminOperateLogger->afterInsert($model)->clean()->save([ 'optType' => 'Apply for moving network', // 申请移网 'userId' => $model->USER_ID, 'userName' => Info::getUserNameByUserId($model->USER_ID), 'remark' => $this->remark ]); return $model; } /** * 修改信息 * @return null * @throws \yii\db\Exception */ public function edit() { //不再可以单独修改 return null; if (!$this->validate()) { return null; } if( !$this->_checkMovingData() ) { return null; } $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $model = $this->_model; if ($model->IS_MOVING == 1) { throw new Exception('This application is being transferred to the net and cannot be edited repeatedly');// 该申请正在移网操作,不能重复编辑 } $model->USER_ID = $this->_moveUserId; $model->FROM_UID = $this->_fromConUserId; $model->TO_UID = $this->_toConUserId; if ($this->type == NetPoint::NET_TYPE_NETWORK) { $model->TYPE = UserMove::TYPE_NETWORK; $model->LOCATION = $this->location; } elseif ($this->type == NetPoint::NET_TYPE_RELATION) { $model->TYPE = UserMove::TYPE_RELATION; } else { throw new Exception('The type of moving net is incorrect'); // 类型不存在 } $model->CREATE_REMARK = $this->remark; if (!$model->save()) { throw new Exception(Form::formatErrorsForApi($model->getErrors())); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); $this->addError('edit', $e->getMessage()); return null; } return $model; } /** * 审核通过 * @return null * @throws \yii\db\Exception */ public function pass() { if (!$this->validate()) { return null; } if( !$this->_checkMovingData() ) { return null; } $this->adminOperateLogger->beforeUpdate($this->_model); $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { $model = $this->_model; if ($model->IS_MOVING == 1) { throw new Exception('This application is being transferred to the net and cannot be reviewed again'); // 该申请正在移网操作,不能重复审核 } $model->USER_ID = $this->_moveUserId; $model->FROM_UID = $this->_fromConUserId; $model->TO_UID = $this->_toConUserId; if ($this->type == NetPoint::NET_TYPE_NETWORK) { $model->TYPE = UserMove::TYPE_NETWORK; $model->LOCATION = $this->location; } elseif ($this->type == NetPoint::NET_TYPE_RELATION) { $model->TYPE = UserMove::TYPE_RELATION; } else { throw new Exception('The type of moving net is incorrect'); // 类型不存在 } if (!$model->save()) { throw new Exception(Form::formatErrorsForApi($model->getErrors())); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); $this->addError('edit', $e->getMessage()); return null; } $this->adminOperateLogger->afterUpdate($model)->clean()->save([ 'optType' => 'Transfer net approved', // 审核通过移网 'userId' => $model->USER_ID, 'userName' => Info::getUserNameByUserId($model->USER_ID), 'remark' => $this->remark??'' ]); $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(); if (!$oneMove) { $this->addError('audit', 'The application to move the net does not exist'); // 移网申请不存在 return null; } if ($oneMove['AUDIT_STATUS'] > 0) { $this->addError('audit', 'The application has been reviewed and cannot be reviewed again');// 已审核过,不能审核 return null; } $this->moveUserName = $oneMove['MOVE_USER_NAME']; $this->toConUserName = $oneMove['TO_USER_NAME']; if ($oneMove['TYPE'] === UserMove::TYPE_NETWORK) { $this->type = NetPoint::NET_TYPE_NETWORK; } elseif ($oneMove['TYPE'] === UserMove::TYPE_RELATION) { $this->type = NetPoint::NET_TYPE_RELATION; } $this->location = $oneMove['LOCATION']; return $this->moveWebToAsync(); } protected function _checkMovingData() { $oneData = UserMove::find()->where('IS_MOVING=:IS_MOVING AND AUDIT_STATUS=:AUDIT_STATUS', [ 'IS_MOVING' => 1, 'AUDIT_STATUS' => \Yii::$app->params['auditStatus']['un']['value'] ])->asArray()->one(); if( $oneData ) { $this->addError('edit', 'There is data being moved, please operate later.'); // 有正在移网的数据,请稍后再操作。 return false; } return true; } /** * 审核移网 * @return null * @throws \yii\db\Exception */ public function changeAudit() { if (!$this->validate()) { return null; } $this->adminOperateLogger->setIsBatch(true)->beforeUpdate($this->selected,'ID'); $period = Period::instance(); //拒绝审核 if ($this->auditStatus == 'reject') { $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { foreach ($this->selected as $select) { $model = UserMove::findOne(['ID' => $select]); if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) continue; $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value']; $model->AUDIT_ADMIN_ID = \Yii::$app->user->id; $model->CREATE_REMARK = $this->remark; $model->AUDIT_PERIOD_NUM = $period->getNowPeriodNum(); $model->AUDITED_AT = Date::nowTime(); if (!$model->save()) { throw new Exception(Form::formatErrorsForApi($model->getErrors())); } } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); $this->addError('changeAudit', $e->getMessage()); return null; } } //审核通过 elseif ($this->auditStatus == 'true') { foreach ($this->selected as $select) { $model = UserMove::findOne(['ID' => $select]); if ($model->AUDIT_STATUS > \Yii::$app->params['auditStatus']['un']['value']) continue; $model->AUDIT_STATUS = \Yii::$app->params['auditStatus'][$this->auditStatus]['value']; $model->AUDIT_ADMIN_ID = \Yii::$app->user->id; $model->CREATE_REMARK = $this->remark; $model->AUDIT_PERIOD_NUM = $period->getNowPeriodNum(); $model->AUDITED_AT = Date::nowTime(); if (!$model->save()) { throw new Exception(Form::formatErrorsForApi($model->getErrors())); } $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(); if (!$oneMove) continue; if ($oneMove['AUDIT_STATUS'] > 0) continue; $this->moveUserName = $oneMove['MOVE_USER_NAME']; $this->toConUserName = $oneMove['TO_USER_NAME']; if ($oneMove['TYPE'] === UserMove::TYPE_NETWORK) { $this->type = NetPoint::NET_TYPE_NETWORK; } elseif ($oneMove['TYPE'] === UserMove::TYPE_RELATION) { $this->type = NetPoint::NET_TYPE_RELATION; } $this->location = $oneMove['LOCATION']; $this->moveWebToAsync(); } } else { $this->addError('audit', 'The wrong way to audit' . $this->auditStatus); // 错误的审核方式 } $this->adminOperateLogger->setIsBatch(true)->afterUpdate($this->selected,'ID')->clean()->save([ 'optType' => '审核移网', 'remark' => $this->remark??'', ]); return true; } /** * 拒绝审核 * @return null|static * @throws \yii\db\Exception */ /*public function reject() { if (!$this->validate()) { return null; } $db = \Yii::$app->db; $transaction = $db->beginTransaction(); try { //todo 是否这么判断未审核的记录 if (!$model = UserMove::findOne(['ID' => $this->moveId, 'AUDIT_STATUS' => \Yii::$app->params['auditStatus']['un']['value']])) { throw new Exception('移网记录不存在或已处理'); } $model->AUDIT_ADMIN_ID = \Yii::$app->user->id; $model->AUDIT_STATUS = \Yii::$app->params['auditStatus']['reject']['value']; $model->AUDIT_REMARK = $this->remark; $model->AUDITED_AT = Date::nowTime(); if (!$model->save()) { throw new Exception(Form::formatErrorsForApi($model->getErrors())); } $transaction->commit(); } catch (Exception $e) { $transaction->rollBack(); $this->addError($e->getMessage()); return null; } return $model; }*/ /** * 审核移网 * @return null */ /*public function moveAudit() { $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(); if (!$oneMove) { $this->addError('audit', '移网申请不存在'); return null; } if ($oneMove['AUDIT_STATUS'] > 0) { $this->addError('audit', '已审核过,不能审核'); return null; } $this->moveUserName = $oneMove['MOVE_USER_NAME']; $this->toConUserName = $oneMove['TO_USER_NAME']; if ($oneMove['TYPE'] === UserMove::TYPE_NETWORK) { $this->type = NetPoint::NET_TYPE_NETWORK; } elseif ($oneMove['TYPE'] === UserMove::TYPE_RELATION) { $this->type = NetPoint::NET_TYPE_RELATION; } $this->location = $oneMove['LOCATION']; return $this->moveWebToAsync(); }*/ /** * 页面端到异步的移网 * @return null */ public function moveWebToAsync() { if (!$this->validate()) { return null; } $period = Period::instance(); $moveModel = UserMove::findOne(['ID' => $this->moveId]); if ($moveModel->IS_MOVING == 1) { $this->addError('moveWebToAsync', 'Please wait while it is moving'); // 正在移动中请稍候 return null; } $moveModel->IS_MOVING = 1; $moveModel->AUDIT_ADMIN_ID = \Yii::$app->user->id; $moveModel->CREATE_REMARK = $this->remark; $moveModel->AUDIT_PERIOD_NUM = $period->getNowPeriodNum(); $moveModel->STARTED_AT = Date::nowTime(); $moveModel->AUDITED_AT = Date::nowTime(); if (!$moveModel->save()) { $this->addError('scenario', Form::formatErrorsForApi($moveModel->getErrors())); return null; } if ($moveModel->TYPE === NetPoint::NET_TYPE_NETWORK) { $route = 'user/move-network'; } elseif ($moveModel->TYPE === NetPoint::NET_TYPE_RELATION) { $route = 'user/move-relation'; } else { $this->addError('scenario', 'The scene does not exist'); // 场景不存在 return null; } // 异步处理添加任务 $params = [ 'moveId' => $this->moveId, 'moveUserName' => $this->moveUserName, 'toConUserName' => $this->toConUserName, 'toConRealName' => $this->toConRealName, 'location' => $this->location, 'type' => $this->type, 'remark' => $this->remark, ]; $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle($route, $params); if ($taskKey === false) { $this->errorHandle(); $this->addError('send', 'The request failed'); // 请求失败 return null; } return $moveModel; } /** * 异步移网 * @return bool|null * @throws \Exception */ public function moveAsync() { if (!$this->validate()) { $this->errorHandle(); return false; } return $this->moveNet(); } /** * 移网 * @return bool * @throws \Exception */ public function moveNet() { $netPoint = NetPoint::factory($this->moveId); if ($this->type == NetPoint::NET_TYPE_NETWORK) { $moveResult = $netPoint->moveNetworkPoint($this->_moveUserId, $this->_toConUserId, $this->location, $this->_fromLocation); } elseif ($this->type == NetPoint::NET_TYPE_RELATION) { $moveResult = $netPoint->moveRelationPoint($this->_moveUserId, $this->_toConUserId); } else { $this->addError('netType', 'The type of moving net is incorrect'); // 移网的网络类型错误 unset($netPoint); return null; } if ($moveResult) { unset($netPoint, $moveResult); return true; } else { $this->errorHandle(); $this->addErrors($netPoint->getErrors()); unset($netPoint, $moveResult); return false; } } public function errorHandle(){ // 把移网的审核状态回归 $oneMove = UserMove::findOne(['ID'=>$this->moveId]); $oneMove->IS_MOVING = 0; $oneMove->AUDIT_ADMIN_ID = null; $oneMove->AUDITED_AT = 0; $oneMove->AUDIT_STATUS = 0; $oneMove->save(); } /** * 删除移网 * @param $selected */ public function beforeDelete($selected) { $this->adminOperateLogger->setIsBatch(true)->setOptObjField('USER_ID')->beforeDelete($selected, 'ID'); } /** * 删除 * @param $selected * @throws Exception */ public function delete($selected) { $this->adminOperateLogger->clean()->save([ 'optType' => 'Delete move network', // 删除移网 ]); } }