BonusCalc.php 169 KB

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