CalcServePerfCalc.php 65 KB

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