BonusCalc.php 170 KB

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