PreparePerfCalc.php 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. <?php
  2. /**
  3. * 月结算的时候,周日晚0点进行预计算,此月用户和团队的业绩信息. 让会员参考,进行补业绩
  4. * Created by PhpStorm.
  5. * User: liyunlong
  6. * Date: 2019-01-11
  7. * Time: 15:27
  8. */
  9. namespace common\helpers\bonus;
  10. use common\helpers\Cache;
  11. use common\helpers\Date;
  12. use common\helpers\Form;
  13. use common\helpers\snowflake\SnowFake;
  14. use common\models\DeclarationLevel;
  15. use common\models\forms\DeclarationForm;
  16. use common\models\Order;
  17. use common\models\OrderDec;
  18. use common\models\OrderShop;
  19. use common\models\OrderStandard;
  20. use common\models\PerfCompany;
  21. use common\models\PerfMonth;
  22. use common\models\PerfOrder;
  23. use common\models\PerfPeriod;
  24. use common\models\PerfStandard;
  25. use common\models\Period;
  26. use common\models\DecOrder;
  27. use common\models\EmployLevel;
  28. use common\models\PerfActiveUser;
  29. use common\models\PerfMonthPrepare;
  30. use common\models\PerfOrderPrepare;
  31. use common\models\PerfPeriodPrepare;
  32. use common\models\PeriodPrepare;
  33. use yii\base\Exception;
  34. use yii\base\StaticInstanceTrait;
  35. class PreparePerfCalc {
  36. use StaticInstanceTrait;
  37. private $_limit = 1000;
  38. private $_handleUserId;
  39. private $_companyMonthPerf = 0;
  40. private $_cfTotalPercent = 0;
  41. private $_lxTotalPercent = 0;
  42. private $_sysConfig = [];
  43. private $_decLevelConfig = [];
  44. private $_empLevelConfig = [];
  45. private $_decRoleConfig = [];
  46. private $_errors = [];
  47. private $_periodNum = 0;
  48. private $_periodId;
  49. private $_isCalcMonth = 0;
  50. private $_calcYear;
  51. private $_calcMonth;
  52. private $_calcYearMonth;
  53. private $_lastCalcYear;
  54. private $_lastCalcMonth;
  55. private $_lastCalcYearMonth;
  56. private $_lastPeriodNum;
  57. private $_lastPeriodYear;
  58. private $_lastPeriodMonth;
  59. private $_lastPeriodYearMonth;
  60. private $_lastTime = 0;
  61. //pv
  62. private $_pvRatio;
  63. const LOOP_FINISH = 1;
  64. const LOOP_CONTINUE = 2;
  65. const ORDER_PAY_TYPE_CASH = 'cash';
  66. // const ORDER_PAY_TYPE_POINT = 'point';
  67. /**
  68. * 获取期数
  69. * @return int
  70. */
  71. public function getPeriodNum() {
  72. return $this->_periodNum;
  73. }
  74. /**
  75. * 加入错误错误
  76. * @param string $attr
  77. * @param string $error
  78. */
  79. public function addError(string $attr, string $error) {
  80. $this->_errors[$attr][] = $error;
  81. }
  82. /**
  83. * 获取错误信息
  84. * @return array
  85. */
  86. public function getErrors() {
  87. return $this->_errors;
  88. }
  89. /**
  90. * 计算步骤
  91. * @param $periodNum
  92. * @param null $handleUserId
  93. * @return bool
  94. */
  95. public function calcStep($periodNum = null, $handleUserId = null) {
  96. try {
  97. // 先计算是否是结算月,并且是否到了周日0点
  98. // 需要将线上的数据perf_order,perf_period两个表的数据同步过来
  99. // Period表数据同步过来,只要是结算月的数据
  100. $checkPrepare = $this->checkCanCalcPref($periodNum);
  101. if ($checkPrepare !== true) {
  102. return true;
  103. }
  104. $this->_errors = [];
  105. $this->_handleUserId = $handleUserId;
  106. $this->_updatePercent(5);
  107. $t1 = microtime(true);
  108. // 初始化结算任务
  109. $this->initCalcTask($periodNum);
  110. $this->_updatePercent(10);
  111. // 设置结算状态
  112. $this->setCalcStatus('start');
  113. // 清空所有本期结算用到的缓存
  114. PrepareCalcCache::clearAll($this->_periodNum);
  115. // 清空会员推荐和接点关系缓存
  116. PrepareCalcCache::clearNetCache();
  117. // 清空相关表数据
  118. $this->clearTableData();
  119. $this->_updatePercent(15);
  120. $t2 = microtime(true);
  121. echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  122. // 计算月奖,才需要向缓存中加入按推荐深度的所有用户
  123. //修改每一期都缓存所有用户
  124. PrepareCalcCache::addUsers($this->_periodNum);
  125. $t3 = microtime(true);
  126. echo('向缓存中加入用户完成,耗时:' . round($t3 - $t2, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  127. $this->_updatePercent(20);
  128. // 周结,循环向上级计入业绩并加入业绩单
  129. $this->loopCalcPeriodPerfByDecOrder();
  130. $this->loopCalcPeriodPerfByOrderDec();
  131. $t4 = microtime(true);
  132. echo('计算周业绩表中的数据完成,耗时:' . round($t4 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  133. $this->_updatePercent(40);
  134. // 从会员的复销订单会员计算复销业绩并加入业绩单
  135. $this->loopCalcPerfByFXOrder();
  136. $this->loopCalcPerfByShopFXOrder();
  137. $t5 = microtime(true);
  138. echo('计算复销业绩并写入业绩单完成,耗时:' . round($t5 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  139. $this->_updatePercent(60);
  140. //本期业绩入库
  141. $this->loopWriteNowPerf();
  142. $t6 = microtime(true);
  143. echo('本期业绩入库完成,耗时:' . round($t6 - $t5, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  144. $this->_updatePercent(70);
  145. //计算月业绩表中的数据
  146. $this->loopCalcMonthPerfTableData();
  147. $t7 = microtime(true);
  148. echo('计算月业绩表中的数据完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  149. $this->_updatePercent(80);
  150. $t8 = microtime(true);
  151. // 计算预计月业绩表的数据,这些数据用户不在前三期非月节点的数据.需要将此期新用户业绩统计一下
  152. $this->loopCalcMonthPerfTableDataPrepare();
  153. //本月业绩入库
  154. $this->loopWriteMonthPerf();
  155. $t7 = microtime(true);
  156. echo('本月业绩入库完成,耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  157. $this->_updatePercent(90);
  158. $t9 = microtime(true);
  159. echo('本月业绩入库完成,耗时:' . round($t9 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  160. $this->_updatePercent(100);
  161. $t10 = microtime(true);
  162. echo('业绩结算全部完成,共耗时:' . round($t10 - $t9, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  163. } catch (\Exception $e) {
  164. $this->errorCalcTask();
  165. file_put_contents('error_test.txt', var_export([
  166. 'err' => $e->getMessage()
  167. ],true));
  168. $this->addError('calc', $e->getMessage());
  169. return false;
  170. }
  171. $this->endCalcTask();
  172. return true;
  173. }
  174. /**
  175. * 结算完成
  176. * @return bool
  177. */
  178. public function endCalcTask() {
  179. // 更新结算状态
  180. $this->setCalcStatus('end');
  181. }
  182. /**
  183. * 结算错误
  184. */
  185. public function errorCalcTask() {
  186. // 清空所有本期结算用到的缓存
  187. PrepareCalcCache::clearAll($this->_periodNum);
  188. // 更新结算状态
  189. $this->setCalcStatus('fail');
  190. }
  191. /**
  192. * 设置生成业绩单状态
  193. * @param $type
  194. * start|end|fail
  195. */
  196. public function setCalcStatus($type) {
  197. if ($type == 'start') {
  198. PeriodPrepare::updateAll(['IS_PERFING' => 1, 'IS_PERFED' => Period::PERF_NONE, 'PERF_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  199. } elseif ($type == 'end') {
  200. PeriodPrepare::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FINISH, 'PERFED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  201. } elseif ($type == 'fail') {
  202. PeriodPrepare::updateAll(['IS_PERFING' => 0, 'IS_PERFED' => Period::PERF_FAIL, 'PERFED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  203. }
  204. }
  205. public function checkCanCalcPref($periodNum = null) {
  206. // 先判断时间,现在是周几,如果不是周日,则直接结束
  207. // 如果是手动触发,即有业绩期$periodNum变量值,则不校验必须周日0点
  208. $w = $h = '';
  209. if (empty($periodNum)) {
  210. $w = date('w',time());
  211. if ($w != '0') {
  212. return false;
  213. }
  214. // 判断是否是周日0点
  215. $h = date('H',time());
  216. if ($h !=0 ) {
  217. return false;
  218. }
  219. }
  220. // 判断此结算周期是否是月结算节点
  221. // 这里 如果定时任务业绩期是nul
  222. $periodObj = Period::instance();
  223. $periodDataArr = $periodObj->setPeriodNum($periodNum);
  224. if ($periodDataArr['IS_MONTH'] != 1) {
  225. return false;
  226. }
  227. // 如果是手动进行计算得月业绩,则不需要判断结算当前状态
  228. // 判断此结算周期是否已经进行业绩计算,如果已进行或者进行过,则不能继续执行
  229. if (empty($periodNum)) {
  230. $prepare = PeriodPrepare::find()
  231. ->select('IS_MONTH, IS_PERFING,IS_PERFED')
  232. ->where(
  233. 'PERIOD_NUM=:PERIOD_NUM',
  234. [
  235. 'PERIOD_NUM'=>$periodDataArr['PERIOD_NUM']
  236. ])
  237. ->asArray()
  238. ->one();
  239. // 如果正在结算或者已计算完,则不进行计算,因为预计计算是定时任务每5秒扫一次,所以不能再触发预计算业绩
  240. if ($prepare['IS_PERFING'] == 1 || $prepare['IS_PERFED'] == 1) {
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. /**
  247. * 初始化结算任务
  248. * @throws \yii\db\Exception
  249. */
  250. public function initCalcTask($periodNum = null) {
  251. $periodObj = Period::instance();
  252. $periodDataArr = $periodObj->setPeriodNum($periodNum);
  253. $this->_lastTime = $periodDataArr['END_TIME'];
  254. $this->_periodNum = $periodDataArr['PERIOD_NUM'];
  255. $this->_sysConfig = Cache::getSystemConfig();
  256. $this->_decLevelConfig = Cache::getDecLevelConfig();
  257. $this->_empLevelConfig = Cache::getEmpLevelConfig();
  258. $this->_decRoleConfig = PrepareCalcCache::getDecRoleConfig($this->_periodNum);
  259. $periodNum = $this->_periodNum;
  260. $this->_periodId = $periodDataArr['ID'];
  261. $this->_isCalcMonth = $periodObj->isCalcMonth($periodNum);
  262. $this->_calcYear = $periodObj->getYear($periodNum);
  263. $this->_calcMonth = $periodObj->getMonth($periodNum);
  264. $this->_calcYearMonth = $periodObj->getYearMonth($periodNum);
  265. $lastYearMonthArr = $periodObj->getLastMonth($periodNum);
  266. $this->_lastCalcYear = $lastYearMonthArr['year'];
  267. $this->_lastCalcMonth = $lastYearMonthArr['month'];
  268. $this->_lastCalcYearMonth = $lastYearMonthArr['yearMonth'];
  269. $this->_lastPeriodNum = $periodNum - 1;
  270. if (Period::isExistsPeriodNum($this->_lastPeriodNum)) {
  271. $this->_lastPeriodYear = $periodObj->getYear($this->_lastPeriodNum);
  272. $this->_lastPeriodMonth = $periodObj->getMonth($this->_lastPeriodNum);
  273. $this->_lastPeriodYearMonth = $periodObj->getYearMonth($this->_lastPeriodNum);
  274. } else {
  275. $this->_lastPeriodYear = 0;
  276. $this->_lastPeriodMonth = 0;
  277. $this->_lastPeriodYearMonth = 0;
  278. }
  279. $this->_pvRatio = $this->_sysConfig['pvRatio']['VALUE'];
  280. }
  281. /**
  282. * 清空相关表数据
  283. */
  284. public function clearTableData() {
  285. // 周业绩
  286. PerfPeriodPrepare::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  287. // 业绩单
  288. PerfOrderPrepare::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  289. // 月结时要清空的数据
  290. if ($this->_isCalcMonth) {
  291. // 月业绩表
  292. PerfMonthPrepare::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  293. }
  294. }
  295. /**
  296. * 周结,向上级算业绩,并计入业绩单
  297. * @param int $offset
  298. * @return bool
  299. * @throws \yii\db\Exception
  300. */
  301. public function loopCalcPeriodPerfByDecOrder($offset = 0) {
  302. // 循环获取全部报单
  303. $allData = DecOrder::findUseDbCalc()
  304. ->select('ID,DEC_SN,ORDER_SN,USER_ID,TYPE,TO_USER_ID,IS_ADMIN,DEC_AMOUNT,DEC_PV,PERIOD_NUM,
  305. CALC_MONTH,IS_DEL,P_CALC_MONTH,CREATED_AT,DEC_ID')
  306. ->where(
  307. "PERIOD_NUM=:PERIOD_NUM AND IS_DEL=0 AND TYPE='ZC'",
  308. [':PERIOD_NUM' => $this->_periodNum]
  309. )
  310. ->orderBy('CREATED_AT DESC,ID DESC')
  311. ->offset($offset)
  312. ->limit($this->_limit)
  313. ->asArray()
  314. ->all();
  315. if ($allData) {
  316. $insertPerfOrderData = [];
  317. foreach ($allData as $data) {
  318. // 是否关停等状态不能拿业绩
  319. if (!$this->isHasPerf($data['TO_USER_ID'])) {
  320. continue;
  321. }
  322. // 给自己增加PCS(个人消费)
  323. PrepareCalcCache::nowPeriodPerf($data['TO_USER_ID'], $this->_periodNum, [
  324. 'PV_PCS' => $data['DEC_PV'],
  325. 'PV_PCS_ZC' => $data['DEC_PV'],
  326. ]);
  327. // 把该会员加入到能拿到业绩的会员缓存中
  328. PrepareCalcCache::addHasPerfUsers($data['TO_USER_ID'], $this->_periodNum);
  329. //加入到报单会员中
  330. $toInfo = PrepareCalcCache::getUserInfo($data['TO_USER_ID'], $this->_periodNum);
  331. PrepareCalcCache::addHasBDUsers($data['TO_USER_ID'], $this->_periodNum, [
  332. 'TO_USER_ID' => $data['TO_USER_ID'],
  333. 'USER_ID' => $data['USER_ID'],
  334. 'DEC_ID' => $data['DEC_ID'],
  335. //考虑可能会移网的情况
  336. 'REC_USER_ID' => $toInfo['REC_UID'] ?? '',
  337. 'CON_USER_ID' => $toInfo['CON_UID'] ?? '',
  338. 'DEC_AMOUNT' => $data['DEC_AMOUNT'],
  339. 'DEC_PV' => $data['DEC_PV'],
  340. ]);
  341. // 给上追加业绩
  342. $this->loopNetworkParentDo($data['TO_USER_ID'], function ($parent) use (&$data) {
  343. // 给上级会员追加本期业绩到缓存中
  344. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  345. 'PV_' . $parent['LOCATION'] . 'L' => $data['DEC_PV'],
  346. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $data['DEC_PV'],
  347. 'PV_' . $parent['LOCATION'] . 'L_' . $data['TYPE'] => $data['DEC_PV'],
  348. ]);
  349. // 把该会员加入到能拿到业绩的会员缓存中
  350. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  351. unset($parent);
  352. });
  353. //给推荐关系累计增加业绩
  354. $this->loopRelationParentDo($data['TO_USER_ID'], function ($parent) use (&$data) {
  355. // 给上级会员追加本期业绩到缓存中
  356. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  357. 'PV_PSS' => $data['DEC_PV'],
  358. ]);
  359. // 把该会员加入到能拿到业绩的会员缓存中
  360. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  361. unset($parent);
  362. });
  363. // 写入业绩单表
  364. $decInfo = PrepareCalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  365. $sn = PerfOrderPrepare::generateSN();
  366. $insertPerfOrderData[] = [
  367. 'ID' => SnowFake::instance()->generateId(),
  368. 'SN' => $sn,
  369. 'DEC_SN' => $data['DEC_SN'],
  370. 'DEC_TYPE' => strtoupper($data['TYPE']),
  371. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  372. 'USER_ID' => $data['TO_USER_ID'],
  373. 'LAST_REC_USER_NAME' => $toInfo['REC_USER_NAME'],
  374. 'LAST_REC_REAL_NAME' => $toInfo['REC_REAL_NAME'],
  375. 'LAST_DEC_LV' => $toInfo['DEC_LV'],
  376. 'LAST_EMP_LV' => $toInfo['EMP_LV'],
  377. 'LAST_STATUS' => $toInfo['STATUS'],
  378. 'PV' => $data['DEC_PV'],
  379. 'DEC_AMOUNT' => $data['DEC_AMOUNT'],
  380. 'LAST_SUB_COM_ID' => $toInfo['SUB_COM_ID'],
  381. 'LAST_PROVINCE' => $toInfo['PROVINCE'],
  382. 'LAST_CITY' => $toInfo['CITY'],
  383. 'LAST_COUNTY' => $toInfo['COUNTY'],
  384. 'DEC_USER_ID' => $data['USER_ID'],
  385. 'LAST_DEC_DEC_LV' => $decInfo['DEC_LV'],
  386. 'LAST_DEC_SUB_COM_ID' => $decInfo['SUB_COM_ID'],
  387. 'LAST_DEC_PROVINCE' => $decInfo['DEC_PROVINCE'],
  388. 'LAST_DEC_CITY' => $decInfo['DEC_CITY'],
  389. 'LAST_DEC_COUNTY' => $decInfo['DEC_COUNTY'],
  390. 'PERIOD_NUM' => $this->_periodNum,
  391. 'CALC_MONTH' => $this->_calcYearMonth,
  392. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  393. 'CREATED_AT' => Date::nowTime(),
  394. 'CLOSED_AT' => 0,
  395. 'ORDER_CREATED_AT' => $data['CREATED_AT'],
  396. ];
  397. unset($data, $decInfo, $sn, $toInfo);
  398. }
  399. PerfOrderPrepare::batchInsert($insertPerfOrderData);
  400. unset($insertPerfOrderData, $allData, $snArr);
  401. return $this->loopCalcPeriodPerfByDecOrder($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 loopCalcPeriodPerfByOrderDec($offset = 0) {
  413. // 循环获取全部报单
  414. $allData = OrderDec::findUseDbCalc()
  415. ->select('CREATED_AT,ID,SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PV,PAY_AMOUNT,PAY_PV,PERIOD_NUM,PAY_TYPE')
  416. ->where(
  417. "PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  418. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>'ZC'])
  419. ->orderBy('ID DESC')
  420. ->offset($offset)
  421. ->limit($this->_limit)
  422. ->asArray()
  423. ->all();
  424. if ($allData) {
  425. $insertPerfOrderData = [];
  426. foreach ($allData as $data) {
  427. // 是否关停等状态不能拿业绩
  428. if (!$this->isHasPerf($data['USER_ID'])) {
  429. continue;
  430. }
  431. // 给自己增加PCS(个人消费)
  432. PrepareCalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  433. 'PV_PCS' => $data['PAY_PV'],
  434. 'PV_PCS_ZC' => $data['PAY_PV'],
  435. ]);
  436. // 把该会员加入到能拿到业绩的会员缓存中
  437. PrepareCalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  438. //加入到报单会员中
  439. $toInfo = PrepareCalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  440. PrepareCalcCache::addHasBDUsers($data['USER_ID'], $this->_periodNum, [
  441. 'TO_USER_ID' => $data['USER_ID'],
  442. 'USER_ID' => $toInfo['DEC_ID'],
  443. 'DEC_ID' => $toInfo['DEC_ID'],
  444. //考虑可能会移网的情况
  445. 'REC_USER_ID' => $toInfo['REC_UID'] ?? '',
  446. 'CON_USER_ID' => $toInfo['CON_UID'] ?? '',
  447. 'DEC_AMOUNT' => $data['ORDER_AMOUNT'],
  448. 'DEC_PV' => $data['PAY_PV'],
  449. ]);
  450. // 给上追加业绩
  451. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data) {
  452. // 给上级会员追加本期业绩到缓存中
  453. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  454. 'PV_' . $parent['LOCATION'] . 'L' => $data['PAY_PV'],
  455. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $data['PAY_PV'],
  456. 'PV_' . $parent['LOCATION'] . 'L_' . $data['ORDER_TYPE'] => $data['PAY_PV'],
  457. ]);
  458. // 把该会员加入到能拿到业绩的会员缓存中
  459. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  460. unset($parent);
  461. });
  462. //给推荐关系累计增加业绩
  463. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use (&$data) {
  464. // 给上级会员追加本期业绩到缓存中
  465. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  466. 'PV_PSS' => $data['PAY_PV'],
  467. ]);
  468. // 把该会员加入到能拿到业绩的会员缓存中
  469. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  470. unset($parent);
  471. });
  472. // 写入业绩单表
  473. $decInfo = PrepareCalcCache::getUserInfo($toInfo['DEC_ID'], $this->_periodNum);
  474. $sn = PerfOrderPrepare::generateSN();
  475. $insertPerfOrderData[] = [
  476. 'ID' => SnowFake::instance()->generateId(),
  477. 'SN' => $sn,
  478. 'DEC_SN' => $data['SN'],
  479. 'DEC_TYPE' => strtoupper($data['ORDER_TYPE']),
  480. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  481. 'USER_ID' => $data['USER_ID'],
  482. 'LAST_REC_USER_NAME' => $toInfo['REC_USER_NAME'],
  483. 'LAST_REC_REAL_NAME' => $toInfo['REC_REAL_NAME'],
  484. 'LAST_DEC_LV' => $toInfo['DEC_LV'],
  485. 'LAST_EMP_LV' => $toInfo['EMP_LV'],
  486. 'LAST_STATUS' => $toInfo['STATUS'],
  487. 'PV' => $data['PAY_PV'],
  488. 'DEC_AMOUNT' => $data['ORDER_AMOUNT'],
  489. 'LAST_SUB_COM_ID' => $toInfo['SUB_COM_ID'],
  490. 'LAST_PROVINCE' => $toInfo['PROVINCE'],
  491. 'LAST_CITY' => $toInfo['CITY'],
  492. 'LAST_COUNTY' => $toInfo['COUNTY'],
  493. 'DEC_USER_ID' => $toInfo['DEC_ID'],
  494. 'LAST_DEC_DEC_LV' => $decInfo['DEC_LV'],
  495. 'LAST_DEC_SUB_COM_ID' => $decInfo['SUB_COM_ID'],
  496. 'LAST_DEC_PROVINCE' => $decInfo['DEC_PROVINCE'],
  497. 'LAST_DEC_CITY' => $decInfo['DEC_CITY'],
  498. 'LAST_DEC_COUNTY' => $decInfo['DEC_COUNTY'],
  499. 'PERIOD_NUM' => $this->_periodNum,
  500. 'CALC_MONTH' => $this->_calcYearMonth,
  501. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  502. 'CREATED_AT' => Date::nowTime(),
  503. 'CLOSED_AT' => 0,
  504. 'ORDER_CREATED_AT' => $data['CREATED_AT']
  505. ];
  506. unset($data, $decInfo, $sn, $toInfo);
  507. }
  508. PerfOrderPrepare::batchInsert($insertPerfOrderData);
  509. unset($insertPerfOrderData, $allData, $snArr);
  510. return $this->loopCalcPeriodPerfByOrderDec($offset + $this->_limit);
  511. }
  512. unset($allData);
  513. return true;
  514. }
  515. /**
  516. * 从会员的复销订单会员计算复销业绩并加入业绩单
  517. * @param int $offset
  518. * @return bool
  519. * @throws \yii\db\Exception
  520. */
  521. public function loopCalcPerfByFXOrder(int $offset = 0) {
  522. // 循环获取全部报单
  523. $allData = Order::findUseDbCalc()
  524. ->select('ID,SN,DEC_SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PAY_AMOUNT,PAY_PV,PAY_TYPE,PERIOD_NUM,STATUS,IS_DELETE,
  525. P_CALC_MONTH,CREATED_AT')
  526. ->where(
  527. "PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  528. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>DeclarationForm::TYPE_FX]
  529. )
  530. ->orderBy('CREATED_AT DESC,ID DESC')
  531. ->offset($offset)
  532. ->limit($this->_limit)
  533. ->asArray()
  534. ->all();
  535. if ($allData) {
  536. $insertPerfOrderData = [];
  537. foreach ($allData as $data) {
  538. // 是否关停等状态不能拿业绩
  539. if (!$this->isHasPerf($data['USER_ID'])) {
  540. continue;
  541. }
  542. //如果支付方式是现金,那么实际业绩是支付PV的50%
  543. if( $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_CASH ) {
  544. $orderCashAmount = $data['ORDER_AMOUNT'];
  545. //111期开始由50%改为60%-by 2020-04-30修改
  546. $payPv = $data['PAY_PV'] * $this->_sysConfig['cashReconsumeBonusPercent']['VALUE'] / 100;
  547. $cacheDataKey = 'PV_PCS_FX_CASH';
  548. }else {
  549. $orderCashAmount = 0;
  550. $payPv = $data['PAY_PV'];
  551. $cacheDataKey = 'PV_PCS_FX_POINT';
  552. }
  553. if( $payPv <= 0 ) continue;
  554. // 给自己增加PCS(个人消费)
  555. PrepareCalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  556. 'FX_AMOUNT_CASH' => $orderCashAmount,
  557. 'PV_PCS' => $payPv,
  558. 'PV_PCS_FX' => $payPv,
  559. $cacheDataKey => $payPv,
  560. ]);
  561. // 把该会员加入到能拿到业绩的会员缓存中
  562. PrepareCalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  563. // 给上追加业绩
  564. try {
  565. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data, $payPv) {
  566. // 给上级会员追加本期业绩到缓存中
  567. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  568. 'PV_' . $parent['LOCATION'] . 'L' => $payPv,
  569. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $payPv,
  570. 'PV_' . $parent['LOCATION'] . 'L_FX' => $payPv,
  571. ]);
  572. // 把该会员加入到能拿到业绩的会员缓存中
  573. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  574. });
  575. } catch(\Exception $e) {
  576. file_put_contents('loopNetworkParentDo_error.txt', var_export([
  577. 'USER_ID' => $data['USER_ID'],
  578. '_periodNum' => $this->_periodNum,
  579. 'error' => $e->getMessage()
  580. ],true));
  581. }
  582. //给推荐关系累计增加业绩
  583. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use ($data, $payPv) {
  584. // 给上级会员追加本期业绩到缓存中
  585. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  586. 'PV_PSS' => $payPv,
  587. ]);
  588. // 把该会员加入到能拿到业绩的会员缓存中
  589. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  590. });
  591. // }
  592. // 写入业绩单表
  593. $baseInfo = PrepareCalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  594. $sn = PerfOrderPrepare::generateSN();
  595. $insertPerfOrderData[] = [
  596. 'ID' => SnowFake::instance()->generateId(),
  597. 'SN' => $sn,
  598. 'DEC_SN' => null,
  599. 'DEC_TYPE' => 'FX',
  600. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  601. 'USER_ID' => $data['USER_ID'],
  602. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  603. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  604. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  605. 'LAST_EMP_LV' => $baseInfo['EMP_LV'],
  606. 'LAST_STATUS' => $baseInfo['STATUS'],
  607. 'PV' => $payPv,
  608. 'DEC_AMOUNT' => $data['PAY_AMOUNT'],
  609. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  610. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  611. 'LAST_CITY' => $baseInfo['CITY'],
  612. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  613. 'DEC_USER_ID' => $data['USER_ID'],
  614. 'LAST_DEC_DEC_LV' => $baseInfo['DEC_LV'],
  615. 'LAST_DEC_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  616. 'LAST_DEC_PROVINCE' => $baseInfo['PROVINCE'],
  617. 'LAST_DEC_CITY' => $baseInfo['CITY'],
  618. 'LAST_DEC_COUNTY' => $baseInfo['COUNTY'],
  619. 'PERIOD_NUM' => $this->_periodNum,
  620. 'CALC_MONTH' => $this->_calcYearMonth,
  621. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  622. 'CREATED_AT' => Date::nowTime(),
  623. 'CLOSED_AT' => 0,
  624. 'ORDER_CREATED_AT' => $data['CREATED_AT']
  625. ];
  626. unset($data, $baseInfo, $sn, $orderCashAmount, $payPv, $cacheDataKey);
  627. }
  628. PerfOrderPrepare::batchInsert($insertPerfOrderData);
  629. unset($insertPerfOrderData, $allData, $snArr);
  630. return $this->loopCalcPerfByFXOrder($offset + $this->_limit);
  631. }
  632. unset($allData);
  633. return true;
  634. }
  635. /**
  636. * 从会员的商城复销订单会员计算复销业绩并加入业绩单
  637. * @param int $offset
  638. * @return bool
  639. * @throws \yii\db\Exception
  640. */
  641. public function loopCalcPerfByShopFXOrder(int $offset = 0) {
  642. // 循环获取全部报单
  643. $allData = OrderShop::findUseDbCalc()
  644. ->select(
  645. 'ID,SN,DEC_SN,USER_ID,ORDER_TYPE,ORDER_AMOUNT,PAY_AMOUNT,PAY_PV,PAY_TYPE,PERIOD_NUM,STATUS,IS_DELETE,
  646. P_CALC_MONTH,CREATED_AT')
  647. ->where(
  648. "PERIOD_NUM=:PERIOD_NUM AND IS_DELETE=0 AND ORDER_TYPE=:ORDER_TYPE",
  649. [':PERIOD_NUM' => $this->_periodNum, ':ORDER_TYPE'=>DeclarationForm::TYPE_FX]
  650. )
  651. ->orderBy('CREATED_AT DESC,ID DESC')
  652. ->offset($offset)
  653. ->limit($this->_limit)
  654. ->asArray()
  655. ->all();
  656. if ($allData) {
  657. $insertPerfOrderData = [];
  658. foreach ($allData as $data) {
  659. // 是否关停等状态不能拿业绩
  660. if (!$this->isHasPerf($data['USER_ID'])) {
  661. continue;
  662. }
  663. //如果支付方式是现金,那么实际业绩是支付PV的50%
  664. if( $data['PAY_TYPE'] === self::ORDER_PAY_TYPE_CASH ) {
  665. $orderCashAmount = $data['ORDER_AMOUNT'];
  666. //111期开始由50%改为60%-by 2020-04-30修改
  667. $payPv = $data['PAY_PV'] * $this->_sysConfig['cashReconsumeBonusPercent']['VALUE'] / 100;
  668. $cacheDataKey = 'PV_PCS_FX_CASH';
  669. }else {
  670. $orderCashAmount = 0;
  671. $payPv = $data['PAY_PV'];
  672. $cacheDataKey = 'PV_PCS_FX_POINT';
  673. }
  674. if( $payPv <= 0 ) continue;
  675. // 给自己增加PCS(个人消费)
  676. PrepareCalcCache::nowPeriodPerf($data['USER_ID'], $this->_periodNum, [
  677. 'FX_AMOUNT_CASH' => $orderCashAmount,
  678. 'PV_PCS' => $payPv,
  679. 'PV_PCS_FX' => $payPv,
  680. $cacheDataKey => $payPv,
  681. ]);
  682. // 把该会员加入到能拿到业绩的会员缓存中
  683. PrepareCalcCache::addHasPerfUsers($data['USER_ID'], $this->_periodNum);
  684. // 给上追加业绩
  685. $this->loopNetworkParentDo($data['USER_ID'], function ($parent) use (&$data, $payPv) {
  686. // 给上级会员追加本期业绩到缓存中
  687. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  688. 'PV_' . $parent['LOCATION'] . 'L' => $payPv,
  689. 'PV_' . $parent['LOCATION'] . 'L_TOUCH' => $payPv,
  690. 'PV_' . $parent['LOCATION'] . 'L_FX' => $payPv,
  691. ]);
  692. // 把该会员加入到能拿到业绩的会员缓存中
  693. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  694. });
  695. //给推荐关系累计增加业绩
  696. $this->loopRelationParentDo($data['USER_ID'], function ($parent) use ($data, $payPv) {
  697. // 给上级会员追加本期业绩到缓存中
  698. PrepareCalcCache::nowPeriodPerf($parent['PARENT_UID'], $this->_periodNum, [
  699. 'PV_PSS' => $payPv,
  700. ]);
  701. // 把该会员加入到能拿到业绩的会员缓存中
  702. PrepareCalcCache::addHasPerfUsers($parent['PARENT_UID'], $this->_periodNum);
  703. });
  704. // }
  705. // 写入业绩单表
  706. $baseInfo = PrepareCalcCache::getUserInfo($data['USER_ID'], $this->_periodNum);
  707. $sn = PerfOrderPrepare::generateSN();
  708. $insertPerfOrderData[] = [
  709. 'ID' => SnowFake::instance()->generateId(),
  710. 'SN' => $sn,
  711. 'DEC_SN' => null,
  712. 'DEC_TYPE' => 'FX',
  713. 'DEC_STATUS' => PerfOrder::STATUS_NORMAL,
  714. 'USER_ID' => $data['USER_ID'],
  715. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  716. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  717. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  718. 'LAST_EMP_LV' => $baseInfo['EMP_LV'],
  719. 'LAST_STATUS' => $baseInfo['STATUS'],
  720. 'PV' => $payPv,
  721. 'DEC_AMOUNT' => $data['PAY_AMOUNT'],
  722. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  723. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  724. 'LAST_CITY' => $baseInfo['CITY'],
  725. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  726. 'DEC_USER_ID' => $data['USER_ID'],
  727. 'LAST_DEC_DEC_LV' => $baseInfo['DEC_LV'],
  728. 'LAST_DEC_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  729. 'LAST_DEC_PROVINCE' => $baseInfo['PROVINCE'],
  730. 'LAST_DEC_CITY' => $baseInfo['CITY'],
  731. 'LAST_DEC_COUNTY' => $baseInfo['COUNTY'],
  732. 'PERIOD_NUM' => $this->_periodNum,
  733. 'CALC_MONTH' => $this->_calcYearMonth,
  734. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  735. 'CREATED_AT' => Date::nowTime(),
  736. 'CLOSED_AT' => 0,
  737. 'ORDER_CREATED_AT' => $data['CREATED_AT']
  738. ];
  739. unset($data, $baseInfo, $sn, $orderCashAmount, $payPv, $cacheDataKey);
  740. }
  741. PerfOrderPrepare::batchInsert($insertPerfOrderData);
  742. unset($insertPerfOrderData, $allData, $snArr);
  743. return $this->loopCalcPerfByShopFXOrder($offset + $this->_limit);
  744. }
  745. unset($allData);
  746. return true;
  747. }
  748. /**
  749. * 计算月业绩表相关的数据并写入数据库
  750. * @param int $offset
  751. * @return bool
  752. * @throws Exception
  753. * @throws \yii\db\Exception
  754. */
  755. public function loopCalcMonthPerfTableData(int $offset = 0) {
  756. if (!$this->_isCalcMonth) {
  757. return true;
  758. }
  759. // AR_PERF_ORDER_PREPARE\AR_PERF_PERIOD_PREPARE\AR_PERIOD_PREPARE\AR_PERF_MONTH_PREPARE
  760. // AR_PERIOD_PREPARE 表只需要是结算月的数据
  761. // AR_PERF_PERIOD_PREPARE 表特殊,需要增加一个字段,是否存在于前三期中
  762. // `IS_HAS_USER` tinyint(1) NOT NULL DEFAULT 0 COMMENT '是否有此用户,默认为0没有此用户1为有
  763. // 如果依旧是0则再次循环此月业绩的时候,需要再次处理'
  764. //
  765. // 月业绩由perfperiod表加上perfperiodprepare表之和,其中perfperiod表数据为此月非计算月的期业绩数据
  766. // 先查询perfperiod表的数据,再查询perfperiodprepare这个表的数据.
  767. // 如果查询到了perfperiodprepare的数据,则将此perfperiodprepare表的新增的是否统计到的字段标识为1.
  768. // 最后再循环 perfperiodprepare 表中标识依旧为0的数据,再次循环.相当于这些数据是此结算月业绩期的新增用户
  769. echo sprintf("时间:[%s]月业绩,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  770. // 从缓存列表里面从底层往上倒序获取会员
  771. $allData = PerfPeriod::findUseDbCalc()
  772. ->select('USER_ID, SUM(FX_AMOUNT_CASH) AS FX_AMOUNT_CASH_SUM,SUM(PV_PCS) AS PV_PCS_SUM,
  773. SUM(PV_PCS_FX) AS PV_PCS_FX_SUM,SUM(PV_PSS) AS PV_PSS_SUM,
  774. SUM(PV_1L) AS PV_1L_SUM,SUM(PV_2L) AS PV_2L_SUM,
  775. SUM(PV_3L) AS PV_3L_SUM,SUM(PV_4L) AS PV_4L_SUM,
  776. SUM(PV_5L) AS PV_5L_SUM,SUM(PV_1L_ZC) AS PV_1L_ZC_SUM,
  777. SUM(PV_2L_ZC) AS PV_2L_ZC_SUM,SUM(PV_3L_ZC) AS PV_3L_ZC_SUM,
  778. SUM(PV_4L_ZC) AS PV_4L_ZC_SUM,SUM(PV_5L_ZC) AS PV_5L_ZC_SUM')
  779. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  780. ->groupBy('USER_ID')
  781. ->orderBy('USER_ID DESC')
  782. ->offset($offset)
  783. ->limit($this->_limit)
  784. ->asArray()
  785. ->all();
  786. if ($allData) {
  787. // 月度业绩表
  788. foreach ($allData as $everyData) {
  789. $userId = $everyData['USER_ID'];
  790. //往期业绩
  791. $userLastPerf = PrepareCalcCache::userPerf($userId, $this->_periodNum);
  792. //本期业绩
  793. $periodPerf = PrepareCalcCache::nowPeriodPerf($userId, $this->_periodNum);
  794. $userBaseInfo = PrepareCalcCache::getUserInfo($userId, $this->_periodNum);
  795. //级别必须为VIP
  796. $isVip = false;
  797. if( $userBaseInfo['DEC_LV'] === DeclarationLevel::VIP_LEVEL_ID ) {
  798. $isVip= true;
  799. }
  800. if( $this->_sysConfig['vipBonusGoldDecLevel']['VALUE'] && $userBaseInfo['DEC_LV'] === DeclarationLevel::JIN_ZUAN_LEVEL_ID ) {
  801. $isVip = true;
  802. }
  803. // 查询perfperiodprepare表中数据,是否有此用户信息.因为是月提前结算,所以此结算期是只有一个数据
  804. $monthPrepare = PerfPeriodPrepare::findUseDbCalc()
  805. ->select('USER_ID, SUM(FX_AMOUNT_CASH) AS FX_AMOUNT_CASH_SUM,SUM(PV_PCS) AS PV_PCS_SUM,
  806. SUM(PV_PCS_FX) AS PV_PCS_FX_SUM,SUM(PV_PSS) AS PV_PSS_SUM,
  807. SUM(PV_1L) AS PV_1L_SUM,SUM(PV_2L) AS PV_2L_SUM,
  808. SUM(PV_3L) AS PV_3L_SUM,SUM(PV_4L) AS PV_4L_SUM,
  809. SUM(PV_5L) AS PV_5L_SUM,SUM(PV_1L_ZC) AS PV_1L_ZC_SUM,
  810. SUM(PV_2L_ZC) AS PV_2L_ZC_SUM,SUM(PV_3L_ZC) AS PV_3L_ZC_SUM,
  811. SUM(PV_4L_ZC) AS PV_4L_ZC_SUM,SUM(PV_5L_ZC) AS PV_5L_ZC_SUM')
  812. ->where('USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM ', [':USER_ID' => $userId, ':PERIOD_NUM' => $this->_periodNum])
  813. ->asArray()
  814. ->one();
  815. if (!empty($monthPrepare)) {
  816. // 如果前三期中,在此提前计算业绩期中有数据,则需要加上此用户信息
  817. $everyData['FX_AMOUNT_CASH_SUM'] = $everyData['FX_AMOUNT_CASH_SUM'] + $monthPrepare['FX_AMOUNT_CASH_SUM'];
  818. $everyData['PV_PCS_SUM'] = $everyData['PV_PCS_SUM'] + $monthPrepare['PV_PCS_SUM'];
  819. $everyData['PV_PCS_FX_SUM'] = $everyData['PV_PCS_FX_SUM'] + $monthPrepare['PV_PCS_FX_SUM'];
  820. $everyData['PV_PSS_SUM'] = $everyData['PV_PSS_SUM'] + $monthPrepare['PV_PSS_SUM'];
  821. $everyData['PV_1L_SUM'] = $everyData['PV_1L_SUM'] + $monthPrepare['PV_1L_SUM'];
  822. $everyData['PV_2L_SUM'] = $everyData['PV_2L_SUM'] + $monthPrepare['PV_2L_SUM'];
  823. $everyData['PV_3L_SUM'] = $everyData['PV_3L_SUM'] + $monthPrepare['PV_3L_SUM'];
  824. $everyData['PV_4L_SUM'] = $everyData['PV_4L_SUM'] + $monthPrepare['PV_4L_SUM'];
  825. $everyData['PV_5L_SUM'] = $everyData['PV_5L_SUM'] + $monthPrepare['PV_5L_SUM'];
  826. $everyData['PV_1L_ZC_SUM'] = $everyData['PV_1L_ZC_SUM'] + $monthPrepare['PV_1L_ZC_SUM'];
  827. $everyData['PV_2L_ZC_SUM'] = $everyData['PV_2L_ZC_SUM'] + $monthPrepare['PV_2L_ZC_SUM'];
  828. $everyData['PV_3L_ZC_SUM'] = $everyData['PV_3L_ZC_SUM'] + $monthPrepare['PV_3L_ZC_SUM'];
  829. $everyData['PV_4L_ZC_SUM'] = $everyData['PV_4L_ZC_SUM'] + $monthPrepare['PV_4L_ZC_SUM'];
  830. $everyData['PV_5L_ZC_SUM'] = $everyData['PV_5L_ZC_SUM'] + $monthPrepare['PV_5L_ZC_SUM'];
  831. // 将用户的状态改成1,为存在此用户.当之后再次循环PerfPeriodPrepare的时候,不再循环此用户了.
  832. PerfPeriodPrepare::updateAll(
  833. [
  834. 'IS_HAS_USER' => 1
  835. ],
  836. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  837. [':PERIOD_NUM' => $this->_periodNum, ':USER_ID' => $userId]
  838. );
  839. }
  840. $nowMonthPerf = [
  841. 'USER_ID' => $userId,
  842. 'FX_AMOUNT_CASH' => $everyData['FX_AMOUNT_CASH_SUM'],
  843. 'PV_PCS' => $everyData['PV_PCS_SUM'],
  844. 'PV_PCS_FX' => $everyData['PV_PCS_FX_SUM'],
  845. 'PV_PSS' => $everyData['PV_PSS_SUM'],
  846. 'PV_1L' => $everyData['PV_1L_SUM'],
  847. 'PV_2L' => $everyData['PV_2L_SUM'],
  848. 'PV_3L' => $everyData['PV_3L_SUM'],
  849. 'PV_4L' => $everyData['PV_4L_SUM'],
  850. 'PV_5L' => $everyData['PV_5L_SUM'],
  851. //VIP统计相关业绩
  852. 'VIP_PV_1L_ZC' => $isVip ? $everyData['PV_1L_ZC_SUM'] : 0,
  853. 'VIP_PV_2L_ZC' => $isVip ? $everyData['PV_2L_ZC_SUM'] : 0,
  854. 'VIP_PV_3L_ZC' => $isVip ? $everyData['PV_3L_ZC_SUM'] : 0,
  855. 'VIP_PV_4L_ZC' => $isVip ? $everyData['PV_4L_ZC_SUM'] : 0,
  856. 'VIP_PV_5L_ZC' => $isVip ? $everyData['PV_5L_ZC_SUM'] : 0,
  857. //总数据,历史+本期。不能用上月加本月,因为上月可能没业绩,上上个月有业绩。
  858. 'PV_1L_TOTAL' => $periodPerf['PV_1L'] + $userLastPerf['PV_1L'],
  859. 'PV_2L_TOTAL' => $periodPerf['PV_2L'] + $userLastPerf['PV_2L'],
  860. 'PV_3L_TOTAL' => $periodPerf['PV_3L'] + $userLastPerf['PV_3L'],
  861. 'PV_4L_TOTAL' => $periodPerf['PV_4L'] + $userLastPerf['PV_4L'],
  862. 'PV_5L_TOTAL' => $periodPerf['PV_5L'] + $userLastPerf['PV_5L'],
  863. 'PV_PSS_TOTAL' => $periodPerf['PV_PSS'] + $userLastPerf['PV_PSS_TOTAL'],
  864. ];
  865. // 把会员的月业绩写入缓存中,以便下面的奖金计算从缓冲中获取数据效率高
  866. PrepareCalcCache::addHasMonthPerfUsers($userId, $this->_periodNum);
  867. PrepareCalcCache::nowMonthPerf($userId, $this->_periodNum, $nowMonthPerf);
  868. unset($userId, $everyData, $nowMonthPerf, $lastMonthData, $userBaseInfo, $isVip);
  869. }
  870. unset($allData);
  871. $this->loopCalcMonthPerfTableData($offset + $this->_limit);
  872. }
  873. unset($allData);
  874. return true;
  875. }
  876. /**
  877. * 预计计算月业绩,当前三期没有此最新业绩单的用户
  878. * 再次添加进去
  879. * @param int $offset
  880. * @return bool
  881. * @throws Exception
  882. * @throws \yii\db\Exception
  883. */
  884. public function loopCalcMonthPerfTableDataPrepare(int $offset = 0) {
  885. if (!$this->_isCalcMonth) {
  886. return true;
  887. }
  888. // 上面的计算月业绩,计算了前三期有的用户,此次只计算此结算周期未含有前三期业绩的用户
  889. echo sprintf("时间:[%s]预计月业绩,结算月新添加的用户,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  890. // 从缓存列表里面从底层往上倒序获取会员 IS_HAS_USER 0 代表是此结算周期新增的业绩
  891. $allData = PerfPeriodPrepare::findUseDbCalc()
  892. ->select('USER_ID, SUM(FX_AMOUNT_CASH) AS FX_AMOUNT_CASH_SUM,SUM(PV_PCS) AS PV_PCS_SUM,
  893. SUM(PV_PCS_FX) AS PV_PCS_FX_SUM,SUM(PV_PSS) AS PV_PSS_SUM,
  894. SUM(PV_1L) AS PV_1L_SUM,SUM(PV_2L) AS PV_2L_SUM,
  895. SUM(PV_3L) AS PV_3L_SUM,SUM(PV_4L) AS PV_4L_SUM,
  896. SUM(PV_5L) AS PV_5L_SUM,SUM(PV_1L_ZC) AS PV_1L_ZC_SUM,
  897. SUM(PV_2L_ZC) AS PV_2L_ZC_SUM,SUM(PV_3L_ZC) AS PV_3L_ZC_SUM,
  898. SUM(PV_4L_ZC) AS PV_4L_ZC_SUM,SUM(PV_5L_ZC) AS PV_5L_ZC_SUM')
  899. ->where('CALC_MONTH=:CALC_MONTH AND IS_HAS_USER=:IS_HAS_USER', [':CALC_MONTH' => $this->_calcYearMonth,':IS_HAS_USER'=>0])
  900. ->groupBy('USER_ID')
  901. ->orderBy('USER_ID DESC')
  902. ->offset($offset)
  903. ->limit($this->_limit)
  904. ->asArray()
  905. ->all();
  906. if ($allData) {
  907. // 月度业绩表
  908. foreach ($allData as $everyData) {
  909. $userId = $everyData['USER_ID'];
  910. //往期业绩
  911. $userLastPerf = PrepareCalcCache::userPerf($userId, $this->_periodNum);
  912. //本期业绩
  913. $periodPerf = PrepareCalcCache::nowPeriodPerf($userId, $this->_periodNum);
  914. $userBaseInfo = PrepareCalcCache::getUserInfo($userId, $this->_periodNum);
  915. //级别必须为VIP
  916. $isVip = false;
  917. if( $userBaseInfo['DEC_LV'] === DeclarationLevel::VIP_LEVEL_ID ) {
  918. $isVip= true;
  919. }
  920. if( $this->_sysConfig['vipBonusGoldDecLevel']['VALUE'] && $userBaseInfo['DEC_LV'] === DeclarationLevel::JIN_ZUAN_LEVEL_ID ) {
  921. $isVip = true;
  922. }
  923. $nowMonthPerf = [
  924. 'USER_ID' => $userId,
  925. 'FX_AMOUNT_CASH' => $everyData['FX_AMOUNT_CASH_SUM'],
  926. 'PV_PCS' => $everyData['PV_PCS_SUM'],
  927. 'PV_PCS_FX' => $everyData['PV_PCS_FX_SUM'],
  928. 'PV_PSS' => $everyData['PV_PSS_SUM'],
  929. 'PV_1L' => $everyData['PV_1L_SUM'],
  930. 'PV_2L' => $everyData['PV_2L_SUM'],
  931. 'PV_3L' => $everyData['PV_3L_SUM'],
  932. 'PV_4L' => $everyData['PV_4L_SUM'],
  933. 'PV_5L' => $everyData['PV_5L_SUM'],
  934. //VIP统计相关业绩
  935. 'VIP_PV_1L_ZC' => $isVip ? $everyData['PV_1L_ZC_SUM'] : 0,
  936. 'VIP_PV_2L_ZC' => $isVip ? $everyData['PV_2L_ZC_SUM'] : 0,
  937. 'VIP_PV_3L_ZC' => $isVip ? $everyData['PV_3L_ZC_SUM'] : 0,
  938. 'VIP_PV_4L_ZC' => $isVip ? $everyData['PV_4L_ZC_SUM'] : 0,
  939. 'VIP_PV_5L_ZC' => $isVip ? $everyData['PV_5L_ZC_SUM'] : 0,
  940. //总数据,历史+本期。不能用上月加本月,因为上月可能没业绩,上上个月有业绩。
  941. 'PV_1L_TOTAL' => $periodPerf['PV_1L'] + $userLastPerf['PV_1L'],
  942. 'PV_2L_TOTAL' => $periodPerf['PV_2L'] + $userLastPerf['PV_2L'],
  943. 'PV_3L_TOTAL' => $periodPerf['PV_3L'] + $userLastPerf['PV_3L'],
  944. 'PV_4L_TOTAL' => $periodPerf['PV_4L'] + $userLastPerf['PV_4L'],
  945. 'PV_5L_TOTAL' => $periodPerf['PV_5L'] + $userLastPerf['PV_5L'],
  946. 'PV_PSS_TOTAL' => $periodPerf['PV_PSS'] + $userLastPerf['PV_PSS_TOTAL'],
  947. ];
  948. // 把会员的月业绩写入缓存中,以便下面的奖金计算从缓冲中获取数据效率高
  949. PrepareCalcCache::addHasMonthPerfUsers($userId, $this->_periodNum);
  950. PrepareCalcCache::nowMonthPerf($userId, $this->_periodNum, $nowMonthPerf);
  951. // 将用户的状态改成1,为存在此用户.当之后再次循环PerfPeriodPrepare的时候,不再循环此用户了.
  952. PerfPeriodPrepare::updateAll(
  953. [
  954. 'IS_HAS_USER' => 1
  955. ],
  956. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  957. [':PERIOD_NUM' => $this->_periodNum, ':USER_ID' => $userId]
  958. );
  959. unset($userId, $everyData, $nowMonthPerf, $lastMonthData, $userBaseInfo, $isVip);
  960. }
  961. unset($allData);
  962. $this->loopCalcMonthPerfTableDataPrepare($offset + $this->_limit);
  963. }
  964. unset($allData);
  965. return true;
  966. }
  967. /**
  968. * 循环有业绩会员,并入库
  969. * @param int $offset
  970. * @return bool
  971. * @throws \yii\db\Exception
  972. */
  973. public function loopWriteNowPerf($offset = 0) {
  974. echo sprintf("时间:[%s]缓存本期业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  975. // 从缓存列表里面从底层往上倒序获取会员
  976. $allData = PrepareCalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  977. if($allData){
  978. $insertDataPeriodPerf = [];
  979. foreach($allData as $userId){
  980. $insertDataPeriodPerf[] = $this->nowPeriodPerfData($userId);
  981. unset($userId);
  982. }
  983. PerfPeriodPrepare::batchInsert($insertDataPeriodPerf);
  984. unset($insertDataPeriodPerf, $allData);
  985. return $this->loopWriteNowPerf($offset + $this->_limit);
  986. }
  987. unset($allData);
  988. return true;
  989. }
  990. /**
  991. * 循环有月业绩会员,并入库
  992. * @param int $offset
  993. * @return bool
  994. * @throws \yii\db\Exception
  995. */
  996. public function loopWriteMonthPerf($offset = 0) {
  997. if(!$this->_isCalcMonth){
  998. return true;
  999. }
  1000. echo sprintf("时间:[%s]缓存本月业绩数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1001. // 从缓存列表里面从底层往上倒序获取会员
  1002. $allData = PrepareCalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  1003. if($allData){
  1004. $insertDataMonthPerf = [];
  1005. foreach($allData as $userId){
  1006. $insertDataMonthPerf[] = $this->nowMonthPerfData($userId);
  1007. unset($userId);
  1008. }
  1009. // 月预计计算业绩插入数据
  1010. PerfMonthPrepare::batchInsert($insertDataMonthPerf);
  1011. unset($insertDataMonthPerf, $allData);
  1012. return $this->loopWriteMonthPerf($offset + $this->_limit);
  1013. }
  1014. unset($allData);
  1015. return true;
  1016. }
  1017. /**
  1018. * 本期业绩数据
  1019. * @param $userId
  1020. * @return array
  1021. */
  1022. public function nowPeriodPerfData($userId){
  1023. $data = PrepareCalcCache::nowPeriodPerf($userId, $this->_periodNum);
  1024. $baseInfo = PrepareCalcCache::getUserInfo($userId, $this->_periodNum);
  1025. $result = [
  1026. 'ID' => SnowFake::instance()->generateId(),
  1027. 'USER_ID' => $userId,
  1028. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1029. 'LAST_EMP_LV' => $baseInfo['EMP_LV'],
  1030. 'LAST_STATUS' => $baseInfo['STATUS'],
  1031. 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
  1032. 'PV_PCS' => $data['PV_PCS'],
  1033. 'PV_PSS' => $data['PV_PSS'],
  1034. 'PV_PCS_ZC' => $data['PV_PCS_ZC'],
  1035. 'PV_PCS_YH' => $data['PV_PCS_YH'],
  1036. 'PV_PCS_ZG' => $data['PV_PCS_ZG'],
  1037. 'PV_PCS_LS' => $data['PV_PCS_LS'],
  1038. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  1039. 'PV_PCS_FX_CASH' => $data['PV_PCS_FX_CASH'],
  1040. 'PV_PCS_FX_POINT' => $data['PV_PCS_FX_POINT'],
  1041. 'PV_1L' => $data['PV_1L'],
  1042. 'PV_1L_TOUCH' => $data['PV_1L_TOUCH'],
  1043. 'PV_1L_ZC' => $data['PV_1L_ZC'],
  1044. 'PV_1L_YH' => $data['PV_1L_YH'],
  1045. 'PV_1L_ZG' => $data['PV_1L_ZG'],
  1046. 'PV_1L_LS' => $data['PV_1L_LS'],
  1047. 'PV_1L_FX' => $data['PV_1L_FX'],
  1048. 'PV_2L' => $data['PV_2L'],
  1049. 'PV_2L_TOUCH' => $data['PV_2L_TOUCH'],
  1050. 'PV_2L_ZC' => $data['PV_2L_ZC'],
  1051. 'PV_2L_YH' => $data['PV_2L_YH'],
  1052. 'PV_2L_ZG' => $data['PV_2L_ZG'],
  1053. 'PV_2L_LS' => $data['PV_2L_LS'],
  1054. 'PV_2L_FX' => $data['PV_2L_FX'],
  1055. 'PV_3L' => $data['PV_3L'],
  1056. 'PV_3L_TOUCH' => $data['PV_3L_TOUCH'],
  1057. 'PV_3L_ZC' => $data['PV_3L_ZC'],
  1058. 'PV_3L_YH' => $data['PV_3L_YH'],
  1059. 'PV_3L_ZG' => $data['PV_3L_ZG'],
  1060. 'PV_3L_LS' => $data['PV_3L_LS'],
  1061. 'PV_3L_FX' => $data['PV_3L_FX'],
  1062. 'PV_4L' => $data['PV_4L'],
  1063. 'PV_4L_TOUCH' => $data['PV_4L_TOUCH'],
  1064. 'PV_4L_ZC' => $data['PV_4L_ZC'],
  1065. 'PV_4L_YH' => $data['PV_4L_YH'],
  1066. 'PV_4L_ZG' => $data['PV_4L_ZG'],
  1067. 'PV_4L_LS' => $data['PV_4L_LS'],
  1068. 'PV_4L_FX' => $data['PV_4L_FX'],
  1069. 'PV_5L' => $data['PV_5L'],
  1070. 'PV_5L_TOUCH' => $data['PV_5L_TOUCH'],
  1071. 'PV_5L_ZC' => $data['PV_5L_ZC'],
  1072. 'PV_5L_YH' => $data['PV_5L_YH'],
  1073. 'PV_5L_ZG' => $data['PV_5L_ZG'],
  1074. 'PV_5L_LS' => $data['PV_5L_LS'],
  1075. 'PV_5L_FX' => $data['PV_5L_FX'],
  1076. 'PV_LS_TOUCH' => $data['PV_LS_TOUCH'],
  1077. 'SURPLUS_1L' => $data['SURPLUS_1L'],
  1078. 'SURPLUS_2L' => $data['SURPLUS_2L'],
  1079. 'SURPLUS_3L' => $data['SURPLUS_3L'],
  1080. 'SURPLUS_4L' => $data['SURPLUS_4L'],
  1081. 'SURPLUS_5L' => $data['SURPLUS_5L'],
  1082. 'SURPLUS_LS' => $data['SURPLUS_LS'],
  1083. 'PERIOD_NUM' => $this->_periodNum,
  1084. 'CALC_MONTH' => $this->_calcYearMonth,
  1085. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1086. 'CREATED_AT' => Date::nowTime(),
  1087. ];
  1088. unset($data);
  1089. return $result;
  1090. }
  1091. /**
  1092. * 本月业绩
  1093. * @param $userId
  1094. * @return array
  1095. */
  1096. public function nowMonthPerfData($userId){
  1097. $data = PrepareCalcCache::nowMonthPerf($userId, $this->_periodNum);
  1098. $baseInfo = PrepareCalcCache::getUserInfo($userId, $this->_periodNum);
  1099. $result = [
  1100. 'ID' => SnowFake::instance()->generateId(),
  1101. 'USER_ID' => $userId,
  1102. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1103. 'LAST_EMP_LV' => $data['EMP_LEVEL'],
  1104. 'LAST_STATUS' => $baseInfo['STATUS'],
  1105. 'FX_AMOUNT_CASH' => $data['FX_AMOUNT_CASH'],
  1106. 'PV_PCS' => $data['PV_PCS'],
  1107. 'PV_PCS_FX' => $data['PV_PCS_FX'],
  1108. 'PV_PSS' => $data['PV_PSS'],
  1109. 'PV_1L' => $data['PV_1L'],
  1110. 'PV_2L' => $data['PV_2L'],
  1111. 'PV_3L' => $data['PV_3L'],
  1112. 'PV_4L' => $data['PV_4L'],
  1113. 'PV_5L' => $data['PV_5L'],
  1114. 'VIP_PV_1L_ZC' => $data['VIP_PV_1L_ZC'],
  1115. 'VIP_PV_2L_ZC' => $data['VIP_PV_2L_ZC'],
  1116. 'VIP_PV_3L_ZC' => $data['VIP_PV_3L_ZC'],
  1117. 'VIP_PV_4L_ZC' => $data['VIP_PV_4L_ZC'],
  1118. 'VIP_PV_5L_ZC' => $data['VIP_PV_5L_ZC'],
  1119. 'PV_1L_TOTAL' => $data['PV_1L_TOTAL'],
  1120. 'PV_2L_TOTAL' => $data['PV_2L_TOTAL'],
  1121. 'PV_3L_TOTAL' => $data['PV_3L_TOTAL'],
  1122. 'PV_4L_TOTAL' => $data['PV_4L_TOTAL'],
  1123. 'PV_5L_TOTAL' => $data['PV_5L_TOTAL'],
  1124. 'PV_PSS_TOTAL' => $data['PV_PSS_TOTAL'],
  1125. 'CF_PERCENT' => $data['CF_PERCENT'],
  1126. 'LX_PERCENT' => $data['LX_PERCENT'],
  1127. 'FX_STATUS' => $data['FX_STATUS'],
  1128. 'CALC_MONTH' => $this->_calcYearMonth,
  1129. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1130. 'CREATED_AT' => Date::nowTime(),
  1131. ];
  1132. unset($data);
  1133. return $result;
  1134. }
  1135. /**
  1136. * 循环父级并执行回调函数
  1137. * @param $userId
  1138. * @param callable $callbackFunc
  1139. * @param int $offset
  1140. * @return bool
  1141. */
  1142. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1143. $allParents = Cache::getAllNetworkParents($userId);
  1144. $allData = array_slice($allParents, $offset, $this->_limit);
  1145. unset($allParents);
  1146. if ($allData) {
  1147. foreach ($allData as $data) {
  1148. $funcResult = $callbackFunc($data);
  1149. if ($funcResult === self::LOOP_FINISH) {
  1150. return true;
  1151. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1152. continue;
  1153. }
  1154. unset($data, $funcResult);
  1155. }
  1156. unset($allData);
  1157. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1158. }
  1159. return true;
  1160. }
  1161. /**
  1162. * 循环推荐网络的父级
  1163. * @param $userId
  1164. * @param callable $callbackFunc
  1165. * @param int $offset
  1166. * @return bool
  1167. */
  1168. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1169. $allParents = Cache::getAllRelationParents($userId);
  1170. $allData = array_slice($allParents, $offset, $this->_limit);
  1171. unset($allParents);
  1172. if ($allData) {
  1173. foreach ($allData as $data) {
  1174. $funcResult = $callbackFunc($data);
  1175. if ($funcResult === self::LOOP_FINISH) {
  1176. return true;
  1177. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1178. continue;
  1179. }
  1180. unset($data, $funcResult);
  1181. }
  1182. unset($allData);
  1183. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1184. }
  1185. return true;
  1186. }
  1187. /**
  1188. * 是否可拿业绩(即注销、关停、停发状态)
  1189. * @param $userId
  1190. * @return bool
  1191. */
  1192. public function isHasPerf($userId) {
  1193. //@todo 所有人都有业绩
  1194. return true;
  1195. }
  1196. /**
  1197. * 更新百分比并发送
  1198. * @param $percent
  1199. */
  1200. private function _updatePercent($percent) {
  1201. // 把数据写入数据库中
  1202. PeriodPrepare::updateAll(['PERF_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  1203. }
  1204. }