PreparePerfCalc.php 61 KB

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