PreparePerfCalc.php 61 KB

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