NetPoint.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/4/28
  6. * Time: 下午6:38
  7. */
  8. namespace common\helpers;
  9. use common\components\ActiveRecord;
  10. use common\helpers\user\Info;
  11. use common\models\PerfMonth;
  12. use common\models\PerfPeriod;
  13. use common\models\Period;
  14. use common\models\DecOrder;
  15. use common\models\User;
  16. use common\models\UserInfo;
  17. use common\models\UserMove;
  18. use common\models\UserNetwork;
  19. use common\models\UserPerf;
  20. use common\models\UserRelation;
  21. use common\models\UserSystem;
  22. use yii\base\BaseObject;
  23. use yii\base\Exception;
  24. use yii\base\StaticInstanceTrait;
  25. use yii\db\Expression;
  26. use yii\helpers\Json;
  27. /**
  28. * Class NetPoint
  29. * 网络接点关系调整类
  30. * 1、点位A从点位B移动到点位C,首先寻找公共接点人,把非公共的点位全部删掉
  31. * 2、循环把自己和自己的所有下级相对这个公共点位的都修改深度+变化的深度,修改区域为点位A到点位C后相应的区位。
  32. * 3、循环查找会员移动到点位C后,所有的点位C及C的上级,并会员A及A的下级,都添加这些上级数据。
  33. * 4、修改user_info表,的接点人
  34. * @package common\helpers
  35. */
  36. class NetPoint extends BaseObject
  37. {
  38. const NET_TYPE_NETWORK = 'network';
  39. const NET_TYPE_RELATION = 'relation';
  40. const REDIS_KEY_PREFIX_CHILD_USER = 'moveNetChildUser:NetType_%s:moveUserId_%s';
  41. private $_limit = 100;
  42. private $_errors = [];
  43. private $_moveInfo;
  44. public $moveId;
  45. private $_moveUserId;
  46. private $_moveUserInfo;
  47. private $_toConUserId;
  48. private $_toConUserInfo;
  49. private $_fromConUserId;
  50. private $_fromConUserInfo;
  51. private $_fromLocation;
  52. private $_location;
  53. private $_commonParentUserId = null;
  54. private $_commonDeepDiff;
  55. //private $_commonNewParentLocation;
  56. private $_tempCalcDeep = 0;
  57. private $_isContinueFindParent = true; // 用于往上找父级,找到公共节点就不在找了
  58. public static function factory($moveId) {
  59. return new self([
  60. 'moveId' => $moveId
  61. ]
  62. );
  63. }
  64. public function init()
  65. {
  66. parent::init();
  67. }
  68. /**
  69. * 加入错误错误
  70. * @param $attr
  71. * @param $error
  72. */
  73. public function addError($attr, $error){
  74. $this->_errors[$attr][] = $error;
  75. }
  76. /**
  77. * 获取错误信息
  78. * @return array
  79. */
  80. public function getErrors(){
  81. return $this->_errors;
  82. }
  83. /**
  84. * 初始化移动
  85. * @param $netType
  86. * @param $moveUserId
  87. * @param $toConUserId
  88. * @param int $location
  89. * @param int $fromLocation
  90. * @throws \Exception
  91. */
  92. public function initMove($netType, $moveUserId, $toConUserId, $location=0, $fromLocation = 0){
  93. $this->_moveInfo = UserMove::findOneAsArray(['ID'=>$this->moveId]);
  94. $this->_moveUserId = $moveUserId;
  95. $this->_moveUserInfo = Info::baseInfo($moveUserId);
  96. if($netType == self::NET_TYPE_NETWORK){
  97. $this->_fromConUserId = $this->_moveUserInfo['CON_UID'];
  98. } else {
  99. $this->_fromConUserId = $this->_moveUserInfo['REC_UID'];
  100. }
  101. // $this->_fromConUserInfo = UserInfo::findOneAsArray(['USER_ID'=>$this->_fromConUserId]);
  102. $this->_toConUserId = $toConUserId;
  103. // $this->_toConUserInfo = UserInfo::findOneAsArray(['USER_ID'=>$this->_toConUserId]);
  104. $this->_location = $location;
  105. $this->_fromLocation = $fromLocation;
  106. $this->_errors = [];
  107. $this->_clearChildUserFromRedis($netType);
  108. $this->allChildUserToRedisList($netType);
  109. }
  110. /**
  111. * 移动安置网络节点
  112. * @param $moveUserId
  113. * @param $toConUserId
  114. * @param $location
  115. * @param $fromLocation
  116. * @return bool
  117. * @throws \Exception
  118. */
  119. public function moveNetworkPoint($moveUserId, $toConUserId, $location, $fromLocation){
  120. return $this->movePoint(self::NET_TYPE_NETWORK, $moveUserId, $toConUserId, $location, $fromLocation);
  121. }
  122. /**
  123. * 移动开拓网络节点
  124. * @param $moveUserId
  125. * @param $toConUserId
  126. * @return bool
  127. * @throws \Exception
  128. */
  129. public function moveRelationPoint($moveUserId, $toConUserId){
  130. return $this->movePoint(self::NET_TYPE_RELATION, $moveUserId, $toConUserId);
  131. }
  132. /**
  133. * 移网
  134. * @param $netType
  135. * @param $moveUserId
  136. * @param $toConUserId
  137. * @param int $location
  138. * @param int $fromLocation
  139. * @return bool
  140. * @throws \Exception
  141. */
  142. public function movePoint($netType, $moveUserId, $toConUserId, $location=0, $fromLocation = 0){
  143. $transaction = \Yii::$app->db->beginTransaction();
  144. try {
  145. $this->initMove($netType, $moveUserId, $toConUserId, $location, $fromLocation);
  146. echo sprintf("时间:[%s],用户ID:[%s]移网,初始化成功".PHP_EOL, date('Y-m-d H:i:s', time()), $moveUserId);
  147. //修改移动会员以及子会员的深度、父ID、以及网体标记等
  148. $this->_moveParentAndChildren($netType);
  149. // 把userInfo表的数据也改掉
  150. if($netType == self::NET_TYPE_NETWORK){
  151. $conNumField = 'CON_NUM';
  152. } else {
  153. $conNumField = 'REC_NUM';
  154. }
  155. // 原上级接点数量减少,新上级接点数量增加
  156. UserInfo::updateAllCounters([$conNumField => -1], 'USER_ID=:USER_ID', [':USER_ID'=>$this->_fromConUserId]);
  157. UserInfo::updateAllCounters([$conNumField =>1], 'USER_ID=:USER_ID', [':USER_ID'=>$this->_toConUserId]);
  158. // 清除redis
  159. if ($netType == self::NET_TYPE_NETWORK) {
  160. \Yii::$app->redis->del(Cache::USER_NETWORK_PARENTS);
  161. } else {
  162. \Yii::$app->redis->del(Cache::USER_RELATION_PARENTS);
  163. }
  164. $this->_endHandle();
  165. $transaction->commit();
  166. } catch (\Exception $e){
  167. echo $e->getMessage() . PHP_EOL;
  168. $transaction->rollBack();
  169. $this->_errorHandle();
  170. $this->addError($netType, $e->getMessage());
  171. $this->_clearChildUserFromRedis($netType);
  172. return false;
  173. }
  174. $this->_clearChildUserFromRedis($netType);
  175. return true;
  176. }
  177. /**
  178. * 删除备份的表
  179. */
  180. public function dropBakTable(){
  181. $oneMove = $this->_moveInfo;
  182. $createTableName = $oneMove['NET_TABLE_NAME'];
  183. if(ActiveRecord::isExistsTable('{{%'.$createTableName.'}}', 'dbNetPoint')){
  184. ActiveRecord::deleteTable($createTableName, 'dbNetPoint');
  185. }
  186. }
  187. /**
  188. * 把所有子会员数据存入缓存中
  189. * @param $netType
  190. * @return bool
  191. */
  192. public function allChildUserToRedisList($netType){
  193. return $this->_getChildUserToRedis($this->_moveUserId, $netType);
  194. }
  195. private function _getChildUserToRedis($parentUid, $netType) {
  196. if($netType == self::NET_TYPE_NETWORK){
  197. $field = 'CON_UID';
  198. } else {
  199. $field = 'REC_UID';
  200. }
  201. $childList = UserInfo::find()->select(['USER_ID'])->where("{$field}=:PARENT_UID", ['PARENT_UID'=>$parentUid])->asArray()->all();
  202. if( !$childList ) {
  203. unset($childList, $field, $parentUid, $netType);
  204. return true;
  205. }
  206. foreach ($childList as $child) {
  207. // 会员ID加入缓存
  208. $this->_addChildUserToRedis($netType, $child['USER_ID']);
  209. $this->_getChildUserToRedis($child['USER_ID'], $netType);
  210. }
  211. unset($childList, $field, $parentUid, $netType, $child);
  212. return true;
  213. }
  214. /**
  215. * 循环删除子节点对应的非公共节点的数据
  216. * @param $netType
  217. * @param $parentUserId
  218. * @param int $offset
  219. */
  220. public function _loopChildDelUnCommonParent($netType, $parentUserId, int $offset=0){
  221. // 分页从缓存中获取子会员
  222. $allData = $this->_getChildUserFromRedis($netType, $offset, $this->_limit);
  223. if($allData){
  224. foreach($allData as $childUserId){
  225. $modelClass = self::getModelClass($netType);
  226. $modelClass::deleteAll('PARENT_UID=:PARENT_UID AND USER_ID=:USER_ID', [':PARENT_UID'=>$parentUserId, ':USER_ID'=>$childUserId]);
  227. }
  228. unset($allData);
  229. $this->_loopChildDelUnCommonParent($netType, $parentUserId, $offset + $this->_limit);
  230. }
  231. }
  232. /**
  233. * 把移动的会员及子会员加入到直接新上级的节点数据
  234. * @param $netType
  235. */
  236. private function _moveParentAndChildren($netType){
  237. $modelClass = self::getModelClass($netType);
  238. // 获取一条新上级的网络数据
  239. $fromData = $modelClass::find()->where('USER_ID=:USER_ID', [':USER_ID'=>$this->_moveUserId])->asArray()->one();
  240. $toParentData = $modelClass::find()->where('USER_ID=:USER_ID', [':USER_ID'=>$this->_toConUserId])->asArray()->one();
  241. if( $toParentData['PARENT_UIDS'] ) {
  242. $updateParentUids = $toParentData['PARENT_UIDS'] . ',' . $this->_toConUserId;
  243. }else {
  244. $updateParentUids = $this->_toConUserId;
  245. }
  246. $updateData = [
  247. 'PARENT_UID' => $this->_toConUserId,
  248. 'PARENT_UIDS' => $updateParentUids,
  249. 'TOP_UID' => $toParentData['TOP_UID'],
  250. 'TOP_DEEP' => $toParentData['TOP_DEEP'] + 1,
  251. 'UPDATED_AT' => Date::nowTime(),
  252. ];
  253. if($netType == self::NET_TYPE_NETWORK){
  254. $updateData['RELATIVE_LOCATION'] = $this->_location;
  255. $updateData['LOCATION_TAG'] = $toParentData['LOCATION_TAG'] . $this->_location;
  256. }
  257. $modelClass::updateAll($updateData, 'USER_ID=:USER_ID', [
  258. 'USER_ID' => $this->_moveUserId
  259. ]);
  260. unset($modelClass, $toParentData, $updateParentUids);
  261. if($netType == self::NET_TYPE_NETWORK){
  262. $conField = 'CON_UID';
  263. } else {
  264. $conField = 'REC_UID';
  265. }
  266. UserInfo::updateAll([
  267. $conField => $this->_toConUserId,
  268. strtoupper($netType).'_DEEP' => $updateData['TOP_DEEP'],
  269. ], 'USER_ID=:USER_ID', [':USER_ID'=>$this->_moveUserId]);
  270. $this->_updatePercent(30);
  271. // 把子会员也更新、准备公共子会员需要的信息
  272. $commonParentData = [
  273. 'toConUserId' => $this->_toConUserId,
  274. 'topUserId' => $updateData['TOP_UID'],
  275. 'commonDiffDeep' => $updateData['TOP_DEEP'] - $fromData['TOP_DEEP'],
  276. 'oldParentUids' => $fromData['PARENT_UIDS'],
  277. 'newParentUids' => $updateData['PARENT_UIDS'],
  278. ];
  279. if($netType == self::NET_TYPE_NETWORK){
  280. $commonParentData['oldParentLocationTag'] = $fromData['LOCATION_TAG'];
  281. $commonParentData['newParentLocationTag'] = $updateData['LOCATION_TAG'];
  282. }
  283. unset($fromData, $updateData);
  284. $this->_updateChildData($netType, $commonParentData);
  285. $this->_changeNetPerfData($netType, $commonParentData);
  286. unset($netType, $commonParentData);
  287. }
  288. /**
  289. * 分页循环给子会员增加相较于上级的数据
  290. * @param $netType
  291. * @param $params
  292. * [
  293. * 'toConUserId' => $this->_toConUserId,
  294. * 'topUserId' => $updateData['TOP_UID'],
  295. * 'commonDiffDeep' => $updateData['TOP_DEEP'] - $fromData['TOP_DEEP'],
  296. * 'oldParentUids' => $fromData['PARENT_UIDS'],
  297. * 'newParentUids' => $updateData['PARENT_UIDS'],
  298. * ]
  299. * @param int $offset
  300. */
  301. private function _updateChildData($netType, $params, int $offset = 0){
  302. // 分页获取要移动的会员的子会员
  303. $allData = $this->_getChildUserFromRedis($netType, $offset, $this->_limit);
  304. if($allData){
  305. $modelClass = self::getModelClass($netType);
  306. foreach($allData as $childUserId){
  307. $childData = $modelClass::find()->where('USER_ID=:USER_ID', [':USER_ID'=>$childUserId])->asArray()->one();
  308. //原PARENT_UIDS、新PARENT_UIDS
  309. $childParentUids = substr($childData['PARENT_UIDS'], strlen($params['oldParentUids']));
  310. $childUpdateData = [
  311. 'PARENT_UIDS' => $params['newParentUids'] . $childParentUids,//不变
  312. 'TOP_UID' => $params['topUserId'],//和父的保持一致
  313. 'TOP_DEEP' => $childData['TOP_DEEP'] + $params['commonDiffDeep'],//所有子会员变化深度一致
  314. 'UPDATED_AT' => Date::nowTime(),
  315. ];
  316. if($netType == self::NET_TYPE_NETWORK){
  317. //RELATIVE_LOCATION不变
  318. //找到moveUser的 原来 locationTag 的length
  319. $childLocationTag = substr($childData['LOCATION_TAG'], strlen($params['oldParentLocationTag']));
  320. $childUpdateData['LOCATION_TAG'] = $params['newParentLocationTag'] . $childLocationTag;
  321. unset($childLocationTag);
  322. }
  323. $modelClass::updateAll($childUpdateData, 'USER_ID=:USER_ID', [
  324. 'USER_ID' => $childUserId
  325. ]);
  326. UserInfo::updateAll([
  327. strtoupper($netType).'_DEEP' => $childUpdateData['TOP_DEEP'],
  328. ], 'USER_ID=:USER_ID', [':USER_ID'=>$childUserId]);
  329. unset($childUserId, $childData, $childParentUids, $childUpdateData);
  330. }
  331. unset($allData, $modelClass);
  332. return $this->_updateChildData($netType, $params, $offset + $this->_limit);
  333. }
  334. unset($allData);
  335. return true;
  336. }
  337. /**
  338. * 移网业绩数据处理
  339. * @param $netType
  340. * @param $commonData
  341. * @return bool
  342. */
  343. private function _changeNetPerfData($netType, $commonData) {
  344. //获取未结算的月份
  345. $noSentPeriod = Period::find()->where('IS_SENT=0')->orderBy('PERIOD_NUM ASC')->asArray()->one();
  346. if( !$noSentPeriod ) return false;
  347. $thisYearMonth = $noSentPeriod['CALC_YEAR'].Tool::numFix($noSentPeriod['CALC_MONTH'], 2);
  348. // $lastYearMonth = Date::lastMonth($noSentPeriod['CALC_YEAR'].'-'.$noSentPeriod['CALC_MONTH'], 'Ym');
  349. $thisYearMonthPerf=[];
  350. if( $noSentPeriod['IS_MONTH'] == 1 ) {
  351. $thisYearMonthPerf = PerfMonth::find()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$this->_moveUserId, 'CALC_MONTH'=>$thisYearMonth])->asArray()->one();
  352. }
  353. unset($noSentPeriod);
  354. //查找移动点位这个人的上个月的累计业绩
  355. // $lastYearMonthPerf = PerfMonth::find()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$this->_moveUserId, 'CALC_MONTH'=>$lastYearMonth])->asArray()->one();
  356. //本月的已结算业绩
  357. $thisPerfPeriodList = PerfPeriod::find()->select(['PV_PCS', 'PV_PSS', 'PERIOD_NUM'])->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$this->_moveUserId, 'CALC_MONTH'=>$thisYearMonth])->asArray()->all();
  358. //总的pss_total
  359. $userPerf = UserPerf::find()->where('USER_ID=:USER_ID', ['USER_ID'=>$this->_moveUserId])->asArray()->one();
  360. $oldParentUids = $commonData['oldParentUids'] ? explode(',', $commonData['oldParentUids']) : [];
  361. $newParentUids = $commonData['newParentUids'] ? explode(',', $commonData['newParentUids']) : [];
  362. if( $netType === self::NET_TYPE_RELATION ) {
  363. //修改上个月的累计业绩
  364. // if( $lastYearMonthPerf ) {
  365. // $lastYearMonthPssTotal = $lastYearMonthPerf['PV_PCS'] + $lastYearMonthPerf['PV_PSS_TOTAL'];
  366. // $lastYearMonthPss = $lastYearMonthPerf['PV_PCS'] + $lastYearMonthPerf['PV_PSS'];
  367. // foreach ($oldParentUids as $oldParentUid) {//减
  368. // PerfMonth::updateAllCounters(['PV_PSS'=>(-1)*$lastYearMonthPss, 'PV_PSS_TOTAL'=>(-1)*$lastYearMonthPssTotal], 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$oldParentUid, 'CALC_MONTH'=>$lastYearMonth]);
  369. // unset($oldParentUid);
  370. // }
  371. // foreach ($newParentUids as $newParentUid) {//加
  372. // PerfMonth::updateAllCounters(['PV_PSS'=>$lastYearMonthPss, 'PV_PSS_TOTAL'=>$lastYearMonthPssTotal], 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$newParentUid, 'CALC_MONTH'=>$lastYearMonth]);
  373. // unset($newParentUid);
  374. // }
  375. // unset($lastYearMonthPssTotal, $lastYearMonthPss);
  376. // }
  377. // unset($lastYearMonthPerf, $lastYearMonth);
  378. //修改本月的累计业绩
  379. if( $thisYearMonthPerf ) {
  380. $thisYearMonthPssTotal = $thisYearMonthPerf['PV_PCS'] + $thisYearMonthPerf['PV_PSS_TOTAL'];
  381. $thisYearMonthPss = $thisYearMonthPerf['PV_PCS'] + $thisYearMonthPerf['PV_PSS'];
  382. foreach ($oldParentUids as $oldParentUid) {//减
  383. $parentThisYearMonthPerf = PerfMonth::find()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$oldParentUid, 'CALC_MONTH'=>$thisYearMonth])->asArray()->one();
  384. if( $parentThisYearMonthPerf ) {
  385. if( $parentThisYearMonthPerf['PV_PSS_TOTAL'] < $thisYearMonthPssTotal ) {
  386. PerfMonth::updateAll(['PV_PSS'=>(-1)*$thisYearMonthPss, 'PV_PSS_TOTAL'=>0], 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$oldParentUid, 'CALC_MONTH'=>$thisYearMonth]);
  387. }else {
  388. PerfMonth::updateAllCounters(['PV_PSS'=>(-1)*$thisYearMonthPss, 'PV_PSS_TOTAL'=>(-1)*$thisYearMonthPssTotal], 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$oldParentUid, 'CALC_MONTH'=>$thisYearMonth]);
  389. }
  390. }
  391. unset($oldParentUid, $parentThisYearMonthPerf);
  392. }
  393. foreach ($newParentUids as $newParentUid) {//加
  394. PerfMonth::updateAllCounters(['PV_PSS'=>$thisYearMonthPss, 'PV_PSS_TOTAL'=>$thisYearMonthPssTotal], 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$newParentUid, 'CALC_MONTH'=>$thisYearMonth]);
  395. unset($newParentUid);
  396. }
  397. unset($thisYearMonthPssTotal, $thisYearMonthPss);
  398. }
  399. unset($thisYearMonthPerf);
  400. //修改本月的已结算业绩
  401. foreach ($thisPerfPeriodList as $periodPerf) {
  402. $periodPss = $periodPerf['PV_PCS'] + $periodPerf['PV_PSS'];
  403. if( $periodPss <= 0 ) continue;
  404. foreach ($oldParentUids as $oldParentUid) {//减
  405. PerfPeriod::updateAllCounters(['PV_PSS'=>(-1)*$periodPss], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', ['USER_ID'=>$oldParentUid, 'PERIOD_NUM'=>$periodPerf['PERIOD_NUM']]);
  406. unset($oldParentUid);
  407. }
  408. foreach ($newParentUids as $newParentUid) {//加
  409. PerfPeriod::updateAllCounters(['PV_PSS'=>$periodPss], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', ['USER_ID'=>$newParentUid, 'PERIOD_NUM'=>$periodPerf['PERIOD_NUM']]);
  410. unset($newParentUid);
  411. }
  412. unset($periodPerf, $periodPss);
  413. }
  414. unset($thisPerfPeriodList, $thisYearMonth);
  415. if( $userPerf ) {
  416. $userPerfPss = $userPerf['PV_PCS_ZC'] + $userPerf['PV_PCS_FX'] + $userPerf['PV_PSS'];
  417. $userPerfPssTotal = $userPerf['PV_PCS_ZC'] + $userPerf['PV_PCS_FX'] + $userPerf['PV_PSS_TOTAL'];
  418. foreach ($oldParentUids as $oldParentUid) {//减
  419. $parentPerf = UserPerf::find()->where('USER_ID=:USER_ID', ['USER_ID'=>$oldParentUid])->asArray()->one();
  420. if( $parentPerf ) {
  421. if( $parentPerf['PV_PSS_TOTAL'] < $userPerfPssTotal ) {
  422. UserPerf::updateAll(['PV_PSS'=>0, 'PV_PSS_TOTAL'=>0], 'USER_ID=:USER_ID', ['USER_ID'=>$oldParentUid]);
  423. }else {
  424. UserPerf::updateAllCounters(['PV_PSS'=>(-1)*$userPerfPss, 'PV_PSS_TOTAL'=>(-1)*$userPerfPssTotal], 'USER_ID=:USER_ID', ['USER_ID'=>$oldParentUid]);
  425. }
  426. }
  427. unset($oldParentUid, $parentPerf);
  428. }
  429. foreach ($newParentUids as $newParentUid) {//加
  430. UserPerf::updateAllCounters(['PV_PSS'=>$userPerfPss, 'PV_PSS_TOTAL'=>$userPerfPssTotal], 'USER_ID=:USER_ID', ['USER_ID'=>$newParentUid]);
  431. unset($newParentUid);
  432. }
  433. unset($userPerfPss, $userPerfPssTotal);
  434. }
  435. unset($userPerf);
  436. }
  437. if( $netType === self::NET_TYPE_NETWORK ) {
  438. //@todo 团队奖相关的,对碰往期对碰剩余业绩是否修改
  439. }
  440. unset($oldParentUids, $newParentUids);
  441. }
  442. /**
  443. * 结束操作
  444. * @throws Exception
  445. */
  446. private function _endHandle(){
  447. // 把移网申请记录的正在移网状态修改
  448. $oneMove = UserMove::findOne(['ID'=>$this->moveId]);
  449. $oneMove->IS_MOVING = 0;
  450. $oneMove->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  451. $oneMove->ENDED_AT = Date::nowTime();
  452. if(!$oneMove->save()){
  453. throw new Exception(Form::formatErrorsForApi($oneMove->getErrors()));
  454. }
  455. $this->_updatePercent(100);
  456. }
  457. /**
  458. * 错误的时候,把移网状态改回原状
  459. */
  460. private function _errorHandle(){
  461. // 把移网的审核状态回归
  462. $oneMove = UserMove::findOne(['ID'=>$this->moveId]);
  463. $oneMove->IS_MOVING = 0;
  464. $oneMove->AUDIT_ADMIN_ID = null;
  465. $oneMove->AUDITED_AT = 0;
  466. $oneMove->AUDIT_STATUS = \Yii::$app->params['auditStatus']['false']['value'];
  467. $oneMove->save();
  468. // 删除创建的表
  469. $this->dropBakTable();
  470. $this->_updatePercent(0);
  471. }
  472. /**
  473. * 更新百分比并发送
  474. * @param $percent
  475. */
  476. private function _updatePercent($percent){
  477. // 把数据写入数据库中
  478. UserMove::updateAll(['MOVE_PERCENT'=>$percent], 'ID=:ID', [':ID'=>$this->moveId]);
  479. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'USER_MOVE' ,'ID' => $this->moveId, 'FIELD' => 'MOVE_PERCENT']);
  480. }
  481. /**
  482. * 把子会员存入到缓存中
  483. * @param $netType
  484. * @param $userId
  485. */
  486. private function _addChildUserToRedis($netType, $userId){
  487. $cacheKey = sprintf(self::REDIS_KEY_PREFIX_CHILD_USER, $netType, $this->_moveUserId);
  488. \Yii::$app->redis->rpush($cacheKey, $userId);
  489. }
  490. /**
  491. * 从缓存中获取子会员数据
  492. * @param $netType
  493. * @param $offset
  494. * @param $limit
  495. * @return mixed
  496. */
  497. private function _getChildUserFromRedis($netType, $offset, $limit){
  498. $cacheKey = sprintf(self::REDIS_KEY_PREFIX_CHILD_USER, $netType, $this->_moveUserId);
  499. return \Yii::$app->redis->lrange($cacheKey, $offset, ($offset + $limit - 1));
  500. }
  501. /**
  502. * 清空子会员数据从缓存中
  503. * @param $netType
  504. * @return mixed
  505. */
  506. private function _clearChildUserFromRedis($netType){
  507. $cacheKey = sprintf(self::REDIS_KEY_PREFIX_CHILD_USER, $netType, $this->_moveUserId);
  508. return \Yii::$app->redis->del($cacheKey);
  509. }
  510. /**
  511. * 获取model类
  512. * @param $netType
  513. * @return null|string
  514. */
  515. private static function getModelClass($netType){
  516. if($netType == self::NET_TYPE_NETWORK){
  517. return UserNetwork::class;
  518. } elseif($netType == self::NET_TYPE_RELATION){
  519. return UserRelation::class;
  520. } else {
  521. return null;
  522. }
  523. }
  524. }