PreparePerfCalc.php 60 KB

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