CalcServePerfCalc.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. <?php
  2. /**
  3. */
  4. namespace common\helpers\bonus;
  5. use common\helpers\Cache;
  6. use common\helpers\Date;
  7. use common\helpers\snowflake\SnowFake;
  8. use common\models\PerfOrder;
  9. use common\models\PerfPeriod;
  10. use common\models\Period;
  11. use common\models\PerfMonth;
  12. use common\models\ServeProcess;
  13. use common\models\User;
  14. use common\models\UserNetwork;
  15. use yii\base\Exception;
  16. use yii\base\StaticInstanceTrait;
  17. class CalcServePerfCalc {
  18. use StaticInstanceTrait;
  19. private $_limit = 3000;
  20. private $_companyMonthPerf = 0;
  21. private $_cfTotalPercent = 0;
  22. private $_lxTotalPercent = 0;
  23. private $_sysConfig = [];
  24. private $_decLevelConfig = [];
  25. private $_empLevelConfig = [];
  26. private $_decRoleConfig = [];
  27. private $_periodNum = 0;
  28. private $_isCalcMonth = 0;
  29. private $_calcYear;
  30. private $_calcMonth;
  31. private $_calcYearMonth;
  32. private $_lastCalcYear;
  33. private $_lastCalcMonth;
  34. private $_lastCalcYearMonth;
  35. private $_lastPeriodNum;
  36. private $_periodObj;
  37. const LOOP_FINISH = 1;
  38. const LOOP_CONTINUE = 2;
  39. const ORDER_PAY_TYPE_CASH = 'cash';
  40. const ORDER_PAY_TYPE_STACK = 'pay_stack'; // pay_stack支付方式
  41. /**
  42. * 获取期数
  43. * @return int
  44. */
  45. public function getPeriodNum() {
  46. return $this->_periodNum;
  47. }
  48. // 校验是否可进行计算
  49. public function isCalcIng($periodNum) {
  50. $periodObj = Period::instance();
  51. $periodDataArr = $periodObj->setPeriodNum($periodNum);
  52. if (empty($periodDataArr)) {
  53. return false;
  54. }
  55. if ($periodDataArr['IS_PREPARE'] != 1) {
  56. return false;
  57. }
  58. $this->_periodNum = $periodDataArr['PERIOD_NUM'];
  59. $this->_periodObj = $periodObj;
  60. return true;
  61. }
  62. /**
  63. * 累计业绩数据
  64. * @param $periodNum
  65. * @return bool
  66. */
  67. public function calcStep($periodNum = null) {
  68. try {
  69. $requestTime = time();
  70. // if (empty($periodNum)) {
  71. // echo('触发时间:【'.$requestTime.'】'.'定时器执行累计业绩计算 ,内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  72. // } else {
  73. // echo('触发时间:【'.$requestTime.'】'.'手动触发累计业绩计算 ,内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  74. // }
  75. //一、查询此业绩状态,是否能进行计算
  76. $calcIng = $this->isCalcIng($periodNum);
  77. ServeProcess::recordRequest($requestTime, $this->_periodNum, '', true);
  78. if ($calcIng !== true) {
  79. //echo('触发时间:【'.date('Y-m-d H:i:s', time()).'】'.'业绩期表中,此期状态不正确');
  80. //ServeProcess::recordRequest($requestTime, $this->_periodNum, '业绩期表中,此期状态不正确');
  81. return false;
  82. } else {
  83. // 将IS_PREPARE改成2,计算中
  84. Period::updateCalcProcess(2, $this->_periodNum);
  85. ServeProcess::recordRequest($requestTime, $this->_periodNum, '调整IS_PREPARE值为2,计算开始');
  86. }
  87. // 记录初始化数据,用户总数,业绩单业绩总pv值.
  88. ServeProcess::recordDataInfo(time(), $this->_periodNum);
  89. $t1 = microtime(true);
  90. //二、初始化
  91. $this->initCalcTask();
  92. echo '业绩期为:'.$this->_periodNum;
  93. Period::updatePercent(10, $this->_periodNum);
  94. ServeProcess::recordProcess($t1, time(), $this->_periodNum, '初始化---初始化配置');
  95. $initT2 = microtime(true);
  96. //三、 设置结算状态
  97. $this->setCalcStatus('start', $this->_periodNum);
  98. ServeProcess::recordProcess($initT2, time(), $this->_periodNum, '初始化---设置结算状态');
  99. $initT3 = microtime(true);
  100. //四、 清空所有本期结算用到的缓存
  101. CalcCache::clearAll($this->_periodNum);
  102. ServeProcess::recordProcess($initT3, time(), $this->_periodNum, '初始化---清空业绩缓存');
  103. $initT4 = microtime(true);
  104. //五、 清空会员推荐和接点关系缓存
  105. CalcCache::clearNetCache();
  106. ServeProcess::recordProcess($initT4, time(), $this->_periodNum, '初始化---清空会员推荐和接点关系缓存');
  107. $initT5 = microtime(true);
  108. //六、 清空相关表数据
  109. $this->clearTableData();
  110. $this->_updatePercent(15);
  111. ServeProcess::recordProcess($initT5, time(), $this->_periodNum, '初始化---清空相关表数据');
  112. $t2 = microtime(true);
  113. echo(date('Y-m-d H:i:s',time()).'初始化、清空缓存及相关数据表完成,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  114. ServeProcess::recordProcess($t1, $t2, $this->_periodNum, '初始化---初始化、清空缓存及相关数据表完成');
  115. //七、 添加缓存中用户数据
  116. CalcCache::addUsers($this->_periodNum);
  117. $t3 = microtime(true);
  118. echo(date('Y-m-d H:i:s',time()).'计算业绩向缓存中加入用户完成,耗时:' . round($t3 - $t2, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  119. ServeProcess::recordProcess($t2, $t3, $this->_periodNum, '计算业绩向缓存中加入用户完成');
  120. $this->_updatePercent(20);
  121. // 八、循环累计用户各项业绩数据
  122. $this->loopGrandPerf();
  123. $t4 = microtime(true);
  124. echo(date('Y-m-d H:i:s',time()).'累计用户业绩完成' . round($t4 - $t3, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  125. ServeProcess::recordProcess($t3, $t4, $this->_periodNum, '累计用户业绩完成');
  126. $this->_updatePercent(60);
  127. // 九、本期业绩入库
  128. $this->loopWriteNowPerf();
  129. $t6 = microtime(true);
  130. echo(date('Y-m-d H:i:s',time()).'本期业绩入库完成,耗时:' . round($t6 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  131. ServeProcess::recordProcess($t4, $t6, $this->_periodNum, '本期业绩入库完成');
  132. $this->_updatePercent(70);
  133. //十、计算月业绩表中的数据
  134. $this->loopCalcMonthPerfTableData();
  135. $t7 = microtime(true);
  136. echo(date('Y-m-d H:i:s',time()).'计算月业绩表中的数据完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  137. if($this->_isCalcMonth) {
  138. ServeProcess::recordProcess($t6, $t7, $this->_periodNum, '计算月业绩表中的数据完成');
  139. }
  140. $this->_updatePercent(80);
  141. //十一、本月业绩入库
  142. $this->loopWriteMonthPerf();
  143. $t8 = microtime(true);
  144. echo(date('Y-m-d H:i:s',time()).'本月业绩入库完成,耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  145. if($this->_isCalcMonth) {
  146. ServeProcess::recordProcess($t7, $t8, $this->_periodNum, '本月业绩入库完成');
  147. }
  148. $this->_updatePercent(100);
  149. //十二、期业绩表,增加是否活跃字段
  150. $this->loopUpdatePeriodActive();
  151. $t9 = microtime(true);
  152. echo(date('Y-m-d H:i:s',time()).'期业绩表活跃状态更新完成,耗时:' . round($t9 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  153. ServeProcess::recordProcess($t6, $t7, $this->_periodNum, '计算业绩业绩结算全部完成');
  154. echo(date('Y-m-d H:i:s',time()).'计算业绩业绩结算全部完成,共耗时:' . round($t9 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  155. } catch (\Exception $e) {
  156. $this->errorCalcTask();
  157. var_dump($e->getMessage());
  158. return false;
  159. }
  160. $this->endCalcTask();
  161. return true;
  162. }
  163. /**
  164. * 结算完成
  165. * @return bool
  166. */
  167. public function endCalcTask() {
  168. // 更新结算状态
  169. $this->setCalcStatus('end');
  170. }
  171. /**
  172. * 结算错误
  173. */
  174. public function errorCalcTask() {
  175. // 清空所有本期结算用到的缓存
  176. CalcCache::clearAll($this->_periodNum);
  177. // 更新结算状态
  178. $this->setCalcStatus('fail');
  179. }
  180. /**
  181. * 设置生成业绩单状态
  182. * @param $type
  183. * start|end|fail
  184. */
  185. public function setCalcStatus($type, $periodNum = null) {
  186. if ($type == 'start') {
  187. Period::updateAll(['IS_PERFING' => 1, 'IS_PERFED' => Period::PERF_NONE, 'PERF_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  188. } elseif ($type == 'end') {
  189. Period::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FINISH, 'PERFED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  190. } elseif ($type == 'fail') {
  191. Period::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FAIL, 'PERFED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  192. }
  193. }
  194. /**
  195. * 初始化结算任务
  196. * @throws \yii\db\Exception
  197. */
  198. public function initCalcTask() {
  199. try {
  200. $periodObj = Period::instance();
  201. $this->_sysConfig = Cache::getSystemConfig();
  202. $this->_decLevelConfig = Cache::getDecLevelConfig();
  203. $this->_empLevelConfig = Cache::getEmpLevelConfig();
  204. $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
  205. $this->_isCalcMonth = $periodObj->isCalcMonth($this->_periodNum);
  206. $this->_calcYear = $periodObj->getYear($this->_periodNum);
  207. $this->_calcMonth = $periodObj->getMonth($this->_periodNum);
  208. $this->_calcYearMonth = $periodObj->getYearMonth($this->_periodNum);
  209. } catch(Exception $e) {
  210. var_dump($e->getMessage());
  211. }
  212. }
  213. /**
  214. * 清空相关表数据
  215. */
  216. public function clearTableData() {
  217. PerfPeriod::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);// 周业绩
  218. if ($this->_isCalcMonth) {
  219. PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");// 月业绩表
  220. }
  221. }
  222. /**
  223. * 累计用户业绩
  224. * @param int $offset
  225. * @return bool
  226. * @throws \yii\db\Exception
  227. */
  228. public function loopGrandPerf($offset = 0) {
  229. $allData = PerfOrder::findUseDbCalc()
  230. ->select('ORDER_AMOUNT,DEC_TYPE,USER_ID,PV,PERIOD_NUM,DEC_USER_ID,PAY_TYPE')
  231. ->where(
  232. "PERIOD_NUM=:PERIOD_NUM",
  233. [':PERIOD_NUM' => $this->_periodNum]
  234. )
  235. ->orderBy('CREATED_AT DESC,ID DESC')
  236. ->offset($offset)
  237. ->limit($this->_limit)
  238. ->asArray()
  239. ->all();
  240. if (!empty($allData)) {
  241. foreach ($allData as $data) {
  242. // 循环累计报单业绩
  243. try{
  244. echo "开始累计用户业绩,用户ID为:".$data['USER_ID'].PHP_EOL;
  245. if ($data['DEC_TYPE'] == PerfOrder::ZC_TYPE) {
  246. // 给自己增加PCS(个人消费)
  247. CalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  248. 'PV_PCS' => $data['PV'],
  249. 'PV_PCS_ZC' => $data['PV'],
  250. ]);
  251. // 把该会员加入到能拿到业绩的会员缓存中
  252. CalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  253. //加入到报单会员中
  254. $toInfo = CalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  255. CalcCache::addHasBDUsers($data['USER_ID'], $this->_periodNum, [
  256. 'TO_USER_ID' => $data['USER_ID'],
  257. 'USER_ID' => $toInfo['DEC_ID'],
  258. 'DEC_ID' => $toInfo['DEC_ID'],
  259. //考虑可能会移网的情况
  260. 'REC_USER_ID' => $toInfo['REC_UID'] ?? '',
  261. 'CON_USER_ID' => $toInfo['CON_UID'] ?? '',
  262. 'DEC_AMOUNT' => $data['ORDER_AMOUNT'],
  263. 'DEC_PV' => $data['PV'],
  264. ]);
  265. // 给上追加业绩
  266. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data) {
  267. // 给上级会员追加本期业绩到缓存中
  268. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  269. 'PV_' . $parent['LOCATION'] . 'L' => $data['PV'],
  270. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $data['PV'],
  271. 'PV_' . $parent['LOCATION'] . 'L_' . $data['DEC_TYPE'] => $data['PV'],
  272. ]);
  273. // 把该会员加入到能拿到业绩的会员缓存中
  274. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  275. unset($parent);
  276. });
  277. //给推荐关系累计增加业绩
  278. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use (&$data) {
  279. // 给上级会员追加本期业绩到缓存中
  280. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  281. 'PV_PSS' => $data['PV'],
  282. ]);
  283. // 把该会员加入到能拿到业绩的会员缓存中
  284. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  285. unset($parent);
  286. });
  287. }
  288. } catch(Exception $e) {
  289. var_dump(__LINE__,__FILE__,$e->getMessage(), $data);
  290. }
  291. // 循环累计复消业绩
  292. try{
  293. if ($data['DEC_TYPE'] == PerfOrder::FX_TYPE) {
  294. if( $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_CASH || $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_STACK) {
  295. $orderCashAmount = $data['ORDER_AMOUNT'];
  296. $payPv = $data['PV'] * $this->_sysConfig['cashReconsumeBonusPercent']['VALUE'] / 100;
  297. $cacheDataKey = 'PV_PCS_FX_CASH';
  298. } else {
  299. $orderCashAmount = 0;
  300. $payPv = $data['PV'];
  301. $cacheDataKey = 'PV_PCS_FX_POINT';
  302. }
  303. if( $payPv <= 0 ) continue;
  304. // 给自己增加PCS(个人消费)
  305. CalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  306. 'FX_AMOUNT_CASH' => $orderCashAmount,
  307. 'PV_PCS' => $payPv,
  308. 'PV_PCS_FX' => $payPv,
  309. $cacheDataKey => $payPv,
  310. ]);
  311. // 把该会员加入到能拿到业绩的会员缓存中
  312. CalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  313. // 给上追加业绩
  314. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data, $payPv) {
  315. // 给上级会员追加本期业绩到缓存中
  316. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  317. 'PV_' . $parent['LOCATION'] . 'L' => $payPv,
  318. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $payPv,
  319. 'PV_' . $parent['LOCATION'] . 'L_FX' => $payPv,
  320. ]);
  321. // 把该会员加入到能拿到业绩的会员缓存中
  322. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  323. });
  324. //给推荐关系累计增加业绩
  325. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use ($data, $payPv) {
  326. // 给上级会员追加本期业绩到缓存中
  327. CalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  328. 'PV_PSS' => $payPv,
  329. ]);
  330. // 把该会员加入到能拿到业绩的会员缓存中
  331. CalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  332. });
  333. }
  334. }catch(Exception $e) {
  335. var_dump(__LINE__,__FILE__,$e->getMessage(), $data);
  336. }
  337. }
  338. return $this->loopGrandPerf($offset + $this->_limit);
  339. }
  340. unset($allData);
  341. return true;
  342. }
  343. /**
  344. * 计算月业绩表相关的数据并写入数据库
  345. * @param int $offset
  346. * @return bool
  347. * @throws Exception
  348. * @throws \yii\db\Exception
  349. */
  350. public function loopCalcMonthPerfTableData(int $offset = 0) {
  351. if (!$this->_isCalcMonth) {
  352. return true;
  353. }
  354. echo sprintf("时间:[%s]月业绩,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  355. $allData = PerfPeriod::findUseDbCalc()
  356. ->select('USER_ID, SUM(FX_AMOUNT_CASH) AS FX_AMOUNT_CASH_SUM,SUM(PV_PCS) AS PV_PCS_SUM,SUM(PV_PCS_FX) AS PV_PCS_FX_SUM,
  357. SUM(PV_PSS) AS PV_PSS_SUM,SUM(PV_1L) AS PV_1L_SUM,SUM(PV_2L) AS PV_2L_SUM,SUM(PV_3L) AS PV_3L_SUM,
  358. SUM(PV_4L) AS PV_4L_SUM,SUM(PV_5L) AS PV_5L_SUM,SUM(PV_1L_ZC) AS PV_1L_ZC_SUM,SUM(PV_2L_ZC) AS PV_2L_ZC_SUM,
  359. SUM(PV_3L_ZC) AS PV_3L_ZC_SUM,SUM(PV_4L_ZC) AS PV_4L_ZC_SUM,SUM(PV_5L_ZC) AS PV_5L_ZC_SUM')
  360. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  361. ->groupBy('USER_ID')
  362. ->orderBy('USER_ID DESC')
  363. ->offset($offset)
  364. ->limit($this->_limit)
  365. ->asArray()
  366. ->all();
  367. if ($allData) {
  368. // 月度业绩表
  369. foreach ($allData as $everyData) {
  370. $userId = $everyData['USER_ID'];
  371. //往期业绩
  372. $userLastPerf = CalcCache::userPerf($userId, $this->_periodNum);
  373. //本期业绩
  374. $periodPerf = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  375. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  376. $nowMonthPerf = [
  377. 'USER_ID' => $userId,
  378. 'FX_AMOUNT_CASH' => $everyData['FX_AMOUNT_CASH_SUM'],
  379. 'PV_PCS' => $everyData['PV_PCS_SUM'],
  380. 'PV_PCS_FX' => $everyData['PV_PCS_FX_SUM'],
  381. 'PV_PSS' => $everyData['PV_PSS_SUM'],
  382. 'PV_1L' => $everyData['PV_1L_SUM'],
  383. 'PV_2L' => $everyData['PV_2L_SUM'],
  384. 'PV_3L' => $everyData['PV_3L_SUM'],
  385. 'PV_4L' => $everyData['PV_4L_SUM'],
  386. 'PV_5L' => $everyData['PV_5L_SUM'],
  387. //总数据,历史+本期。不能用上月加本月,因为上月可能没业绩,上上个月有业绩。
  388. 'PV_1L_TOTAL' => $periodPerf['PV_1L'] + $userLastPerf['PV_1L'],
  389. 'PV_2L_TOTAL' => $periodPerf['PV_2L'] + $userLastPerf['PV_2L'],
  390. 'PV_3L_TOTAL' => $periodPerf['PV_3L'] + $userLastPerf['PV_3L'],
  391. 'PV_4L_TOTAL' => $periodPerf['PV_4L'] + $userLastPerf['PV_4L'],
  392. 'PV_5L_TOTAL' => $periodPerf['PV_5L'] + $userLastPerf['PV_5L'],
  393. 'PV_PSS_TOTAL' => $periodPerf['PV_PSS'] + $userLastPerf['PV_PSS_TOTAL'],
  394. ];
  395. // 把会员的月业绩写入缓存中,以便下面的奖金计算从缓冲中获取数据效率高
  396. CalcCache::addHasMonthPerfUsers($userId, $this->_periodNum);
  397. CalcCache::nowMonthPerf($userId, $this->_periodNum, $nowMonthPerf);
  398. unset($userId, $everyData, $nowMonthPerf, $lastMonthData, $userBaseInfo, $isVip);
  399. }
  400. unset($allData);
  401. $this->loopCalcMonthPerfTableData($offset + $this->_limit);
  402. }
  403. unset($allData);
  404. return true;
  405. }
  406. /**
  407. * 循环有业绩会员,并入库
  408. * @param int $offset
  409. * @return bool
  410. * @throws \yii\db\Exception
  411. */
  412. public function loopWriteNowPerf($offset = 0) {
  413. echo sprintf("时间:[%s]缓存本期业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  414. // 从缓存列表里面从底层往上倒序获取会员
  415. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  416. if($allData){
  417. $insertDataPeriodPerf = [];
  418. foreach($allData as $userId){
  419. $insertDataPeriodPerf[] = $this->nowPeriodPerfData($userId);
  420. unset($userId);
  421. }
  422. PerfPeriod::batchInsert($insertDataPeriodPerf);
  423. unset($insertDataPeriodPerf, $allData);
  424. return $this->loopWriteNowPerf($offset + $this->_limit);
  425. }
  426. unset($allData);
  427. return true;
  428. }
  429. /**
  430. * 循环有月业绩会员,并入库
  431. * @param int $offset
  432. * @return bool
  433. * @throws \yii\db\Exception
  434. */
  435. public function loopWriteMonthPerf($offset = 0) {
  436. if(!$this->_isCalcMonth){
  437. return true;
  438. }
  439. echo sprintf("时间:[%s]缓存本月业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  440. // 从缓存列表里面从底层往上倒序获取会员
  441. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  442. if($allData){
  443. $insertDataMonthPerf = [];
  444. foreach($allData as $userId){
  445. $insertDataMonthPerf[] = $this->nowMonthPerfData($userId);
  446. unset($userId);
  447. }
  448. PerfMonth::batchInsert($insertDataMonthPerf);
  449. unset($insertDataMonthPerf, $allData);
  450. return $this->loopWriteMonthPerf($offset + $this->_limit);
  451. }
  452. unset($allData);
  453. return true;
  454. }
  455. /**
  456. * 本期业绩数据
  457. * @param $userId
  458. * @return array
  459. */
  460. public function nowPeriodPerfData($userId){
  461. $data = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  462. $userInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  463. $result = [
  464. 'ID' => SnowFake::instance()->generateId(),
  465. 'USER_ID' => $userId,
  466. 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
  467. 'PV_PCS' => $data['PV_PCS'],
  468. 'PV_PSS' => $data['PV_PSS'],
  469. 'PV_PCS_ZC' => $data['PV_PCS_ZC'],
  470. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  471. 'PV_PCS_FX_CASH' => $data['PV_PCS_FX_CASH'],
  472. 'PV_PCS_FX_POINT' => $data['PV_PCS_FX_POINT'],
  473. 'PV_1L' => $data['PV_1L'],
  474. 'PV_1L_TOUCH' => $data['PV_1L_TOUCH'],
  475. 'PV_1L_ZC' => $data['PV_1L_ZC'],
  476. 'PV_1L_FX' => $data['PV_1L_FX'],
  477. 'PV_2L' => $data['PV_2L'],
  478. 'PV_2L_TOUCH' => $data['PV_2L_TOUCH'],
  479. 'PV_2L_ZC' => $data['PV_2L_ZC'],
  480. 'PV_2L_FX' => $data['PV_2L_FX'],
  481. 'PV_3L' => $data['PV_3L'],
  482. 'PV_3L_TOUCH' => $data['PV_3L_TOUCH'],
  483. 'PV_3L_ZC' => $data['PV_3L_ZC'],
  484. 'PV_3L_FX' => $data['PV_3L_FX'],
  485. 'PV_4L' => $data['PV_4L'],
  486. 'PV_4L_TOUCH' => $data['PV_4L_TOUCH'],
  487. 'PV_4L_ZC' => $data['PV_4L_ZC'],
  488. 'PV_4L_FX' => $data['PV_4L_FX'],
  489. 'PV_5L' => $data['PV_5L'],
  490. 'PV_5L_TOUCH' => $data['PV_5L_TOUCH'],
  491. 'PV_5L_ZC' => $data['PV_5L_ZC'],
  492. 'PV_5L_FX' => $data['PV_5L_FX'],
  493. 'SURPLUS_1L' => $data['SURPLUS_1L'],
  494. 'SURPLUS_2L' => $data['SURPLUS_2L'],
  495. 'SURPLUS_3L' => $data['SURPLUS_3L'],
  496. 'SURPLUS_4L' => $data['SURPLUS_4L'],
  497. 'SURPLUS_5L' => $data['SURPLUS_5L'],
  498. 'PERIOD_NUM' => $this->_periodNum,
  499. 'CALC_MONTH' => $this->_calcYearMonth,
  500. 'CREATED_AT' => Date::nowTime(),
  501. 'LAST_EMP_LV' => $userInfo['LAST_EMP_LV']
  502. ];
  503. unset($data);
  504. return $result;
  505. }
  506. /**
  507. * 本月业绩
  508. * @param $userId
  509. * @return array
  510. */
  511. public function nowMonthPerfData($userId){
  512. $data = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  513. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  514. $result = [
  515. 'ID' => SnowFake::instance()->generateId(),
  516. 'USER_ID' => $userId,
  517. 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
  518. 'PV_PCS' => $data['PV_PCS'],
  519. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  520. 'PV_PSS' => $data['PV_PSS'],
  521. 'PV_1L' => $data['PV_1L'],
  522. 'PV_2L' => $data['PV_2L'],
  523. 'PV_3L' => $data['PV_3L'],
  524. 'PV_4L' => $data['PV_4L'],
  525. 'PV_5L' => $data['PV_5L'],
  526. 'PV_1L_TOTAL' => $data['PV_1L_TOTAL'],
  527. 'PV_2L_TOTAL' => $data['PV_2L_TOTAL'],
  528. 'PV_3L_TOTAL' => $data['PV_3L_TOTAL'],
  529. 'PV_4L_TOTAL' => $data['PV_4L_TOTAL'],
  530. 'PV_5L_TOTAL' => $data['PV_5L_TOTAL'],
  531. 'PV_PSS_TOTAL' => $data['PV_PSS_TOTAL'],
  532. 'CALC_MONTH' => $this->_calcYearMonth,
  533. 'CREATED_AT' => Date::nowTime(),
  534. 'LAST_EMP_LV' => $baseInfo['LAST_EMP_LV']
  535. ];
  536. unset($data);
  537. return $result;
  538. }
  539. /**
  540. * 循环父级并执行回调函数
  541. * @param $userId
  542. * @param callable $callbackFunc
  543. * @param int $offset
  544. * @return bool
  545. */
  546. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  547. $allParents = Cache::getAllNetworkParents($userId, true);
  548. $allData = array_slice($allParents, $offset, $this->_limit);
  549. unset($allParents);
  550. if ($allData) {
  551. foreach ($allData as $data) {
  552. $funcResult = $callbackFunc($data);
  553. if ($funcResult === self::LOOP_FINISH) {
  554. return true;
  555. } elseif ($funcResult === self::LOOP_CONTINUE) {
  556. continue;
  557. }
  558. unset($data, $funcResult);
  559. }
  560. unset($allData);
  561. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  562. }
  563. return true;
  564. }
  565. /**
  566. * 循环推荐网络的父级
  567. * @param $userId
  568. * @param callable $callbackFunc
  569. * @param int $offset
  570. * @return bool
  571. */
  572. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  573. $allParents = Cache::getAllRelationParents($userId,true);
  574. $allData = array_slice($allParents, $offset, $this->_limit);
  575. unset($allParents);
  576. if ($allData) {
  577. foreach ($allData as $data) {
  578. $funcResult = $callbackFunc($data);
  579. if ($funcResult === self::LOOP_FINISH) {
  580. return true;
  581. } elseif ($funcResult === self::LOOP_CONTINUE) {
  582. continue;
  583. }
  584. unset($data, $funcResult);
  585. }
  586. unset($allData);
  587. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  588. }
  589. return true;
  590. }
  591. /**
  592. * 更新百分比并发送
  593. * @param $percent
  594. */
  595. private function _updatePercent($percent) {
  596. // 把数据写入数据库中
  597. Period::updateAll(['PERF_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  598. }
  599. // 期业绩表,增加是否活跃字段判断,就用isPerfActive 方法进行判断。
  600. public function loopUpdatePeriodActive($offset = 0) {
  601. echo sprintf("时间:[%s]更新期业绩表活跃状态字段,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  602. $allData = PerfPeriod::findUseDbCalc()
  603. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  604. ->offset($offset)
  605. ->limit($this->_limit)
  606. ->asArray()
  607. ->all();
  608. if ($allData) {
  609. foreach ($allData as $data) {
  610. $isActive = User::isPerfActive($data['USER_ID'], $this->_periodNum, $this->_calcYearMonth);
  611. // 期结算结果
  612. PerfPeriod::updateAll(['IS_ACTIVE' => $isActive], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  613. [
  614. ':USER_ID' => $data['USER_ID'],
  615. ':PERIOD_NUM' => $this->_periodNum
  616. ]
  617. );
  618. }
  619. unset($allData);
  620. return $this->loopUpdatePeriodActive($offset + $this->_limit);
  621. }
  622. unset($allData);
  623. return true;
  624. }
  625. }