BonusCalc.php 169 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/8/2
  6. * Time: 上午10:32
  7. */
  8. namespace common\helpers\bonus;
  9. use common\helpers\Cache;
  10. use common\helpers\Date;
  11. use common\helpers\snowflake\SnowFake;
  12. use common\helpers\Tool;
  13. use common\models\CalcBonus;
  14. use common\models\CalcBonusBD;
  15. use common\models\CalcBonusBS;
  16. use common\models\CalcBonusBT;
  17. use common\models\CalcBonusCF;
  18. use common\models\CalcBonusFL;
  19. use common\models\CalcBonusFW;
  20. use common\models\CalcBonusFX;
  21. use common\models\CalcBonusGL;
  22. use common\models\CalcBonusGX;
  23. use common\models\CalcBonusHB;
  24. use common\models\CalcBonusLS;
  25. use common\models\CalcBonusLX;
  26. use common\models\CalcBonusQY;
  27. use common\models\CalcBonusStandard;
  28. use common\models\CalcBonusTG;
  29. use common\models\CalcBonusVIP;
  30. use common\models\CalcBonusXF;
  31. use common\models\CalcBonusYC;
  32. use common\models\CalcBonusYJ;
  33. use common\models\CalcBonusZone;
  34. use common\models\CalcMonthBonusUser;
  35. use common\models\Config;
  36. use common\models\FlowDeductZR;
  37. use common\models\PerfCompany;
  38. use common\models\PerfMonth;
  39. use common\models\PerfPeriod;
  40. use common\models\Period;
  41. use common\models\ResendQY;
  42. use common\models\DeclarationLevel;
  43. use common\models\DecOrder;
  44. use common\models\EmployLevel;
  45. use common\models\FlowBonus;
  46. use common\models\PerfActiveUser;
  47. use common\models\ScoreMonth;
  48. use common\models\User;
  49. use common\models\UserInfo;
  50. use common\models\UserPerf;
  51. use SebastianBergmann\CodeCoverage\Report\PHP;
  52. use yii\base\BaseObject;
  53. use yii\base\Exception;
  54. use yii\base\StaticInstanceTrait;
  55. use yii\helpers\Json;
  56. class BonusCalc extends BaseObject {
  57. use StaticInstanceTrait;
  58. private $_limit = 1000;
  59. private $_gxLimit = 500;
  60. private $_handleUserId;
  61. private $_companyMonthPerf = 0;
  62. private $_cfTotalPercent = 0;
  63. private $_lxTotalPercent = 0;
  64. private $_sysConfig = [];
  65. private $_decLevelConfig = [];
  66. private $_empLevelConfig = [];
  67. private $_decRoleConfig = [];
  68. private $_errors = [];
  69. private $_periodNum = 0;
  70. private $_periodId;
  71. private $_isCalcMonth = 0;
  72. private $_calcYear;
  73. private $_calcMonth;
  74. private $_calcYearMonth;
  75. private $_calcMonthPeriodNumCount = 0;
  76. private $_lastCalcYear;
  77. private $_lastCalcMonth;
  78. private $_lastCalcYearMonth;
  79. private $_lastPeriodNum;
  80. private $_lastPeriodYear;
  81. private $_lastPeriodMonth;
  82. private $_lastPeriodYearMonth;
  83. //pv
  84. private $_pvRatio;
  85. private $_calcZone = ['openTravel', 'openCar', 'openHouse'];
  86. const LOOP_FINISH = 1;
  87. const LOOP_CONTINUE = 2;
  88. const GX_FLOOR_LIMIT = 5; // 共享奖向上找的最大层级
  89. const GX_LIMIT_SWITCH = true; // 是否开启共享奖向上找的最大限制 true 为开启 false 关闭
  90. const ORDER_TYPE_TO_FW_COEFFICIENT = [
  91. 'ZC' => 'fwCoefficientFromZc',
  92. 'FX_CASH' => 'fwCoefficientFromFxCash',
  93. 'FX_POINT' => 'fwCoefficientFromFxPoint',
  94. ];
  95. const JX_STANDARD_BASE_AMOUNT = 10000;
  96. const JX_STANDARD_MAX_TIMES = 50;
  97. const JX_STANDARD_BONUS_PERCENT = 15;
  98. //最小报单pv
  99. const MIN_BD_PV = 980;
  100. /**
  101. * 1、抓取所有报单、订单,把能当时产生业绩的报单存入业绩单。例如注册单,订单
  102. * 2、从业绩单中循环,并计算上下级业绩
  103. * 3、计算级别
  104. * 4、计算各个奖项
  105. * 5、给会员追加总业绩,在结算中不妥,打算把追加总业绩放到挂网中,因为结算完成以后的业绩都不是最终的,有可能还会变
  106. */
  107. /**
  108. * 初始化
  109. */
  110. public function init() {
  111. parent::init();
  112. }
  113. /**
  114. * 设置期数
  115. * @param int $periodNum
  116. * @return int
  117. */
  118. public function setPeriodNum(int $periodNum) {
  119. return $this->_periodNum = $periodNum;
  120. }
  121. /**
  122. * 获取期数
  123. * @return int
  124. */
  125. public function getPeriodNum() {
  126. return $this->_periodNum;
  127. }
  128. /**
  129. * 加入错误错误
  130. * @param $attr
  131. * @param $error
  132. */
  133. public function addError($attr, $error) {
  134. $this->_errors[$attr][] = $error;
  135. }
  136. /**
  137. * 获取错误信息
  138. * @return array
  139. */
  140. public function getErrors() {
  141. return $this->_errors;
  142. }
  143. /**
  144. * 开始执行结算步骤
  145. * @param $periodNum
  146. * @param null $handleUserId
  147. * @return bool
  148. */
  149. public function calcStep($periodNum, $handleUserId = null) {
  150. try {
  151. $this->_errors = [];
  152. $this->setPeriodNum($periodNum);
  153. $this->_handleUserId = $handleUserId;
  154. $t1 = microtime(true);
  155. // 初始化结算任务
  156. $this->initCalcTask();
  157. // 设置结算状态
  158. $this->setCalcStatus('start');
  159. // 清空所有本期结算用到的缓存
  160. CalcCache::clearCalcBonusCache($this->_periodNum);
  161. // 清空相关表数据
  162. $this->clearCalcTableData();
  163. $t2 = microtime(true);
  164. echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t2 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  165. $this->_updatePercent(5);
  166. $t3 = microtime(true);
  167. $this->_updatePercent(10);
  168. // 奖金部分
  169. if($this->_sysConfig['openFW']['VALUE']) {
  170. $this->calcBonusBDStepOne();
  171. $this->calcBonusBDStepTwo();
  172. }
  173. $t4 = microtime(true);
  174. echo('计算服务奖'.($this->_sysConfig['openFW']['VALUE']?'完成':'关闭').',耗时:' . round($t4 - $t3, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  175. $this->_updatePercent(15);
  176. if($this->_sysConfig['openTG']['VALUE']) {
  177. $this->calcBonusTG();
  178. }
  179. $t5 = microtime(true);
  180. echo('计算推广奖'.($this->_sysConfig['openTG']['VALUE']?'完成':'关闭').',耗时:' . round($t5 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  181. $this->_updatePercent(20);
  182. if($this->_sysConfig['openXF']['VALUE']) {
  183. if( $this->_sysConfig['consumeRecPercent']['VALUE'] > 0 ) {
  184. $this->calcBonusXFToRec();
  185. }
  186. if( $this->_sysConfig['consumeSelfPercent']['VALUE'] > 0 ) {
  187. $this->calcBonusXFToSelf();
  188. }
  189. }
  190. $t6 = microtime(true);
  191. echo('计算消费奖'.($this->_sysConfig['openXF']['VALUE']?'完成':'关闭').',耗时:' . round($t6 - $t5, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  192. $this->_updatePercent(25);
  193. if($this->_sysConfig['openYJ']['VALUE']) {
  194. $this->calcBonusBdYJ();
  195. }
  196. $t7 = microtime(true);
  197. echo('计算报单业绩奖'.($this->_sysConfig['openYJ']['VALUE']?'完成':'关闭').',耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  198. $this->_updatePercent(30);
  199. if($this->_sysConfig['fxOpenYJ']['VALUE']) {
  200. $this->calcBonusFxYJ();
  201. }
  202. $t8 = microtime(true);
  203. echo('计算复消业绩奖'.($this->_sysConfig['fxOpenYJ']['VALUE']?'完成':'关闭').',耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  204. $this->_updatePercent(35);
  205. if($this->_sysConfig['openQY']['VALUE']) {
  206. $this->calcBonusQY();
  207. $this->calcBonusBdQY();
  208. $this->calcBonusFxQY();
  209. }
  210. $t9 = microtime(true);
  211. echo('计算团队奖'.($this->_sysConfig['openQY']['VALUE']?'完成':'关闭').',耗时:' . round($t9 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  212. $this->_updatePercent(65);
  213. if($this->_sysConfig['openGX']['VALUE']) {
  214. $this->calcBonusGXBefore();
  215. $this->calcBonusGX();
  216. }
  217. $t11 = microtime(true);
  218. echo('计算共享奖'.($this->_sysConfig['openGX']['VALUE']?'完成':'关闭').',耗时:' . round($t11 - $t9, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  219. $this->_updatePercent(68);
  220. if($this->_sysConfig['openGL']['VALUE']) {
  221. $this->calcBonusBdGL();
  222. }
  223. $t13 = microtime(true);
  224. echo('计算报单管理奖'.($this->_sysConfig['openGL']['VALUE']?'完成':'关闭').',耗时:' . round($t13 - $t11, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  225. $this->_updatePercent(50);
  226. // if($this->_sysConfig['fxOpenGL']['VALUE']) {
  227. // $this->calcBonusFxGL();
  228. // }
  229. // $t14 = microtime(true);
  230. // echo('计算复消管理奖'.($this->_sysConfig['fxOpenGL']['VALUE']?'完成':'关闭').',耗时:' . round($t14 - $t13, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  231. // $this->_updatePercent(70);
  232. if($this->_sysConfig['openYC']['VALUE']) {
  233. $this->calcBonusYCStepOne();
  234. $this->calcBonusYCStepTwo();
  235. }
  236. $t16 = microtime(true);
  237. echo('计算荣衔奖'.($this->_sysConfig['openYC']['VALUE']?'完成':'关闭').',耗时:' . round($t16 - $t13, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  238. $this->_updatePercent(55);
  239. if($this->_sysConfig['openVIP']['VALUE']) {
  240. $this->calcBonusVIP();
  241. }
  242. $t17 = microtime(true);
  243. echo('计算VIP奖'.($this->_sysConfig['openVIP']['VALUE']?'完成':'关闭').',耗时:' . round($t17 - $t16, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  244. $this->_updatePercent(60);
  245. if($this->_sysConfig['openJXS']['VALUE']) {
  246. $this->calcBonusStandard();
  247. }
  248. $t18 = microtime(true);
  249. echo('计算达标奖'.($this->_sysConfig['openJXS']['VALUE']?'完成':'关闭').',耗时:' . round($t18 - $t17, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  250. $this->_updatePercent(65);
  251. // 蓝星奖入库,实际上是插入有奖金会员数据缓存中.
  252. // 调用存储过程,计算蓝星管理奖金
  253. $this->calcBsProcedure();
  254. // 将有【蓝星业绩奖金】的用户加入到有奖金缓存用户中
  255. $this->calcBonusBsYJ();
  256. // 将有【蓝星管理奖金】的用户加入到有奖金缓存用户中
  257. $this->calcBonusBsGL();
  258. $t18temp = microtime(true);
  259. echo('计算蓝星奖'.($this->_sysConfig['openGL']['VALUE']?'完成':'关闭').',耗时:' . round($t18temp - $t18, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  260. $this->_updatePercent(65);
  261. $this->calcBonusZoneBS($this->_sysConfig['openTravel']);
  262. $t18travel = microtime(true);
  263. echo('计算旅游奖' . ($this->_sysConfig['openTravel']['VALUE'] ? '完成' : '关闭') . ',耗时:' . round($t18travel - $t18temp, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  264. $this->_updatePercent(67);
  265. $this->calcBonusZoneBS($this->_sysConfig['openCar']);
  266. $t18car = microtime(true);
  267. echo('计算名车奖' . ($this->_sysConfig['openCar']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t18car - $t18travel, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  268. $this->_updatePercent(68);
  269. $this->calcBonusZoneBS($this->_sysConfig['openHouse']);
  270. $t18house = microtime(true);
  271. echo('计算豪宅奖' . ($this->_sysConfig['openHouse']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t18house - $t18car, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  272. $this->_updatePercent(69);
  273. //把奖金会员写入缓存
  274. $this->loopMonthBonusUserFromDbToCache();
  275. $t19 = microtime(true);
  276. echo('奖金会员写入缓存完成,耗时:' . round($t19 - $t18, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  277. $this->_updatePercent(70);
  278. //奖金写库
  279. // 这里增加新的奖金入库操作.
  280. $this->loopBonusUsers();
  281. // 入库完成,将各个奖金计算流水会员聘级,更新成蓝星奖当时计算的聘级
  282. $this->loopCalcBlueEmpLv();
  283. $this->_updatePercent(75);
  284. unset($calcWrite);
  285. $t20 = microtime(true);
  286. echo('奖金写库操作完成,耗时:' . round($t20 - $t19, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  287. //把本期奖金会员入库 - 把缓存中的月奖用户信息存到数据库.存储过程的入库不在这里进行,这里代码取的缓存,存储过程没有缓存,在上面进行入库
  288. $this->loopMonthBonusUserToDb();
  289. $t21 = microtime(true);
  290. echo('奖金会员入库完成,耗时:' . round($t21 - $t20, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  291. $this->_updatePercent(80);
  292. // 计算基础积分,不可以奖金入库之前计算这样可能会丢掉只有本期的奖金的会员
  293. $this->calcBaseScore();
  294. $this->_updatePercent(90);
  295. $t22 = microtime(true);
  296. echo('计算基础积分,耗时:' . round($t22 - $t21, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  297. //积分入库
  298. $this->loopWriteScore();
  299. $this->_updatePercent(100);
  300. $t23 = microtime(true);
  301. echo('积分写库操作完成,耗时:' . round($t23 - $t22, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  302. //@todo 计算房产奖
  303. $t30 = microtime(true);
  304. echo('结算全部完成,共耗时:' . round($t30 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  305. } catch (\Exception $e) {
  306. $this->errorCalcTask();
  307. $this->addError('calc', $e->getMessage());
  308. return false;
  309. }
  310. return true;
  311. }
  312. /**
  313. * 结算完成
  314. */
  315. public function endCalcTask() {
  316. // 更新结算状态
  317. $this->setCalcStatus('end');
  318. }
  319. /**
  320. * 结算错误
  321. */
  322. public function errorCalcTask() {
  323. // 清空所有本期结算用到的缓存
  324. CalcCache::clearCalcBonusCache($this->_periodNum);
  325. // 更新结算状态
  326. $this->setCalcStatus('fail');
  327. }
  328. /**
  329. * 初始化结算任务
  330. * @throws \yii\db\Exception
  331. */
  332. public function initCalcTask() {
  333. $this->_sysConfig = Cache::getSystemConfig();
  334. $this->_decLevelConfig = Cache::getDecLevelConfig();
  335. $this->_empLevelConfig = Cache::getEmpLevelConfig();
  336. $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
  337. $periodNum = $this->_periodNum;
  338. // 获取本年月和上年月
  339. $periodObj = Period::instance();
  340. $periodDataArr = $periodObj->setPeriodNum($periodNum);
  341. $this->_periodId = $periodDataArr['ID'];
  342. $this->_isCalcMonth = $periodObj->isCalcMonth($periodNum);
  343. $this->_calcYear = $periodObj->getYear($periodNum);
  344. $this->_calcMonth = $periodObj->getMonth($periodNum);
  345. $this->_calcYearMonth = $periodObj->getYearMonth($periodNum);
  346. $this->_calcMonthPeriodNumCount = $periodObj->getYearMonthAllPeriodNumCount($this->_calcYear, $this->_calcMonth);
  347. $lastYearMonthArr = $periodObj->getLastMonth($periodNum);
  348. $this->_lastCalcYear = $lastYearMonthArr['year'];
  349. $this->_lastCalcMonth = $lastYearMonthArr['month'];
  350. $this->_lastCalcYearMonth = $lastYearMonthArr['yearMonth'];
  351. $this->_lastPeriodNum = $periodNum - 1;
  352. if (Period::isExistsPeriodNum($this->_lastPeriodNum)) {
  353. $this->_lastPeriodYear = $periodObj->getYear($this->_lastPeriodNum);
  354. $this->_lastPeriodMonth = $periodObj->getMonth($this->_lastPeriodNum);
  355. $this->_lastPeriodYearMonth = $periodObj->getYearMonth($this->_lastPeriodNum);
  356. } else {
  357. $this->_lastPeriodYear = 0;
  358. $this->_lastPeriodMonth = 0;
  359. $this->_lastPeriodYearMonth = 0;
  360. }
  361. $this->_cfTotalPercent = CalcCache::getCFTotalPercent($this->_periodNum);
  362. $this->_lxTotalPercent = CalcCache::getLXTotalPercent($this->_periodNum);
  363. $this->_pvRatio = $this->_sysConfig['pvRatio']['VALUE'];
  364. if($perfCompany = PerfCompany::findOne(['CALC_MONTH' => $this->_calcYearMonth])){
  365. $this->_companyMonthPerf = $perfCompany['PV'];
  366. }
  367. }
  368. /**
  369. * 设置结算状态
  370. * @param $type
  371. * start|end|fail
  372. */
  373. public function setCalcStatus($type) {
  374. if ($type == 'start') {
  375. Period::updateAll(['IS_CALCING' => 1, 'IS_CALCULATED' => Period::CALCULATE_NONE, 'CALCULATE_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  376. } elseif ($type == 'end') {
  377. Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FINISH, 'CALCULATED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  378. } elseif ($type == 'fail') {
  379. Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FAIL, 'CALCULATED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  380. }
  381. }
  382. /**
  383. * 清空相关表数据
  384. */
  385. public function clearCalcTableData() {
  386. // 奖金表
  387. CalcBonus::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  388. CalcBonusQY::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  389. CalcBonusBD::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  390. CalcBonusTG::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  391. CalcBonusYJ::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  392. CalcBonusGX::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  393. CalcBonusGL::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  394. // 月结时要清空的数据
  395. if ($this->_isCalcMonth) {
  396. CalcBonusYC::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  397. CalcBonusVIP::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  398. CalcBonusStandard::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  399. ScoreMonth::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  400. CalcBonusZone::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  401. }
  402. }
  403. /**
  404. * === 奖金结算部分 ===
  405. */
  406. /**
  407. * 服务奖
  408. * @param int $offset
  409. * @return bool
  410. * @throws \yii\db\Exception
  411. */
  412. public function calcBonusBD(int $offset = 0) {
  413. $periodNum = $this->_periodNum;
  414. // 从缓存获取分页有业绩的会员信息
  415. $allData = CalcCache::getHasBDUsers($this->_periodNum, $offset, $this->_limit);
  416. if ($allData) {
  417. $insertBonusData = [];
  418. foreach ($allData as $userId) {
  419. $decInfo = CalcCache::getBDUsersInfo($userId, $this->_periodNum);
  420. if( !$decInfo ) continue;
  421. //服务奖使用报单原金额
  422. $decBonus = Tool::formatPrice($decInfo['DEC_AMOUNT'] * $this->_sysConfig['decPercent']['VALUE'] / 100);
  423. if ($decBonus <= 0) continue;
  424. $bonusUserId = $decInfo['DEC_ID'];
  425. if( !$bonusUserId ) continue;
  426. // 获取会员的报单级别
  427. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  428. //总金额限制
  429. $decBonus = $this->bonusTotalLimit($decBonus, $bonusUserId, $bonusUserInfo['REC_NUM'], $bonusUserInfo['ZC_AMOUNT']);
  430. if( $decBonus <= 0 ) continue;
  431. //扣除相应的复消积分和管理费
  432. $deductData = $this->deduct($bonusUserId, $decBonus);
  433. // 把对碰后的奖金存入缓存中
  434. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_BD', $decBonus, $deductData);
  435. //来源会员信息
  436. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  437. //服务奖流水
  438. $insertBonusData[] = [
  439. 'ID' => SnowFake::instance()->generateId(),
  440. 'USER_ID' => $bonusUserId,
  441. 'LAST_DEC_LV' => $bonusUserInfo['DEC_LV'],
  442. 'LAST_EMP_LV' => $bonusUserInfo['EMP_LV'],
  443. 'LAST_STATUS' => $bonusUserInfo['STATUS'],
  444. 'FROM_USER_ID' => $userId,
  445. 'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'],
  446. 'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'],
  447. 'LAST_FROM_STATUS' => $fromUserInfo['STATUS'],
  448. 'AMOUNT' => $deductData['surplus'],
  449. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  450. 'MANAGE_TAX' => $deductData['manageTax'],
  451. 'ORI_BONUS' => $decBonus,
  452. 'PERIOD_NUM' => $this->_periodNum,
  453. 'CALC_YEAR' => $this->_calcYear,
  454. 'CALC_MONTH' => $this->_calcYearMonth,
  455. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  456. 'CREATED_AT' => Date::nowTime(),
  457. 'LOGS' => json_encode([
  458. 'decInfoArr' => $decInfo,
  459. 'decPercentConfig' => $this->_sysConfig['decPercent']['VALUE'],
  460. 'recNum' => $bonusUserInfo['REC_NUM'],
  461. 'decAmount' => $bonusUserInfo['ZC_AMOUNT'],
  462. 'bonusTotalLimit' => [
  463. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  464. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  465. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  466. ],
  467. ]),
  468. ];
  469. unset($decInfo, $decBonus, $bonusUserId, $bonusUserInfo, $userId, $deductData, $fromUserInfo);
  470. }
  471. CalcBonusBD::batchInsert($insertBonusData);
  472. unset($allData, $insertBonusData);
  473. return $this->calcBonusBD($offset + $this->_limit);
  474. }
  475. unset($allData);
  476. return true;
  477. }
  478. /**
  479. * 推广奖
  480. * @param int $offset
  481. * @return bool
  482. * @throws \yii\db\Exception
  483. */
  484. public function calcBonusTG(int $offset = 0) {
  485. $periodNum = $this->_periodNum;
  486. // 从缓存获取分页有业绩的会员信息
  487. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  488. if ($allData) {
  489. $insertBonusData = [];
  490. foreach ($allData as $userId) {
  491. // 从缓存中获取会员的业绩信息
  492. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  493. if( !$perfData ) continue;
  494. //个人业绩都算推荐奖,包括报单和复消、二次购物
  495. $perfPv = $perfData['PV_PCS_ZC'] ?? 0;
  496. if( $perfPv <= 0 ) continue;
  497. //推广奖使用个人PCS
  498. $recBonus = Tool::formatPrice($perfPv * $this->_sysConfig['recPercent']['VALUE'] / 100);
  499. if ($recBonus <= 0) continue;
  500. // 把对碰后的奖金存入缓存中
  501. $perfUserInfo = CalcCache::getUserInfo($userId, $periodNum);
  502. $bonusUserId = $perfUserInfo['REC_UID'] ?? '';
  503. if( !$bonusUserId ) continue;
  504. // 获取会员的报单级别
  505. $userBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  506. //判断级别上限
  507. // $recBonus = $this->declarationLevelCap($recBonus, $bonusUserId, $userBaseInfo['DEC_LV']);
  508. // if( $recBonus <= 0 ) continue;
  509. $recBonus = $this->bonusTotalLimit($recBonus, $bonusUserId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  510. if( $recBonus <= 0 ) continue;
  511. //扣除相应的复消积分和管理费
  512. $deductData = $this->deduct($bonusUserId, $recBonus);
  513. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_TG', $recBonus, $deductData);
  514. //来源会员信息
  515. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  516. //推广奖流水
  517. $insertBonusData[] = [
  518. 'ID' => SnowFake::instance()->generateId(),
  519. 'USER_ID' => $bonusUserId,
  520. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  521. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  522. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  523. 'FROM_USER_ID' => $userId,
  524. 'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'],
  525. 'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'],
  526. 'LAST_FROM_STATUS' => $fromUserInfo['STATUS'],
  527. 'AMOUNT' => $deductData['surplus'],
  528. 'ORI_BONUS' => $recBonus,
  529. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  530. 'MANAGE_TAX' => $deductData['manageTax'],
  531. 'PERIOD_NUM' => $this->_periodNum,
  532. 'CALC_YEAR' => $this->_calcYear,
  533. 'CALC_MONTH' => $this->_calcYearMonth,
  534. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  535. 'CREATED_AT' => Date::nowTime(),
  536. 'LOGS' => json_encode([
  537. 'perfPv' => $perfPv,
  538. 'recPercentConfig' => $this->_sysConfig['recPercent']['VALUE'],
  539. 'recNum' => $userBaseInfo['REC_NUM'],
  540. 'decAmount' => $userBaseInfo['ZC_AMOUNT'],
  541. 'decLevel' => $userBaseInfo['DEC_LV'],
  542. 'bonusTotalLimit' => [
  543. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  544. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  545. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  546. ],
  547. ]),
  548. ];
  549. unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
  550. }
  551. CalcBonusTG::batchInsert($insertBonusData);
  552. unset($allData, $insertBonusData);
  553. return $this->calcBonusTG($offset + $this->_limit);
  554. }
  555. unset($allData);
  556. return true;
  557. }
  558. /**
  559. * 消费奖-推荐人
  560. * @param int $offset
  561. * @return bool
  562. * @throws \yii\db\Exception
  563. */
  564. public function calcBonusXFToRec(int $offset = 0) {
  565. $periodNum = $this->_periodNum;
  566. // 从缓存获取分页有业绩的会员信息
  567. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  568. if ($allData) {
  569. $insertBonusData = [];
  570. foreach ($allData as $userId) {
  571. // 从缓存中获取会员的业绩信息
  572. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  573. if( !$perfData ) continue;
  574. //个人业绩都算推荐奖,包括报单和复消、二次购物
  575. $perfPv = $perfData['PV_PCS_FX'] ?? 0;
  576. if( $perfPv <= 0 ) continue;
  577. //推广奖使用个人PCS
  578. $recBonus = Tool::formatPrice($perfPv * $this->_sysConfig['consumeRecPercent']['VALUE'] / 100);
  579. if ($recBonus <= 0) continue;
  580. // 把对碰后的奖金存入缓存中
  581. $perfUserInfo = CalcCache::getUserInfo($userId, $periodNum);
  582. $bonusUserId = $perfUserInfo['REC_UID'] ?? '';
  583. if( !$bonusUserId ) continue;
  584. // 获取会员的报单级别
  585. $userBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  586. //判断级别上限
  587. // $recBonus = $this->declarationLevelCap($recBonus, $bonusUserId, $userBaseInfo['DEC_LV']);
  588. // if( $recBonus <= 0 ) continue;
  589. $recBonus = $this->bonusTotalLimit($recBonus, $bonusUserId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  590. if( $recBonus <= 0 ) continue;
  591. //扣除相应的复消积分和管理费
  592. $deductData = $this->deduct($bonusUserId, $recBonus);
  593. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_XF', $recBonus, $deductData);
  594. //来源会员信息
  595. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  596. //推广奖流水
  597. $insertBonusData[] = [
  598. 'ID' => SnowFake::instance()->generateId(),
  599. 'USER_ID' => $bonusUserId,
  600. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  601. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  602. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  603. 'FROM_USER_ID' => $userId,
  604. 'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'],
  605. 'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'],
  606. 'LAST_FROM_STATUS' => $fromUserInfo['STATUS'],
  607. 'AMOUNT' => $deductData['surplus'],
  608. 'ORI_BONUS' => $recBonus,
  609. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  610. 'MANAGE_TAX' => $deductData['manageTax'],
  611. 'PERIOD_NUM' => $this->_periodNum,
  612. 'CALC_YEAR' => $this->_calcYear,
  613. 'CALC_MONTH' => $this->_calcYearMonth,
  614. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  615. 'CREATED_AT' => Date::nowTime(),
  616. 'LOGS' => json_encode([
  617. 'perfPv' => $perfPv,
  618. 'recPercentConfig' => $this->_sysConfig['consumeRecPercent']['VALUE'],
  619. 'recNum' => $userBaseInfo['REC_NUM'],
  620. 'decAmount' => $userBaseInfo['ZC_AMOUNT'],
  621. 'decLevel' => $userBaseInfo['DEC_LV'],
  622. 'bonusTotalLimit' => [
  623. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  624. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  625. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  626. ],
  627. ]),
  628. ];
  629. unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
  630. }
  631. CalcBonusXF::batchInsert($insertBonusData);
  632. unset($allData, $insertBonusData);
  633. return $this->calcBonusXFToRec($offset + $this->_limit);
  634. }
  635. unset($allData);
  636. return true;
  637. }
  638. /**
  639. * 消费奖-自己
  640. * @param int $offset
  641. * @return bool
  642. * @throws \yii\db\Exception
  643. */
  644. public function calcBonusXFToSelf(int $offset = 0) {
  645. $periodNum = $this->_periodNum;
  646. // 从缓存获取分页有业绩的会员信息
  647. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  648. if ($allData) {
  649. $insertBonusData = [];
  650. foreach ($allData as $userId) {
  651. // 从缓存中获取会员的业绩信息
  652. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  653. if( !$perfData ) continue;
  654. //个人业绩都算推荐奖,包括报单和复消、二次购物
  655. $perfPv = $perfData['PV_PCS_FX'] ?? 0;
  656. if( $perfPv <= 0 ) continue;
  657. //推广奖使用个人PCS
  658. $recBonus = Tool::formatPrice($perfPv * $this->_sysConfig['consumeSelfPercent']['VALUE'] / 100);
  659. if ($recBonus <= 0) continue;
  660. // 把对碰后的奖金存入缓存中
  661. // $perfUserInfo = CalcCache::getUserInfo($userId, $periodNum);
  662. $bonusUserId = $userId;
  663. if( !$bonusUserId ) continue;
  664. // 获取会员的报单级别
  665. $userBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  666. //判断级别上限
  667. // $recBonus = $this->declarationLevelCap($recBonus, $bonusUserId, $userBaseInfo['DEC_LV']);
  668. // if( $recBonus <= 0 ) continue;
  669. $recBonus = $this->bonusTotalLimit($recBonus, $bonusUserId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  670. if( $recBonus <= 0 ) continue;
  671. //扣除相应的复消积分和管理费
  672. $deductData = $this->deduct($bonusUserId, $recBonus);
  673. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_XF', $recBonus, $deductData);
  674. //来源会员信息
  675. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  676. //推广奖流水
  677. $insertBonusData[] = [
  678. 'ID' => SnowFake::instance()->generateId(),
  679. 'USER_ID' => $bonusUserId,
  680. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  681. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  682. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  683. 'FROM_USER_ID' => $userId,
  684. 'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'],
  685. 'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'],
  686. 'LAST_FROM_STATUS' => $fromUserInfo['STATUS'],
  687. 'AMOUNT' => $deductData['surplus'],
  688. 'ORI_BONUS' => $recBonus,
  689. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  690. 'MANAGE_TAX' => $deductData['manageTax'],
  691. 'PERIOD_NUM' => $this->_periodNum,
  692. 'CALC_YEAR' => $this->_calcYear,
  693. 'CALC_MONTH' => $this->_calcYearMonth,
  694. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  695. 'CREATED_AT' => Date::nowTime(),
  696. 'LOGS' => json_encode([
  697. 'perfPv' => $perfPv,
  698. 'recPercentConfig' => $this->_sysConfig['consumeSelfPercent']['VALUE'],
  699. 'recNum' => $userBaseInfo['REC_NUM'],
  700. 'decAmount' => $userBaseInfo['ZC_AMOUNT'],
  701. 'decLevel' => $userBaseInfo['DEC_LV'],
  702. 'bonusTotalLimit' => [
  703. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  704. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  705. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  706. ],
  707. ]),
  708. ];
  709. unset($perfData, $perfPv, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
  710. }
  711. CalcBonusXF::batchInsert($insertBonusData);
  712. unset($allData, $insertBonusData);
  713. return $this->calcBonusXFToSelf($offset + $this->_limit);
  714. }
  715. unset($allData);
  716. return true;
  717. }
  718. /**
  719. * 服务奖第一步
  720. * @param int $offset
  721. * @return bool
  722. * @throws \yii\db\Exception
  723. */
  724. public function calcBonusBDStepOne(int $offset = 0) {
  725. echo sprintf("时间:[%s]服务奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  726. $periodNum = $this->_periodNum;
  727. // 从缓存获取分页有业绩的会员信息
  728. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  729. if ($allData) {
  730. $insertBonusData = [];
  731. foreach ($allData as $userId) {
  732. // 从缓存中获取会员的业绩信息
  733. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  734. if( !$perfData ) continue;
  735. //
  736. $decRoleBonusFrom = explode(',', $this->_sysConfig['decRoleBonusFrom']['VALUE']);
  737. $validPvPcs = 0;
  738. foreach ($decRoleBonusFrom as $orderType) {
  739. $orderTypeName = sprintf('PV_PCS_%s', $orderType);
  740. $orderTypeValue = $perfData[$orderTypeName] ?? 0;
  741. $coefficientName = self::ORDER_TYPE_TO_FW_COEFFICIENT[$orderType];
  742. $coefficient = $this->_sysConfig[$coefficientName]['VALUE'] ?? 1;
  743. $validPvPcs += $orderTypeValue * $coefficient;
  744. unset($orderType, $orderTypeName, $orderTypeValue, $coefficientName, $coefficient);
  745. }
  746. unset($perfData, $decRoleBonusFrom);
  747. if ( $validPvPcs <= 0 ) continue;
  748. $this->loopRelationParentDo($userId, function ($parent) use($userId, $validPvPcs){
  749. //判断parent的报单中心级别 和 服务奖比例
  750. $bonusUserId = $parent['PARENT_UID'];
  751. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  752. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  753. $isDec = $bonusUserInfo['IS_DEC'];
  754. if($isDec == 0) return self::LOOP_CONTINUE;
  755. $decRoleId = $bonusUserInfo['DEC_ROLE_ID'];
  756. if( !$decRoleId ) return self::LOOP_CONTINUE;
  757. if( !isset($this->_decRoleConfig[$decRoleId]) ) return self::LOOP_CONTINUE;
  758. $parentDecRoleLevel = $this->_decRoleConfig[$decRoleId];
  759. $parentFwBonusPercent = $parentDecRoleLevel['FW_BONUS_PERCENT'] ?? 0;
  760. $cacheMaxPercent = CalcCache::fwMaxBonusPercent($userId, $this->_periodNum);
  761. $diffPercent = $parentFwBonusPercent - $cacheMaxPercent;
  762. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  763. $fwBonus = $validPvPcs * $diffPercent / 100;
  764. if( $fwBonus <= 0 ) return self::LOOP_CONTINUE;
  765. //给本人添加服务奖比例
  766. CalcCache::fwMaxBonusPercent($userId, $this->_periodNum, $parentFwBonusPercent);
  767. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  768. CalcCache::saveFwBonusList($bonusUserId, $this->_periodNum, $fwBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$validPvPcs]);
  769. CalcCache::addHasFwBonusUsers($bonusUserId, $this->_periodNum);
  770. //达到最大的比例就不在向上找了
  771. // if( $parentFwBonusPercent >= $maxLevelPercent ) return self::LOOP_FINISH;
  772. unset($bonusUserId, $bonusUserInfo, $isDec, $decRoleId, $parentDecRoleLevel, $parentFwBonusPercent, $cacheMaxPercent, $diffPercent, $fwBonus);
  773. });
  774. unset($userId, $validPvPcs);
  775. }
  776. unset($allData, $insertBonusData);
  777. return $this->calcBonusBDStepOne($offset + $this->_limit);
  778. }
  779. unset($allData);
  780. return true;
  781. }
  782. /**
  783. * 服务奖第二步
  784. * @param int $offset
  785. * @return bool
  786. * @throws \yii\db\Exception
  787. */
  788. public function calcBonusBDStepTwo(int $offset = 0) {
  789. echo sprintf("时间:[%s]服务奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  790. $allData = CalcCache::getHasFwBonusUsers($this->_periodNum, $offset, $this->_limit);
  791. if ($allData) {
  792. $insertBonusData = [];
  793. foreach ($allData as $userId) {
  794. $fwBonusData = CalcCache::getFwBonusList($userId, $this->_periodNum);
  795. if( !$fwBonusData ) continue;
  796. $fwBonus = $fwBonusData['fwBonus'] ?? 0;
  797. if( $fwBonus <=0 ) continue;
  798. //总金额限制
  799. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  800. $fwBonus = $this->bonusTotalLimit($fwBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  801. if( $fwBonus <= 0 ) continue;
  802. //扣除相应的复消积分和管理费
  803. // $deductData = $this->deduct($userId, $fwBonus);
  804. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_BD', $fwBonus);
  805. $decRoleId = $userBaseInfo['DEC_ROLE_ID'];
  806. $insertBonusData[] = [
  807. 'ID' => SnowFake::instance()->generateId(),
  808. 'USER_ID' => $userId,
  809. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  810. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  811. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  812. 'FROM_USER_ID' => $userId,
  813. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  814. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  815. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  816. 'AMOUNT' => $fwBonus,
  817. 'ORI_BONUS' => $fwBonus,
  818. 'RECONSUME_POINTS' => 0,
  819. 'MANAGE_TAX' => 0,
  820. 'PERIOD_NUM' => $this->_periodNum,
  821. 'CALC_YEAR' => $this->_calcYear,
  822. 'CALC_MONTH' => $this->_calcYearMonth,
  823. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  824. 'CREATED_AT' => Date::nowTime(),
  825. 'LOGS' => json_encode([
  826. 'decRoleId' => $decRoleId,
  827. ])
  828. // 'FROM_DATA' => json_encode($ycBonusData['fromData']),
  829. ];
  830. unset($userId, $fwBonusData, $userBaseInfo, $decRoleId, $fwBonus);
  831. }
  832. CalcBonusBD::batchInsert($insertBonusData);
  833. unset($insertBonusData, $allData);
  834. $this->calcBonusBDStepTwo($offset + $this->_limit);
  835. }
  836. unset($allData);
  837. return true;
  838. }
  839. /**
  840. * 报单业绩奖
  841. * @param int $offset
  842. * @return bool
  843. * @throws \yii\db\Exception
  844. */
  845. public function calcBonusBdYJ(int $offset = 0) {
  846. $periodNum = $this->_periodNum;
  847. // 从缓存获取分页有业绩的会员信息
  848. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  849. if ($allData) {
  850. foreach ($allData as $userId) {
  851. $insertBonusData = [];
  852. // 从缓存中获取会员的业绩信息
  853. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  854. if( !$perfData ) continue;
  855. //个人业绩都算见点奖,包括报单和复消、二次购物
  856. $perfPv = $perfData['PV_PCS_ZC'] ?? 0;
  857. if( $perfPv <= 0 ) continue;
  858. //业绩奖使用报单PV
  859. // $oriPointsBonus = Tool::formatPrice($perfPv * $this->_sysConfig['pointsPercent']['VALUE'] / 100);
  860. // if ($oriPointsBonus <= 0) continue;
  861. //偶数层数
  862. $pointsEvenLayer = $this->_sysConfig['pointsLayer']['VALUE'];
  863. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  864. $userEmpLevel = $userBaseInfo['EMP_LV'];
  865. $netWorkParents = Cache::getAllNetworkParents($userId);
  866. foreach ($netWorkParents as $netWorkParent) {
  867. // $pointsBonus = $oriPointsBonus;
  868. //自已距顶端的深度减去父级距顶端的深度
  869. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  870. if( $diffLayer <= 0) continue;
  871. if( $diffLayer > 2 * $pointsEvenLayer ) continue;
  872. if( $diffLayer % 2 == 1 ) continue;
  873. $pointsPercentName = sprintf('pointsPercent_%d', $diffLayer);
  874. if( isset($this->_sysConfig[$pointsPercentName]['VALUE']) ) {
  875. $diffLayerPercent = $this->_sysConfig[$pointsPercentName]['VALUE'];
  876. }else {
  877. $diffLayerPercent = $this->_sysConfig['pointsPercent']['VALUE'];
  878. }
  879. $pointsBonus = Tool::formatPrice($perfPv * $diffLayerPercent / 100);
  880. if( $pointsBonus <= 0 ) continue;
  881. // 把对碰后的奖金存入缓存中
  882. $bonusUserId = $netWorkParent['PARENT_UID'];
  883. if( !$bonusUserId ) continue;
  884. // 获取会员的报单级别
  885. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  886. //判断上限 包括级别奖金上限和总上限
  887. // $pointsBonus = $this->declarationLevelCap($pointsBonus, $bonusUserId, $bonusUserBaseInfo['DEC_LV']);
  888. // if( $pointsBonus <= 0 ) continue;
  889. //总金额限制
  890. $pointsBonus = $this->bonusTotalLimit($pointsBonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  891. if( $pointsBonus <= 0 ) continue;
  892. //扣除相应的复消积分和管理费
  893. $deductData = $this->deduct($bonusUserId, $pointsBonus);
  894. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_YJ', $pointsBonus, $deductData, CalcCache::FROM_MEANS_BD);
  895. //业绩奖流水
  896. $insertBonusData[] = [
  897. 'ID' => SnowFake::instance()->generateId(),
  898. 'USER_ID' => $bonusUserId,
  899. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  900. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  901. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  902. 'FROM_USER_ID' => $userId,
  903. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  904. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  905. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  906. 'AMOUNT' => $deductData['surplus'],
  907. 'ORI_BONUS' => $pointsBonus,
  908. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  909. 'MANAGE_TAX' => $deductData['manageTax'],
  910. 'PERIOD_NUM' => $this->_periodNum,
  911. 'CALC_YEAR' => $this->_calcYear,
  912. 'CALC_MONTH' => $this->_calcYearMonth,
  913. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  914. 'CREATED_AT' => Date::nowTime(),
  915. 'LOGS' => json_encode([
  916. 'perfPv' => $perfPv,
  917. 'pointsLayerConfig' => $this->_sysConfig['pointsLayer']['VALUE'],
  918. 'pointsPercentConfig' => $diffLayerPercent,
  919. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  920. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  921. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  922. 'bonusTotalLimit' => [
  923. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  924. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  925. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  926. ],
  927. ]),
  928. ];
  929. unset($netWorkParent, $pointsBonus, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $deductData, $diffLayerPercent, $pointsPercentName);
  930. }
  931. CalcBonusYJ::batchInsert($insertBonusData);
  932. unset($insertBonusData, $userId, $perfData, $perfPv, $oriPointsBonus, $pointsEvenLayer, $userBaseInfo, $userEmpLevel, $netWorkParents);
  933. }
  934. unset($allData);
  935. return $this->calcBonusBdYJ($offset + $this->_limit);
  936. }
  937. unset($allData);
  938. return true;
  939. }
  940. /**
  941. * 复消业绩奖
  942. * @param int $offset
  943. * @return bool
  944. * @throws \yii\db\Exception
  945. */
  946. public function calcBonusFxYJ(int $offset = 0) {
  947. $periodNum = $this->_periodNum;
  948. // 从缓存获取分页有业绩的会员信息
  949. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  950. if ($allData) {
  951. foreach ($allData as $userId) {
  952. $insertBonusData = [];
  953. // 从缓存中获取会员的业绩信息
  954. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  955. if( !$perfData ) continue;
  956. //个人业绩都算见点奖,包括报单和复消、二次购物
  957. $perfPv = $perfData['PV_PCS_FX'] ?? 0;
  958. if( $perfPv <= 0 ) continue;
  959. //业绩奖使用报单PV
  960. // $oriPointsBonus = Tool::formatPrice($perfPv * $this->_sysConfig['pointsPercent']['VALUE'] / 100);
  961. // if ($oriPointsBonus <= 0) continue;
  962. //偶数层数
  963. $pointsEvenLayer = $this->_sysConfig['fxPointsLayer']['VALUE'];
  964. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  965. $userEmpLevel = $userBaseInfo['EMP_LV'];
  966. $netWorkParents = Cache::getAllNetworkParents($userId);
  967. foreach ($netWorkParents as $netWorkParent) {
  968. // $pointsBonus = $oriPointsBonus;
  969. //自已距顶端的深度减去父级距顶端的深度
  970. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  971. if( $diffLayer <= 0) continue;
  972. if( $diffLayer > 2 * $pointsEvenLayer ) continue;
  973. if( $diffLayer % 2 == 1 ) continue;
  974. $pointsPercentName = sprintf('fxPointsPercent_%d', $diffLayer);
  975. if( isset($this->_sysConfig[$pointsPercentName]['VALUE']) ) {
  976. $diffLayerPercent = $this->_sysConfig[$pointsPercentName]['VALUE'];
  977. }else {
  978. $diffLayerPercent = $this->_sysConfig['fxPointsPercent']['VALUE'];
  979. }
  980. $pointsBonus = Tool::formatPrice($perfPv * $diffLayerPercent / 100);
  981. if( $pointsBonus <= 0 ) continue;
  982. // 把对碰后的奖金存入缓存中
  983. $bonusUserId = $netWorkParent['PARENT_UID'];
  984. if( !$bonusUserId ) continue;
  985. // 获取会员的报单级别
  986. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  987. //判断上限 包括级别奖金上限和总上限
  988. // $pointsBonus = $this->declarationLevelCap($pointsBonus, $bonusUserId, $bonusUserBaseInfo['DEC_LV']);
  989. // if( $pointsBonus <= 0 ) continue;
  990. //总金额限制
  991. $pointsBonus = $this->bonusTotalLimit($pointsBonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  992. if( $pointsBonus <= 0 ) continue;
  993. //扣除相应的复消积分和管理费
  994. $deductData = $this->deduct($bonusUserId, $pointsBonus);
  995. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_YJ', $pointsBonus, $deductData, CalcCache::FROM_MEANS_FX);
  996. //业绩奖流水
  997. $insertBonusData[] = [
  998. 'ID' => SnowFake::instance()->generateId(),
  999. 'USER_ID' => $bonusUserId,
  1000. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1001. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1002. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1003. 'FROM_USER_ID' => $userId,
  1004. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1005. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1006. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1007. 'AMOUNT' => $deductData['surplus'],
  1008. 'ORI_BONUS' => $pointsBonus,
  1009. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1010. 'MANAGE_TAX' => $deductData['manageTax'],
  1011. 'PERIOD_NUM' => $this->_periodNum,
  1012. 'CALC_YEAR' => $this->_calcYear,
  1013. 'CALC_MONTH' => $this->_calcYearMonth,
  1014. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1015. 'CREATED_AT' => Date::nowTime(),
  1016. 'LOGS' => json_encode([
  1017. 'perfPv' => $perfPv,
  1018. 'pointsLayerConfig' => $this->_sysConfig['fxPointsLayer']['VALUE'],
  1019. 'pointsPercentConfig' => $diffLayerPercent,
  1020. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1021. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1022. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1023. 'bonusTotalLimit' => [
  1024. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  1025. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  1026. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  1027. ],
  1028. ]),
  1029. ];
  1030. unset($netWorkParent, $pointsBonus, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $deductData, $diffLayerPercent, $pointsPercentName);
  1031. }
  1032. CalcBonusYJ::batchInsert($insertBonusData);
  1033. unset($insertBonusData, $userId, $perfData, $perfPv, $oriPointsBonus, $pointsEvenLayer, $userBaseInfo, $userEmpLevel, $netWorkParents);
  1034. }
  1035. unset($allData);
  1036. return $this->calcBonusFxYJ($offset + $this->_limit);
  1037. }
  1038. unset($allData);
  1039. return true;
  1040. }
  1041. /**
  1042. * 团队奖
  1043. * @param int $offset
  1044. * @return bool
  1045. * @throws \yii\db\Exception
  1046. */
  1047. public function calcBonusQY(int $offset = 0) {
  1048. echo sprintf("时间:[%s]团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1049. $periodNum = $this->_periodNum;
  1050. // 从缓存获取分页有业绩的会员信息
  1051. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1052. if ($allData) {
  1053. $insertBonusData = [];
  1054. foreach ($allData as $userId) {
  1055. // 从缓存中获取会员的业绩信息
  1056. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1057. // 从缓存中获取会员的上期结余业绩信息
  1058. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1059. // 本期 + 上期结余
  1060. $perfArr = [
  1061. 'SURPLUS_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  1062. 'SURPLUS_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  1063. 'SURPLUS_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  1064. 'SURPLUS_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  1065. 'SURPLUS_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  1066. ];
  1067. $oriPerfArr = [
  1068. 'perfArr' => $perfArr,
  1069. 'touchBonus' => 0,
  1070. ];
  1071. // 获取会员的报单级别
  1072. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1073. $decLevelConfig = $this->_decLevelConfig;
  1074. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1075. // 对碰
  1076. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1077. $touchPerfArr = [];
  1078. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1079. $touchPerfArr[$keyR] = $perfR;
  1080. }
  1081. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1082. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1083. //更新数据库
  1084. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1085. 'USER_ID' => $userId,
  1086. 'PERIOD_NUM' => $periodNum,
  1087. ]);
  1088. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1089. $teamBonus = $touchBonusArr['touchBonus'];
  1090. //判断级别上限
  1091. $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1092. if( $teamBonus <= 0 ) continue;
  1093. $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1094. if( $teamBonus <= 0 ) continue;
  1095. //扣除相应的复消积分和管理费
  1096. $deductData = $this->deduct($userId, $teamBonus);
  1097. // 把对碰后的奖金存入缓存中
  1098. CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus, $deductData);
  1099. //团队奖流水
  1100. $insertBonusData[] = [
  1101. 'ID' => SnowFake::instance()->generateId(),
  1102. 'USER_ID' => $userId,
  1103. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  1104. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  1105. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  1106. 'AMOUNT' => $deductData['surplus'],
  1107. 'ORI_BONUS' => $teamBonus,
  1108. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1109. 'MANAGE_TAX' => $deductData['manageTax'],
  1110. 'PERIOD_NUM' => $this->_periodNum,
  1111. 'CALC_YEAR' => $this->_calcYear,
  1112. 'CALC_MONTH' => $this->_calcYearMonth,
  1113. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1114. 'CREATED_AT' => Date::nowTime(),
  1115. 'LOGS' => json_encode([
  1116. 'perfArr' => $perfArr,
  1117. 'touchPerfArrOri' => $touchBonusArr['perfArr'],
  1118. 'touchPerfArr' => $touchPerfArr,
  1119. 'nowDecLevelConfig' => $nowDecLevelConfig,
  1120. 'decLevel' => $userBaseInfo['DEC_LV'],
  1121. ]),
  1122. ];
  1123. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1124. }
  1125. CalcBonusQY::batchInsert($insertBonusData);
  1126. unset($allData, $insertBonusData);
  1127. return $this->calcBonusQY($offset + $this->_limit);
  1128. }
  1129. unset($allData);
  1130. return true;
  1131. }
  1132. /**
  1133. * 报单团队奖
  1134. * @param int $offset
  1135. * @return bool
  1136. * @throws \yii\db\Exception
  1137. */
  1138. public function calcBonusBdQY(int $offset = 0) {
  1139. echo sprintf("时间:[%s]报单团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1140. $periodNum = $this->_periodNum;
  1141. // 从缓存获取分页有业绩的会员信息
  1142. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1143. if ($allData) {
  1144. $insertBonusData = [];
  1145. foreach ($allData as $userId) {
  1146. // 从缓存中获取会员的业绩信息
  1147. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1148. // 从缓存中获取会员的上期结余业绩信息
  1149. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1150. // 本期 + 上期结余
  1151. $perfArr = [
  1152. 'SURPLUS_1L_ZC' => $perfData['PV_1L_ZC'] + $pervSurplusPerf['SURPLUS_1L_ZC'],
  1153. 'SURPLUS_2L_ZC' => $perfData['PV_2L_ZC'] + $pervSurplusPerf['SURPLUS_2L_ZC'],
  1154. 'SURPLUS_3L_ZC' => $perfData['PV_3L_ZC'] + $pervSurplusPerf['SURPLUS_3L_ZC'],
  1155. 'SURPLUS_4L_ZC' => $perfData['PV_4L_ZC'] + $pervSurplusPerf['SURPLUS_4L_ZC'],
  1156. 'SURPLUS_5L_ZC' => $perfData['PV_5L_ZC'] + $pervSurplusPerf['SURPLUS_5L_ZC'],
  1157. ];
  1158. $oriPerfArr = [
  1159. 'perfArr' => $perfArr,
  1160. 'touchBonus' => 0,
  1161. ];
  1162. // 获取会员的报单级别
  1163. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1164. $decLevelConfig = $this->_decLevelConfig;
  1165. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1166. // 对碰
  1167. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1168. $touchPerfArr = [];
  1169. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1170. $touchPerfArr[$keyR] = $perfR;
  1171. }
  1172. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1173. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1174. //更新数据库
  1175. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1176. 'USER_ID' => $userId,
  1177. 'PERIOD_NUM' => $periodNum,
  1178. ]);
  1179. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1180. $teamBonus = $touchBonusArr['touchBonus'];
  1181. //判断级别上限
  1182. // $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1183. // if( $teamBonus <= 0 ) continue;
  1184. // $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1185. // if( $teamBonus <= 0 ) continue;
  1186. //扣除相应的复消积分和管理费
  1187. // $deductData = $this->deduct($userId, $teamBonus);
  1188. // 把对碰后的奖金存入缓存中
  1189. CalcCache::bonus($userId, $periodNum, 'BONUS_QY_BD', $teamBonus);
  1190. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1191. }
  1192. unset($allData, $insertBonusData);
  1193. return $this->calcBonusBdQY($offset + $this->_limit);
  1194. }
  1195. unset($allData);
  1196. return true;
  1197. }
  1198. /**
  1199. * 复消团队奖
  1200. * @param int $offset
  1201. * @return bool
  1202. * @throws \yii\db\Exception
  1203. */
  1204. public function calcBonusFxQY(int $offset = 0) {
  1205. echo sprintf("时间:[%s]复消团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1206. $periodNum = $this->_periodNum;
  1207. // 从缓存获取分页有业绩的会员信息
  1208. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1209. if ($allData) {
  1210. $insertBonusData = [];
  1211. foreach ($allData as $userId) {
  1212. // 从缓存中获取会员的业绩信息
  1213. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1214. // 从缓存中获取会员的上期结余业绩信息
  1215. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1216. // 本期 + 上期结余
  1217. $perfArr = [
  1218. 'SURPLUS_1L_FX' => $perfData['PV_1L_FX'] + $pervSurplusPerf['SURPLUS_1L_FX'],
  1219. 'SURPLUS_2L_FX' => $perfData['PV_2L_FX'] + $pervSurplusPerf['SURPLUS_2L_FX'],
  1220. 'SURPLUS_3L_FX' => $perfData['PV_3L_FX'] + $pervSurplusPerf['SURPLUS_3L_FX'],
  1221. 'SURPLUS_4L_FX' => $perfData['PV_4L_FX'] + $pervSurplusPerf['SURPLUS_4L_FX'],
  1222. 'SURPLUS_5L_FX' => $perfData['PV_5L_FX'] + $pervSurplusPerf['SURPLUS_5L_FX'],
  1223. ];
  1224. $oriPerfArr = [
  1225. 'perfArr' => $perfArr,
  1226. 'touchBonus' => 0,
  1227. ];
  1228. // 获取会员的报单级别
  1229. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1230. $decLevelConfig = $this->_decLevelConfig;
  1231. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1232. // 对碰
  1233. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1234. //大区封顶
  1235. $touchPerfArr = [];
  1236. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1237. // if ($perfR > $nowDecLevelConfig['QY_BIG_CAP']) $perfR = $nowDecLevelConfig['QY_BIG_CAP'];
  1238. $touchPerfArr[$keyR] = $perfR;
  1239. }
  1240. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1241. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1242. //更新数据库
  1243. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1244. 'USER_ID' => $userId,
  1245. 'PERIOD_NUM' => $periodNum,
  1246. ]);
  1247. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1248. $teamBonus = $touchBonusArr['touchBonus'];
  1249. //判断级别上限
  1250. // $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1251. // if( $teamBonus <= 0 ) continue;
  1252. //
  1253. // $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1254. // if( $teamBonus <= 0 ) continue;
  1255. //扣除相应的复消积分和管理费
  1256. // $deductData = $this->deduct($userId, $teamBonus);
  1257. // 把对碰后的奖金存入缓存中
  1258. CalcCache::bonus($userId, $periodNum, 'BONUS_QY_FX', $teamBonus);
  1259. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1260. }
  1261. unset($allData, $insertBonusData);
  1262. return $this->calcBonusFxQY($offset + $this->_limit);
  1263. }
  1264. unset($allData);
  1265. return true;
  1266. }
  1267. /**
  1268. * 计算共享奖之前的操作
  1269. * @param int $offset
  1270. * @return bool
  1271. */
  1272. public function calcBonusGXBefore(int $offset = 0) {
  1273. //获取所有激活会员
  1274. $allData = CalcCache::getActiveUsers($this->_periodNum, $offset, $this->_limit);
  1275. if ($allData) {
  1276. echo sprintf("时间:[%s]共享奖之前,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1277. foreach ($allData as $userId) {
  1278. //判断$userId 能否得共享奖
  1279. // //判断 是否本期小市场(安置关系)新增980pv 能否得奖
  1280. // if ( !$this->checkSmallMarketPerf($userId) ) {
  1281. // unset($userId);
  1282. // continue;
  1283. // }
  1284. // 20220407 小市场980判断->改成判断此期用户是否有原报单团队奖金
  1285. if ( !$this->checkHasOriBonusQyBd($userId) ) {
  1286. unset($userId);
  1287. continue;
  1288. }
  1289. //获取当前会员所有的推荐上级, 并查看是否有首单团队奖
  1290. $validDeep = 0;
  1291. $this->loopRelationParentDo($userId, function ($parent) use ($userId, &$validDeep) {
  1292. //判断$parent是否有首单团队奖
  1293. $parentBonus = CalcCache::bonus($parent['PARENT_UID'], $this->_periodNum);
  1294. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {
  1295. $validDeep += 1;
  1296. //记录得奖缓存
  1297. CalcCache::addShareBonusOneRelation($userId, $parent['PARENT_UID'], $this->_periodNum, $validDeep);
  1298. }
  1299. unset($parent, $parentBonus);
  1300. if ( $validDeep >= 2 ) {
  1301. return self::LOOP_FINISH;
  1302. }
  1303. });
  1304. unset($userId, $validDeep);
  1305. }
  1306. unset($allData);
  1307. return $this->calcBonusGXBefore($offset + $this->_limit);
  1308. }
  1309. unset($allData);
  1310. return true;
  1311. }
  1312. /**
  1313. * 共享奖
  1314. * @param int $offset
  1315. * @return bool
  1316. * @throws \yii\db\Exception
  1317. */
  1318. public function calcBonusGX(int $offset = 0) {
  1319. // 从缓存获取分页有的会员信息
  1320. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_gxLimit);
  1321. if ($allData) {
  1322. echo sprintf("时间:[%s]共享奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1323. $insertBonusData = [];
  1324. // 获取线上找几层和是否开启找几层的限制
  1325. $configsGx = Config::find()->where(
  1326. 'CONFIG_NAME="gxNoPvFindLimitSwitch" OR CONFIG_NAME="gxNoPvFindLimitTimes"'
  1327. )->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  1328. $openFindLimit = isset($configsGx['gxNoPvFindLimitSwitch']) ? $configsGx['gxNoPvFindLimitSwitch']['VALUE'] : 1; // 是否开放了 得奖人不满足980就找一个满足980的人来得这个奖 的最大向上循环次数
  1329. $findLimitTimes = isset($configsGx['gxNoPvFindLimitTimes']) ? $configsGx['gxNoPvFindLimitTimes']['VALUE'] : 5 ; // 开放的限制层数
  1330. foreach ($allData as $userId) {
  1331. $bonusUserData = [];
  1332. $bonusPercentOne = $this->_sysConfig['shareRecPercent']['VALUE'] / 100;
  1333. $bonusPercentTwo = $this->_sysConfig['sharePercent']['VALUE'] / 100;
  1334. // 从缓存中获取会员的收入信息
  1335. $incomeBonus = CalcCache::bonus($userId, $this->_periodNum);
  1336. $lastTwoIncome = $incomeBonus['ORI_BONUS_QY_BD'] ?? 0;
  1337. $shareBonusOne = $lastTwoIncome * $bonusPercentOne;
  1338. unset($lastTwoIncome);
  1339. //找到上2代 新增个人+推荐团队业绩 加权平均分配 这个奖
  1340. if( $shareBonusOne > 0) {
  1341. $bonusOneData = $this->gxLastTwoBonusData($userId, 1, $shareBonusOne);
  1342. $bonusTwoData = $this->gxLastTwoBonusData($userId, 2, $shareBonusOne);
  1343. $bonusUserData = array_merge($bonusUserData, $bonusOneData);
  1344. $bonusUserData = array_merge($bonusUserData, $bonusTwoData);
  1345. unset($bonusOneData, $bonusTwoData);
  1346. }
  1347. $nextFiveIncome = $incomeBonus['ORI_BONUS_QY_BD'] ?? 0;
  1348. $shareBonusTwo = $nextFiveIncome * $bonusPercentTwo;
  1349. unset($nextFiveIncome);
  1350. //找到下5代(安置网) 紧缩 相对偶数层2、4、6、8、10层
  1351. if( $shareBonusTwo > 0 ) {
  1352. $validDeep = 1;
  1353. $this->loopNetworkParentDo($userId, function ($parent) use ($userId, $shareBonusTwo, &$validDeep, &$bonusUserData) {
  1354. if ( $validDeep % 2 === 0 ) {
  1355. $bonusUserData[] = [
  1356. 'bonusUid' => $parent['PARENT_UID'],
  1357. 'bonus' => $shareBonusTwo,
  1358. 'validMinPv' => true,
  1359. ];
  1360. }
  1361. //判断$parent是否有首单团队奖
  1362. $parentBonus = CalcCache::bonus($parent['PARENT_UID'], $this->_periodNum);
  1363. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {//有效层
  1364. $validDeep += 1;
  1365. }
  1366. unset($parent, $parentBonus);
  1367. if ( $validDeep >= 11 ) {
  1368. return self::LOOP_FINISH;
  1369. }
  1370. });
  1371. unset($validDeep);
  1372. }
  1373. if ( !$bonusUserData ) continue;
  1374. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1375. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1376. foreach ($bonusUserData as $bonusUserDataEvery) {
  1377. $bonusUserId = $bonusUserDataEvery['bonusUid'];
  1378. //判断 是否本期小市场(安置关系)新增980pv 能否得奖->20220407改成判断得奖人此期是否有报单团队奖金
  1379. // if ( $bonusUserDataEvery['validMinPv'] && !$this->checkSmallMarketPerf($bonusUserId) ) { 20220407
  1380. if ( $bonusUserDataEvery['validMinPv'] && !$this->checkHasOriBonusQyBd($bonusUserId) ) {
  1381. // unset($bonusUserDataEvery, $bonusUserId);
  1382. // continue;
  1383. //得奖人不满足980就找一个满足980的人来得这个奖
  1384. // 20220407改成判断得奖人此期是否有报单团队奖金,如果没有则找一个此期有报单团队奖金得人
  1385. $bonusUserId = $this->getMinBdPvNetworkParent($bonusUserId, $openFindLimit, $findLimitTimes);
  1386. if ( !$bonusUserId ) {
  1387. unset($bonusUserDataEvery, $bonusUserId);
  1388. continue;
  1389. }
  1390. }
  1391. $bonus = Tool::formatPrice($bonusUserDataEvery['bonus']);
  1392. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1393. //总金额限制
  1394. $bonus = $this->bonusTotalLimit($bonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  1395. if( $bonus <= 0 ) continue;
  1396. //扣除相应的复消积分和管理费
  1397. $deductData = $this->deduct($bonusUserId, $bonus);
  1398. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GX', $bonus, $deductData);
  1399. CalcCache::addHasMonthBonusUsers($bonusUserId, $this->_periodNum);
  1400. //共享奖流水
  1401. $insertBonusData[] = [
  1402. 'ID' => SnowFake::instance()->generateId(),
  1403. 'USER_ID' => $bonusUserId,
  1404. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1405. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1406. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1407. 'FROM_USER_ID' => $userId,
  1408. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1409. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1410. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1411. 'AMOUNT' => $deductData['surplus'],
  1412. 'ORI_BONUS' => $bonus,
  1413. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1414. 'MANAGE_TAX' => $deductData['manageTax'],
  1415. 'PERIOD_NUM' => $this->_periodNum,
  1416. 'CALC_YEAR' => $this->_calcYear,
  1417. 'CALC_MONTH' => $this->_calcYearMonth,
  1418. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1419. 'CREATED_AT' => Date::nowTime(),
  1420. 'LOGS' => json_encode([
  1421. 'sharePercent' => $bonusPercentOne,
  1422. 'incomeBonus' => $incomeBonus,
  1423. 'recNum' => $userBaseInfo['REC_NUM'],
  1424. 'decAmount' => $userBaseInfo['ZC_AMOUNT'],
  1425. 'bonusTotalLimit' => [
  1426. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  1427. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  1428. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  1429. ],
  1430. ]),
  1431. ];
  1432. unset($bonusUserDataEvery, $bonusUserId, $bonus, $bonusUserBaseInfo, $deductData);
  1433. }
  1434. unset($userId, $bonusUserData, $userBaseInfo, $userEmpLevel, $bonusPercentOne, $bonusPercentTwo, $incomeBonus, $sharePerson);
  1435. // echo ('内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  1436. }
  1437. if($insertBonusData) CalcBonusGX::batchInsert($insertBonusData);
  1438. unset($allData, $insertBonusData);
  1439. return $this->calcBonusGX($offset + $this->_gxLimit);
  1440. }
  1441. unset($allData);
  1442. return true;
  1443. }
  1444. // 执行蓝星管理奖金的存储过程
  1445. public function calcBsProcedure() {
  1446. if( !$this->_isCalcMonth ) {
  1447. // 不是结算月,则不进行计算
  1448. return false;
  1449. }
  1450. $result = \Yii::$app->db->createCommand("CALL CalcBlue(:periodNum)")
  1451. ->bindValue(':periodNum' , $this->_periodNum )
  1452. ->execute();
  1453. return $result;
  1454. }
  1455. // 执行旅游奖、名车奖、豪宅奖的计算
  1456. public function calcBonusZoneBS(array $bonusConfig) {
  1457. // 月结,如果不是月结点,则直接退出
  1458. if (!$this->_isCalcMonth) {
  1459. return true;
  1460. }
  1461. // 达标条件:聘级、级别、奖项比例
  1462. $config = json_decode($bonusConfig['OPTIONS'], true);
  1463. // 奖金总比例
  1464. $mate = $bonusConfig['VALUE'] / 100;
  1465. // 会员级别
  1466. $minDecLevel = $config['OPTIONS']['declarationLevel'] ?? [];
  1467. // 会员聘级
  1468. $minEmpLevel = $config['OPTIONS']['employLevel'] ?? [];
  1469. // 月度公司总PV
  1470. $monthTotalPV = PerfMonth::find()
  1471. ->yearMonth($this->_calcYearMonth)
  1472. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  1473. ->sum('PV_PCS');
  1474. if ($monthTotalPV <= 0) {
  1475. return true;
  1476. }
  1477. // 基于蓝星奖结果计算符合获奖条件的会员
  1478. $userBonusData = CalcBonusBS::find()
  1479. ->yearMonth($this->_calcYearMonth)
  1480. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  1481. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_EMP_LV,LAST_STATUS,RECONSUME_POINTS')
  1482. ->groupBy('USER_ID')
  1483. ->asArray()
  1484. ->all();
  1485. // 获奖会员数
  1486. $userBonusNumber = count($userBonusData);
  1487. if (!$userBonusNumber) {
  1488. return true;
  1489. }
  1490. // 计算奖金:(公司月总PV * 奖金比) / 得奖人数
  1491. $amount = Tool::formatPrice(($monthTotalPV * $mate) / $userBonusNumber);
  1492. if ($amount <= 0) {
  1493. return true;
  1494. }
  1495. // 奖金标识
  1496. if ($bonusConfig['CONFIG_NAME'] == 'openTravel') {
  1497. $bonusCacheKey = 'calc:travel:bonus_';
  1498. } else if ($bonusConfig['CONFIG_NAME'] == 'openCar') {
  1499. $bonusCacheKey = 'calc:car:bonus_';
  1500. } else {
  1501. $bonusCacheKey = 'calc:house:bonus_';
  1502. }
  1503. $insertBonusData = [];
  1504. foreach($userBonusData as $userBonus) {
  1505. // 会员级别+聘级都达到要求才会发放奖金
  1506. if (in_array($userBonus['LAST_DEC_LV'], $minDecLevel) && in_array($userBonus['LEVEL_ID'], $minEmpLevel)) {
  1507. // 放入缓存
  1508. CalcCache::zoneBonus($userBonus['USER_ID'], $this->_periodNum, $bonusCacheKey, $amount);
  1509. // 加入月奖的会员
  1510. CalcCache::addHasMonthBonusUsers($userBonus['USER_ID'], $this->_periodNum);
  1511. }
  1512. $insertBonusData[] = [
  1513. 'ID' => SnowFake::instance()->generateId(),
  1514. 'USER_ID' => $userBonus['USER_ID'],
  1515. 'LEVEL_ID' => $userBonus['LEVEL_ID'],
  1516. 'LAST_DEC_LV' => $userBonus['LAST_DEC_LV'],
  1517. 'LAST_EMP_LV' => $userBonus['LAST_EMP_LV'],
  1518. 'LAST_STATUS' => $userBonus['LAST_STATUS'],
  1519. 'AMOUNT' => $amount,
  1520. 'PERIOD_NUM' => $this->_periodNum,
  1521. 'CALC_YEAR' => $this->_calcYear,
  1522. 'CALC_MONTH' => $this->_calcYearMonth,
  1523. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1524. 'CREATED_AT' => Date::nowTime(),
  1525. 'BONUS_TYPE' => $bonusConfig['CONFIG_NAME'],
  1526. ];
  1527. }
  1528. // 数据写入总表
  1529. if ($insertBonusData) {
  1530. CalcBonusZone::batchInsert($insertBonusData);
  1531. }
  1532. return true;
  1533. }
  1534. /**
  1535. * 蓝星管理奖金
  1536. * @param int $offset
  1537. * @return bool
  1538. * @throws \yii\db\Exception
  1539. */
  1540. public function calcBonusBsGL(int $offset = 0) {
  1541. if( !$this->_isCalcMonth ) {
  1542. // 不是结算月,则不进行计算
  1543. return false;
  1544. }
  1545. // 从缓存获取分页有收入的会员信息
  1546. $allData = CalcBonusBS::findUseDbCalc()
  1547. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1548. ->groupBy('USER_ID')
  1549. ->offset($offset)
  1550. ->limit($this->_limit)
  1551. ->asArray()
  1552. ->all();
  1553. if ($allData) {
  1554. // 达标条件:会员级别:钻卡
  1555. $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
  1556. $minDecLevel = $config['mntDec'] ?? [];
  1557. foreach ($allData as $user) {
  1558. //扣除相应的复消积分和管理费
  1559. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_MNT']);
  1560. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1561. $manageTax = $deductData['manageTax']; // 管理费
  1562. $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
  1563. // 管理奖钻卡发放
  1564. if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
  1565. // 把对碰后的奖金存入缓存中
  1566. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_MNT', $user['ORI_BONUS_MNT'], $deductData);
  1567. // 加入月奖的会员
  1568. CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
  1569. }
  1570. // 更新蓝星奖金存储过程的实发金额数据
  1571. CalcBonusBS::updateAll([
  1572. 'RECONSUME_POINTS' => $point,
  1573. 'AMOUNT_MNT' => $realBonusBs,
  1574. 'MANAGE_TAX_MNT' => $manageTax],
  1575. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1576. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1577. }
  1578. return $this->calcBonusBsGL($offset + $this->_limit);
  1579. }
  1580. unset($allData);
  1581. return true;
  1582. }
  1583. /**
  1584. * 蓝星业绩奖金
  1585. * @param int $offset
  1586. * @return bool
  1587. * @throws \yii\db\Exception
  1588. */
  1589. public function calcBonusBsYJ(int $offset = 0) {
  1590. if( !$this->_isCalcMonth ) {
  1591. // 不是结算月,则不进行计算
  1592. return false;
  1593. }
  1594. // 从缓存获取分页有收入的会员信息
  1595. $allData = CalcBonusBS::findUseDbCalc()
  1596. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1597. ->groupBy('USER_ID')
  1598. ->offset($offset)
  1599. ->limit($this->_limit)
  1600. ->asArray()
  1601. ->all();
  1602. if ($allData) {
  1603. // 达标条件:会员级别:金卡、钻卡
  1604. $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true);
  1605. $minDecLevel = $config['abbrDec'] ?? [];
  1606. foreach ($allData as $user) {
  1607. //扣除相应的复消积分和管理费
  1608. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']);
  1609. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1610. $manageTax = $deductData['manageTax']; // 管理费
  1611. $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分
  1612. // 业绩奖金卡、钻卡发放
  1613. if (in_array($user['LAST_DEC_LV'], $minDecLevel)) {
  1614. // 把对碰后的奖金存入缓存中
  1615. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_ABBR', $user['ORI_BONUS_ABBR'], $deductData);
  1616. // 加入月奖的会员
  1617. CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum);
  1618. }
  1619. // 更新蓝星业绩奖金存储过程的实发金额数据
  1620. CalcBonusBS::updateAll([
  1621. 'AMOUNT_ABBR' => $realBonusBs,
  1622. 'MANAGE_TAX_ABBR' => $manageTax,
  1623. 'RECONSUME_POINTS' => $point],
  1624. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1625. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1626. }
  1627. return $this->calcBonusBsYJ($offset + $this->_limit);
  1628. }
  1629. unset($allData);
  1630. return true;
  1631. }
  1632. /**
  1633. * 报单管理奖
  1634. * @param int $offset
  1635. * @return bool
  1636. * @throws \yii\db\Exception
  1637. */
  1638. public function calcBonusBdGL(int $offset = 0) {
  1639. // 从缓存获取分页有收入的会员信息
  1640. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1641. if ($allData) {
  1642. $insertBonusData = [];
  1643. foreach ($allData as $userId) {
  1644. $bonusUserData = [];
  1645. //会员信息
  1646. $validDeep = 1;
  1647. $this->loopRelationParentDo($userId, function ($parent) use ($userId, &$validDeep, &$bonusUserData) {
  1648. //判断$parent是否有首单团队奖
  1649. $bonusUserId = $parent['PARENT_UID'];
  1650. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1651. $nowDecLevelConfig = $this->_decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1652. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1653. if( $recNum <= 0 ) {
  1654. $maxOddLayer = 0;
  1655. }else if( $recNum == 1 ) {
  1656. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'] ?? 0;
  1657. }else if( $recNum == 2 ) {
  1658. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'] ?? 0;
  1659. }else {
  1660. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'] ?? 0;
  1661. }
  1662. if ( $validDeep < 2 * $maxOddLayer && $validDeep % 2 === 1 ) {//奇数层
  1663. $bonusUserData[] = [
  1664. 'bonusUid' => $parent['PARENT_UID'],
  1665. 'validDeep' => $validDeep
  1666. ];
  1667. }
  1668. $parentBonus = CalcCache::bonus($bonusUserId, $this->_periodNum);
  1669. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {//有效层
  1670. $validDeep += 1;
  1671. }
  1672. unset($parent, $parentBonus, $bonusUserId, $bonusUserBaseInfo, $nowDecLevelConfig, $recNum, $maxOddLayer);
  1673. //超过代数上限则结束
  1674. if ( $validDeep >= 18 ) {
  1675. return self::LOOP_FINISH;
  1676. }
  1677. });
  1678. //发奖
  1679. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1680. $userBonus = CalcCache::bonus($userId, $this->_periodNum);
  1681. foreach ($bonusUserData as $everyData) {
  1682. $bonusUserId = $everyData['bonusUid'];
  1683. //判断 是否本期小市场(安置关系)新增980pv 能否得奖
  1684. if ( !$this->checkSmallMarketPerf($bonusUserId) ) {
  1685. // unset($bonusUserId);
  1686. // continue;
  1687. //得奖人不满足980就找一个满足980的人来得这个奖
  1688. $bonusUserId = $this->getMinBdPvRelationParent($bonusUserId);
  1689. if ( !$bonusUserId ) {
  1690. unset($bonusUserId);
  1691. continue;
  1692. }
  1693. }
  1694. $parentOddPercentValue = floor($everyData['validDeep']/6);
  1695. $parentOddPercentName = sprintf('parentOddPercent_%s', $parentOddPercentValue);
  1696. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1697. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1698. $manageBonus = Tool::formatPrice($userBonus['ORI_BONUS_QY_BD'] * $bonusPercent / 100);
  1699. if ($manageBonus <= 0) continue;
  1700. //总金额限制
  1701. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1702. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1703. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1704. if( $manageBonus <= 0 ) continue;
  1705. //扣除相应的复消积分和管理费
  1706. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1707. // 钻卡会员奖金记入缓存
  1708. $decLevelConfig = $this->_decLevelConfig;
  1709. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1710. if ($nowDecLevelConfig == '67ABCE0ECE705575E055736AECE8644D') {
  1711. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_BD);
  1712. CalcCache::addHasMonthBonusUsers($bonusUserId, $this->_periodNum);
  1713. }
  1714. //管理奖流水
  1715. $insertBonusData[] = [
  1716. 'ID' => SnowFake::instance()->generateId(),
  1717. 'USER_ID' => $bonusUserId,
  1718. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1719. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1720. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1721. 'FROM_USER_ID' => $userId,
  1722. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1723. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  1724. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1725. 'AMOUNT' => $deductData['surplus'],
  1726. 'ORI_BONUS' => $manageBonus,
  1727. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1728. 'MANAGE_TAX' => $deductData['manageTax'],
  1729. 'PERIOD_NUM' => $this->_periodNum,
  1730. 'CALC_YEAR' => $this->_calcYear,
  1731. 'CALC_MONTH' => $this->_calcYearMonth,
  1732. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1733. 'CREATED_AT' => Date::nowTime(),
  1734. 'LOGS' => json_encode([
  1735. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1736. 'validDeep' => $everyData['validDeep'],
  1737. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1738. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1739. 'fromUserId' => $userId,
  1740. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1741. 'bonusTotalLimit' => [
  1742. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  1743. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  1744. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  1745. ],
  1746. ]),
  1747. ];
  1748. unset($everyData, $bonusUserId, $parentOddPercentValue, $parentOddPercentName, $bonusPercent, $manageBonus, $deductData);
  1749. }
  1750. unset($userId, $validDeep, $userBaseInfo, $userBonus, $bonusUserData);
  1751. }
  1752. if($insertBonusData) CalcBonusGL::batchInsert($insertBonusData);
  1753. unset($allData, $insertBonusData);
  1754. return $this->calcBonusBdGL($offset + $this->_limit);
  1755. }
  1756. unset($allData);
  1757. return true;
  1758. }
  1759. /**
  1760. * 复消管理奖
  1761. * @param int $offset
  1762. * @return bool
  1763. * @throws \yii\db\Exception
  1764. */
  1765. public function calcBonusFxGL(int $offset = 0) {
  1766. $periodNum = $this->_periodNum;
  1767. // 从缓存获取分页有收入的会员信息
  1768. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1769. if ($allData) {
  1770. $insertBonusData = [];
  1771. foreach ($allData as $userId) {
  1772. //会员信息
  1773. $decLevelConfig = $this->_decLevelConfig;
  1774. $netWorkParents = Cache::getAllRelationParents($userId);
  1775. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1776. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1777. foreach ($netWorkParents as $netWorkParent) {
  1778. //自已距顶端的深度减去父级距顶端的深度
  1779. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  1780. if( $diffLayer <= 0) continue;
  1781. //@todo 超过最大代数就跳出本次循环
  1782. //偶数代跳过
  1783. if( $diffLayer % 2 == 0 ) continue;
  1784. // 获取会员的报单级别
  1785. $bonusUserId = $netWorkParent['PARENT_UID'];
  1786. if( !$bonusUserId ) continue;
  1787. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1788. $nowDecLevelConfig = $decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1789. if( !$nowDecLevelConfig ) continue;
  1790. //推荐人数
  1791. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1792. if( $recNum <= 0 ) continue;
  1793. if( $recNum == 1 ) {
  1794. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'];
  1795. }else if( $recNum == 2 ) {
  1796. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'];
  1797. }else {
  1798. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'];
  1799. }
  1800. //超过代数上限也跳过
  1801. if( $diffLayer >= 2 * $maxOddLayer ) continue;
  1802. // $everyLayer = floor(2 * $nowDecLevelConfig['GL_ODD_DEEP_THREE'] / $nowDecLevelConfig['SORT']);
  1803. //多少代
  1804. // $theLayer = $diffLayer % $everyLayer;
  1805. // $theLayerLevel = floor($diffLayer/$everyLayer);
  1806. $theLayerLevel = floor($diffLayer/6);
  1807. $parentOddPercentName = sprintf('parentOddPercent_%s', $theLayerLevel);
  1808. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1809. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1810. // 从缓存中获取会员的收入信息
  1811. $incomeBonus = CalcCache::bonus($userId, $periodNum);
  1812. $parentOddIncomeFromName = sprintf('parentOddIncomeFrom_%s', $theLayerLevel);
  1813. $parentOddIncomeFrom = explode(',', $this->_sysConfig[$parentOddIncomeFromName]['VALUE']);
  1814. $parentOddIncome = 0;
  1815. foreach ($parentOddIncomeFrom as $incomeType) {
  1816. if( $incomeType === 'XF' ) {
  1817. $incomeTypeName = sprintf('ORI_BONUS_%s', $incomeType);
  1818. }else {
  1819. $incomeTypeName = sprintf('ORI_BONUS_%s_FX', $incomeType);
  1820. }
  1821. $incomeTypeValue = $incomeBonus[$incomeTypeName] ?? 0;
  1822. $parentOddIncome += $incomeTypeValue;
  1823. unset($incomeType, $incomeTypeName, $incomeTypeValue);
  1824. }
  1825. unset($parentOddIncomeFrom);
  1826. $manageBonus = Tool::formatPrice($parentOddIncome * $bonusPercent / 100);
  1827. if ($manageBonus <= 0) continue;
  1828. //总金额限制
  1829. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1830. if( $manageBonus <= 0 ) continue;
  1831. //扣除相应的复消积分和管理费
  1832. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1833. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_FX);
  1834. //管理奖流水
  1835. $insertBonusData[] = [
  1836. 'ID' => SnowFake::instance()->generateId(),
  1837. 'USER_ID' => $bonusUserId,
  1838. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1839. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1840. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1841. 'FROM_USER_ID' => $userId,
  1842. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1843. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1844. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1845. 'AMOUNT' => $deductData['surplus'],
  1846. 'ORI_BONUS' => $manageBonus,
  1847. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1848. 'MANAGE_TAX' => $deductData['manageTax'],
  1849. 'PERIOD_NUM' => $this->_periodNum,
  1850. 'CALC_YEAR' => $this->_calcYear,
  1851. 'CALC_MONTH' => $this->_calcYearMonth,
  1852. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1853. 'CREATED_AT' => Date::nowTime(),
  1854. 'LOGS' => json_encode([
  1855. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1856. $parentOddIncomeFromName => $this->_sysConfig[$parentOddIncomeFromName]['VALUE'],
  1857. 'decLevelConfig' => $nowDecLevelConfig,
  1858. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1859. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1860. 'fromUserId' => $userId,
  1861. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1862. 'bonusTotalLimit' => [
  1863. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  1864. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  1865. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  1866. ],
  1867. ]),
  1868. ];
  1869. unset($netWorkParent, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $bonusPercent, $nowDecLevelConfig, $manageBonus, $incomeBonus, $maxOddLayer, $theLayerLevel, $recNum, $parentOddPercentName, $parentOddIncomeFromName, $deductData);
  1870. }
  1871. unset($userId, $userBaseInfo, $userEmpLevel, $netWorkParents, $decLevelConfig);
  1872. }
  1873. CalcBonusGL::batchInsert($insertBonusData);
  1874. unset($allData, $insertBonusData);
  1875. return $this->calcBonusFxGL($offset + $this->_limit);
  1876. }
  1877. unset($allData);
  1878. return true;
  1879. }
  1880. /**
  1881. * 结算荣衔奖第一步
  1882. * @param int $offset
  1883. * @return bool
  1884. * @throws \yii\db\Exception
  1885. * @throws \Exception
  1886. */
  1887. public function calcBonusYCStepOne(int $offset = 0) {
  1888. // 月结,如果不是月结点,则直接退出
  1889. if (!$this->_isCalcMonth) {
  1890. return true;
  1891. }
  1892. echo sprintf("时间:[%s]荣衔奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1893. //从缓存获取分页有本月业绩的会员
  1894. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  1895. if ($allData) {
  1896. $maxLevelId = EmployLevel::SHX_ZJ_LEVEL_ID;
  1897. $maxLevel = $this->_empLevelConfig[$maxLevelId];
  1898. $maxLevelPercent = $maxLevel['RX_PERCENT'] ?? 0;
  1899. unset($maxLevelId, $maxLevel);
  1900. foreach ($allData as $userId) {
  1901. // echo sprintf("时间:[%s]荣衔奖,当前用户ID为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $userId);
  1902. $nowMonthPerf = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1903. $monthPvPcs = $nowMonthPerf['PV_PCS'] ?? 0;
  1904. if( $monthPvPcs <= 0 ) continue;
  1905. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1906. //个人业绩的奖金
  1907. $empLevelId = $userBaseInfo['EMP_LV'];
  1908. $empLevel = $this->_empLevelConfig[$empLevelId];
  1909. $rxPercent = $empLevel['RX_PERCENT'] ?? 0;
  1910. $empBonus = $monthPvPcs * $rxPercent / 100;
  1911. if( $empBonus > 0 ) {
  1912. //给本人添加聘级比例
  1913. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $rxPercent);
  1914. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1915. CalcCache::saveYCBonusList($userId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1916. CalcCache::addHasYcBonusUsers($userId, $this->_periodNum);
  1917. //达到最大的比例其它人就得不到了
  1918. if( $rxPercent >= $maxLevelPercent ) continue;
  1919. }
  1920. unset($empLevelId, $empLevel, $rxPercent, $empBonus);
  1921. //获取所有的父级
  1922. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $monthPvPcs, $maxLevelPercent) {
  1923. $bonusUserId = $parent['PARENT_UID'];
  1924. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  1925. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1926. $parentEmpLevelId = $bonusUserInfo['EMP_LV'];
  1927. $parentEmpLevel = $this->_empLevelConfig[$parentEmpLevelId];
  1928. $parentRxPercent = $parentEmpLevel['RX_PERCENT'] ?? 0;
  1929. $cacheMaxPercent = CalcCache::ycMaxBonusPercent($userId, $this->_periodNum);
  1930. $diffPercent = $parentRxPercent - $cacheMaxPercent;
  1931. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  1932. $empBonus = $monthPvPcs * $diffPercent / 100;
  1933. if( $empBonus <= 0 ) return self::LOOP_CONTINUE;
  1934. //给本人添加聘级比例
  1935. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $parentRxPercent);
  1936. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1937. CalcCache::saveYCBonusList($bonusUserId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1938. CalcCache::addHasYcBonusUsers($bonusUserId, $this->_periodNum);
  1939. unset($cacheMaxPercent, $diffPercent, $parentEmpLevel, $bonusUserId, $bonusUserInfo, $parentEmpLevelId, $empBonus);
  1940. //达到最大的比例就不在向上找了
  1941. if( $parentRxPercent >= $maxLevelPercent ) return self::LOOP_FINISH;
  1942. unset($parentRxPercent);
  1943. });
  1944. unset($nowMonthPerf, $monthPvPcs);
  1945. }
  1946. unset($allData, $maxLevelPercent);
  1947. $this->calcBonusYCStepOne($offset + $this->_limit);
  1948. }
  1949. unset($periodNum, $allData);
  1950. return true;
  1951. }
  1952. /**
  1953. * 结算荣衔奖第二步
  1954. * @param int $offset
  1955. * @return bool
  1956. * @throws \yii\db\Exception
  1957. */
  1958. public function calcBonusYCStepTwo(int $offset = 0) {
  1959. // 月结,如果不是月结点,则直接退出
  1960. if (!$this->_isCalcMonth) {
  1961. return true;
  1962. }
  1963. echo sprintf("时间:[%s]荣衔奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1964. $allData = CalcCache::getHasYcBonusUsers($this->_periodNum, $offset, $this->_limit);
  1965. if ($allData) {
  1966. $insertBonusData = [];
  1967. foreach ($allData as $userId) {
  1968. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1969. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  1970. unset($monthPerfData);
  1971. if( !$fxPvStatus ) continue;
  1972. unset($fxPvStatus);
  1973. $ycBonusData = CalcCache::getYCBonusList($userId, $this->_periodNum);
  1974. if( !$ycBonusData ) continue;
  1975. $empBonus = $ycBonusData['empBonus'] ?? 0;
  1976. if( $empBonus <=0 ) continue;
  1977. //总金额限制
  1978. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1979. $empBonus = $this->bonusTotalLimit($empBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1980. if( $empBonus <= 0 ) continue;
  1981. //扣除相应的复消积分和管理费
  1982. $empBonus = Tool::formatPrice($empBonus);
  1983. $deductData = $this->deduct($userId, $empBonus);
  1984. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_YC', $empBonus, $deductData);
  1985. $empLevelId = $userBaseInfo['EMP_LV'];
  1986. $insertBonusData[] = [
  1987. 'ID' => SnowFake::instance()->generateId(),
  1988. 'USER_ID' => $userId,
  1989. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  1990. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  1991. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  1992. 'FROM_USER_ID' => $userId,
  1993. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1994. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  1995. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1996. 'DEEP' => 0,
  1997. 'AMOUNT' => $deductData['surplus'],
  1998. 'ORI_BONUS' => $empBonus,
  1999. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2000. 'MANAGE_TAX' => $deductData['manageTax'],
  2001. 'BONUS_TYPE' => CalcBonusYC::BONUS_TYPE_YC,
  2002. 'PERIOD_NUM' => $this->_periodNum,
  2003. 'CALC_MONTH' => $this->_calcYearMonth,
  2004. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2005. 'CREATED_AT' => Date::nowTime(),
  2006. 'LOGS' => json_encode([
  2007. 'empLevelId' => $empLevelId,
  2008. ])
  2009. // 'FROM_DATA' => json_encode($ycBonusData['fromData']),
  2010. ];
  2011. unset($ycBonusData, $deductData);
  2012. //如果是总监以上拿平级2代
  2013. $empLevel = $this->_empLevelConfig[$empLevelId];
  2014. $userEmpLevelSort = $empLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  2015. // if( $userEmpLevelSort >= EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {}
  2016. $newInsertBonusData = $this->calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $empBonus);
  2017. $insertBonusData = array_merge($insertBonusData, $newInsertBonusData);
  2018. unset($newInsertBonusData);
  2019. unset($userId, $userBaseInfo, $empLevel, $empLevelId, $userEmpLevelSort, $empBonus);
  2020. }
  2021. CalcBonusYC::batchInsert($insertBonusData);
  2022. unset($insertBonusData, $allData);
  2023. $this->calcBonusYCStepTwo($offset + $this->_limit);
  2024. }
  2025. unset($allData);
  2026. return true;
  2027. }
  2028. /**
  2029. * VIP奖
  2030. * @param int $offset
  2031. * @return bool
  2032. * @throws \yii\db\Exception
  2033. */
  2034. public function calcBonusVIP(int $offset = 0)
  2035. {
  2036. // 月结,如果不是月结点,则直接退出
  2037. if (!$this->_isCalcMonth) {
  2038. return true;
  2039. }
  2040. echo sprintf("时间:[%s]VIP奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2041. //从缓存获取分页有本月业绩的会员
  2042. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2043. if ($allData) {
  2044. $insertBonusData = [];
  2045. foreach ($allData as $userId) {
  2046. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2047. //条件一:级别必须为VIP
  2048. $isVip = false;
  2049. if( $userBaseInfo['DEC_LV'] === DeclarationLevel::VIP_LEVEL_ID ) {
  2050. $isVip= true;
  2051. }
  2052. if( $this->_sysConfig['vipBonusGoldDecLevel']['VALUE'] && $userBaseInfo['DEC_LV'] === DeclarationLevel::JIN_ZUAN_LEVEL_ID ) {
  2053. $isVip = true;
  2054. }
  2055. if ( !$isVip ) {
  2056. unset($userId, $userBaseInfo);
  2057. continue;
  2058. }
  2059. //判断其它两个条件,满足则计算月团队奖,不满足则留下大区剩余业绩,清除其它区的剩余业绩
  2060. //获取本月的业绩
  2061. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  2062. // 从缓存中获取会员的上期结余业绩信息
  2063. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2064. // 本期 + 上期结余
  2065. $perfArr = [
  2066. 'VIP_SURPLUS_1L_ZC' => $monthPerfData['VIP_PV_1L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_1L_ZC'],
  2067. 'VIP_SURPLUS_2L_ZC' => $monthPerfData['VIP_PV_2L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_2L_ZC'],
  2068. 'VIP_SURPLUS_3L_ZC' => $monthPerfData['VIP_PV_3L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_3L_ZC'],
  2069. 'VIP_SURPLUS_4L_ZC' => $monthPerfData['VIP_PV_4L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_4L_ZC'],
  2070. 'VIP_SURPLUS_5L_ZC' => $monthPerfData['VIP_PV_5L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_5L_ZC'],
  2071. ];
  2072. //条件二:本月复消金额达到300元
  2073. //fix-2021-03-26修改 条件二:本月个人复消PV300
  2074. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2075. unset($monthPerfData, $pervSurplusPerf);
  2076. if( !$fxPvStatus ) {
  2077. $this->dealWithMaxPerf($userId, $perfArr);
  2078. unset($userBaseInfo, $userId, $perfArr, $fxPvStatus);
  2079. continue;
  2080. }
  2081. //条件三:其它市场业绩
  2082. $isCanOtherDepartPerf = $this->_isCanVipBonusOtherAreaPerf($perfArr);
  2083. if ( !$isCanOtherDepartPerf ) {
  2084. $this->dealWithMaxPerf($userId, $perfArr);
  2085. unset($userBaseInfo, $userId, $perfArr, $isCanOtherDepartPerf);
  2086. continue;
  2087. }
  2088. $oriPerfArr = [
  2089. 'perfArr' => $perfArr,
  2090. 'touchBonus' => 0,
  2091. ];
  2092. // 对碰
  2093. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $this->_sysConfig['vipBonusPercent']['VALUE']/100);
  2094. unset($oriPerfArr, $perfArr);
  2095. $touchPerfArr = [];
  2096. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  2097. $touchPerfArr[$keyR] = $perfR;
  2098. }
  2099. // 对碰完成后把结余的业绩存入本期业绩缓存中
  2100. // CalcCache::nowPeriodPerf($userId, $this->_periodNum, $touchPerfArr);
  2101. //更新数据库
  2102. PerfMonth::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2103. 'USER_ID' => $userId,
  2104. 'CALC_MONTH' => $this->_calcYearMonth,
  2105. ]);
  2106. if ($touchBonusArr['touchBonus'] <= 0) continue;
  2107. $vipBonus = $touchBonusArr['touchBonus'];
  2108. //判断级别上限
  2109. $vipBonus = $this->declarationLevelCap($vipBonus, $userId, $userBaseInfo['DEC_LV']);
  2110. if( $vipBonus <= 0 ) continue;
  2111. $vipBonus = $this->bonusTotalLimit($vipBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  2112. if( $vipBonus <= 0 ) continue;
  2113. //扣除相应的复消积分和管理费
  2114. $deductData = $this->deduct($userId, $vipBonus);
  2115. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_VIP', $vipBonus, $deductData);
  2116. $insertBonusData[] = [
  2117. 'ID' => SnowFake::instance()->generateId(),
  2118. 'USER_ID' => $userId,
  2119. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2120. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2121. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2122. 'FROM_USER_ID' => $userId,
  2123. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2124. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2125. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2126. 'AMOUNT' => $deductData['surplus'],
  2127. 'ORI_BONUS' => $vipBonus,
  2128. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2129. 'MANAGE_TAX' => $deductData['manageTax'],
  2130. 'PERIOD_NUM' => $this->_periodNum,
  2131. 'CALC_YEAR' => $this->_calcYear,
  2132. 'CALC_MONTH' => $this->_calcYearMonth,
  2133. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2134. 'CREATED_AT' => Date::nowTime(),
  2135. 'LOGS' => json_encode($touchPerfArr)
  2136. ];
  2137. unset($userBaseInfo, $userId, $perfArr, $fxAmountStatus, $isCanOtherDepartPerf, $touchBonusArr, $touchPerfArr, $vipBonus, $deductData);
  2138. }
  2139. if( $insertBonusData ) CalcBonusVIP::batchInsert($insertBonusData);
  2140. unset($insertBonusData, $allData);
  2141. $this->calcBonusVIP($offset + $this->_limit);
  2142. }
  2143. unset($allData);
  2144. return true;
  2145. }
  2146. /**
  2147. * 见习达标奖
  2148. * @param int $offset
  2149. * @return bool
  2150. * @throws \yii\db\Exception
  2151. */
  2152. public function calcBonusStandard(int $offset = 0)
  2153. {
  2154. // 月结,如果不是月结点,则直接退出
  2155. if (!$this->_isCalcMonth) {
  2156. return true;
  2157. }
  2158. echo sprintf("时间:[%s]见习达标奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2159. //从缓存获取分页有本月业绩的会员
  2160. $allData = CalcCache::getHasStandardMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2161. if ($allData) {
  2162. $minSaleCondition = $this->_jxStandardMinSaleCondition();
  2163. $insertBonusData = [];
  2164. foreach ($allData as $userId) {
  2165. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2166. //条件一:聘级必须为见习主任
  2167. if( $userBaseInfo['EMP_LV'] != EmployLevel::JX_ZR_LEVEL_ID ) {
  2168. unset($userId, $userBaseInfo);
  2169. continue;
  2170. }
  2171. $standardMonthPerf = CalcCache::nowStandardMonthPerf($userId, $this->_periodNum);
  2172. $totalAmount = $standardMonthPerf['AMOUNT_PCS'] + $standardMonthPerf['AMOUNT_PSS'];
  2173. if( $totalAmount < $minSaleCondition ) {
  2174. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2175. continue;
  2176. }
  2177. $standardBonus = $this->_jxStandardAwardAmount($totalAmount);
  2178. if( $standardBonus <= 0 ) {
  2179. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount, $standardBonus);
  2180. continue;
  2181. }
  2182. CalcCache::standardBonus($userId, $this->_periodNum, $standardBonus);
  2183. $insertBonusData[] = [
  2184. 'ID' => SnowFake::instance()->generateId(),
  2185. 'USER_ID' => $userId,
  2186. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2187. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2188. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2189. 'FROM_USER_ID' => $userId,
  2190. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2191. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2192. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2193. 'AMOUNT' => $standardBonus,
  2194. 'ORI_BONUS' => $standardBonus,
  2195. 'RECONSUME_POINTS' => 0,
  2196. 'MANAGE_TAX' => 0,
  2197. 'PERIOD_NUM' => $this->_periodNum,
  2198. 'CALC_YEAR' => $this->_calcYear,
  2199. 'CALC_MONTH' => $this->_calcYearMonth,
  2200. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2201. 'CREATED_AT' => Date::nowTime(),
  2202. 'LOGS' => json_encode($standardMonthPerf)
  2203. ];
  2204. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2205. }
  2206. if( $insertBonusData ) CalcBonusStandard::batchInsert($insertBonusData);
  2207. unset($insertBonusData, $minSaleCondition, $insertBonusData, $allData);
  2208. $this->calcBonusStandard($offset + $this->_limit);
  2209. }
  2210. unset($allData);
  2211. return true;
  2212. }
  2213. /**
  2214. * === 运算相关的辅助功能开始 ===
  2215. */
  2216. /**
  2217. * 获取上2代的得奖人信息
  2218. * @param $userId
  2219. * @param $validDeep
  2220. * @param $shareBonusOne
  2221. * @return array
  2222. */
  2223. protected function gxLastTwoBonusData($userId, $validDeep, $shareBonusOne) {
  2224. $bonusUserList = CalcCache::getShareBonusOneRelation($userId, $this->_periodNum, $validDeep);
  2225. if( !$bonusUserList ) {
  2226. unset($bonusUserList);
  2227. return [];
  2228. }
  2229. $bonusUserData = [];
  2230. $bonusUserTotalPerf = 0;
  2231. $bonusUserRealList = [];
  2232. foreach ($bonusUserList as $bonusUserId) {
  2233. //查看该用户的个人业绩和团队新增业绩总和
  2234. $nowPeriodData = CalcCache::nowPeriodPerf($bonusUserId, $this->_periodNum);
  2235. //本期新增业绩
  2236. $bonusUserPerf = $nowPeriodData['PV_PCS'] + $nowPeriodData['PV_PSS'];
  2237. if ($bonusUserPerf <= 0) {
  2238. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2239. continue;
  2240. }
  2241. $bonusUserTotalPerf += $bonusUserPerf;
  2242. $bonusUserRealList[] = [
  2243. 'bonusUserId' => $bonusUserId,
  2244. 'bonusUserPerf' => $bonusUserPerf,
  2245. ];
  2246. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2247. }
  2248. unset($bonusUserList);
  2249. foreach ($bonusUserRealList as $everyData) {
  2250. $bonusUserData[] = [
  2251. 'bonusUid' => $everyData['bonusUserId'],
  2252. 'bonus' => $shareBonusOne * $everyData['bonusUserPerf'] / $bonusUserTotalPerf,
  2253. 'validMinPv' => false
  2254. ];
  2255. unset($everyData);
  2256. }
  2257. unset($bonusUserRealList);
  2258. return $bonusUserData;
  2259. }
  2260. /**
  2261. * 获取达标奖的金额
  2262. * @param $amount
  2263. * @return int|mixed
  2264. */
  2265. private function _jxStandardAwardAmount($amount) {
  2266. // $jxStandardConfig = Json::decode($this->_sysConfig['jxStandard']['VALUE']);
  2267. // $awardAmount = 0;
  2268. // foreach ($jxStandardConfig as $everyData) {
  2269. // if( $amount >= $everyData['salesCondition'] ) {
  2270. // $awardAmount = $everyData['awardAmount'];
  2271. // }
  2272. // unset($everyData);
  2273. // }
  2274. //
  2275. // return $awardAmount;
  2276. $realTimes = floor($amount/self::JX_STANDARD_BASE_AMOUNT);
  2277. $calcTimes = min($realTimes, self::JX_STANDARD_MAX_TIMES);
  2278. $awardAmount = $calcTimes * self::JX_STANDARD_BASE_AMOUNT * self::JX_STANDARD_BONUS_PERCENT / 100;
  2279. unset($amount, $realTimes, $calcTimes);
  2280. return $awardAmount;
  2281. }
  2282. /**
  2283. * 获取达标奖的最小条件
  2284. * @return int|mixed
  2285. */
  2286. private function _jxStandardMinSaleCondition() {
  2287. return self::JX_STANDARD_BASE_AMOUNT;
  2288. }
  2289. /**
  2290. * 处理只留最大业绩的逻辑
  2291. * @param $userId
  2292. * @param $perfArr
  2293. */
  2294. private function dealWithMaxPerf($userId, $perfArr) {
  2295. $maxPerf = max($perfArr);
  2296. $hitMaxPerf = false;
  2297. foreach ($perfArr as $key=>$everyPerf) {
  2298. if( !$hitMaxPerf && $everyPerf == $maxPerf ) {
  2299. $hitMaxPerf = true;
  2300. }else {
  2301. $perfArr[$key] = 0;
  2302. }
  2303. unset($key, $everyPerf);
  2304. }
  2305. //更新数据库
  2306. PerfMonth::updateAll($perfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2307. 'USER_ID' => $userId,
  2308. 'CALC_MONTH' => $this->_calcYearMonth
  2309. ]);
  2310. unset($userId, $maxPerf, $hitMaxPerf, $perfArr);
  2311. }
  2312. /**
  2313. * VIP奖其它市场的报单业绩是否能满足
  2314. * @param $perfArr
  2315. * @return bool
  2316. */
  2317. private function _isCanVipBonusOtherAreaPerf($perfArr) {
  2318. $maxAreaPerf = max($perfArr);
  2319. $totalAreaPerf = array_sum($perfArr);
  2320. $diffAreaPerf = strval($totalAreaPerf - $maxAreaPerf);
  2321. if( $diffAreaPerf >= $this->_sysConfig['vipBonusOtherDepartPvCondition']['VALUE'] ) {
  2322. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2323. return true;
  2324. }else {
  2325. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2326. return false;
  2327. }
  2328. }
  2329. /**
  2330. * 给父级计算的荣衔奖
  2331. * @param $userId
  2332. * @param $userBaseInfo
  2333. * @param $userEmpLevelSort
  2334. * @param $qyBonus
  2335. * @return array
  2336. */
  2337. public function calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus) {
  2338. if( $userEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return [];
  2339. $validDeep = 0;
  2340. $bonusDataList = [];
  2341. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $userEmpLevelSort, &$validDeep, &$bonusDataList) {
  2342. $bonusUserId = $parent['PARENT_UID'];
  2343. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2344. $bonusUserEmpLevel = $this->_empLevelConfig[$bonusUserInfo['EMP_LV']];
  2345. $bonusUserEmpLevelSort = $bonusUserEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  2346. unset($bonusUserInfo, $bonusUserEmpLevel);
  2347. //必须是有聘级的用户
  2348. if( $bonusUserEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return self::LOOP_CONTINUE;
  2349. //超过级别就停止
  2350. if( $bonusUserEmpLevelSort > $userEmpLevelSort) return self::LOOP_FINISH;
  2351. //级别小于就跳过
  2352. if( $bonusUserEmpLevelSort < $userEmpLevelSort ) return self::LOOP_CONTINUE;
  2353. $validDeep++;
  2354. array_push($bonusDataList, [
  2355. 'bonusUserId' => $bonusUserId,
  2356. 'bonusType' => $validDeep,
  2357. ]);
  2358. if( $validDeep >= 2 ) return self::LOOP_FINISH;
  2359. unset($bonusUserId, $bonusUserEmpLevelSort);
  2360. });
  2361. $insertBonusData = [];
  2362. foreach ($bonusDataList as $bonusUserData) {
  2363. $bonusUserId = $bonusUserData['bonusUserId'];
  2364. $bonusMonthPerfData = CalcCache::nowMonthPerf($bonusUserId, $this->_periodNum);
  2365. $fxPvStatus = $bonusMonthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2366. unset($bonusMonthPerfData);
  2367. if( !$fxPvStatus ) continue;
  2368. unset($fxPvStatus);
  2369. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2370. $bonusEmpLevelId = $bonusUserInfo['EMP_LV'];
  2371. $newEmpBonus = Tool::formatPrice($qyBonus * $this->_sysConfig['sameEmpLevelPercent']['VALUE'] / 100);
  2372. //总金额限制
  2373. $newEmpBonus = $this->bonusTotalLimit($newEmpBonus, $bonusUserId, $bonusUserInfo['REC_NUM'], $bonusUserInfo['ZC_AMOUNT']);
  2374. if( $newEmpBonus <= 0 ) continue;
  2375. //扣除相应的复消积分和管理费
  2376. $newEmpBonus = Tool::formatPrice($newEmpBonus);
  2377. $deductData = $this->deduct($bonusUserId, $newEmpBonus);
  2378. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_YC_EXTRA', $newEmpBonus, $deductData);
  2379. // 把育成津贴追加到育成津贴结算表,以便奖金追溯育成津贴用
  2380. $insertBonusData[] = [
  2381. 'ID' => SnowFake::instance()->generateId(),
  2382. 'USER_ID' => $bonusUserId,
  2383. 'LAST_DEC_LV' => $bonusUserInfo['DEC_LV'],
  2384. 'LAST_EMP_LV' => $bonusUserInfo['EMP_LV'],
  2385. 'LAST_STATUS' => $bonusUserInfo['STATUS'],
  2386. 'FROM_USER_ID' => $userId,
  2387. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2388. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2389. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2390. 'DEEP' => 1,
  2391. 'AMOUNT' => $deductData['surplus'],
  2392. 'ORI_BONUS' => $newEmpBonus,
  2393. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2394. 'MANAGE_TAX' => $deductData['manageTax'],
  2395. 'BONUS_TYPE' => $bonusUserData['bonusType'],
  2396. 'PERIOD_NUM' => $this->_periodNum,
  2397. 'CALC_MONTH' => $this->_calcYearMonth,
  2398. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2399. 'CREATED_AT' => Date::nowTime(),
  2400. 'LOGS' => json_encode([
  2401. 'empLevelId' => $bonusEmpLevelId,
  2402. 'qyBonus' => $qyBonus,
  2403. 'sameEmpLevelPercent' => $this->_sysConfig['sameEmpLevelPercent']['VALUE'],
  2404. ]),
  2405. ];
  2406. unset($newEmpBonus, $bonusEmpLevelId, $bonusUserId, $bonusUserInfo, $deductData);
  2407. }
  2408. unset($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus, $bonusDataList);
  2409. return $insertBonusData;
  2410. }
  2411. /**
  2412. * 对碰
  2413. * @param array $oriPerfArr
  2414. * @param array $perfArr
  2415. * @param $percent
  2416. * @param $loopTimes
  2417. * @return array
  2418. */
  2419. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  2420. $resultArr = $oriPerfArr;
  2421. foreach ($perfArr as $keyT => $perfT) {
  2422. if ($perfT <= 0) {
  2423. unset($perfArr[$keyT]);
  2424. }
  2425. }
  2426. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2427. arsort($perfArr, SORT_NUMERIC);
  2428. $onePerf = null;
  2429. $oneKey = null;
  2430. // $touchSurplusAmount = 0;
  2431. $touchAmount = 0;
  2432. // 前两个进行对碰
  2433. foreach ($perfArr as $key => $perf) {
  2434. if ($onePerf === null) {
  2435. $oneKey = $key;
  2436. $onePerf = $perf;
  2437. } else {
  2438. $touchSurplusAmount = $perf - $onePerf;
  2439. if ($touchSurplusAmount > 0) {
  2440. unset($perfArr[$oneKey]);
  2441. $resultArr['perfArr'][$oneKey] = 0;
  2442. $perfArr[$key] = $touchSurplusAmount;
  2443. $touchAmount = $onePerf;
  2444. } elseif ($touchSurplusAmount < 0) {
  2445. unset($perfArr[$key]);
  2446. $resultArr['perfArr'][$key] = 0;
  2447. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2448. $touchAmount = $perf;
  2449. } else {
  2450. unset($perfArr[$oneKey], $perfArr[$key]);
  2451. $resultArr['perfArr'][$oneKey] = 0;
  2452. $resultArr['perfArr'][$key] = 0;
  2453. $touchAmount = $perf;
  2454. }
  2455. break;
  2456. }
  2457. }
  2458. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2459. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2460. }*/
  2461. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  2462. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2463. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2464. // } else {
  2465. // $resultArr['touchBonus'] += $touchBonus;
  2466. // }
  2467. $resultArr['touchBonus'] += $touchBonus;
  2468. foreach ($perfArr as $keyR => $perfR) {
  2469. $resultArr['perfArr'][$keyR] = $perfR;
  2470. }
  2471. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  2472. }
  2473. return $resultArr;
  2474. }
  2475. /**
  2476. * 对碰
  2477. * @param array $oriPerfArr
  2478. * @param array $perfArr
  2479. * @param $decLevel
  2480. * @param $decLevelConfig
  2481. * @param $loopTimes
  2482. * @return array
  2483. */
  2484. public function touchPerfBefore(array $oriPerfArr, array $perfArr, $decLevel, $decLevelConfig, $loopTimes=1) {
  2485. $resultArr = $oriPerfArr;
  2486. foreach ($perfArr as $keyT => $perfT) {
  2487. if ($perfT <= 0) {
  2488. unset($perfArr[$keyT]);
  2489. }
  2490. }
  2491. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2492. $nowDecLevelConfig = $decLevelConfig[$decLevel];
  2493. arsort($perfArr, SORT_NUMERIC);
  2494. $onePerf = null;
  2495. $oneKey = null;
  2496. // $touchSurplusAmount = 0;
  2497. $touchAmount = 0;
  2498. // 前两个进行对碰
  2499. foreach ($perfArr as $key => $perf) {
  2500. if ($onePerf === null) {
  2501. $oneKey = $key;
  2502. $onePerf = $perf;
  2503. } else {
  2504. $touchSurplusAmount = $perf - $onePerf;
  2505. if ($touchSurplusAmount > 0) {
  2506. unset($perfArr[$oneKey]);
  2507. $resultArr['perfArr'][$oneKey] = 0;
  2508. $perfArr[$key] = $touchSurplusAmount;
  2509. $touchAmount = $onePerf;
  2510. } elseif ($touchSurplusAmount < 0) {
  2511. unset($perfArr[$key]);
  2512. $resultArr['perfArr'][$key] = 0;
  2513. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2514. $touchAmount = $perf;
  2515. } else {
  2516. unset($perfArr[$oneKey], $perfArr[$key]);
  2517. $resultArr['perfArr'][$oneKey] = 0;
  2518. $resultArr['perfArr'][$key] = 0;
  2519. $touchAmount = $perf;
  2520. }
  2521. break;
  2522. }
  2523. }
  2524. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2525. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2526. }*/
  2527. $touchBonus = Tool::formatPrice($touchAmount * 2 / 100);
  2528. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2529. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2530. // } else {
  2531. // $resultArr['touchBonus'] += $touchBonus;
  2532. // }
  2533. $resultArr['touchBonus'] += $touchBonus;
  2534. foreach ($perfArr as $keyR => $perfR) {
  2535. $resultArr['perfArr'][$keyR] = $perfR;
  2536. }
  2537. return $this->touchPerfBefore($resultArr, $perfArr, $decLevel, $decLevelConfig, $loopTimes+1);
  2538. }
  2539. return $resultArr;
  2540. }
  2541. /**
  2542. * 循环父级并执行回调函数
  2543. * @param $userId
  2544. * @param callable $callbackFunc
  2545. * @param int $offset
  2546. * @return bool
  2547. */
  2548. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2549. $allParents = Cache::getAllNetworkParents($userId);
  2550. $allData = array_slice($allParents, $offset, $this->_limit);
  2551. unset($allParents);
  2552. if ($allData) {
  2553. foreach ($allData as $data) {
  2554. $funcResult = $callbackFunc($data);
  2555. if ($funcResult === self::LOOP_FINISH) {
  2556. return true;
  2557. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2558. continue;
  2559. }
  2560. unset($data, $funcResult);
  2561. }
  2562. unset($allData);
  2563. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2564. }
  2565. return true;
  2566. }
  2567. /**
  2568. * 循环推荐网络的父级
  2569. * @param $userId
  2570. * @param callable $callbackFunc
  2571. * @param int $offset
  2572. * @return bool
  2573. */
  2574. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2575. $allParents = Cache::getAllRelationParents($userId);
  2576. $allData = array_slice($allParents, $offset, $this->_limit);
  2577. unset($allParents);
  2578. if ($allData) {
  2579. foreach ($allData as $data) {
  2580. $funcResult = $callbackFunc($data);
  2581. if ($funcResult === self::LOOP_FINISH) {
  2582. return true;
  2583. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2584. continue;
  2585. }
  2586. unset($data, $funcResult);
  2587. }
  2588. unset($allData);
  2589. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2590. }
  2591. return true;
  2592. }
  2593. /**
  2594. * 共享奖,判断当期此用户,是否有原报单团队奖金
  2595. */
  2596. public function checkHasOriBonusQyBd($userId) {
  2597. //判断$parent是否有首单团队奖
  2598. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  2599. if( isset($bonus['ORI_BONUS_QY_BD']) && $bonus['ORI_BONUS_QY_BD'] > 0 ) {
  2600. return true;
  2601. }
  2602. return false;
  2603. }
  2604. /**
  2605. * 判断除大市场外 其它市场的报单业绩
  2606. * @param $userId
  2607. * @return bool
  2608. * @throws \yii\db\Exception
  2609. */
  2610. public function checkSmallMarketPerf($userId) {
  2611. // 从缓存中获取会员的上期结余业绩信息
  2612. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2613. //判断出大市场
  2614. $areaNums = [1, 2, 3];
  2615. $maxArea = 0;
  2616. $maxSurplusPerf = 0;
  2617. foreach ($areaNums as $area) {
  2618. $areaKey = sprintf('SURPLUS_%dL', $area);
  2619. if ( isset($pervSurplusPerf[$areaKey]) && $pervSurplusPerf[$areaKey] > $maxSurplusPerf ) {
  2620. $maxArea = $area;
  2621. $maxSurplusPerf = $pervSurplusPerf[$areaKey];
  2622. }
  2623. unset($area, $areaKey);
  2624. }
  2625. unset($pervSurplusPerf, $maxSurplusPerf);
  2626. $otherAreaZcPv = 0;
  2627. $nowPeriodPerf = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  2628. foreach ($areaNums as $area) {
  2629. if( $maxArea > 0 && $area === $maxArea ) continue;
  2630. $areaKey = sprintf('PV_%dL_ZC', $area);
  2631. if ( isset($nowPeriodPerf[$areaKey]) && $nowPeriodPerf[$areaKey] > 0 ) {
  2632. $otherAreaZcPv += $nowPeriodPerf[$areaKey];
  2633. }
  2634. unset($area, $areaKey);
  2635. }
  2636. unset($nowPeriodPerf, $areaNums, $maxArea);
  2637. return $otherAreaZcPv >= self::MIN_BD_PV;
  2638. }
  2639. /**
  2640. * 获取有最小报单PV的用效父级
  2641. * @param $userId
  2642. * @return string
  2643. */
  2644. public function getMinBdPvRelationParent($userId) {
  2645. $validParentId = '';
  2646. $this->loopRelationParentDo($userId, function ($parent) use(&$validParentId) {
  2647. if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2648. $validParentId = $parent['PARENT_UID'];
  2649. unset($parent);
  2650. return self::LOOP_FINISH;
  2651. }
  2652. unset($parent);
  2653. });
  2654. return$validParentId;
  2655. }
  2656. /**
  2657. * 获取有最小报单PV的用效父级 20220407修改成是否此期此用户有原报单团队奖金
  2658. * @param $userId
  2659. * @return string
  2660. */
  2661. public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2662. $validParentId = '';
  2663. $validDeep = 1;
  2664. $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2665. if ( $this->checkHasOriBonusQyBd($parent['PARENT_UID']) ) {
  2666. $validParentId = $parent['PARENT_UID'];
  2667. unset($parent);
  2668. return self::LOOP_FINISH;
  2669. }
  2670. // 只找5层
  2671. if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2672. unset($parent);
  2673. return self::LOOP_FINISH;
  2674. }
  2675. $validDeep += 1;
  2676. unset($parent);
  2677. });
  2678. return$validParentId;
  2679. }
  2680. // public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2681. // $validParentId = '';
  2682. // $validDeep = 1;
  2683. // $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2684. // if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2685. // $validParentId = $parent['PARENT_UID'];
  2686. // unset($parent);
  2687. // return self::LOOP_FINISH;
  2688. // }
  2689. // // 只找5层
  2690. // if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2691. // unset($parent);
  2692. // return self::LOOP_FINISH;
  2693. // }
  2694. // $validDeep += 1;
  2695. // unset($parent);
  2696. // });
  2697. // return$validParentId;
  2698. // }
  2699. /**
  2700. * 按级别的收入上限
  2701. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  2702. * @param $bonus
  2703. * @param $userId
  2704. * @param $declarationLevel
  2705. * @return float
  2706. */
  2707. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2708. $decLevelConfig = $this->_decLevelConfig;
  2709. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2710. unset($decLevelConfig);
  2711. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  2712. if( $bonus <= $maxGetBonus) {
  2713. return $bonus;
  2714. }else {
  2715. return $maxGetBonus;
  2716. }
  2717. }
  2718. // public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2719. // $decLevelConfig = $this->_decLevelConfig;
  2720. // $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2721. // unset($decLevelConfig);
  2722. // //本月往期奖金 这里执行速度可能慢一些,但需求如此没有办法
  2723. // $lastData = CalcCache::lastPeriodMonthCalcBonus($userId, $this->_periodNum, $this->_calcMonth);
  2724. // $lastTotal = $lastData['ORI_BONUS_QY_SUM'] + $lastData['ORI_BONUS_VIP_SUM'];
  2725. // // 从缓存中获取用户的奖金
  2726. // $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2727. // $thisTotal = $bonusData['ORI_BONUS_QY'] + $bonusData['ORI_BONUS_VIP'];
  2728. // $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'] * $this->_calcMonthPeriodNumCount - $lastTotal - $thisTotal;
  2729. // unset($lastData, $lastTotal, $bonusData, $thisTotal);
  2730. // if( $maxGetBonus <= 0 ) return 0.00;
  2731. // if( $bonus <= $maxGetBonus) {
  2732. // return $bonus;
  2733. // }else {
  2734. // return $maxGetBonus;
  2735. // }
  2736. // }
  2737. /**
  2738. * 总奖金限制
  2739. * @param $bonus
  2740. * @param $userId
  2741. * @param $recNum
  2742. * @param $decAmount
  2743. * @return float
  2744. */
  2745. public function bonusTotalLimit($bonus, $userId, $recNum, $decAmount) {
  2746. //@todo 临时不做总奖金限制
  2747. return $bonus;
  2748. if( $recNum >= 3 ) return $bonus;
  2749. $limitAmount = 0;
  2750. switch ($recNum) {
  2751. case 0:
  2752. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalZeroLimit']['VALUE'];
  2753. break;
  2754. case 1:
  2755. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalOneLimit']['VALUE'];
  2756. break;
  2757. case 2:
  2758. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalTwoLimit']['VALUE'];
  2759. break;
  2760. default:
  2761. break;
  2762. }
  2763. // 从缓存中获取用户的奖金
  2764. $lastUserBonus = CalcCache::LastUserBonus($userId, $this->_periodNum);//往期的总金额
  2765. $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2766. $bonusTotal = $bonusData['BONUS_TOTAL'] + $lastUserBonus['BONUS_TOTAL'];
  2767. unset($lastUserBonus, $userId, $recNum, $decAmount, $bonusData);
  2768. if( $bonusTotal >= $limitAmount ) return 0.00;
  2769. if( $bonusTotal + $bonus <= $limitAmount) {
  2770. return $bonus;
  2771. }else {
  2772. return $limitAmount - $bonusTotal;
  2773. }
  2774. }
  2775. /**
  2776. * 扣除复消积分和管理费
  2777. * @param $userId
  2778. * @param $bonus
  2779. * @return array
  2780. * @throws \yii\db\Exception
  2781. */
  2782. public function deductNoReconsumePoints($userId, $bonus): array
  2783. {
  2784. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2785. $surplus = $bonus - $manageTax;
  2786. return [
  2787. 'reConsumePoints' => 0.00,
  2788. 'manageTax' => Tool::formatPrice($manageTax),
  2789. 'surplus' => Tool::formatPrice($surplus),
  2790. ];
  2791. }
  2792. /**
  2793. * 扣除复消积分和管理费
  2794. * @param $userId
  2795. * @param $bonus
  2796. * @return array
  2797. * @throws \yii\db\Exception
  2798. */
  2799. public function deduct($userId, $bonus) {
  2800. //判断是否达到了本月扣除复消的上限
  2801. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  2802. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  2803. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $cacheData['RECONSUME_POINTS_SUM'];
  2804. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  2805. unset($cacheData, $bonusCache);
  2806. $reConsumePoints = 0;
  2807. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  2808. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  2809. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  2810. }
  2811. unset($reConsumePointsTotal, $reConsumePointsCap);
  2812. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2813. $surplus = $bonus - $reConsumePoints - $manageTax;
  2814. return [
  2815. 'reConsumePoints' => Tool::formatPrice($reConsumePoints),
  2816. 'manageTax' => Tool::formatPrice($manageTax),
  2817. 'surplus' => Tool::formatPrice($surplus),
  2818. ];
  2819. }
  2820. /**
  2821. * 更新百分比并发送
  2822. * @param $percent
  2823. */
  2824. private function _updatePercent($percent) {
  2825. // 把数据写入数据库中
  2826. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  2827. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  2828. }
  2829. /**
  2830. * 把往期有月奖金的会员加到本期有奖会员缓存列表中
  2831. * @param int $offset
  2832. * @return bool
  2833. */
  2834. public function loopMonthBonusUserFromDbToCache($offset = 0) {
  2835. if (!$this->_isCalcMonth) {
  2836. return true;
  2837. }
  2838. $allData = CalcMonthBonusUser::findUseDbCalc()->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->asArray()->all();
  2839. if ( $allData ) {
  2840. foreach ($allData as $everyData) {
  2841. CalcCache::addHasBonusUsers($everyData['USER_ID'], $this->_periodNum);
  2842. unset($everyData);
  2843. }
  2844. unset($allData);
  2845. $this->loopMonthBonusUserFromDbToCache($offset + $this->_limit);
  2846. }
  2847. unset($allData);
  2848. return true;
  2849. }
  2850. /**
  2851. * 把缓存中的月奖用户信息存到数据库
  2852. * @param int $offset
  2853. * @return bool
  2854. * @throws \yii\db\Exception
  2855. */
  2856. public function loopMonthBonusUserToDb($offset = 0) {
  2857. echo sprintf("时间:[%s]月奖会员入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2858. $allData = CalcCache::getHasMonthBonusUsers($this->_periodNum, $offset, $this->_limit);
  2859. if($allData) {
  2860. $insertDataBonusUser = [];
  2861. foreach($allData as $userId){
  2862. $insertDataBonusUser[] = [
  2863. 'ID' => SnowFake::instance()->generateId(),
  2864. 'USER_ID' => $userId,
  2865. 'CALC_MONTH' => $this->_calcYearMonth,
  2866. 'PERIOD_NUM' => $this->_periodNum,
  2867. 'CREATED_AT' => Date::nowTime()
  2868. ];
  2869. unset($userId);
  2870. }
  2871. if( $insertDataBonusUser ) CalcMonthBonusUser::batchInsert($insertDataBonusUser);
  2872. unset($insertDataBonus, $allData);
  2873. return $this->loopMonthBonusUserToDb($offset + $this->_limit);
  2874. }
  2875. unset($allData);
  2876. return true;
  2877. }
  2878. //奖金入库
  2879. /**
  2880. * 循环奖服务奖金会员,并入库
  2881. * @param int $offset
  2882. * @return bool
  2883. * @throws \yii\db\Exception
  2884. */
  2885. public function loopBonusUsers($offset = 0) {
  2886. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2887. // 从缓存列表里面从底层往上倒序获取会员
  2888. // 这里有问题,因为蓝星奖,是存储过程计算的,并没有加入缓存,这里需要修改.
  2889. // 给用户发送兑换积分,也需要进行调整查看。
  2890. // CalcCache::addHasBonusUsers($everyData['USER_ID'], $this->_periodNum);
  2891. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  2892. if($allData){
  2893. $insertDataBonus = [];
  2894. foreach($allData as $userId){
  2895. $tempBonusData = $this->bonusData($userId);
  2896. if(!empty($tempBonusData['result'])){
  2897. $insertDataBonus[] = $tempBonusData['result'];
  2898. }
  2899. unset($userId, $tempBonusData);
  2900. }
  2901. CalcBonus::batchInsert($insertDataBonus);
  2902. unset($insertDataBonus, $allData);
  2903. return $this->loopBonusUsers($offset + $this->_limit);
  2904. }
  2905. return true;
  2906. }
  2907. // 奖金入库完成,将各个奖金计算流水会员聘级,更新成蓝星奖当时计算的聘级
  2908. public function loopCalcBlueEmpLv($offset = 0) {
  2909. if( !$this->_isCalcMonth ) {
  2910. // 不是结算月,则不需要进行聘级调整
  2911. return false;
  2912. }
  2913. // 从缓存获取分页有收入的会员信息
  2914. $allData = CalcBonusBS::findUseDbCalc()
  2915. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  2916. ->groupBy('USER_ID')
  2917. ->offset($offset)
  2918. ->limit($this->_limit)
  2919. ->asArray()
  2920. ->all();
  2921. if ($allData) {
  2922. foreach ($allData as $data) {
  2923. $nowBsEmpLv = $data['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
  2924. // 修改AR_CALC_BONUS的LAST_EMP_LV聘级,修改奖金流水的聘级AR_FLOW_BONUS表的LAST_EMP_LV
  2925. // 期结算结果
  2926. CalcBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2927. [
  2928. ':USER_ID' => $data['USER_ID'],
  2929. ':PERIOD_NUM' => $this->_periodNum
  2930. ]
  2931. );
  2932. // 奖金流水
  2933. FlowBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2934. [
  2935. ':USER_ID' => $data['USER_ID'],
  2936. ':PERIOD_NUM' => $this->_periodNum
  2937. ]
  2938. );
  2939. // 共享奖流水
  2940. CalcBonusGX::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2941. [
  2942. ':USER_ID' => $data['USER_ID'],
  2943. ':PERIOD_NUM' => $this->_periodNum
  2944. ]
  2945. );
  2946. // 推广奖流水
  2947. CalcBonusTG::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2948. [
  2949. ':USER_ID' => $data['USER_ID'],
  2950. ':PERIOD_NUM' => $this->_periodNum
  2951. ]
  2952. );
  2953. // 团队奖流水
  2954. CalcBonusQY::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2955. [
  2956. ':USER_ID' => $data['USER_ID'],
  2957. ':PERIOD_NUM' => $this->_periodNum
  2958. ]
  2959. );
  2960. // 服务奖流水
  2961. CalcBonusBD::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2962. [
  2963. ':USER_ID' => $data['USER_ID'],
  2964. ':PERIOD_NUM' => $this->_periodNum
  2965. ]
  2966. );
  2967. // 更新form的聘级
  2968. CalcBonusGX::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2969. [
  2970. ':FROM_USER_ID' => $data['USER_ID'],
  2971. ':PERIOD_NUM' => $this->_periodNum
  2972. ]
  2973. );
  2974. CalcBonusTG::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2975. [
  2976. ':FROM_USER_ID' => $data['USER_ID'],
  2977. ':PERIOD_NUM' => $this->_periodNum
  2978. ]
  2979. );
  2980. CalcBonusBD::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2981. [
  2982. ':FROM_USER_ID' => $data['USER_ID'],
  2983. ':PERIOD_NUM' => $this->_periodNum
  2984. ]
  2985. );
  2986. }
  2987. unset($allData);
  2988. return $this->loopCalcBlueEmpLv($offset + $this->_limit);
  2989. }
  2990. unset($allData);
  2991. return true;
  2992. }
  2993. /**
  2994. * 奖金
  2995. * @param $userId
  2996. * @return array
  2997. * @throws \yii\db\Exception
  2998. */
  2999. public function bonusData($userId) {
  3000. // 从缓存中获取用户的奖金
  3001. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  3002. $standardBonus = CalcCache::standardBonus($userId, $this->_periodNum);
  3003. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  3004. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  3005. $travelBonus = CalcCache::zoneBonus($userId, $this->_periodNum, CalcCache::REDIS_KEY_PREFIX_TRAVEL_BONUS);
  3006. $carBonus = CalcCache::zoneBonus($userId, $this->_periodNum, CalcCache::REDIS_KEY_PREFIX_CAR_BONUS);
  3007. $houseBonus = CalcCache::zoneBonus($userId, $this->_periodNum, CalcCache::REDIS_KEY_PREFIX_HOUSE_BONUS);
  3008. $empLv = $baseInfo['EMP_LV'];
  3009. // if($this->_isCalcMonth){
  3010. // $empLv = $this->nowMonthPerfData($userId)['LAST_EMP_LV'];
  3011. // }
  3012. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  3013. // //没有共享和管理奖 以前的管理奖和共享奖逻辑
  3014. // $bonusReal = $bonus['BONUS_BD'] + $bonus['BONUS_TG'] + $bonus['BONUS_XF'] + $bonus['BONUS_YJ'] + $bonus['BONUS_QY'] + $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'] + $bonus['BONUS_VIP'] + $standardBonus;
  3015. // $realBonusGx = 0;
  3016. // $realBonusGl = 0;
  3017. // if( $this->_isCalcMonth ) {
  3018. // //查看是否复消300
  3019. // $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  3020. // $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  3021. // if ( $fxPvStatus ) {//加上本期和往期的共享和管理奖
  3022. // $monthSumData = CalcBonus::findUseSlaves()->select('SUM(BONUS_GX) AS BONUS_GX_SUM, SUM(BONUS_GL) AS BONUS_GL_SUM')->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth])->asArray()->one();
  3023. // $bonusGxSum = $monthSumData['BONUS_GX_SUM'] ?? 0;
  3024. // $bonusGlSum = $monthSumData['BONUS_GL_SUM'] ?? 0;
  3025. // $realBonusGx += $bonusGxSum + $bonus['BONUS_GX'];
  3026. // $realBonusGl += $bonusGlSum + $bonus['BONUS_GL'];
  3027. // $bonusReal += $realBonusGx + $realBonusGl;
  3028. // unset($monthSumData, $bonusGxSum, $bonusGlSum);
  3029. // }
  3030. // }
  3031. //没有共享和管理奖
  3032. $bonusReal = $bonus['BONUS_BD'] + $bonus['BONUS_TG'] + $bonus['BONUS_XF'] + $bonus['BONUS_YJ'] + $bonus['BONUS_QY'] + $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'] + $bonus['BONUS_VIP'] + $standardBonus + $bonus['BONUS_BS_MNT'] + $bonus['BONUS_BS_ABBR'];
  3033. $realBonusGx = 0;
  3034. $realBonusGl = 0;
  3035. $realBonusBs = 0; // 蓝星管理奖. BlueStar
  3036. $blueStartOriBonus = 0;
  3037. $blueStartManageTax = 0;
  3038. $exchangePoints = 0; // 蓝星奖管理奖. 产生的兑换积分
  3039. $realBonusBsMnt = 0; // 蓝星管理奖——实发奖金
  3040. $blueStartOriBonusMnt = 0; // 蓝星管理奖——原奖金
  3041. $blueStartManageTaxMnt = 0; // 蓝星管理奖——管理费
  3042. $realBonusBsAbbr = 0; // 蓝星业绩奖——实发奖金
  3043. $blueStartOriBonusAbbr = 0; // 蓝星业绩奖——原奖金
  3044. $blueStartManageTaxAbbr = 0; // 蓝星业绩奖——管理费
  3045. if( $this->_isCalcMonth ) {
  3046. // 逻辑改成,用户此月累计消费总额大于等于配置,取消订单类型的配置
  3047. $userMonthTotal = PerfMonth::find()
  3048. ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth])
  3049. ->asArray()
  3050. ->one();
  3051. // 个人月消费PV大于配置值,才会计算发放蓝星奖
  3052. $fxPvStatus = false;
  3053. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  3054. $fxPvStatus = true;
  3055. }
  3056. // BONUS_REAL 字段是发到用户的真实奖金
  3057. if ( $fxPvStatus ) {
  3058. // 管理奖改成了蓝星奖,但是对于用户来说依旧叫管理奖.字段改成bs.
  3059. // 因为管理奖(即蓝星奖,是用存储过程计算的,则此处直接取AR_CALC_BONUS_BS表中计算的结果数据使用,
  3060. // 并将管理奖结果同步到当前结算月的CalcBonus中)
  3061. // 由于单独奖金流水记录每次都插入,所以倒叙找最新的数据.
  3062. $userBS = CalcBonusBS::find()
  3063. ->where(
  3064. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  3065. [
  3066. ':PERIOD_NUM' => $this->_periodNum,
  3067. ':USER_ID' => $userId
  3068. ]
  3069. )
  3070. ->select('AMOUNT,ORI_BONUS,MANAGE_TAX,LEVEL_ID,PRODUCT_POINT,AMOUNT_MNT,ORI_BONUS_MNT,MANAGE_TAX_MNT,AMOUNT_ABBR,ORI_BONUS_ABBR,MANAGE_TAX_ABBR')
  3071. ->limit(1)
  3072. ->orderBy('CREATED_AT DESC')
  3073. ->asArray()
  3074. ->all();
  3075. $userBS = is_array($userBS) ? reset($userBS) : [];
  3076. $blueStartAmount = isset($userBS['AMOUNT']) && !empty($userBS['AMOUNT']) ? $userBS['AMOUNT'] : 0; // 奖金
  3077. $blueStartOriBonus = isset($userBS['ORI_BONUS']) && !empty($userBS['ORI_BONUS']) ? $userBS['ORI_BONUS'] : 0; // 原奖金
  3078. $blueStartManageTax = isset($userBS['MANAGE_TAX']) && !empty($userBS['MANAGE_TAX']) ? $userBS['MANAGE_TAX'] : 0; // 管理费
  3079. $realBonusBsMnt = $userBS['AMOUNT_MNT'] ?? 0; // 蓝星管理奖. 实发奖金
  3080. $blueStartOriBonusMnt = $userBS['ORI_BONUS_MNT'] ?? 0; // 蓝星管理奖. 原奖金
  3081. $blueStartManageTaxMnt = $userBS['MANAGE_TAX_MNT'] ?? 0; // 蓝星管理奖. 管理费
  3082. $realBonusBsAbbr = $userBS['AMOUNT_ABBR'] ?? 0; // 蓝星业绩奖. 奖金
  3083. $blueStartOriBonusAbbr = $userBS['ORI_BONUS_ABBR'] ?? 0; // 蓝星业绩奖. 原奖金
  3084. $blueStartManageTaxAbbr = $userBS['MANAGE_TAX_ABBR'] ?? 0; // 蓝星业绩奖. 管理费
  3085. $blueStartManageTax += $blueStartManageTaxMnt + $blueStartManageTaxAbbr; // 管理费
  3086. $monthSumData = CalcBonus::findUseSlaves()
  3087. ->select('SUM(BONUS_GX) AS BONUS_GX_SUM, SUM(BONUS_GL) AS BONUS_GL_SUM')
  3088. ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  3089. [
  3090. 'USER_ID'=>$userId,
  3091. 'CALC_MONTH'=>$this->_calcYearMonth
  3092. ]
  3093. )
  3094. ->asArray()
  3095. ->one();
  3096. $realBonusBs = $blueStartAmount; // 蓝星奖直接取数据库中算好的值PRODUCT_POINT
  3097. $exchangePoints = isset($userBS['PRODUCT_POINT']) && !empty($userBS['PRODUCT_POINT']) ? $userBS['PRODUCT_POINT'] : 0; // 兑换积分
  3098. // 蓝星奖总奖金:管理奖+业绩奖
  3099. $blueStartOriBonus = $blueStartOriBonusMnt + $blueStartOriBonusAbbr;
  3100. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] + $blueStartOriBonus; // 管理奖在存储过程计算,这里单独加上管理奖(即蓝星奖)
  3101. unset($monthSumData, $bonusGxSum, $bonusGlSum);
  3102. }
  3103. }
  3104. $result = [
  3105. 'USER_ID' => $userId,
  3106. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  3107. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  3108. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  3109. 'LAST_EMP_LV' => $empLv,
  3110. 'LAST_STATUS' => $baseInfo['STATUS'],
  3111. 'LAST_MOBILE' => $baseInfo['MOBILE'],
  3112. 'LAST_PERIOD_AT' => $baseInfo['PERIOD_NUM'],
  3113. 'LAST_CREATED_AT' => $baseInfo['CREATED_AT'],
  3114. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  3115. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  3116. 'LAST_CITY' => $baseInfo['CITY'],
  3117. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  3118. 'LAST_SYSTEM_ID' => $baseInfo['SYSTEM_ID'] ? $baseInfo['SYSTEM_ID'] : '',
  3119. 'LAST_IS_DIRECT_SELLER' => $baseInfo['IS_DIRECT_SELLER'],
  3120. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  3121. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  3122. 'LAST_CON_USER_NAME' => $baseInfo['CON_USER_NAME'],
  3123. 'LAST_CON_REAL_NAME' => $baseInfo['CON_REAL_NAME'],
  3124. 'EXCHANGE_POINTS' => $exchangePoints, // 兑换积分
  3125. // 'LAST_LOCATION' => $baseInfo['LOCATION'] ? $baseInfo['LOCATION'] : 1,
  3126. //@todo
  3127. 'LAST_LOCATION' => 1,
  3128. 'BONUS_BD' => $bonus['BONUS_BD'],
  3129. 'BONUS_TG' => $bonus['BONUS_TG'],
  3130. 'BONUS_XF' => $bonus['BONUS_XF'],
  3131. 'BONUS_YJ' => $bonus['BONUS_YJ'],
  3132. 'BONUS_GX' => $bonus['BONUS_GX'],
  3133. 'BONUS_GL' => $bonus['BONUS_GL'],
  3134. 'BONUS_QY' => $bonus['BONUS_QY'],
  3135. 'BONUS_YC' => $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'],
  3136. 'BONUS_VIP' => $bonus['BONUS_VIP'],
  3137. 'RECONSUME_POINTS' => $bonus['RECONSUME_POINTS'],
  3138. 'MANAGE_TAX' => $blueStartManageTax, // 管理费
  3139. 'BONUS_INCOME'=>$bonus['INCOME_TOTAL'],
  3140. 'BONUS_REAL'=> $bonusReal,
  3141. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  3142. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  3143. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  3144. 'ORI_BONUS_XF' => $bonus['ORI_BONUS_XF'],
  3145. 'ORI_BONUS_YJ' => $bonus['ORI_BONUS_YJ'],
  3146. 'ORI_BONUS_YJ_BD' => $bonus['ORI_BONUS_YJ_BD'],
  3147. 'ORI_BONUS_YJ_FX' => $bonus['ORI_BONUS_YJ_FX'],
  3148. 'ORI_BONUS_GX' => $bonus['ORI_BONUS_GX'],
  3149. 'REAL_BONUS_GX' => $realBonusGx,
  3150. 'ORI_BONUS_GL' => $bonus['ORI_BONUS_GL'],
  3151. 'REAL_BONUS_GL' => $realBonusGl,
  3152. 'BONUS_BS' => $realBonusBs, // 新的管理奖金,即蓝星管理奖
  3153. 'ORI_BONUS_BS' => $blueStartOriBonus, // 蓝星管理奖金原奖金,即包含管理费
  3154. 'REAL_BONUS_BS' => $realBonusBs, // 实发蓝星管理奖金
  3155. 'BONUS_BS_MNT' => $realBonusBsMnt, // 蓝星管理奖
  3156. 'ORI_BONUS_BS_MNT' => $blueStartOriBonusMnt, // 蓝星管理奖金原奖金,即包含管理费
  3157. 'REAL_BONUS_BS_MNT' => $realBonusBsMnt, // 实发蓝星管理奖金
  3158. 'MANAGE_TAX_MNT' => $blueStartManageTaxMnt, // 实发蓝星管理——管理费
  3159. 'BONUS_BS_ABBR' => $realBonusBsAbbr, // 蓝星业绩奖
  3160. 'ORI_BONUS_BS_ABBR' => $blueStartOriBonusAbbr, // 蓝星业绩奖金原奖金,即包含管理费
  3161. 'REAL_BONUS_BS_ABBR' => $realBonusBsAbbr, // 实发蓝星业绩奖金
  3162. 'MANAGE_TAX_ABBR' => $blueStartManageTaxAbbr, // 实发蓝星业绩奖——管理费
  3163. 'ORI_BONUS_GL_BD' => $bonus['ORI_BONUS_GL_BD'],
  3164. 'ORI_BONUS_GL_FX' => $bonus['ORI_BONUS_GL_FX'],
  3165. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  3166. 'ORI_BONUS_QY_BD' => $bonus['ORI_BONUS_QY_BD'],
  3167. 'ORI_BONUS_QY_FX' => $bonus['ORI_BONUS_QY_FX'],
  3168. 'ORI_BONUS_YC' => $bonus['ORI_BONUS_YC'] + $bonus['ORI_BONUS_YC_EXTRA'],
  3169. 'ORI_BONUS_VIP' => $bonus['ORI_BONUS_VIP'],
  3170. 'ORI_BONUS_STANDARD' => $standardBonus,
  3171. 'BONUS_TRAVEL' => $travelBonus, // 旅游奖
  3172. 'BONUS_CAR' => $carBonus, // 名车奖
  3173. 'BONUS_HOUSE' => $houseBonus, // 豪宅奖
  3174. //以下没有用
  3175. 'BONUS_FW' => 0,
  3176. 'BONUS_FX' => $bonus['BONUS_FX'],
  3177. 'BONUS_LS' => $bonus['BONUS_LS'],
  3178. 'BONUS_BT' => $bonus['BONUS_BT'],
  3179. 'BONUS_BT_PROD' => $bonus['BONUS_BT_PROD'],
  3180. 'BONUS_BT_TOOL' => $bonus['BONUS_BT_TOOL'],
  3181. 'DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3182. 'BONUS_FL' => $bonus['BONUS_FL'],
  3183. 'BONUS_CF' => $bonus['BONUS_CF'],
  3184. 'BONUS_LX' => $bonus['BONUS_LX'],
  3185. 'SHOULD_QY' => $bonus['BONUS_QY'],
  3186. 'SHOULD_DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3187. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  3188. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  3189. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  3190. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  3191. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  3192. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  3193. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  3194. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  3195. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  3196. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  3197. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  3198. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  3199. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  3200. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  3201. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  3202. 'PV_PCS' => $perfData['PV_PCS'],
  3203. 'PV_LS_TOUCH' => 0.00,
  3204. 'SURPLUS_LS' => 0.00,
  3205. 'QY_LS' => 0.00,
  3206. 'PV_TOUCH' => Tool::formatPrice($perfData['PV_1L_TOUCH'] + $perfData['PV_2L_TOUCH'] + $perfData['PV_3L_TOUCH'] + $perfData['PV_4L_TOUCH'] + $perfData['PV_5L_TOUCH'] + $perfData['PV_LS_TOUCH']),
  3207. 'PERIOD_NUM' => $this->_periodNum,
  3208. 'CALC_YEAR' => $this->_calcYear,
  3209. 'CALC_MONTH' => $this->_calcYearMonth,
  3210. 'CALCULATED_AT' => Date::nowTime(),
  3211. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  3212. 'CREATED_AT' => Date::nowTime(),
  3213. ];
  3214. $resend = [];
  3215. unset($bonus, $realBonusGx, $realBonusGl, $bonusReal);
  3216. return ['result'=>$result,'resend'=>$resend];
  3217. }
  3218. /**
  3219. * 根据本有收入计算基础积分
  3220. * @param int $offset
  3221. * @return bool
  3222. */
  3223. public function calcBaseScore(int $offset=0) {
  3224. if (!$this->_isCalcMonth) {
  3225. return true;
  3226. }
  3227. echo sprintf("时间:[%s]基础积分,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3228. //查询本月的,每人的累计总收入
  3229. $allData = CalcBonus::findUseDbCalc()->select('USER_ID, SUM(BONUS_TOTAL) AS TOTAL_SUM, SUM(ORI_BONUS_BD) AS BD_TOTAL_SUM')->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->groupBy('USER_ID')->orderBy('USER_ID DESC')->offset($offset)->limit($this->_limit)->asArray()->all();
  3230. if ($allData) {
  3231. foreach ($allData as $everyData) {
  3232. $validTotalSum = $everyData['TOTAL_SUM'] - $everyData['BD_TOTAL_SUM'];
  3233. if( $validTotalSum <=0 ) continue;
  3234. //达不到 见习主任级别,不产生基础积分
  3235. $userBaseInfo = CalcCache::getUserInfo($everyData['USER_ID'], $this->_periodNum);
  3236. $userEmpLevel = $this->_empLevelConfig[$userBaseInfo['EMP_LV']];
  3237. $userEmpLevelSort = $userEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  3238. unset($userBaseInfo, $userEmpLevel);
  3239. if( $userEmpLevelSort < EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {
  3240. unset($userEmpLevelSort);
  3241. continue;
  3242. }
  3243. unset($userEmpLevelSort);
  3244. //根据总收入获取积分
  3245. $baseScore = $this->_getBaseScoreByTotalBonus($validTotalSum);
  3246. //根据用户的级别判断 能否得到级别积分
  3247. if( $baseScore > 0 ) {
  3248. CalcCache::nowMonthScore($everyData['USER_ID'], $this->_periodNum, [
  3249. 'BASE_SCORE' => $baseScore,
  3250. ]);
  3251. CalcCache::addHasScoreUsers($everyData['USER_ID'], $this->_periodNum);
  3252. }
  3253. unset($everyData, $validTotalSum, $baseScore);
  3254. }
  3255. unset($allData);
  3256. return $this->calcBaseScore($offset + $this->_limit);
  3257. }
  3258. unset($allData);
  3259. return true;
  3260. }
  3261. /**
  3262. * 循环积分入库
  3263. * @param int $offset
  3264. * @return bool
  3265. * @throws \yii\db\Exception
  3266. */
  3267. public function loopWriteScore(int $offset = 0) {
  3268. if(!$this->_isCalcMonth){
  3269. return true;
  3270. }
  3271. echo sprintf("时间:[%s]积分入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3272. // 从缓存列表里面从底层往上倒序获取会员
  3273. $allData = CalcCache::getHasScoreUsers($this->_periodNum, $offset, $this->_limit);
  3274. if($allData){
  3275. $insertDataMonthScore = [];
  3276. foreach($allData as $userId){
  3277. $monthScoreData = CalcCache::nowMonthScore($userId, $this->_periodNum);
  3278. $totalScore = $monthScoreData['BASE_SCORE'] + $monthScoreData['LEVEL_SCORE'] + $monthScoreData['UPGRADE_SCORE'];
  3279. if( $totalScore <= 0 ) continue;
  3280. $insertDataMonthScore[] = [
  3281. 'USER_ID' => $userId,
  3282. 'BASE_SCORE' => $monthScoreData['BASE_SCORE'],
  3283. 'LEVEL_SCORE' => $monthScoreData['LEVEL_SCORE'],
  3284. 'UPGRADE_SCORE' => $monthScoreData['UPGRADE_SCORE'],
  3285. 'TOTAL_SCORE' => $totalScore,
  3286. 'PERIOD_NUM' => $this->_periodNum,
  3287. 'CALC_MONTH' => $this->_calcYearMonth,
  3288. 'CREATED_AT' => Date::nowTime(),
  3289. ];
  3290. unset($userId, $monthScoreData, $totalScore);
  3291. }
  3292. ScoreMonth::batchInsert($insertDataMonthScore);
  3293. unset($insertDataMonthScore, $allData);
  3294. return $this->loopWriteScore($offset + $this->_limit);
  3295. }
  3296. unset($allData);
  3297. return true;
  3298. }
  3299. /**
  3300. * 根据总收入获取相应的基础积分
  3301. * @param $totalBonus
  3302. * @return int|mixed
  3303. */
  3304. private function _getBaseScoreByTotalBonus($totalBonus) {
  3305. $baseScoreConfig = Json::decode($this->_sysConfig['baseScore']['VALUE']);
  3306. $baseScore = 0;
  3307. foreach ($baseScoreConfig as $everyScoreData) {
  3308. if( $totalBonus < $everyScoreData['monthTotalPvMin'] ) continue;
  3309. if( $everyScoreData['monthTotalPvMax'] != 0 && $totalBonus >= $everyScoreData['monthTotalPvMax'] ) continue;
  3310. $baseScore = $everyScoreData['score'];
  3311. unset($everyScoreData);
  3312. break;
  3313. }
  3314. unset($baseScoreConfig, $totalBonus);
  3315. return $baseScore;
  3316. }
  3317. }