BonusCalc.php 172 KB

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