CalcServePerfCalc.php 28 KB

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