PerfCalc.php 66 KB

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