CalcServePerfCalc.php 28 KB

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