PreparePerfCalc.php 50 KB

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