CalcServePerfCalc.php 60 KB

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