PreparePerfCalc.php 59 KB

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