BonusCalc.php 172 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809
  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]店服务奖金非月节点,不进行计算" . PHP_EOL, date('Y-m-d H:i:s', time()));
  769. return false;
  770. }
  771. echo sprintf("时间:[%s]店服务奖金计算--紧缩方式" . PHP_EOL, date('Y-m-d H:i:s', time()));
  772. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  773. LoggerTool::debug(json_encode(['calcStoreBonusReduce', count($allData)]));
  774. if ($allData) {
  775. $insertBonusData = [];
  776. foreach ($allData as $userId) {
  777. $storeBonus = $this->verifyStorePerf($userId);
  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. $bonusUserId = '';
  786. // 活跃 + 主任聘级
  787. if (!$isActive || !$bonusBs || $empLv[$bonusBs['LEVEL_ID']] < 3) {
  788. // 如果此店铺不活跃,则找最近的一个店铺获得此奖
  789. $this->loopRelationParentDo($userId, function ($parent) use (&$bonusUserId, $empLv, $userId) {
  790. if ($this->_isMonthPerfLimit($parent['PARENT_UID'])) {
  791. // 判断是否是店铺
  792. $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
  793. if ($parentUser['IS_STUDIO'] == 1) {
  794. // 本期蓝星奖
  795. $bonusBs = CalcBonusBS::findUseDbCalc()->where('PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID', [':PERIOD_NUM' => $this->_periodNum, ':USER_ID' => $parent['PARENT_UID']])->asArray()->one();
  796. // 蓝星奖星级>=主任
  797. if ($bonusBs && $empLv[$bonusBs['LEVEL_ID']] >= 3) {
  798. $bonusUserId = $parent['PARENT_UID'];
  799. return self::LOOP_FINISH;
  800. }
  801. }
  802. }
  803. unset($parent, $bonusBs);
  804. });
  805. }
  806. if (!$isActive && !$bonusUserId) {
  807. continue;
  808. }
  809. $realBonusUserId = $bonusUserId ?: $userId;
  810. //扣除相应的复消积分和管理费
  811. $deductData = $this->deduct($realBonusUserId, $storeBonus);
  812. // 把对碰后的奖金存入缓存中
  813. CalcCache::bonus($realBonusUserId, $this->_periodNum, 'BONUS_STORE', $storeBonus, $deductData);
  814. $surplus = $deductData['surplus'];
  815. $manageTax = $deductData['manageTax'];
  816. $reConsumePoints = $deductData['reConsumePoints'];
  817. //店服务奖流水
  818. $insertBonusData[] = [
  819. 'ID' => SnowFake::instance()->generateId(),
  820. 'USER_ID' => $realBonusUserId,
  821. 'FROM_USER_ID' => $bonusUserId ? $userId : '', // 分享店铺的ID.只有紧缩且未活跃,才有此值
  822. 'AMOUNT' => $surplus,
  823. 'ORI_BONUS' => $storeBonus,
  824. 'RECONSUME_POINTS' => $reConsumePoints,
  825. 'MANAGE_TAX' => $manageTax,
  826. 'PERIOD_NUM' => $this->_periodNum,
  827. 'CALC_YEAR' => $this->_calcYear,
  828. 'CALC_MONTH' => $this->_calcYearMonth,
  829. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  830. 'CREATED_AT' => Date::nowTime()
  831. ];
  832. unset($bonusUserId, $userId, $deductData);
  833. }
  834. CalcBonusST::batchInsert($insertBonusData);
  835. unset($allData, $insertBonusData);
  836. return $this->calcStoreBonusReduce($offset + $this->_limit);
  837. }
  838. unset($allData);
  839. return true;
  840. }
  841. // 未开启紧缩方式计算店服务奖,不活跃即不给奖金
  842. public function calcStoreBonus(int $offset = 0) {
  843. if( !$this->_isCalcMonth ) {
  844. // 不是结算月,则不进行计算
  845. echo sprintf("时间:[%s]店服务奖金非月节点,不进行计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  846. return false;
  847. }
  848. echo sprintf("时间:[%s]店服务奖金计算--非紧缩方式,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  849. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  850. if ($allData) {
  851. $insertBonusData = [];
  852. foreach ($allData as $userId) {
  853. $storeBonus = $this->verifyStorePerf($userId);
  854. if (empty($storeBonus)) continue;
  855. // 未开启紧缩,不活跃,则不发放了
  856. $isActive = $this->_isMonthPerfLimit($userId);
  857. if (!$isActive) continue;
  858. $surplus = $storeBonus;
  859. //扣除相应的复消积分和管理费
  860. $deductData = $this->deduct($userId, $storeBonus);
  861. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_STORE', $storeBonus, $deductData);
  862. $surplus = $deductData['surplus'];
  863. $manageTax = $deductData['manageTax'];
  864. $reConsumePoints = $deductData['reConsumePoints'];
  865. //店服务奖流水
  866. $insertBonusData[] = [
  867. 'ID' => SnowFake::instance()->generateId(),
  868. 'USER_ID' => $userId,
  869. 'AMOUNT' => $surplus,
  870. 'ORI_BONUS' => $storeBonus,
  871. 'RECONSUME_POINTS' => $reConsumePoints,
  872. 'MANAGE_TAX' => $manageTax,
  873. 'PERIOD_NUM' => $this->_periodNum,
  874. 'CALC_YEAR' => $this->_calcYear,
  875. 'CALC_MONTH' => $this->_calcYearMonth,
  876. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  877. 'CREATED_AT' => Date::nowTime()
  878. ];
  879. unset($userId, $deductData);
  880. }
  881. CalcBonusST::batchInsert($insertBonusData);
  882. unset($allData, $insertBonusData);
  883. return $this->calcStoreBonus($offset + $this->_limit);
  884. }
  885. unset($allData);
  886. return true;
  887. }
  888. public function verifyStorePerfNew($userId, $groupPv) {
  889. // 业绩乘以比例为奖金
  890. $storeBonus = Tool::formatPrice($groupPv * $this->_sysConfig['storePercent']['VALUE'] / 100);
  891. if ($storeBonus <= 0) {
  892. return false;
  893. }
  894. // 获取会员的报单级别
  895. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  896. $storeBonus = $this->bonusTotalLimit($storeBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  897. if( $storeBonus <= 0 ) {
  898. return false;
  899. }
  900. unset($perfPv, $userBaseInfo, $userId);
  901. return $storeBonus;
  902. }
  903. /**
  904. * 通过用户id,校验店服务奖,此用户是否有原奖金
  905. */
  906. public function verifyStorePerf($userId) {
  907. // 从缓存中获取会员的业绩信息
  908. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  909. if( !$perfData ) {
  910. return false;
  911. }
  912. // 获取小组+自己的业绩
  913. $perfPv = $perfData['STORE_PV_GRAND'] ?? 0;
  914. LoggerTool::debug(json_encode(['verifyStorePerf-1-' . $userId, $perfData['STORE_PV_GRAND']]));
  915. if( $perfPv <= 0 ) {
  916. LoggerTool::debug('verifyStorePerf-2-' . $userId);
  917. return false;
  918. }
  919. StorePerfLog::stGroupPv($this->_periodNum,$this->_calcYearMonth, $userId, $perfPv);
  920. // 业绩乘以比例为奖金
  921. $storeBonus = Tool::formatPrice($perfPv * $this->_sysConfig['storePercent']['VALUE'] / 100);
  922. if ($storeBonus <= 0) {
  923. LoggerTool::debug('verifyStorePerf-3-' . $userId);
  924. return false;
  925. }
  926. // 获取会员的报单级别
  927. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  928. $storeBonus = $this->bonusTotalLimit($storeBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  929. if( $storeBonus <= 0 ) {
  930. LoggerTool::debug('verifyStorePerf-4-' . $userId);
  931. return false;
  932. }
  933. unset($perfData, $perfPv, $userBaseInfo, $userId);
  934. return $storeBonus;
  935. }
  936. /**
  937. * 报单业绩奖
  938. * @param int $offset
  939. * @return bool
  940. * @throws \yii\db\Exception
  941. */
  942. public function calcBonusBdYJ(int $offset = 0) {
  943. $periodNum = $this->_periodNum;
  944. // 从缓存获取分页有业绩的会员信息
  945. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  946. if ($allData) {
  947. foreach ($allData as $userId) {
  948. $insertBonusData = [];
  949. // 从缓存中获取会员的业绩信息
  950. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  951. if( !$perfData ) continue;
  952. //个人业绩都算见点奖,包括报单和复消、二次购物
  953. $perfPv = $perfData['PV_PCS_ZC'] ?? 0;
  954. if( $perfPv <= 0 ) continue;
  955. //业绩奖使用报单PV
  956. // $oriPointsBonus = Tool::formatPrice($perfPv * $this->_sysConfig['pointsPercent']['VALUE'] / 100);
  957. // if ($oriPointsBonus <= 0) continue;
  958. //偶数层数
  959. $pointsEvenLayer = $this->_sysConfig['pointsLayer']['VALUE'];
  960. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  961. $userEmpLevel = $userBaseInfo['EMP_LV'];
  962. $netWorkParents = Cache::getAllNetworkParents($userId);
  963. foreach ($netWorkParents as $netWorkParent) {
  964. // $pointsBonus = $oriPointsBonus;
  965. //自已距顶端的深度减去父级距顶端的深度
  966. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  967. if( $diffLayer <= 0) continue;
  968. if( $diffLayer > 2 * $pointsEvenLayer ) continue;
  969. if( $diffLayer % 2 == 1 ) continue;
  970. $pointsPercentName = sprintf('pointsPercent_%d', $diffLayer);
  971. if( isset($this->_sysConfig[$pointsPercentName]['VALUE']) ) {
  972. $diffLayerPercent = $this->_sysConfig[$pointsPercentName]['VALUE'];
  973. }else {
  974. $diffLayerPercent = $this->_sysConfig['pointsPercent']['VALUE'];
  975. }
  976. $pointsBonus = Tool::formatPrice($perfPv * $diffLayerPercent / 100);
  977. if( $pointsBonus <= 0 ) continue;
  978. // 把对碰后的奖金存入缓存中
  979. $bonusUserId = $netWorkParent['PARENT_UID'];
  980. if( !$bonusUserId ) continue;
  981. // 获取会员的报单级别
  982. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  983. //判断上限 包括级别奖金上限和总上限
  984. // $pointsBonus = $this->declarationLevelCap($pointsBonus, $bonusUserId, $bonusUserBaseInfo['DEC_LV']);
  985. // if( $pointsBonus <= 0 ) continue;
  986. //总金额限制
  987. $pointsBonus = $this->bonusTotalLimit($pointsBonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  988. if( $pointsBonus <= 0 ) continue;
  989. //扣除相应的复消积分和管理费
  990. $deductData = $this->deduct($bonusUserId, $pointsBonus);
  991. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_YJ', $pointsBonus, $deductData, CalcCache::FROM_MEANS_BD);
  992. //业绩奖流水
  993. $insertBonusData[] = [
  994. 'ID' => SnowFake::instance()->generateId(),
  995. 'USER_ID' => $bonusUserId,
  996. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  997. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  998. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  999. 'FROM_USER_ID' => $userId,
  1000. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1001. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1002. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1003. 'AMOUNT' => $deductData['surplus'],
  1004. 'ORI_BONUS' => $pointsBonus,
  1005. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1006. 'MANAGE_TAX' => $deductData['manageTax'],
  1007. 'PERIOD_NUM' => $this->_periodNum,
  1008. 'CALC_YEAR' => $this->_calcYear,
  1009. 'CALC_MONTH' => $this->_calcYearMonth,
  1010. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1011. 'CREATED_AT' => Date::nowTime(),
  1012. 'LOGS' => json_encode([
  1013. 'perfPv' => $perfPv,
  1014. 'pointsLayerConfig' => $this->_sysConfig['pointsLayer']['VALUE'],
  1015. 'pointsPercentConfig' => $diffLayerPercent,
  1016. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1017. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1018. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1019. ]),
  1020. ];
  1021. unset($netWorkParent, $pointsBonus, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $deductData, $diffLayerPercent, $pointsPercentName);
  1022. }
  1023. CalcBonusYJ::batchInsert($insertBonusData);
  1024. unset($insertBonusData, $userId, $perfData, $perfPv, $oriPointsBonus, $pointsEvenLayer, $userBaseInfo, $userEmpLevel, $netWorkParents);
  1025. }
  1026. unset($allData);
  1027. return $this->calcBonusBdYJ($offset + $this->_limit);
  1028. }
  1029. unset($allData);
  1030. return true;
  1031. }
  1032. /**
  1033. * 复消业绩奖
  1034. * @param int $offset
  1035. * @return bool
  1036. * @throws \yii\db\Exception
  1037. */
  1038. public function calcBonusFxYJ(int $offset = 0) {
  1039. $periodNum = $this->_periodNum;
  1040. // 从缓存获取分页有业绩的会员信息
  1041. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1042. if ($allData) {
  1043. foreach ($allData as $userId) {
  1044. $insertBonusData = [];
  1045. // 从缓存中获取会员的业绩信息
  1046. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1047. if( !$perfData ) continue;
  1048. //个人业绩都算见点奖,包括报单和复消、二次购物
  1049. $perfPv = $perfData['PV_PCS_FX'] ?? 0;
  1050. if( $perfPv <= 0 ) continue;
  1051. //业绩奖使用报单PV
  1052. // $oriPointsBonus = Tool::formatPrice($perfPv * $this->_sysConfig['pointsPercent']['VALUE'] / 100);
  1053. // if ($oriPointsBonus <= 0) continue;
  1054. //偶数层数
  1055. $pointsEvenLayer = $this->_sysConfig['fxPointsLayer']['VALUE'];
  1056. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1057. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1058. $netWorkParents = Cache::getAllNetworkParents($userId);
  1059. foreach ($netWorkParents as $netWorkParent) {
  1060. // $pointsBonus = $oriPointsBonus;
  1061. //自已距顶端的深度减去父级距顶端的深度
  1062. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  1063. if( $diffLayer <= 0) continue;
  1064. if( $diffLayer > 2 * $pointsEvenLayer ) continue;
  1065. if( $diffLayer % 2 == 1 ) continue;
  1066. $pointsPercentName = sprintf('fxPointsPercent_%d', $diffLayer);
  1067. if( isset($this->_sysConfig[$pointsPercentName]['VALUE']) ) {
  1068. $diffLayerPercent = $this->_sysConfig[$pointsPercentName]['VALUE'];
  1069. }else {
  1070. $diffLayerPercent = $this->_sysConfig['fxPointsPercent']['VALUE'];
  1071. }
  1072. $pointsBonus = Tool::formatPrice($perfPv * $diffLayerPercent / 100);
  1073. if( $pointsBonus <= 0 ) continue;
  1074. // 把对碰后的奖金存入缓存中
  1075. $bonusUserId = $netWorkParent['PARENT_UID'];
  1076. if( !$bonusUserId ) continue;
  1077. // 获取会员的报单级别
  1078. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1079. //判断上限 包括级别奖金上限和总上限
  1080. // $pointsBonus = $this->declarationLevelCap($pointsBonus, $bonusUserId, $bonusUserBaseInfo['DEC_LV']);
  1081. // if( $pointsBonus <= 0 ) continue;
  1082. //总金额限制
  1083. $pointsBonus = $this->bonusTotalLimit($pointsBonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  1084. if( $pointsBonus <= 0 ) continue;
  1085. //扣除相应的复消积分和管理费
  1086. $deductData = $this->deduct($bonusUserId, $pointsBonus);
  1087. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_YJ', $pointsBonus, $deductData, CalcCache::FROM_MEANS_FX);
  1088. //业绩奖流水
  1089. $insertBonusData[] = [
  1090. 'ID' => SnowFake::instance()->generateId(),
  1091. 'USER_ID' => $bonusUserId,
  1092. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1093. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1094. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1095. 'FROM_USER_ID' => $userId,
  1096. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1097. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1098. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1099. 'AMOUNT' => $deductData['surplus'],
  1100. 'ORI_BONUS' => $pointsBonus,
  1101. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1102. 'MANAGE_TAX' => $deductData['manageTax'],
  1103. 'PERIOD_NUM' => $this->_periodNum,
  1104. 'CALC_YEAR' => $this->_calcYear,
  1105. 'CALC_MONTH' => $this->_calcYearMonth,
  1106. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1107. 'CREATED_AT' => Date::nowTime(),
  1108. 'LOGS' => json_encode([
  1109. 'perfPv' => $perfPv,
  1110. 'pointsLayerConfig' => $this->_sysConfig['fxPointsLayer']['VALUE'],
  1111. 'pointsPercentConfig' => $diffLayerPercent,
  1112. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1113. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1114. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1115. ]),
  1116. ];
  1117. unset($netWorkParent, $pointsBonus, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $deductData, $diffLayerPercent, $pointsPercentName);
  1118. }
  1119. CalcBonusYJ::batchInsert($insertBonusData);
  1120. unset($insertBonusData, $userId, $perfData, $perfPv, $oriPointsBonus, $pointsEvenLayer, $userBaseInfo, $userEmpLevel, $netWorkParents);
  1121. }
  1122. unset($allData);
  1123. return $this->calcBonusFxYJ($offset + $this->_limit);
  1124. }
  1125. unset($allData);
  1126. return true;
  1127. }
  1128. /**
  1129. * 团队奖
  1130. * @param int $offset
  1131. * @return bool
  1132. * @throws \yii\db\Exception
  1133. */
  1134. public function calcBonusQY(int $offset = 0) {
  1135. echo sprintf("时间:[%s]团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1136. $periodNum = $this->_periodNum;
  1137. // 从缓存获取分页有业绩的会员信息
  1138. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1139. if ($allData) {
  1140. $insertBonusData = [];
  1141. foreach ($allData as $userId) {
  1142. // 从缓存中获取会员的业绩信息
  1143. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1144. // 从缓存中获取会员的上期结余业绩信息
  1145. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1146. // 本期 + 上期结余
  1147. $perfArr = [
  1148. 'SURPLUS_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  1149. 'SURPLUS_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  1150. 'SURPLUS_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  1151. 'SURPLUS_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  1152. 'SURPLUS_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  1153. ];
  1154. $oriPerfArr = [
  1155. 'perfArr' => $perfArr,
  1156. 'touchBonus' => 0,
  1157. ];
  1158. // 获取会员的报单级别
  1159. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1160. $decLevelConfig = $this->_decLevelConfig;
  1161. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1162. // 对碰
  1163. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1164. $touchPerfArr = [];
  1165. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1166. $touchPerfArr[$keyR] = $perfR;
  1167. }
  1168. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1169. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1170. //更新数据库
  1171. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1172. 'USER_ID' => $userId,
  1173. 'PERIOD_NUM' => $periodNum,
  1174. ]);
  1175. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1176. $teamBonus = $touchBonusArr['touchBonus'];
  1177. //判断级别上限
  1178. $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1179. if( $teamBonus <= 0 ) continue;
  1180. $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1181. if( $teamBonus <= 0 ) continue;
  1182. //扣除相应的复消积分和管理费
  1183. $deductData = $this->deduct($userId, $teamBonus);
  1184. // 把对碰后的奖金存入缓存中
  1185. CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus, $deductData);
  1186. //团队奖流水
  1187. $insertBonusData[] = [
  1188. 'ID' => SnowFake::instance()->generateId(),
  1189. 'USER_ID' => $userId,
  1190. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  1191. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  1192. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  1193. 'AMOUNT' => $deductData['surplus'],
  1194. 'ORI_BONUS' => $teamBonus,
  1195. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1196. 'MANAGE_TAX' => $deductData['manageTax'],
  1197. 'PERIOD_NUM' => $this->_periodNum,
  1198. 'CALC_YEAR' => $this->_calcYear,
  1199. 'CALC_MONTH' => $this->_calcYearMonth,
  1200. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1201. 'CREATED_AT' => Date::nowTime(),
  1202. 'LOGS' => json_encode([
  1203. 'perfArr' => $perfArr,
  1204. 'touchPerfArrOri' => $touchBonusArr['perfArr'],
  1205. 'touchPerfArr' => $touchPerfArr,
  1206. 'nowDecLevelConfig' => $nowDecLevelConfig,
  1207. 'decLevel' => $userBaseInfo['DEC_LV'],
  1208. ]),
  1209. ];
  1210. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1211. }
  1212. CalcBonusQY::batchInsert($insertBonusData);
  1213. unset($allData, $insertBonusData);
  1214. return $this->calcBonusQY($offset + $this->_limit);
  1215. }
  1216. unset($allData);
  1217. return true;
  1218. }
  1219. /**
  1220. * 报单团队奖
  1221. * @param int $offset
  1222. * @return bool
  1223. * @throws \yii\db\Exception
  1224. */
  1225. public function calcBonusBdQY(int $offset = 0) {
  1226. echo sprintf("时间:[%s]报单团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1227. $periodNum = $this->_periodNum;
  1228. // 从缓存获取分页有业绩的会员信息
  1229. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1230. if ($allData) {
  1231. $insertBonusData = [];
  1232. foreach ($allData as $userId) {
  1233. // 从缓存中获取会员的业绩信息
  1234. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1235. // 从缓存中获取会员的上期结余业绩信息
  1236. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1237. // 本期 + 上期结余
  1238. $perfArr = [
  1239. 'SURPLUS_1L_ZC' => $perfData['PV_1L_ZC'] + $pervSurplusPerf['SURPLUS_1L_ZC'],
  1240. 'SURPLUS_2L_ZC' => $perfData['PV_2L_ZC'] + $pervSurplusPerf['SURPLUS_2L_ZC'],
  1241. 'SURPLUS_3L_ZC' => $perfData['PV_3L_ZC'] + $pervSurplusPerf['SURPLUS_3L_ZC'],
  1242. 'SURPLUS_4L_ZC' => $perfData['PV_4L_ZC'] + $pervSurplusPerf['SURPLUS_4L_ZC'],
  1243. 'SURPLUS_5L_ZC' => $perfData['PV_5L_ZC'] + $pervSurplusPerf['SURPLUS_5L_ZC'],
  1244. ];
  1245. $oriPerfArr = [
  1246. 'perfArr' => $perfArr,
  1247. 'touchBonus' => 0,
  1248. ];
  1249. // 获取会员的报单级别
  1250. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1251. $decLevelConfig = $this->_decLevelConfig;
  1252. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1253. // 对碰
  1254. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1255. $touchPerfArr = [];
  1256. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1257. $touchPerfArr[$keyR] = $perfR;
  1258. }
  1259. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1260. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1261. //更新数据库
  1262. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1263. 'USER_ID' => $userId,
  1264. 'PERIOD_NUM' => $periodNum,
  1265. ]);
  1266. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1267. $teamBonus = $touchBonusArr['touchBonus'];
  1268. //判断级别上限
  1269. // $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1270. // if( $teamBonus <= 0 ) continue;
  1271. // $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1272. // if( $teamBonus <= 0 ) continue;
  1273. //扣除相应的复消积分和管理费
  1274. // $deductData = $this->deduct($userId, $teamBonus);
  1275. // 把对碰后的奖金存入缓存中
  1276. CalcCache::bonus($userId, $periodNum, 'BONUS_QY_BD', $teamBonus);
  1277. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1278. }
  1279. unset($allData, $insertBonusData);
  1280. return $this->calcBonusBdQY($offset + $this->_limit);
  1281. }
  1282. unset($allData);
  1283. return true;
  1284. }
  1285. /**
  1286. * 复消团队奖
  1287. * @param int $offset
  1288. * @return bool
  1289. * @throws \yii\db\Exception
  1290. */
  1291. public function calcBonusFxQY(int $offset = 0) {
  1292. echo sprintf("时间:[%s]复消团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1293. $periodNum = $this->_periodNum;
  1294. // 从缓存获取分页有业绩的会员信息
  1295. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1296. if ($allData) {
  1297. $insertBonusData = [];
  1298. foreach ($allData as $userId) {
  1299. // 从缓存中获取会员的业绩信息
  1300. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1301. // 从缓存中获取会员的上期结余业绩信息
  1302. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1303. // 本期 + 上期结余
  1304. $perfArr = [
  1305. 'SURPLUS_1L_FX' => $perfData['PV_1L_FX'] + $pervSurplusPerf['SURPLUS_1L_FX'],
  1306. 'SURPLUS_2L_FX' => $perfData['PV_2L_FX'] + $pervSurplusPerf['SURPLUS_2L_FX'],
  1307. 'SURPLUS_3L_FX' => $perfData['PV_3L_FX'] + $pervSurplusPerf['SURPLUS_3L_FX'],
  1308. 'SURPLUS_4L_FX' => $perfData['PV_4L_FX'] + $pervSurplusPerf['SURPLUS_4L_FX'],
  1309. 'SURPLUS_5L_FX' => $perfData['PV_5L_FX'] + $pervSurplusPerf['SURPLUS_5L_FX'],
  1310. ];
  1311. $oriPerfArr = [
  1312. 'perfArr' => $perfArr,
  1313. 'touchBonus' => 0,
  1314. ];
  1315. // 获取会员的报单级别
  1316. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1317. $decLevelConfig = $this->_decLevelConfig;
  1318. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1319. // 对碰
  1320. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1321. //大区封顶
  1322. $touchPerfArr = [];
  1323. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1324. // if ($perfR > $nowDecLevelConfig['QY_BIG_CAP']) $perfR = $nowDecLevelConfig['QY_BIG_CAP'];
  1325. $touchPerfArr[$keyR] = $perfR;
  1326. }
  1327. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1328. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1329. //更新数据库
  1330. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1331. 'USER_ID' => $userId,
  1332. 'PERIOD_NUM' => $periodNum,
  1333. ]);
  1334. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1335. $teamBonus = $touchBonusArr['touchBonus'];
  1336. //判断级别上限
  1337. // $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1338. // if( $teamBonus <= 0 ) continue;
  1339. //
  1340. // $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1341. // if( $teamBonus <= 0 ) continue;
  1342. //扣除相应的复消积分和管理费
  1343. // $deductData = $this->deduct($userId, $teamBonus);
  1344. // 把对碰后的奖金存入缓存中
  1345. CalcCache::bonus($userId, $periodNum, 'BONUS_QY_FX', $teamBonus);
  1346. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1347. }
  1348. unset($allData, $insertBonusData);
  1349. return $this->calcBonusFxQY($offset + $this->_limit);
  1350. }
  1351. unset($allData);
  1352. return true;
  1353. }
  1354. /**
  1355. * 计算共享奖之前的操作
  1356. * @param int $offset
  1357. * @return bool
  1358. */
  1359. public function calcBonusGXBefore(int $offset = 0) {
  1360. //获取所有激活会员
  1361. $allData = CalcCache::getActiveUsers($this->_periodNum, $offset, $this->_limit);
  1362. if ($allData) {
  1363. echo sprintf("时间:[%s]共享奖之前,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1364. foreach ($allData as $userId) {
  1365. //判断$userId 能否得共享奖
  1366. // //判断 是否本期小市场(安置关系)新增980pv 能否得奖
  1367. // if ( !$this->checkSmallMarketPerf($userId) ) {
  1368. // unset($userId);
  1369. // continue;
  1370. // }
  1371. // 20220407 小市场980判断->改成判断此期用户是否有原报单团队奖金
  1372. if ( !$this->checkHasOriBonusQyBd($userId) ) {
  1373. unset($userId);
  1374. continue;
  1375. }
  1376. //获取当前会员所有的推荐上级, 并查看是否有首单团队奖
  1377. $validDeep = 0;
  1378. $this->loopRelationParentDo($userId, function ($parent) use ($userId, &$validDeep) {
  1379. //判断$parent是否有首单团队奖
  1380. $parentBonus = CalcCache::bonus($parent['PARENT_UID'], $this->_periodNum);
  1381. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {
  1382. $validDeep += 1;
  1383. //记录得奖缓存
  1384. CalcCache::addShareBonusOneRelation($userId, $parent['PARENT_UID'], $this->_periodNum, $validDeep);
  1385. }
  1386. unset($parent, $parentBonus);
  1387. if ( $validDeep >= 2 ) {
  1388. return self::LOOP_FINISH;
  1389. }
  1390. });
  1391. unset($userId, $validDeep);
  1392. }
  1393. unset($allData);
  1394. return $this->calcBonusGXBefore($offset + $this->_limit);
  1395. }
  1396. unset($allData);
  1397. return true;
  1398. }
  1399. /**
  1400. * 共享奖
  1401. * @param int $offset
  1402. * @return bool
  1403. * @throws \yii\db\Exception
  1404. */
  1405. public function calcBonusGX(int $offset = 0) {
  1406. // 从缓存获取分页有的会员信息
  1407. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_gxLimit);
  1408. if ($allData) {
  1409. echo sprintf("时间:[%s]共享奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1410. $insertBonusData = [];
  1411. // 获取线上找几层和是否开启找几层的限制
  1412. $configsGx = Config::find()->where(
  1413. 'CONFIG_NAME="gxNoPvFindLimitSwitch" OR CONFIG_NAME="gxNoPvFindLimitTimes"'
  1414. )->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  1415. $openFindLimit = isset($configsGx['gxNoPvFindLimitSwitch']) ? $configsGx['gxNoPvFindLimitSwitch']['VALUE'] : 1; // 是否开放了 得奖人不满足980就找一个满足980的人来得这个奖 的最大向上循环次数
  1416. $findLimitTimes = isset($configsGx['gxNoPvFindLimitTimes']) ? $configsGx['gxNoPvFindLimitTimes']['VALUE'] : 5 ; // 开放的限制层数
  1417. $shareOutTimes = Config::find()
  1418. ->where(
  1419. 'CONFIG_NAME="shareOutTimes"'
  1420. )
  1421. ->indexBy('CONFIG_NAME')
  1422. ->asArray()
  1423. ->all();// 获取向上找五次超过次数限制
  1424. $shareOutTimes = isset($shareOutTimes['shareOutTimes']) ? $shareOutTimes['shareOutTimes']['VALUE'] : 5;
  1425. foreach ($allData as $userId) {
  1426. $bonusUserData = [];
  1427. $bonusPercentOne = $this->_sysConfig['shareRecPercent']['VALUE'] / 100;
  1428. $bonusPercentTwo = $this->_sysConfig['sharePercent']['VALUE'] / 100;
  1429. // 从缓存中获取会员的收入信息
  1430. $incomeBonus = CalcCache::bonus($userId, $this->_periodNum);
  1431. $lastTwoIncome = $incomeBonus['ORI_BONUS_QY_BD'] ?? 0;
  1432. $shareBonusOne = $lastTwoIncome * $bonusPercentOne;
  1433. unset($lastTwoIncome);
  1434. //找到上2代 新增个人+推荐团队业绩 加权平均分配 这个奖
  1435. if( $shareBonusOne > 0) {
  1436. $bonusOneData = $this->gxLastTwoBonusData($userId, 1, $shareBonusOne);
  1437. $bonusTwoData = $this->gxLastTwoBonusData($userId, 2, $shareBonusOne);
  1438. $bonusUserData = array_merge($bonusUserData, $bonusOneData);
  1439. $bonusUserData = array_merge($bonusUserData, $bonusTwoData);
  1440. unset($bonusOneData, $bonusTwoData);
  1441. }
  1442. $nextFiveIncome = $incomeBonus['ORI_BONUS_QY_BD'] ?? 0;
  1443. $shareBonusTwo = $nextFiveIncome * $bonusPercentTwo;
  1444. unset($nextFiveIncome);
  1445. //找到下5代(安置网) 紧缩 相对偶数层2、4、6、8、10层
  1446. if( $shareBonusTwo > 0 ) {
  1447. $validDeep = 1;
  1448. $periodNum = $this->_periodNum;
  1449. $this->loopNetworkParentDo($userId, function ($parent) use ($shareOutTimes, $periodNum, $userId, $shareBonusTwo, &$validDeep, &$bonusUserData) {
  1450. if ( $validDeep % 2 === 0 ) {
  1451. $shareTimes = CalcCache::gxShareTimes($userId, $periodNum);
  1452. // 增加判断,是否分共享奖奖金次数超过了五次,如果超过五次则不进行循环了
  1453. if ($shareTimes > $shareOutTimes) {
  1454. return self::LOOP_FINISH;
  1455. }
  1456. $bonusUserData[] = [
  1457. 'bonusUid' => $parent['PARENT_UID'],
  1458. 'bonus' => $shareBonusTwo,
  1459. 'validMinPv' => true,
  1460. ];
  1461. }
  1462. //判断$parent是否有首单团队奖
  1463. $parentBonus = CalcCache::bonus($parent['PARENT_UID'], $this->_periodNum);
  1464. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {//有效层
  1465. $validDeep += 1;
  1466. }
  1467. unset($parent, $parentBonus);
  1468. if ( $validDeep >= 11 ) {
  1469. return self::LOOP_FINISH;
  1470. }
  1471. });
  1472. unset($validDeep);
  1473. }
  1474. if ( !$bonusUserData ) continue;
  1475. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1476. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1477. foreach ($bonusUserData as $bonusUserDataEvery) {
  1478. $bonusUserId = $bonusUserDataEvery['bonusUid'];
  1479. //判断 是否本期小市场(安置关系)新增980pv 能否得奖->20220407改成判断得奖人此期是否有报单团队奖金
  1480. // if ( $bonusUserDataEvery['validMinPv'] && !$this->checkSmallMarketPerf($bonusUserId) ) { 20220407
  1481. if ( $bonusUserDataEvery['validMinPv'] && !$this->checkHasOriBonusQyBd($bonusUserId) ) {
  1482. // unset($bonusUserDataEvery, $bonusUserId);
  1483. // continue;
  1484. //得奖人不满足980就找一个满足980的人来得这个奖
  1485. // 20220407改成判断得奖人此期是否有报单团队奖金,如果没有则找一个此期有报单团队奖金得人
  1486. $bonusUserId = $this->getMinBdPvNetworkParent($bonusUserId, $openFindLimit, $findLimitTimes);
  1487. if ( !$bonusUserId ) {
  1488. unset($bonusUserDataEvery, $bonusUserId);
  1489. continue;
  1490. }
  1491. }
  1492. $bonus = Tool::formatPrice($bonusUserDataEvery['bonus']);
  1493. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1494. //总金额限制
  1495. $bonus = $this->bonusTotalLimit($bonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  1496. if( $bonus <= 0 ) continue;
  1497. // //扣除相应的复消积分和管理费
  1498. // 共享奖调整,周共享奖信息,不扣除管理费和复消费积分
  1499. // $deductData = $this->deduct($bonusUserId, $bonus);
  1500. // CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GX', $bonus, $deductData,'',true);
  1501. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GX', $bonus);
  1502. CalcCache::addHasMonthBonusUsers($bonusUserId, $this->_periodNum);
  1503. //共享奖流水 rm1820 月总扣除复消积分和管理费,流水中不扣除.
  1504. $insertBonusData[] = [
  1505. 'ID' => SnowFake::instance()->generateId(),
  1506. 'USER_ID' => $bonusUserId,
  1507. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1508. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1509. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1510. 'FROM_USER_ID' => $userId,
  1511. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1512. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1513. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1514. // 'AMOUNT' => 0,
  1515. 'ORI_BONUS' => $bonus,
  1516. // 'RECONSUME_POINTS' => 0,
  1517. // 'MANAGE_TAX' => 0,
  1518. 'PERIOD_NUM' => $this->_periodNum,
  1519. 'CALC_YEAR' => $this->_calcYear,
  1520. 'CALC_MONTH' => $this->_calcYearMonth,
  1521. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1522. 'CREATED_AT' => Date::nowTime(),
  1523. 'LOGS' => json_encode([
  1524. 'sharePercent' => $bonusPercentOne,
  1525. 'incomeBonus' => $incomeBonus,
  1526. 'recNum' => $userBaseInfo['REC_NUM'],
  1527. 'decAmount' => $userBaseInfo['ZC_AMOUNT'],
  1528. ]),
  1529. ];
  1530. unset($bonusUserDataEvery, $bonusUserId, $bonus, $bonusUserBaseInfo);
  1531. }
  1532. unset($userId, $bonusUserData, $userBaseInfo, $userEmpLevel, $bonusPercentOne, $bonusPercentTwo, $incomeBonus, $sharePerson);
  1533. // echo ('内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  1534. }
  1535. if($insertBonusData) CalcBonusGX::batchInsert($insertBonusData);
  1536. unset($allData, $insertBonusData);
  1537. return $this->calcBonusGX($offset + $this->_gxLimit);
  1538. }
  1539. unset($allData);
  1540. return true;
  1541. }
  1542. // 执行蓝星管理奖金的存储过程
  1543. public function calcBsProcedure() {
  1544. if( !$this->_isCalcMonth ) {
  1545. // 不是结算月,则不进行计算
  1546. return false;
  1547. }
  1548. $result = \Yii::$app->db->createCommand("CALL CalcBlue(:periodNum)")
  1549. ->bindValue(':periodNum' , $this->_periodNum )
  1550. ->execute();
  1551. return $result;
  1552. }
  1553. /**
  1554. * 蓝星管理奖金未拆分
  1555. * @param int $offset
  1556. * @return bool
  1557. * @throws \yii\db\Exception
  1558. */
  1559. public function calcBonusBsGL(int $offset = 0) {
  1560. if( !$this->_isCalcMonth ) {
  1561. // 不是结算月,则不进行计算
  1562. return false;
  1563. }
  1564. // 从缓存获取分页有收入的会员信息
  1565. $allData = CalcBonusBS::findUseDbCalc()
  1566. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1567. ->groupBy('USER_ID')
  1568. ->offset($offset)
  1569. ->limit($this->_limit)
  1570. ->asArray()
  1571. ->all();
  1572. if ($allData) {
  1573. foreach ($allData as $user) {
  1574. // 添加到有奖励信息的数据中
  1575. CalcCache::addHasBonusUsers($user['USER_ID'], $this->_periodNum);
  1576. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  1577. if ($fxStatus) {
  1578. //扣除相应的复消积分和管理费
  1579. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS']);
  1580. // 把对碰后的奖金存入缓存中
  1581. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS'], $deductData);
  1582. //CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  1583. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1584. $manageTax = $deductData['manageTax']; // 管理费
  1585. $point = $deductData['reConsumePoints'];// 复消积分
  1586. } else {
  1587. // 如果未满足月消费,则不扣除管理费和复消积分了
  1588. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  1589. $realBonusBs = $user['ORI_BONUS'];
  1590. $manageTax = 0; // 管理费
  1591. $point = 0;// 复消积分
  1592. }
  1593. // 更新蓝星奖金存储过程的实发金额数据
  1594. CalcBonusBS::updateAll(['AMOUNT' => $realBonusBs, 'MANAGE_TAX' => $manageTax, 'RECONSUME_POINTS' => $point],
  1595. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1596. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1597. }
  1598. return $this->calcBonusBsGL($offset + $this->_limit);
  1599. }
  1600. unset($allData);
  1601. return true;
  1602. }
  1603. /**
  1604. * 蓝星管理奖金
  1605. * @param int $offset
  1606. * @return bool
  1607. * @throws \yii\db\Exception
  1608. */
  1609. public function calcBonusBsGLCF(int $offset = 0) {
  1610. if( !$this->_isCalcMonth ) {
  1611. // 不是结算月,则不进行计算
  1612. return false;
  1613. }
  1614. // 从缓存获取分页有收入的会员信息
  1615. $allData = CalcBonusBS::findUseDbCalc()
  1616. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1617. ->groupBy('USER_ID')
  1618. ->offset($offset)
  1619. ->limit($this->_limit)
  1620. ->asArray()
  1621. ->all();
  1622. if ($allData) {
  1623. foreach ($allData as $user) {
  1624. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  1625. if ($fxStatus) {
  1626. //扣除相应的复消积分和管理费
  1627. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_MNT']);
  1628. // 把对碰后的奖金存入缓存中
  1629. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_MNT', $user['ORI_BONUS_MNT']);
  1630. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1631. $manageTax = $deductData['manageTax']; // 管理费
  1632. $point = $deductData['reConsumePoints'];// 复消积分
  1633. } else {
  1634. // 如果未满足月消费,则不扣除管理费和复消积分了
  1635. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_MNT', $user['ORI_BONUS_MNT']);
  1636. $realBonusBs = $user['ORI_BONUS_MNT'];
  1637. $manageTax = 0; // 管理费
  1638. $point = 0;// 复消积分
  1639. }
  1640. // 更新蓝星奖金存储过程的实发金额数据
  1641. CalcBonusBS::updateAll(['AMOUNT_MNT' => $realBonusBs, 'MANAGE_TAX_MNT' => $manageTax, 'MNT_RECONSUME_POINTS' => $point],
  1642. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1643. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1644. }
  1645. return $this->calcBonusBsGLCF($offset + $this->_limit);
  1646. }
  1647. unset($allData);
  1648. return true;
  1649. }
  1650. /**
  1651. * 蓝星业绩奖金
  1652. * @param int $offset
  1653. * @return bool
  1654. * @throws \yii\db\Exception
  1655. */
  1656. public function calcBonusBsYJCF(int $offset = 0) {
  1657. if( !$this->_isCalcMonth ) {
  1658. // 不是结算月,则不进行计算
  1659. return false;
  1660. }
  1661. // 从缓存获取分页有收入的会员信息
  1662. $allData = CalcBonusBS::findUseDbCalc()
  1663. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1664. ->groupBy('USER_ID')
  1665. ->offset($offset)
  1666. ->limit($this->_limit)
  1667. ->asArray()
  1668. ->all();
  1669. if ($allData) {
  1670. foreach ($allData as $user) {
  1671. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  1672. if ($fxStatus) {
  1673. //扣除相应的复消积分和管理费
  1674. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']);
  1675. // 把对碰后的奖金存入缓存中
  1676. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_ABBR', $user['ORI_BONUS_ABBR']);
  1677. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1678. $manageTax = $deductData['manageTax']; // 管理费
  1679. $point = $deductData['reConsumePoints'];// 复消积分
  1680. } else {
  1681. // 如果未满足月消费,则不扣除管理费和复消积分了
  1682. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_ABBR', $user['ORI_BONUS_ABBR']);
  1683. $realBonusBs = $user['ORI_BONUS_ABBR'];
  1684. $manageTax = 0; // 管理费
  1685. $point = 0;// 复消积分
  1686. }
  1687. // 更新蓝星奖金存储过程的实发金额数据
  1688. CalcBonusBS::updateAll(['AMOUNT_ABBR' => $realBonusBs, 'MANAGE_TAX_ABBR' => $manageTax, 'ABBR_RECONSUME_POINTS' => $point],
  1689. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1690. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1691. }
  1692. return $this->calcBonusBsYJCF($offset + $this->_limit);
  1693. }
  1694. unset($allData);
  1695. return true;
  1696. }
  1697. /**
  1698. * 报单管理奖
  1699. * @param int $offset
  1700. * @return bool
  1701. * @throws \yii\db\Exception
  1702. */
  1703. public function calcBonusBdGL(int $offset = 0) {
  1704. // 从缓存获取分页有收入的会员信息
  1705. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1706. if ($allData) {
  1707. $insertBonusData = [];
  1708. foreach ($allData as $userId) {
  1709. $bonusUserData = [];
  1710. //会员信息
  1711. $validDeep = 1;
  1712. $this->loopRelationParentDo($userId, function ($parent) use ($userId, &$validDeep, &$bonusUserData) {
  1713. //判断$parent是否有首单团队奖
  1714. $bonusUserId = $parent['PARENT_UID'];
  1715. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1716. $nowDecLevelConfig = $this->_decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1717. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1718. if( $recNum <= 0 ) {
  1719. $maxOddLayer = 0;
  1720. }else if( $recNum == 1 ) {
  1721. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'] ?? 0;
  1722. }else if( $recNum == 2 ) {
  1723. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'] ?? 0;
  1724. }else {
  1725. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'] ?? 0;
  1726. }
  1727. if ( $validDeep < 2 * $maxOddLayer && $validDeep % 2 === 1 ) {//奇数层
  1728. $bonusUserData[] = [
  1729. 'bonusUid' => $parent['PARENT_UID'],
  1730. 'validDeep' => $validDeep
  1731. ];
  1732. }
  1733. $parentBonus = CalcCache::bonus($bonusUserId, $this->_periodNum);
  1734. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {//有效层
  1735. $validDeep += 1;
  1736. }
  1737. unset($parent, $parentBonus, $bonusUserId, $bonusUserBaseInfo, $nowDecLevelConfig, $recNum, $maxOddLayer);
  1738. //超过代数上限则结束
  1739. if ( $validDeep >= 18 ) {
  1740. return self::LOOP_FINISH;
  1741. }
  1742. });
  1743. //发奖
  1744. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1745. $userBonus = CalcCache::bonus($userId, $this->_periodNum);
  1746. foreach ($bonusUserData as $everyData) {
  1747. $bonusUserId = $everyData['bonusUid'];
  1748. //判断 是否本期小市场(安置关系)新增980pv 能否得奖
  1749. if ( !$this->checkSmallMarketPerf($bonusUserId) ) {
  1750. // unset($bonusUserId);
  1751. // continue;
  1752. //得奖人不满足980就找一个满足980的人来得这个奖
  1753. $bonusUserId = $this->getMinBdPvRelationParent($bonusUserId);
  1754. if ( !$bonusUserId ) {
  1755. unset($bonusUserId);
  1756. continue;
  1757. }
  1758. }
  1759. $parentOddPercentValue = floor($everyData['validDeep']/6);
  1760. $parentOddPercentName = sprintf('parentOddPercent_%s', $parentOddPercentValue);
  1761. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1762. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1763. $manageBonus = Tool::formatPrice($userBonus['ORI_BONUS_QY_BD'] * $bonusPercent / 100);
  1764. if ($manageBonus <= 0) continue;
  1765. //总金额限制
  1766. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1767. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1768. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1769. if( $manageBonus <= 0 ) continue;
  1770. //扣除相应的复消积分和管理费
  1771. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1772. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_BD);
  1773. CalcCache::addHasMonthBonusUsers($bonusUserId, $this->_periodNum);
  1774. //管理奖流水
  1775. $insertBonusData[] = [
  1776. 'ID' => SnowFake::instance()->generateId(),
  1777. 'USER_ID' => $bonusUserId,
  1778. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1779. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1780. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1781. 'FROM_USER_ID' => $userId,
  1782. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1783. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  1784. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1785. 'AMOUNT' => $deductData['surplus'],
  1786. 'ORI_BONUS' => $manageBonus,
  1787. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1788. 'MANAGE_TAX' => $deductData['manageTax'],
  1789. 'PERIOD_NUM' => $this->_periodNum,
  1790. 'CALC_YEAR' => $this->_calcYear,
  1791. 'CALC_MONTH' => $this->_calcYearMonth,
  1792. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1793. 'CREATED_AT' => Date::nowTime(),
  1794. 'LOGS' => json_encode([
  1795. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1796. 'validDeep' => $everyData['validDeep'],
  1797. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1798. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1799. 'fromUserId' => $userId,
  1800. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1801. ]),
  1802. ];
  1803. unset($everyData, $bonusUserId, $parentOddPercentValue, $parentOddPercentName, $bonusPercent, $manageBonus, $deductData);
  1804. }
  1805. unset($userId, $validDeep, $userBaseInfo, $userBonus, $bonusUserData);
  1806. }
  1807. if($insertBonusData) CalcBonusGL::batchInsert($insertBonusData);
  1808. unset($allData, $insertBonusData);
  1809. return $this->calcBonusBdGL($offset + $this->_limit);
  1810. }
  1811. unset($allData);
  1812. return true;
  1813. }
  1814. /**
  1815. * 复消管理奖
  1816. * @param int $offset
  1817. * @return bool
  1818. * @throws \yii\db\Exception
  1819. */
  1820. public function calcBonusFxGL(int $offset = 0) {
  1821. $periodNum = $this->_periodNum;
  1822. // 从缓存获取分页有收入的会员信息
  1823. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1824. if ($allData) {
  1825. $insertBonusData = [];
  1826. foreach ($allData as $userId) {
  1827. //会员信息
  1828. $decLevelConfig = $this->_decLevelConfig;
  1829. $netWorkParents = Cache::getAllRelationParents($userId);
  1830. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1831. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1832. foreach ($netWorkParents as $netWorkParent) {
  1833. //自已距顶端的深度减去父级距顶端的深度
  1834. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  1835. if( $diffLayer <= 0) continue;
  1836. //@todo 超过最大代数就跳出本次循环
  1837. //偶数代跳过
  1838. if( $diffLayer % 2 == 0 ) continue;
  1839. // 获取会员的报单级别
  1840. $bonusUserId = $netWorkParent['PARENT_UID'];
  1841. if( !$bonusUserId ) continue;
  1842. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1843. $nowDecLevelConfig = $decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1844. if( !$nowDecLevelConfig ) continue;
  1845. //推荐人数
  1846. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1847. if( $recNum <= 0 ) continue;
  1848. if( $recNum == 1 ) {
  1849. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'];
  1850. }else if( $recNum == 2 ) {
  1851. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'];
  1852. }else {
  1853. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'];
  1854. }
  1855. //超过代数上限也跳过
  1856. if( $diffLayer >= 2 * $maxOddLayer ) continue;
  1857. // $everyLayer = floor(2 * $nowDecLevelConfig['GL_ODD_DEEP_THREE'] / $nowDecLevelConfig['SORT']);
  1858. //多少代
  1859. // $theLayer = $diffLayer % $everyLayer;
  1860. // $theLayerLevel = floor($diffLayer/$everyLayer);
  1861. $theLayerLevel = floor($diffLayer/6);
  1862. $parentOddPercentName = sprintf('parentOddPercent_%s', $theLayerLevel);
  1863. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1864. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1865. // 从缓存中获取会员的收入信息
  1866. $incomeBonus = CalcCache::bonus($userId, $periodNum);
  1867. $parentOddIncomeFromName = sprintf('parentOddIncomeFrom_%s', $theLayerLevel);
  1868. $parentOddIncomeFrom = explode(',', $this->_sysConfig[$parentOddIncomeFromName]['VALUE']);
  1869. $parentOddIncome = 0;
  1870. foreach ($parentOddIncomeFrom as $incomeType) {
  1871. if( $incomeType === 'XF' ) {
  1872. $incomeTypeName = sprintf('ORI_BONUS_%s', $incomeType);
  1873. }else {
  1874. $incomeTypeName = sprintf('ORI_BONUS_%s_FX', $incomeType);
  1875. }
  1876. $incomeTypeValue = $incomeBonus[$incomeTypeName] ?? 0;
  1877. $parentOddIncome += $incomeTypeValue;
  1878. unset($incomeType, $incomeTypeName, $incomeTypeValue);
  1879. }
  1880. unset($parentOddIncomeFrom);
  1881. $manageBonus = Tool::formatPrice($parentOddIncome * $bonusPercent / 100);
  1882. if ($manageBonus <= 0) continue;
  1883. //总金额限制
  1884. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1885. if( $manageBonus <= 0 ) continue;
  1886. //扣除相应的复消积分和管理费
  1887. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1888. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_FX);
  1889. //管理奖流水
  1890. $insertBonusData[] = [
  1891. 'ID' => SnowFake::instance()->generateId(),
  1892. 'USER_ID' => $bonusUserId,
  1893. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1894. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1895. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1896. 'FROM_USER_ID' => $userId,
  1897. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1898. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1899. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1900. 'AMOUNT' => $deductData['surplus'],
  1901. 'ORI_BONUS' => $manageBonus,
  1902. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1903. 'MANAGE_TAX' => $deductData['manageTax'],
  1904. 'PERIOD_NUM' => $this->_periodNum,
  1905. 'CALC_YEAR' => $this->_calcYear,
  1906. 'CALC_MONTH' => $this->_calcYearMonth,
  1907. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1908. 'CREATED_AT' => Date::nowTime(),
  1909. 'LOGS' => json_encode([
  1910. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1911. $parentOddIncomeFromName => $this->_sysConfig[$parentOddIncomeFromName]['VALUE'],
  1912. 'decLevelConfig' => $nowDecLevelConfig,
  1913. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1914. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1915. 'fromUserId' => $userId,
  1916. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1917. ]),
  1918. ];
  1919. unset($netWorkParent, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $bonusPercent, $nowDecLevelConfig, $manageBonus, $incomeBonus, $maxOddLayer, $theLayerLevel, $recNum, $parentOddPercentName, $parentOddIncomeFromName, $deductData);
  1920. }
  1921. unset($userId, $userBaseInfo, $userEmpLevel, $netWorkParents, $decLevelConfig);
  1922. }
  1923. CalcBonusGL::batchInsert($insertBonusData);
  1924. unset($allData, $insertBonusData);
  1925. return $this->calcBonusFxGL($offset + $this->_limit);
  1926. }
  1927. unset($allData);
  1928. return true;
  1929. }
  1930. /**
  1931. * 结算荣衔奖第一步
  1932. * @param int $offset
  1933. * @return bool
  1934. * @throws \yii\db\Exception
  1935. * @throws \Exception
  1936. */
  1937. public function calcBonusYCStepOne(int $offset = 0) {
  1938. // 月结,如果不是月结点,则直接退出
  1939. if (!$this->_isCalcMonth) {
  1940. return true;
  1941. }
  1942. echo sprintf("时间:[%s]荣衔奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1943. //从缓存获取分页有本月业绩的会员
  1944. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  1945. if ($allData) {
  1946. $maxLevelId = EmployLevel::SHX_ZJ_LEVEL_ID;
  1947. $maxLevel = $this->_empLevelConfig[$maxLevelId];
  1948. $maxLevelPercent = $maxLevel['RX_PERCENT'] ?? 0;
  1949. unset($maxLevelId, $maxLevel);
  1950. foreach ($allData as $userId) {
  1951. // echo sprintf("时间:[%s]荣衔奖,当前用户ID为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $userId);
  1952. $nowMonthPerf = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1953. $monthPvPcs = $nowMonthPerf['PV_PCS'] ?? 0;
  1954. if( $monthPvPcs <= 0 ) continue;
  1955. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1956. //个人业绩的奖金
  1957. $empLevelId = $userBaseInfo['EMP_LV'];
  1958. $empLevel = $this->_empLevelConfig[$empLevelId];
  1959. $rxPercent = $empLevel['RX_PERCENT'] ?? 0;
  1960. $empBonus = $monthPvPcs * $rxPercent / 100;
  1961. if( $empBonus > 0 ) {
  1962. //给本人添加聘级比例
  1963. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $rxPercent);
  1964. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1965. CalcCache::saveYCBonusList($userId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1966. CalcCache::addHasYcBonusUsers($userId, $this->_periodNum);
  1967. //达到最大的比例其它人就得不到了
  1968. if( $rxPercent >= $maxLevelPercent ) continue;
  1969. }
  1970. unset($empLevelId, $empLevel, $rxPercent, $empBonus);
  1971. //获取所有的父级
  1972. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $monthPvPcs, $maxLevelPercent) {
  1973. $bonusUserId = $parent['PARENT_UID'];
  1974. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  1975. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1976. $parentEmpLevelId = $bonusUserInfo['EMP_LV'];
  1977. $parentEmpLevel = $this->_empLevelConfig[$parentEmpLevelId];
  1978. $parentRxPercent = $parentEmpLevel['RX_PERCENT'] ?? 0;
  1979. $cacheMaxPercent = CalcCache::ycMaxBonusPercent($userId, $this->_periodNum);
  1980. $diffPercent = $parentRxPercent - $cacheMaxPercent;
  1981. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  1982. $empBonus = $monthPvPcs * $diffPercent / 100;
  1983. if( $empBonus <= 0 ) return self::LOOP_CONTINUE;
  1984. //给本人添加聘级比例
  1985. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $parentRxPercent);
  1986. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1987. CalcCache::saveYCBonusList($bonusUserId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1988. CalcCache::addHasYcBonusUsers($bonusUserId, $this->_periodNum);
  1989. unset($cacheMaxPercent, $diffPercent, $parentEmpLevel, $bonusUserId, $bonusUserInfo, $parentEmpLevelId, $empBonus);
  1990. //达到最大的比例就不在向上找了
  1991. if( $parentRxPercent >= $maxLevelPercent ) return self::LOOP_FINISH;
  1992. unset($parentRxPercent);
  1993. });
  1994. unset($nowMonthPerf, $monthPvPcs);
  1995. }
  1996. unset($allData, $maxLevelPercent);
  1997. $this->calcBonusYCStepOne($offset + $this->_limit);
  1998. }
  1999. unset($periodNum, $allData);
  2000. return true;
  2001. }
  2002. /**
  2003. * 结算荣衔奖第二步
  2004. * @param int $offset
  2005. * @return bool
  2006. * @throws \yii\db\Exception
  2007. */
  2008. public function calcBonusYCStepTwo(int $offset = 0) {
  2009. // 月结,如果不是月结点,则直接退出
  2010. if (!$this->_isCalcMonth) {
  2011. return true;
  2012. }
  2013. echo sprintf("时间:[%s]荣衔奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2014. $allData = CalcCache::getHasYcBonusUsers($this->_periodNum, $offset, $this->_limit);
  2015. if ($allData) {
  2016. $insertBonusData = [];
  2017. foreach ($allData as $userId) {
  2018. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  2019. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2020. unset($monthPerfData);
  2021. if( !$fxPvStatus ) continue;
  2022. unset($fxPvStatus);
  2023. $ycBonusData = CalcCache::getYCBonusList($userId, $this->_periodNum);
  2024. if( !$ycBonusData ) continue;
  2025. $empBonus = $ycBonusData['empBonus'] ?? 0;
  2026. if( $empBonus <=0 ) continue;
  2027. //总金额限制
  2028. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2029. $empBonus = $this->bonusTotalLimit($empBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  2030. if( $empBonus <= 0 ) continue;
  2031. //扣除相应的复消积分和管理费
  2032. $empBonus = Tool::formatPrice($empBonus);
  2033. $deductData = $this->deduct($userId, $empBonus);
  2034. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_YC', $empBonus, $deductData);
  2035. $empLevelId = $userBaseInfo['EMP_LV'];
  2036. $insertBonusData[] = [
  2037. 'ID' => SnowFake::instance()->generateId(),
  2038. 'USER_ID' => $userId,
  2039. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2040. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2041. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2042. 'FROM_USER_ID' => $userId,
  2043. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2044. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2045. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2046. 'DEEP' => 0,
  2047. 'AMOUNT' => $deductData['surplus'],
  2048. 'ORI_BONUS' => $empBonus,
  2049. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2050. 'MANAGE_TAX' => $deductData['manageTax'],
  2051. 'BONUS_TYPE' => CalcBonusYC::BONUS_TYPE_YC,
  2052. 'PERIOD_NUM' => $this->_periodNum,
  2053. 'CALC_MONTH' => $this->_calcYearMonth,
  2054. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2055. 'CREATED_AT' => Date::nowTime(),
  2056. 'LOGS' => json_encode([
  2057. 'empLevelId' => $empLevelId,
  2058. ])
  2059. // 'FROM_DATA' => json_encode($ycBonusData['fromData']),
  2060. ];
  2061. unset($ycBonusData, $deductData);
  2062. //如果是总监以上拿平级2代
  2063. $empLevel = $this->_empLevelConfig[$empLevelId];
  2064. $userEmpLevelSort = $empLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  2065. // if( $userEmpLevelSort >= EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {}
  2066. $newInsertBonusData = $this->calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $empBonus);
  2067. $insertBonusData = array_merge($insertBonusData, $newInsertBonusData);
  2068. unset($newInsertBonusData);
  2069. unset($userId, $userBaseInfo, $empLevel, $empLevelId, $userEmpLevelSort, $empBonus);
  2070. }
  2071. CalcBonusYC::batchInsert($insertBonusData);
  2072. unset($insertBonusData, $allData);
  2073. $this->calcBonusYCStepTwo($offset + $this->_limit);
  2074. }
  2075. unset($allData);
  2076. return true;
  2077. }
  2078. /**
  2079. * VIP奖
  2080. * @param int $offset
  2081. * @return bool
  2082. * @throws \yii\db\Exception
  2083. */
  2084. public function calcBonusVIP(int $offset = 0)
  2085. {
  2086. // 月结,如果不是月结点,则直接退出
  2087. if (!$this->_isCalcMonth) {
  2088. return true;
  2089. }
  2090. echo sprintf("时间:[%s]VIP奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2091. //从缓存获取分页有本月业绩的会员
  2092. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2093. if ($allData) {
  2094. $insertBonusData = [];
  2095. foreach ($allData as $userId) {
  2096. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2097. //条件一:级别必须为VIP
  2098. $isVip = false;
  2099. if( $userBaseInfo['DEC_LV'] === DeclarationLevel::VIP_LEVEL_ID ) {
  2100. $isVip= true;
  2101. }
  2102. if( $this->_sysConfig['vipBonusGoldDecLevel']['VALUE'] && $userBaseInfo['DEC_LV'] === DeclarationLevel::JIN_ZUAN_LEVEL_ID ) {
  2103. $isVip = true;
  2104. }
  2105. if ( !$isVip ) {
  2106. unset($userId, $userBaseInfo);
  2107. continue;
  2108. }
  2109. //判断其它两个条件,满足则计算月团队奖,不满足则留下大区剩余业绩,清除其它区的剩余业绩
  2110. //获取本月的业绩
  2111. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  2112. // 从缓存中获取会员的上期结余业绩信息
  2113. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2114. // 本期 + 上期结余
  2115. $perfArr = [
  2116. 'VIP_SURPLUS_1L_ZC' => $monthPerfData['VIP_PV_1L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_1L_ZC'],
  2117. 'VIP_SURPLUS_2L_ZC' => $monthPerfData['VIP_PV_2L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_2L_ZC'],
  2118. 'VIP_SURPLUS_3L_ZC' => $monthPerfData['VIP_PV_3L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_3L_ZC'],
  2119. 'VIP_SURPLUS_4L_ZC' => $monthPerfData['VIP_PV_4L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_4L_ZC'],
  2120. 'VIP_SURPLUS_5L_ZC' => $monthPerfData['VIP_PV_5L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_5L_ZC'],
  2121. ];
  2122. //条件二:本月复消金额达到300元
  2123. //fix-2021-03-26修改 条件二:本月个人复消PV300
  2124. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2125. unset($monthPerfData, $pervSurplusPerf);
  2126. if( !$fxPvStatus ) {
  2127. $this->dealWithMaxPerf($userId, $perfArr);
  2128. unset($userBaseInfo, $userId, $perfArr, $fxPvStatus);
  2129. continue;
  2130. }
  2131. //条件三:其它市场业绩
  2132. $isCanOtherDepartPerf = $this->_isCanVipBonusOtherAreaPerf($perfArr);
  2133. if ( !$isCanOtherDepartPerf ) {
  2134. $this->dealWithMaxPerf($userId, $perfArr);
  2135. unset($userBaseInfo, $userId, $perfArr, $isCanOtherDepartPerf);
  2136. continue;
  2137. }
  2138. $oriPerfArr = [
  2139. 'perfArr' => $perfArr,
  2140. 'touchBonus' => 0,
  2141. ];
  2142. // 对碰
  2143. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $this->_sysConfig['vipBonusPercent']['VALUE']/100);
  2144. unset($oriPerfArr, $perfArr);
  2145. $touchPerfArr = [];
  2146. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  2147. $touchPerfArr[$keyR] = $perfR;
  2148. }
  2149. // 对碰完成后把结余的业绩存入本期业绩缓存中
  2150. // CalcCache::nowPeriodPerf($userId, $this->_periodNum, $touchPerfArr);
  2151. //更新数据库
  2152. PerfMonth::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2153. 'USER_ID' => $userId,
  2154. 'CALC_MONTH' => $this->_calcYearMonth,
  2155. ]);
  2156. if ($touchBonusArr['touchBonus'] <= 0) continue;
  2157. $vipBonus = $touchBonusArr['touchBonus'];
  2158. //判断级别上限
  2159. $vipBonus = $this->declarationLevelCap($vipBonus, $userId, $userBaseInfo['DEC_LV']);
  2160. if( $vipBonus <= 0 ) continue;
  2161. $vipBonus = $this->bonusTotalLimit($vipBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  2162. if( $vipBonus <= 0 ) continue;
  2163. //扣除相应的复消积分和管理费
  2164. $deductData = $this->deduct($userId, $vipBonus);
  2165. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_VIP', $vipBonus, $deductData);
  2166. $insertBonusData[] = [
  2167. 'ID' => SnowFake::instance()->generateId(),
  2168. 'USER_ID' => $userId,
  2169. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2170. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2171. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2172. 'FROM_USER_ID' => $userId,
  2173. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2174. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2175. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2176. 'AMOUNT' => $deductData['surplus'],
  2177. 'ORI_BONUS' => $vipBonus,
  2178. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2179. 'MANAGE_TAX' => $deductData['manageTax'],
  2180. 'PERIOD_NUM' => $this->_periodNum,
  2181. 'CALC_YEAR' => $this->_calcYear,
  2182. 'CALC_MONTH' => $this->_calcYearMonth,
  2183. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2184. 'CREATED_AT' => Date::nowTime(),
  2185. 'LOGS' => json_encode($touchPerfArr)
  2186. ];
  2187. unset($userBaseInfo, $userId, $perfArr, $fxAmountStatus, $isCanOtherDepartPerf, $touchBonusArr, $touchPerfArr, $vipBonus, $deductData);
  2188. }
  2189. if( $insertBonusData ) CalcBonusVIP::batchInsert($insertBonusData);
  2190. unset($insertBonusData, $allData);
  2191. $this->calcBonusVIP($offset + $this->_limit);
  2192. }
  2193. unset($allData);
  2194. return true;
  2195. }
  2196. /**
  2197. * 见习达标奖
  2198. * @param int $offset
  2199. * @return bool
  2200. * @throws \yii\db\Exception
  2201. */
  2202. public function calcBonusStandard(int $offset = 0)
  2203. {
  2204. // 月结,如果不是月结点,则直接退出
  2205. if (!$this->_isCalcMonth) {
  2206. return true;
  2207. }
  2208. echo sprintf("时间:[%s]见习达标奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2209. //从缓存获取分页有本月业绩的会员
  2210. $allData = CalcCache::getHasStandardMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2211. if ($allData) {
  2212. $minSaleCondition = $this->_jxStandardMinSaleCondition();
  2213. $insertBonusData = [];
  2214. foreach ($allData as $userId) {
  2215. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2216. //条件一:聘级必须为见习主任
  2217. if( $userBaseInfo['EMP_LV'] != EmployLevel::JX_ZR_LEVEL_ID ) {
  2218. unset($userId, $userBaseInfo);
  2219. continue;
  2220. }
  2221. $standardMonthPerf = CalcCache::nowStandardMonthPerf($userId, $this->_periodNum);
  2222. $totalAmount = $standardMonthPerf['AMOUNT_PCS'] + $standardMonthPerf['AMOUNT_PSS'];
  2223. if( $totalAmount < $minSaleCondition ) {
  2224. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2225. continue;
  2226. }
  2227. $standardBonus = $this->_jxStandardAwardAmount($totalAmount);
  2228. if( $standardBonus <= 0 ) {
  2229. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount, $standardBonus);
  2230. continue;
  2231. }
  2232. CalcCache::standardBonus($userId, $this->_periodNum, $standardBonus);
  2233. $insertBonusData[] = [
  2234. 'ID' => SnowFake::instance()->generateId(),
  2235. 'USER_ID' => $userId,
  2236. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2237. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2238. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2239. 'FROM_USER_ID' => $userId,
  2240. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2241. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2242. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2243. 'AMOUNT' => $standardBonus,
  2244. 'ORI_BONUS' => $standardBonus,
  2245. 'RECONSUME_POINTS' => 0,
  2246. 'MANAGE_TAX' => 0,
  2247. 'PERIOD_NUM' => $this->_periodNum,
  2248. 'CALC_YEAR' => $this->_calcYear,
  2249. 'CALC_MONTH' => $this->_calcYearMonth,
  2250. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2251. 'CREATED_AT' => Date::nowTime(),
  2252. 'LOGS' => json_encode($standardMonthPerf)
  2253. ];
  2254. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2255. }
  2256. if( $insertBonusData ) CalcBonusStandard::batchInsert($insertBonusData);
  2257. unset($insertBonusData, $minSaleCondition, $insertBonusData, $allData);
  2258. $this->calcBonusStandard($offset + $this->_limit);
  2259. }
  2260. unset($allData);
  2261. return true;
  2262. }
  2263. /**
  2264. * === 运算相关的辅助功能开始 ===
  2265. */
  2266. /**
  2267. * 获取上2代的得奖人信息
  2268. * @param $userId
  2269. * @param $validDeep
  2270. * @param $shareBonusOne
  2271. * @return array
  2272. */
  2273. protected function gxLastTwoBonusData($userId, $validDeep, $shareBonusOne) {
  2274. $bonusUserList = CalcCache::getShareBonusOneRelation($userId, $this->_periodNum, $validDeep);
  2275. if( !$bonusUserList ) {
  2276. unset($bonusUserList);
  2277. return [];
  2278. }
  2279. $bonusUserData = [];
  2280. $bonusUserTotalPerf = 0;
  2281. $bonusUserRealList = [];
  2282. foreach ($bonusUserList as $bonusUserId) {
  2283. //查看该用户的个人业绩和团队新增业绩总和
  2284. $nowPeriodData = CalcCache::nowPeriodPerf($bonusUserId, $this->_periodNum);
  2285. //本期新增业绩
  2286. $bonusUserPerf = $nowPeriodData['PV_PCS'] + $nowPeriodData['PV_PSS'];
  2287. if ($bonusUserPerf <= 0) {
  2288. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2289. continue;
  2290. }
  2291. $bonusUserTotalPerf += $bonusUserPerf;
  2292. $bonusUserRealList[] = [
  2293. 'bonusUserId' => $bonusUserId,
  2294. 'bonusUserPerf' => $bonusUserPerf,
  2295. ];
  2296. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2297. }
  2298. unset($bonusUserList);
  2299. foreach ($bonusUserRealList as $everyData) {
  2300. $bonusUserData[] = [
  2301. 'bonusUid' => $everyData['bonusUserId'],
  2302. 'bonus' => $shareBonusOne * $everyData['bonusUserPerf'] / $bonusUserTotalPerf,
  2303. 'validMinPv' => false
  2304. ];
  2305. unset($everyData);
  2306. }
  2307. unset($bonusUserRealList);
  2308. return $bonusUserData;
  2309. }
  2310. /**
  2311. * 获取达标奖的金额
  2312. * @param $amount
  2313. * @return int|mixed
  2314. */
  2315. private function _jxStandardAwardAmount($amount) {
  2316. // $jxStandardConfig = Json::decode($this->_sysConfig['jxStandard']['VALUE']);
  2317. // $awardAmount = 0;
  2318. // foreach ($jxStandardConfig as $everyData) {
  2319. // if( $amount >= $everyData['salesCondition'] ) {
  2320. // $awardAmount = $everyData['awardAmount'];
  2321. // }
  2322. // unset($everyData);
  2323. // }
  2324. //
  2325. // return $awardAmount;
  2326. $realTimes = floor($amount/self::JX_STANDARD_BASE_AMOUNT);
  2327. $calcTimes = min($realTimes, self::JX_STANDARD_MAX_TIMES);
  2328. $awardAmount = $calcTimes * self::JX_STANDARD_BASE_AMOUNT * self::JX_STANDARD_BONUS_PERCENT / 100;
  2329. unset($amount, $realTimes, $calcTimes);
  2330. return $awardAmount;
  2331. }
  2332. /**
  2333. * 获取达标奖的最小条件
  2334. * @return int|mixed
  2335. */
  2336. private function _jxStandardMinSaleCondition() {
  2337. return self::JX_STANDARD_BASE_AMOUNT;
  2338. }
  2339. /**
  2340. * 处理只留最大业绩的逻辑
  2341. * @param $userId
  2342. * @param $perfArr
  2343. */
  2344. private function dealWithMaxPerf($userId, $perfArr) {
  2345. $maxPerf = max($perfArr);
  2346. $hitMaxPerf = false;
  2347. foreach ($perfArr as $key=>$everyPerf) {
  2348. if( !$hitMaxPerf && $everyPerf == $maxPerf ) {
  2349. $hitMaxPerf = true;
  2350. }else {
  2351. $perfArr[$key] = 0;
  2352. }
  2353. unset($key, $everyPerf);
  2354. }
  2355. //更新数据库
  2356. PerfMonth::updateAll($perfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2357. 'USER_ID' => $userId,
  2358. 'CALC_MONTH' => $this->_calcYearMonth
  2359. ]);
  2360. unset($userId, $maxPerf, $hitMaxPerf, $perfArr);
  2361. }
  2362. /**
  2363. * VIP奖其它市场的报单业绩是否能满足
  2364. * @param $perfArr
  2365. * @return bool
  2366. */
  2367. private function _isCanVipBonusOtherAreaPerf($perfArr) {
  2368. $maxAreaPerf = max($perfArr);
  2369. $totalAreaPerf = array_sum($perfArr);
  2370. $diffAreaPerf = strval($totalAreaPerf - $maxAreaPerf);
  2371. if( $diffAreaPerf >= $this->_sysConfig['vipBonusOtherDepartPvCondition']['VALUE'] ) {
  2372. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2373. return true;
  2374. }else {
  2375. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2376. return false;
  2377. }
  2378. }
  2379. /**
  2380. * 给父级计算的荣衔奖
  2381. * @param $userId
  2382. * @param $userBaseInfo
  2383. * @param $userEmpLevelSort
  2384. * @param $qyBonus
  2385. * @return array
  2386. */
  2387. public function calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus) {
  2388. if( $userEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return [];
  2389. $validDeep = 0;
  2390. $bonusDataList = [];
  2391. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $userEmpLevelSort, &$validDeep, &$bonusDataList) {
  2392. $bonusUserId = $parent['PARENT_UID'];
  2393. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2394. $bonusUserEmpLevel = $this->_empLevelConfig[$bonusUserInfo['EMP_LV']];
  2395. $bonusUserEmpLevelSort = $bonusUserEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  2396. unset($bonusUserInfo, $bonusUserEmpLevel);
  2397. //必须是有聘级的用户
  2398. if( $bonusUserEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return self::LOOP_CONTINUE;
  2399. //超过级别就停止
  2400. if( $bonusUserEmpLevelSort > $userEmpLevelSort) return self::LOOP_FINISH;
  2401. //级别小于就跳过
  2402. if( $bonusUserEmpLevelSort < $userEmpLevelSort ) return self::LOOP_CONTINUE;
  2403. $validDeep++;
  2404. array_push($bonusDataList, [
  2405. 'bonusUserId' => $bonusUserId,
  2406. 'bonusType' => $validDeep,
  2407. ]);
  2408. if( $validDeep >= 2 ) return self::LOOP_FINISH;
  2409. unset($bonusUserId, $bonusUserEmpLevelSort);
  2410. });
  2411. $insertBonusData = [];
  2412. foreach ($bonusDataList as $bonusUserData) {
  2413. $bonusUserId = $bonusUserData['bonusUserId'];
  2414. $bonusMonthPerfData = CalcCache::nowMonthPerf($bonusUserId, $this->_periodNum);
  2415. $fxPvStatus = $bonusMonthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2416. unset($bonusMonthPerfData);
  2417. if( !$fxPvStatus ) continue;
  2418. unset($fxPvStatus);
  2419. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2420. $bonusEmpLevelId = $bonusUserInfo['EMP_LV'];
  2421. $newEmpBonus = Tool::formatPrice($qyBonus * $this->_sysConfig['sameEmpLevelPercent']['VALUE'] / 100);
  2422. //总金额限制
  2423. $newEmpBonus = $this->bonusTotalLimit($newEmpBonus, $bonusUserId, $bonusUserInfo['REC_NUM'], $bonusUserInfo['ZC_AMOUNT']);
  2424. if( $newEmpBonus <= 0 ) continue;
  2425. //扣除相应的复消积分和管理费
  2426. $newEmpBonus = Tool::formatPrice($newEmpBonus);
  2427. $deductData = $this->deduct($bonusUserId, $newEmpBonus);
  2428. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_YC_EXTRA', $newEmpBonus, $deductData);
  2429. // 把育成津贴追加到育成津贴结算表,以便奖金追溯育成津贴用
  2430. $insertBonusData[] = [
  2431. 'ID' => SnowFake::instance()->generateId(),
  2432. 'USER_ID' => $bonusUserId,
  2433. 'LAST_DEC_LV' => $bonusUserInfo['DEC_LV'],
  2434. 'LAST_EMP_LV' => $bonusUserInfo['EMP_LV'],
  2435. 'LAST_STATUS' => $bonusUserInfo['STATUS'],
  2436. 'FROM_USER_ID' => $userId,
  2437. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2438. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2439. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2440. 'DEEP' => 1,
  2441. 'AMOUNT' => $deductData['surplus'],
  2442. 'ORI_BONUS' => $newEmpBonus,
  2443. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2444. 'MANAGE_TAX' => $deductData['manageTax'],
  2445. 'BONUS_TYPE' => $bonusUserData['bonusType'],
  2446. 'PERIOD_NUM' => $this->_periodNum,
  2447. 'CALC_MONTH' => $this->_calcYearMonth,
  2448. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2449. 'CREATED_AT' => Date::nowTime(),
  2450. 'LOGS' => json_encode([
  2451. 'empLevelId' => $bonusEmpLevelId,
  2452. 'qyBonus' => $qyBonus,
  2453. 'sameEmpLevelPercent' => $this->_sysConfig['sameEmpLevelPercent']['VALUE'],
  2454. ]),
  2455. ];
  2456. unset($newEmpBonus, $bonusEmpLevelId, $bonusUserId, $bonusUserInfo, $deductData);
  2457. }
  2458. unset($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus, $bonusDataList);
  2459. return $insertBonusData;
  2460. }
  2461. /**
  2462. * 对碰
  2463. * @param array $oriPerfArr
  2464. * @param array $perfArr
  2465. * @param $percent
  2466. * @param $loopTimes
  2467. * @return array
  2468. */
  2469. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  2470. $resultArr = $oriPerfArr;
  2471. foreach ($perfArr as $keyT => $perfT) {
  2472. if ($perfT <= 0) {
  2473. unset($perfArr[$keyT]);
  2474. }
  2475. }
  2476. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2477. arsort($perfArr, SORT_NUMERIC);
  2478. $onePerf = null;
  2479. $oneKey = null;
  2480. // $touchSurplusAmount = 0;
  2481. $touchAmount = 0;
  2482. // 前两个进行对碰
  2483. foreach ($perfArr as $key => $perf) {
  2484. if ($onePerf === null) {
  2485. $oneKey = $key;
  2486. $onePerf = $perf;
  2487. } else {
  2488. $touchSurplusAmount = $perf - $onePerf;
  2489. if ($touchSurplusAmount > 0) {
  2490. unset($perfArr[$oneKey]);
  2491. $resultArr['perfArr'][$oneKey] = 0;
  2492. $perfArr[$key] = $touchSurplusAmount;
  2493. $touchAmount = $onePerf;
  2494. } elseif ($touchSurplusAmount < 0) {
  2495. unset($perfArr[$key]);
  2496. $resultArr['perfArr'][$key] = 0;
  2497. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2498. $touchAmount = $perf;
  2499. } else {
  2500. unset($perfArr[$oneKey], $perfArr[$key]);
  2501. $resultArr['perfArr'][$oneKey] = 0;
  2502. $resultArr['perfArr'][$key] = 0;
  2503. $touchAmount = $perf;
  2504. }
  2505. break;
  2506. }
  2507. }
  2508. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2509. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2510. }*/
  2511. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  2512. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2513. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2514. // } else {
  2515. // $resultArr['touchBonus'] += $touchBonus;
  2516. // }
  2517. $resultArr['touchBonus'] += $touchBonus;
  2518. foreach ($perfArr as $keyR => $perfR) {
  2519. $resultArr['perfArr'][$keyR] = $perfR;
  2520. }
  2521. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  2522. }
  2523. return $resultArr;
  2524. }
  2525. /**
  2526. * 对碰
  2527. * @param array $oriPerfArr
  2528. * @param array $perfArr
  2529. * @param $decLevel
  2530. * @param $decLevelConfig
  2531. * @param $loopTimes
  2532. * @return array
  2533. */
  2534. public function touchPerfBefore(array $oriPerfArr, array $perfArr, $decLevel, $decLevelConfig, $loopTimes=1) {
  2535. $resultArr = $oriPerfArr;
  2536. foreach ($perfArr as $keyT => $perfT) {
  2537. if ($perfT <= 0) {
  2538. unset($perfArr[$keyT]);
  2539. }
  2540. }
  2541. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2542. $nowDecLevelConfig = $decLevelConfig[$decLevel];
  2543. arsort($perfArr, SORT_NUMERIC);
  2544. $onePerf = null;
  2545. $oneKey = null;
  2546. // $touchSurplusAmount = 0;
  2547. $touchAmount = 0;
  2548. // 前两个进行对碰
  2549. foreach ($perfArr as $key => $perf) {
  2550. if ($onePerf === null) {
  2551. $oneKey = $key;
  2552. $onePerf = $perf;
  2553. } else {
  2554. $touchSurplusAmount = $perf - $onePerf;
  2555. if ($touchSurplusAmount > 0) {
  2556. unset($perfArr[$oneKey]);
  2557. $resultArr['perfArr'][$oneKey] = 0;
  2558. $perfArr[$key] = $touchSurplusAmount;
  2559. $touchAmount = $onePerf;
  2560. } elseif ($touchSurplusAmount < 0) {
  2561. unset($perfArr[$key]);
  2562. $resultArr['perfArr'][$key] = 0;
  2563. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2564. $touchAmount = $perf;
  2565. } else {
  2566. unset($perfArr[$oneKey], $perfArr[$key]);
  2567. $resultArr['perfArr'][$oneKey] = 0;
  2568. $resultArr['perfArr'][$key] = 0;
  2569. $touchAmount = $perf;
  2570. }
  2571. break;
  2572. }
  2573. }
  2574. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2575. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2576. }*/
  2577. $touchBonus = Tool::formatPrice($touchAmount * 2 / 100);
  2578. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2579. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2580. // } else {
  2581. // $resultArr['touchBonus'] += $touchBonus;
  2582. // }
  2583. $resultArr['touchBonus'] += $touchBonus;
  2584. foreach ($perfArr as $keyR => $perfR) {
  2585. $resultArr['perfArr'][$keyR] = $perfR;
  2586. }
  2587. return $this->touchPerfBefore($resultArr, $perfArr, $decLevel, $decLevelConfig, $loopTimes+1);
  2588. }
  2589. return $resultArr;
  2590. }
  2591. /**
  2592. * 循环父级并执行回调函数
  2593. * @param $userId
  2594. * @param callable $callbackFunc
  2595. * @param int $offset
  2596. * @return bool
  2597. */
  2598. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2599. $allParents = Cache::getAllNetworkParents($userId);
  2600. $allData = array_slice($allParents, $offset, $this->_limit);
  2601. unset($allParents);
  2602. if ($allData) {
  2603. foreach ($allData as $data) {
  2604. $funcResult = $callbackFunc($data);
  2605. if ($funcResult === self::LOOP_FINISH) {
  2606. return true;
  2607. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2608. continue;
  2609. }
  2610. unset($data, $funcResult);
  2611. }
  2612. unset($allData);
  2613. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2614. }
  2615. return true;
  2616. }
  2617. /**
  2618. * 循环推荐网络的父级
  2619. * @param $userId
  2620. * @param callable $callbackFunc
  2621. * @param int $offset
  2622. * @return bool
  2623. */
  2624. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2625. $allParents = Cache::getAllRelationParents($userId);
  2626. $allData = array_slice($allParents, $offset, $this->_limit);
  2627. unset($allParents);
  2628. if ($allData) {
  2629. foreach ($allData as $data) {
  2630. $funcResult = $callbackFunc($data);
  2631. if ($funcResult === self::LOOP_FINISH) {
  2632. return true;
  2633. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2634. continue;
  2635. }
  2636. unset($data, $funcResult);
  2637. }
  2638. unset($allData);
  2639. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2640. }
  2641. return true;
  2642. }
  2643. /**
  2644. * 共享奖,判断当期此用户,是否有原报单团队奖金
  2645. */
  2646. public function checkHasOriBonusQyBd($userId) {
  2647. //判断$parent是否有首单团队奖
  2648. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  2649. if( isset($bonus['ORI_BONUS_QY_BD']) && $bonus['ORI_BONUS_QY_BD'] > 0 ) {
  2650. return true;
  2651. }
  2652. return false;
  2653. }
  2654. /**
  2655. * 判断除大市场外 其它市场的报单业绩
  2656. * @param $userId
  2657. * @return bool
  2658. * @throws \yii\db\Exception
  2659. */
  2660. public function checkSmallMarketPerf($userId) {
  2661. // 从缓存中获取会员的上期结余业绩信息
  2662. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2663. //判断出大市场
  2664. $areaNums = [1, 2, 3];
  2665. $maxArea = 0;
  2666. $maxSurplusPerf = 0;
  2667. foreach ($areaNums as $area) {
  2668. $areaKey = sprintf('SURPLUS_%dL', $area);
  2669. if ( isset($pervSurplusPerf[$areaKey]) && $pervSurplusPerf[$areaKey] > $maxSurplusPerf ) {
  2670. $maxArea = $area;
  2671. $maxSurplusPerf = $pervSurplusPerf[$areaKey];
  2672. }
  2673. unset($area, $areaKey);
  2674. }
  2675. unset($pervSurplusPerf, $maxSurplusPerf);
  2676. $otherAreaZcPv = 0;
  2677. $nowPeriodPerf = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  2678. foreach ($areaNums as $area) {
  2679. if( $maxArea > 0 && $area === $maxArea ) continue;
  2680. $areaKey = sprintf('PV_%dL_ZC', $area);
  2681. if ( isset($nowPeriodPerf[$areaKey]) && $nowPeriodPerf[$areaKey] > 0 ) {
  2682. $otherAreaZcPv += $nowPeriodPerf[$areaKey];
  2683. }
  2684. unset($area, $areaKey);
  2685. }
  2686. unset($nowPeriodPerf, $areaNums, $maxArea);
  2687. return $otherAreaZcPv >= self::MIN_BD_PV;
  2688. }
  2689. /**
  2690. * 获取有最小报单PV的用效父级
  2691. * @param $userId
  2692. * @return string
  2693. */
  2694. public function getMinBdPvRelationParent($userId) {
  2695. $validParentId = '';
  2696. $this->loopRelationParentDo($userId, function ($parent) use(&$validParentId) {
  2697. if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2698. $validParentId = $parent['PARENT_UID'];
  2699. unset($parent);
  2700. return self::LOOP_FINISH;
  2701. }
  2702. unset($parent);
  2703. });
  2704. return$validParentId;
  2705. }
  2706. /**
  2707. * 获取有最小报单PV的用效父级 20220407修改成是否此期此用户有原报单团队奖金
  2708. * @param $userId
  2709. * @return string
  2710. */
  2711. public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2712. $validParentId = '';
  2713. $validDeep = 1;
  2714. $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2715. if ( $this->checkHasOriBonusQyBd($parent['PARENT_UID']) ) {
  2716. $validParentId = $parent['PARENT_UID'];
  2717. unset($parent);
  2718. return self::LOOP_FINISH;
  2719. }
  2720. // 只找5层
  2721. if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2722. unset($parent);
  2723. return self::LOOP_FINISH;
  2724. }
  2725. $validDeep += 1;
  2726. unset($parent);
  2727. });
  2728. return$validParentId;
  2729. }
  2730. // public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2731. // $validParentId = '';
  2732. // $validDeep = 1;
  2733. // $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2734. // if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2735. // $validParentId = $parent['PARENT_UID'];
  2736. // unset($parent);
  2737. // return self::LOOP_FINISH;
  2738. // }
  2739. // // 只找5层
  2740. // if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2741. // unset($parent);
  2742. // return self::LOOP_FINISH;
  2743. // }
  2744. // $validDeep += 1;
  2745. // unset($parent);
  2746. // });
  2747. // return$validParentId;
  2748. // }
  2749. /**
  2750. * 按级别的收入上限
  2751. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  2752. * @param $bonus
  2753. * @param $userId
  2754. * @param $declarationLevel
  2755. * @return float
  2756. */
  2757. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2758. $decLevelConfig = $this->_decLevelConfig;
  2759. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2760. unset($decLevelConfig);
  2761. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  2762. if( $bonus <= $maxGetBonus) {
  2763. return $bonus;
  2764. }else {
  2765. return $maxGetBonus;
  2766. }
  2767. }
  2768. // public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2769. // $decLevelConfig = $this->_decLevelConfig;
  2770. // $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2771. // unset($decLevelConfig);
  2772. // //本月往期奖金 这里执行速度可能慢一些,但需求如此没有办法
  2773. // $lastData = CalcCache::lastPeriodMonthCalcBonus($userId, $this->_periodNum, $this->_calcMonth);
  2774. // $lastTotal = $lastData['ORI_BONUS_QY_SUM'] + $lastData['ORI_BONUS_VIP_SUM'];
  2775. // // 从缓存中获取用户的奖金
  2776. // $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2777. // $thisTotal = $bonusData['ORI_BONUS_QY'] + $bonusData['ORI_BONUS_VIP'];
  2778. // $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'] * $this->_calcMonthPeriodNumCount - $lastTotal - $thisTotal;
  2779. // unset($lastData, $lastTotal, $bonusData, $thisTotal);
  2780. // if( $maxGetBonus <= 0 ) return 0.00;
  2781. // if( $bonus <= $maxGetBonus) {
  2782. // return $bonus;
  2783. // }else {
  2784. // return $maxGetBonus;
  2785. // }
  2786. // }
  2787. /**
  2788. * 总奖金限制
  2789. * @param $bonus
  2790. * @param $userId
  2791. * @param $recNum
  2792. * @param $decAmount
  2793. * @return float
  2794. */
  2795. public function bonusTotalLimit($bonus, $userId, $recNum, $decAmount) {
  2796. //@todo 临时不做总奖金限制
  2797. return $bonus;
  2798. if( $recNum >= 3 ) return $bonus;
  2799. $limitAmount = 0;
  2800. switch ($recNum) {
  2801. case 0:
  2802. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalZeroLimit']['VALUE'];
  2803. break;
  2804. case 1:
  2805. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalOneLimit']['VALUE'];
  2806. break;
  2807. case 2:
  2808. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalTwoLimit']['VALUE'];
  2809. break;
  2810. default:
  2811. break;
  2812. }
  2813. // 从缓存中获取用户的奖金
  2814. $lastUserBonus = CalcCache::LastUserBonus($userId, $this->_periodNum);//往期的总金额
  2815. $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2816. $bonusTotal = $bonusData['BONUS_TOTAL'] + $lastUserBonus['BONUS_TOTAL'];
  2817. unset($lastUserBonus, $userId, $recNum, $decAmount, $bonusData);
  2818. if( $bonusTotal >= $limitAmount ) return 0.00;
  2819. if( $bonusTotal + $bonus <= $limitAmount) {
  2820. return $bonus;
  2821. }else {
  2822. return $limitAmount - $bonusTotal;
  2823. }
  2824. }
  2825. /**
  2826. * 扣除复消积分和管理费
  2827. * @param $userId
  2828. * @param $bonus
  2829. * @return array
  2830. * @throws \yii\db\Exception
  2831. */
  2832. public function deductNoReconsumePoints($userId, $bonus): array
  2833. {
  2834. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2835. $surplus = $bonus - $manageTax;
  2836. return [
  2837. 'reConsumePoints' => 0.00,
  2838. 'manageTax' => Tool::formatPrice($manageTax),
  2839. 'surplus' => Tool::formatPrice($surplus),
  2840. ];
  2841. }
  2842. /**
  2843. * 扣除复消积分和管理费
  2844. * @param $userId
  2845. * @param $bonus
  2846. * @return array
  2847. * @throws \yii\db\Exception
  2848. */
  2849. public function deduct($userId, $bonus, $isGx = false) {
  2850. // 如果是共享奖金,则只有最后一期,才扣除管理费?其他三期不扣除管理费.
  2851. //判断是否达到了本月扣除复消的上限
  2852. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  2853. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  2854. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $cacheData['RECONSUME_POINTS_SUM'];
  2855. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  2856. unset($cacheData, $bonusCache);
  2857. $reConsumePoints = 0;
  2858. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  2859. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  2860. // 2022/06/07上限改成10000000 奖金乘以比例 上限1000 // 当月总复消积分 前三期加当前的
  2861. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  2862. }
  2863. unset($reConsumePointsTotal, $reConsumePointsCap);
  2864. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2865. $surplus = $bonus - $reConsumePoints - $manageTax;
  2866. return [
  2867. 'reConsumePoints' => Tool::formatPrice($reConsumePoints),
  2868. 'manageTax' => Tool::formatPrice($manageTax),
  2869. 'surplus' => Tool::formatPrice($surplus),
  2870. ];
  2871. }
  2872. /**
  2873. * 更新百分比并发送
  2874. * @param $percent
  2875. */
  2876. private function _updatePercent($percent) {
  2877. // 把数据写入数据库中
  2878. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  2879. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  2880. }
  2881. /**
  2882. * 把往期有月奖金的会员加到本期有奖会员缓存列表中
  2883. * @param int $offset
  2884. * @return bool
  2885. */
  2886. public function loopMonthBonusUserFromDbToCache($offset = 0) {
  2887. if (!$this->_isCalcMonth) {
  2888. return true;
  2889. }
  2890. $allData = CalcMonthBonusUser::findUseDbCalc()->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->asArray()->all();
  2891. if ( $allData ) {
  2892. foreach ($allData as $everyData) {
  2893. CalcCache::addHasBonusUsers($everyData['USER_ID'], $this->_periodNum);
  2894. unset($everyData);
  2895. }
  2896. unset($allData);
  2897. $this->loopMonthBonusUserFromDbToCache($offset + $this->_limit);
  2898. }
  2899. unset($allData);
  2900. return true;
  2901. }
  2902. /**
  2903. * 把缓存中的月奖用户信息存到数据库
  2904. * @param int $offset
  2905. * @return bool
  2906. * @throws \yii\db\Exception
  2907. */
  2908. public function loopMonthBonusUserToDb($offset = 0) {
  2909. echo sprintf("时间:[%s]月奖会员入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2910. $allData = CalcCache::getHasMonthBonusUsers($this->_periodNum, $offset, $this->_limit);
  2911. if($allData) {
  2912. $insertDataBonusUser = [];
  2913. foreach($allData as $userId){
  2914. $insertDataBonusUser[] = [
  2915. 'ID' => SnowFake::instance()->generateId(),
  2916. 'USER_ID' => $userId,
  2917. 'CALC_MONTH' => $this->_calcYearMonth,
  2918. 'PERIOD_NUM' => $this->_periodNum,
  2919. 'CREATED_AT' => Date::nowTime()
  2920. ];
  2921. unset($userId);
  2922. }
  2923. if( $insertDataBonusUser ) CalcMonthBonusUser::batchInsert($insertDataBonusUser);
  2924. unset($insertDataBonus, $allData);
  2925. return $this->loopMonthBonusUserToDb($offset + $this->_limit);
  2926. }
  2927. unset($allData);
  2928. return true;
  2929. }
  2930. //奖金入库
  2931. /**
  2932. * 循环奖服务奖金会员,并入库
  2933. * @param int $offset
  2934. * @return bool
  2935. * @throws \yii\db\Exception
  2936. */
  2937. public function loopBonusUsers($offset = 0) {
  2938. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2939. // 从缓存列表里面从底层往上倒序获取会员
  2940. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  2941. if($allData){
  2942. $insertDataBonus = [];
  2943. foreach($allData as $userId){
  2944. $tempBonusData = $this->bonusData($userId);
  2945. if(!empty($tempBonusData['result'])){
  2946. $insertDataBonus[] = $tempBonusData['result'];
  2947. }
  2948. unset($userId, $tempBonusData);
  2949. }
  2950. // echo json_encode($insertDataBonus);
  2951. // exit();
  2952. CalcBonus::batchInsert($insertDataBonus);
  2953. unset($insertDataBonus, $allData);
  2954. return $this->loopBonusUsers($offset + $this->_limit);
  2955. }
  2956. return true;
  2957. }
  2958. // 奖金入库完成,将各个奖金计算流水会员聘级,更新成蓝星奖当时计算的聘级
  2959. public function loopCalcBlueEmpLv($offset = 0) {
  2960. $allData = CalcBonus::findUseDbCalc()
  2961. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  2962. ->offset($offset)
  2963. ->limit($this->_limit)
  2964. ->asArray()
  2965. ->all();
  2966. if ($allData) {
  2967. foreach ($allData as $data) {
  2968. // 会员的最新级别,查看上一期,如果无数据,则就是无级别。 上一期到下一期结算月之间,都显示上一期的最新级别。
  2969. if ($this->_isCalcMonth) {
  2970. // 如果是结算月,则直接获取当期结算结果
  2971. $blueBsInfo = CalcBonusBS::findOneAsArray(
  2972. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  2973. [':PERIOD_NUM'=>$this->_periodNum, ':USER_ID'=>$data['USER_ID']]
  2974. );
  2975. if (!empty($blueBsInfo)) {
  2976. $nowBsEmpLv = $blueBsInfo['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
  2977. } else {
  2978. $nowBsEmpLv = EmployLevel::NO_LEVEL_ID;
  2979. }
  2980. } else {
  2981. // 如果不是结算月,则找上一期的结算月数据
  2982. $forwardMonthPeriod = Period::find()
  2983. ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM'=>$this->_periodNum])
  2984. ->orderBy('PERIOD_NUM DESC')
  2985. ->asArray()
  2986. ->one();
  2987. $forwardPeriodNum = $forwardMonthPeriod['PERIOD_NUM'];
  2988. $blueBsInfo = CalcBonusBS::findOneAsArray(
  2989. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  2990. [':PERIOD_NUM'=>$forwardPeriodNum, ':USER_ID'=>$data['USER_ID']]
  2991. );
  2992. if (!empty($blueBsInfo)) {
  2993. $nowBsEmpLv = $blueBsInfo['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
  2994. } else {
  2995. $nowBsEmpLv = EmployLevel::NO_LEVEL_ID;
  2996. }
  2997. }
  2998. // 修改AR_CALC_BONUS的LAST_EMP_LV聘级,修改奖金流水的聘级AR_FLOW_BONUS表的LAST_EMP_LV
  2999. // 期结算结果
  3000. CalcBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3001. [
  3002. ':USER_ID' => $data['USER_ID'],
  3003. ':PERIOD_NUM' => $this->_periodNum
  3004. ]
  3005. );
  3006. // // 奖金流水
  3007. // FlowBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3008. // [
  3009. // ':USER_ID' => $data['USER_ID'],
  3010. // ':PERIOD_NUM' => $this->_periodNum
  3011. // ]
  3012. // );
  3013. // 共享奖流水
  3014. CalcBonusGX::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3015. [
  3016. ':USER_ID' => $data['USER_ID'],
  3017. ':PERIOD_NUM' => $this->_periodNum
  3018. ]
  3019. );
  3020. // 推广奖流水
  3021. CalcBonusTG::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3022. [
  3023. ':USER_ID' => $data['USER_ID'],
  3024. ':PERIOD_NUM' => $this->_periodNum
  3025. ]
  3026. );
  3027. // 团队奖流水
  3028. CalcBonusQY::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3029. [
  3030. ':USER_ID' => $data['USER_ID'],
  3031. ':PERIOD_NUM' => $this->_periodNum
  3032. ]
  3033. );
  3034. // 服务奖流水
  3035. CalcBonusBD::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3036. [
  3037. ':USER_ID' => $data['USER_ID'],
  3038. ':PERIOD_NUM' => $this->_periodNum
  3039. ]
  3040. );
  3041. // 更新form的聘级
  3042. CalcBonusGX::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. CalcBonusTG::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3049. [
  3050. ':FROM_USER_ID' => $data['USER_ID'],
  3051. ':PERIOD_NUM' => $this->_periodNum
  3052. ]
  3053. );
  3054. CalcBonusBD::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3055. [
  3056. ':FROM_USER_ID' => $data['USER_ID'],
  3057. ':PERIOD_NUM' => $this->_periodNum
  3058. ]
  3059. );
  3060. }
  3061. unset($allData);
  3062. return $this->loopCalcBlueEmpLv($offset + $this->_limit);
  3063. }
  3064. unset($allData);
  3065. return true;
  3066. }
  3067. /**
  3068. * 奖金
  3069. * @param $userId
  3070. * @return array
  3071. * @throws \yii\db\Exception
  3072. */
  3073. public function bonusData($userId) {
  3074. // 从缓存中获取用户的奖金
  3075. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  3076. $standardBonus = CalcCache::standardBonus($userId, $this->_periodNum);
  3077. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  3078. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  3079. $empLv = $baseInfo['EMP_LV'];
  3080. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  3081. //没有共享和管理奖
  3082. $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;
  3083. $realBonusGx = 0;
  3084. $realBonusGl = 0;
  3085. $realBonusBs = 0; // 蓝星管理奖. BlueStar
  3086. $blueStartAmount = 0;
  3087. $buleStartOriBonus = 0;
  3088. $buleStartManageTax = 0;
  3089. $exchangePoints = 0; // 蓝星奖管理奖. 产生的兑换积分
  3090. $blueBonusGL = 0; //拆分蓝星管理奖,原奖金
  3091. $blueBonusYJ = 0; //拆分蓝星业绩奖,原奖金
  3092. $blueBonusGLSF = 0;//拆分蓝星管理奖,实发
  3093. $blueBonusYJSF = 0;//拆分蓝星业绩奖,实发
  3094. // rm1820 共享奖周算且不计入总原奖金中,所以需要在总原奖金中去掉
  3095. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $bonus['ORI_BONUS_GX'];
  3096. $oriGxMonthBonus = 0; // 月共享奖金,结算月之和原总金额
  3097. $monthGxReal = $monthGxManageTax = $monthGxpoint = 0;
  3098. $bonus['BONUS_GX'] = 0; // 以前的周共享奖实发金额,新版本每周不发,不扣除复消积分管理费,所以这里给0
  3099. if( $this->_isCalcMonth ) {
  3100. $fxPvStatus = $this->_isMonthPerfLimit($userId);
  3101. // BONUS_REAL 字段是发到用户的真实奖金
  3102. if ( $fxPvStatus ) {
  3103. // 月节点,获取蓝星管理奖数据
  3104. $userBS = CalcBonusBS::find()
  3105. ->where(
  3106. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  3107. [
  3108. ':PERIOD_NUM' => $this->_periodNum,
  3109. ':USER_ID' => $userId
  3110. ]
  3111. )
  3112. ->select('AMOUNT,ORI_BONUS,MANAGE_TAX,LEVEL_ID,PRODUCT_POINT,ORI_BONUS_ABBR,AMOUNT_ABBR,ORI_BONUS_MNT,AMOUNT_MNT')
  3113. ->limit(1)
  3114. ->orderBy('CREATED_AT DESC')
  3115. ->asArray()
  3116. ->all();
  3117. $userBS = is_array($userBS) ? reset($userBS) : [];
  3118. $blueStartAmount = isset($userBS['AMOUNT']) && !empty($userBS['AMOUNT']) ? $userBS['AMOUNT'] : 0; // 奖金
  3119. $buleStartOriBonus = isset($userBS['ORI_BONUS']) && !empty($userBS['ORI_BONUS']) ? $userBS['ORI_BONUS'] : 0; // 原奖金
  3120. $blueBonusGL = isset($userBS['ORI_BONUS_MNT']) && !empty($userBS['ORI_BONUS_MNT']) ? $userBS['ORI_BONUS_MNT'] : 0; // 拆分管理奖原奖金
  3121. $blueBonusYJ = isset($userBS['ORI_BONUS_ABBR']) && !empty($userBS['ORI_BONUS_ABBR']) ? $userBS['ORI_BONUS_ABBR'] : 0; // 拆分业绩奖原奖金
  3122. $blueBonusGLSF = isset($userBS['AMOUNT_MNT']) && !empty($userBS['AMOUNT_MNT']) ? $userBS['AMOUNT_MNT'] : 0; // 拆分管理奖 实发
  3123. $blueBonusYJSF = isset($userBS['AMOUNT_ABBR']) && !empty($userBS['AMOUNT_ABBR']) ? $userBS['AMOUNT_ABBR'] : 0; // 拆分业绩奖 实发
  3124. //$blueStartAmount = $blueBonusGLSF + $blueBonusYJSF; // 总实发,管理奖实发+业绩奖实发
  3125. #rm1820 共享奖金月节点合起来原奖金一起扣除管理费和复消积分
  3126. $monthSumData = CalcBonus::findUseSlaves()
  3127. ->select('SUM(ORI_BONUS_GX) AS BONUS_GX_SUM, SUM(BONUS_GL) AS BONUS_GL_SUM')
  3128. ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  3129. [
  3130. 'USER_ID'=>$userId,
  3131. 'CALC_MONTH'=>$this->_calcYearMonth
  3132. ]
  3133. )
  3134. ->asArray()
  3135. ->one();
  3136. $bonusGxSum = $monthSumData['BONUS_GX_SUM'] ?? 0;
  3137. $bonusGxSum = $bonus['ORI_BONUS_GX'] + $bonusGxSum;
  3138. $oriGxMonthBonus = $bonusGxSum;
  3139. if ($bonusGxSum > 0) {
  3140. // 扣除管理费和复消积分
  3141. //扣除相应的复消积分和管理费
  3142. $deductData = $this->deduct($userId, $bonusGxSum);
  3143. $monthGxReal = $deductData['surplus']; // 扣除管理费和复消积分后的实发金额
  3144. $monthGxManageTax = $deductData['manageTax']; // 管理费
  3145. $monthGxpoint = $deductData['reConsumePoints'];// 复消积分
  3146. }
  3147. $realBonusGx = $monthGxReal;
  3148. $bonusGlSum = $monthSumData['BONUS_GL_SUM'] ?? 0;
  3149. $bonus['RECONSUME_POINTS'] = $bonus['RECONSUME_POINTS'] + $monthGxpoint;
  3150. $bonus['MANAGE_TAX'] = $bonus['MANAGE_TAX'] + $monthGxManageTax;
  3151. $realBonusGl += $bonusGlSum + $bonus['BONUS_GL'];
  3152. $realBonusBs = $blueStartAmount;
  3153. $bonusReal += $realBonusGx + $realBonusBs + $realBonusGl + $bonus['BONUS_STORE']; // 再加上店服务奖的实发
  3154. $exchangePoints = isset($userBS['PRODUCT_POINT']) && !empty($userBS['PRODUCT_POINT']) ? $userBS['PRODUCT_POINT'] : 0; // 兑换积分
  3155. unset($monthSumData, $bonusGxSum, $bonusGlSum);
  3156. $oriBonusBs = isset($bonus['ORI_BONUS_BS']) && $bonus['ORI_BONUS_BS'] > 0 ? $bonus['ORI_BONUS_BS'] : 0;
  3157. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $blueBonusGL - $blueBonusYJ;
  3158. // 共享奖最后一起算,所以总原奖金中,需要加上整个月的数据
  3159. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] + $oriGxMonthBonus;
  3160. $empLv = $userBS['LEVEL_ID'] ?? $empLv;
  3161. } else {
  3162. // 蓝星奖原奖金在扣管理费的时候被加入到了BONUS_TOTAL,需要再减去原奖金
  3163. $oriBonusBs = isset($bonus['ORI_BONUS_BS']) && $bonus['ORI_BONUS_BS'] > 0 ? $bonus['ORI_BONUS_BS'] : 0;
  3164. // 如果未满足,则复消积分,应该不能发放
  3165. $oriBonusBsYJCF = isset($bonus['ORI_BONUS_ABBR']) && $bonus['ORI_BONUS_ABBR'] > 0 ? $bonus['ORI_BONUS_ABBR'] : 0;
  3166. $oriBonusBsGLCF = isset($bonus['ORI_BONUS_MNT']) && $bonus['ORI_BONUS_MNT'] > 0 ? $bonus['ORI_BONUS_MNT'] : 0;
  3167. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $oriBonusBs - $oriBonusBsYJCF - $oriBonusBsGLCF;
  3168. $empLv = EmployLevel::getDefaultLevelId();
  3169. }
  3170. }
  3171. $result = [
  3172. 'USER_ID' => $userId,
  3173. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  3174. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  3175. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  3176. 'LAST_EMP_LV' => $empLv,
  3177. 'LAST_STATUS' => $baseInfo['STATUS'],
  3178. 'LAST_MOBILE' => $baseInfo['MOBILE'],
  3179. 'LAST_PERIOD_AT' => $baseInfo['PERIOD_NUM'],
  3180. 'LAST_CREATED_AT' => $baseInfo['CREATED_AT'],
  3181. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  3182. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  3183. 'LAST_CITY' => $baseInfo['CITY'],
  3184. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  3185. 'LAST_SYSTEM_ID' => $baseInfo['SYSTEM_ID'] ? $baseInfo['SYSTEM_ID'] : '',
  3186. 'LAST_IS_DIRECT_SELLER' => $baseInfo['IS_DIRECT_SELLER'],
  3187. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  3188. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  3189. 'LAST_CON_USER_NAME' => $baseInfo['CON_USER_NAME'],
  3190. 'LAST_CON_REAL_NAME' => $baseInfo['CON_REAL_NAME'],
  3191. 'EXCHANGE_POINTS' => $exchangePoints, // 兑换积分
  3192. // 'LAST_LOCATION' => $baseInfo['LOCATION'] ? $baseInfo['LOCATION'] : 1,
  3193. //@todo
  3194. 'LAST_LOCATION' => 1,
  3195. 'BONUS_BD' => $bonus['BONUS_BD'],
  3196. 'BONUS_TG' => $bonus['BONUS_TG'],
  3197. 'BONUS_XF' => $bonus['BONUS_XF'],
  3198. 'BONUS_YJ' => $bonus['BONUS_YJ'],
  3199. 'BONUS_GX' => $bonus['BONUS_GX'],
  3200. 'BONUS_GL' => $bonus['BONUS_GL'],
  3201. 'BONUS_QY' => $bonus['BONUS_QY'],
  3202. 'BONUS_YC' => $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'],
  3203. 'BONUS_VIP' => $bonus['BONUS_VIP'],
  3204. 'RECONSUME_POINTS' => $bonus['RECONSUME_POINTS'],
  3205. 'MANAGE_TAX' => $bonus['MANAGE_TAX'],
  3206. 'BONUS_INCOME'=>$bonus['INCOME_TOTAL'],
  3207. 'BONUS_REAL'=> $bonusReal,
  3208. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  3209. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  3210. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  3211. 'ORI_BONUS_XF' => $bonus['ORI_BONUS_XF'],
  3212. 'ORI_BONUS_YJ' => $bonus['ORI_BONUS_YJ'],
  3213. 'ORI_BONUS_YJ_BD' => $bonus['ORI_BONUS_YJ_BD'],
  3214. 'ORI_BONUS_YJ_FX' => $bonus['ORI_BONUS_YJ_FX'],
  3215. 'ORI_BONUS_GX' => $bonus['ORI_BONUS_GX'],
  3216. 'ORI_MONTH_GX_BONUS' => $oriGxMonthBonus,
  3217. 'GX_MANAGE_TAX' => $monthGxManageTax, //月共享奖管理费
  3218. 'GX_RECONSUME_POINTS' => $monthGxpoint, //月共享奖复消积分
  3219. 'REAL_BONUS_GX' => $realBonusGx,
  3220. 'ORI_BONUS_GL' => $bonus['ORI_BONUS_GL'],
  3221. 'REAL_BONUS_GL' => $realBonusGl,
  3222. 'BONUS_BS' => $realBonusBs, // 新的管理奖金,即蓝星管理奖
  3223. 'ORI_BONUS_BS' => $buleStartOriBonus, // 蓝星管理奖金原奖金,即包含管理费
  3224. 'REAL_BONUS_BS' => $realBonusBs, // 实发蓝星管理奖金
  3225. 'ORI_BONUS_GL_BD' => $bonus['ORI_BONUS_GL_BD'],
  3226. 'ORI_BONUS_GL_FX' => $bonus['ORI_BONUS_GL_FX'],
  3227. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  3228. 'ORI_BONUS_QY_BD' => $bonus['ORI_BONUS_QY_BD'],
  3229. 'ORI_BONUS_QY_FX' => $bonus['ORI_BONUS_QY_FX'],
  3230. 'ORI_BONUS_YC' => $bonus['ORI_BONUS_YC'] + $bonus['ORI_BONUS_YC_EXTRA'],
  3231. 'ORI_BONUS_VIP' => $bonus['ORI_BONUS_VIP'],
  3232. 'ORI_BONUS_STANDARD' => $standardBonus,
  3233. 'BONUS_ST' => $bonus['BONUS_STORE'], // 店服务奖实发
  3234. 'ORI_BONUS_ST' => $bonus['ORI_BONUS_STORE'], // 店服务奖原奖金
  3235. 'BONUS_MNT' => $blueBonusGLSF,
  3236. 'ORI_BONUS_MNT' => $blueBonusGL,
  3237. 'ORI_BONUS_ABBR' => $blueBonusYJ,
  3238. 'BONUS_ABBR' => $blueBonusYJSF,
  3239. //以下没有用
  3240. 'BONUS_FW' => 0,
  3241. 'BONUS_FX' => $bonus['BONUS_FX'],
  3242. 'BONUS_LS' => $bonus['BONUS_LS'],
  3243. 'BONUS_BT' => $bonus['BONUS_BT'],
  3244. 'BONUS_BT_PROD' => $bonus['BONUS_BT_PROD'],
  3245. 'BONUS_BT_TOOL' => $bonus['BONUS_BT_TOOL'],
  3246. 'DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3247. 'BONUS_FL' => $bonus['BONUS_FL'],
  3248. 'BONUS_CF' => $bonus['BONUS_CF'],
  3249. 'BONUS_LX' => $bonus['BONUS_LX'],
  3250. 'SHOULD_QY' => $bonus['BONUS_QY'],
  3251. 'SHOULD_DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3252. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  3253. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  3254. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  3255. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  3256. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  3257. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  3258. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  3259. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  3260. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  3261. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  3262. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  3263. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  3264. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  3265. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  3266. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  3267. 'PV_PCS' => $perfData['PV_PCS'],
  3268. 'PV_LS_TOUCH' => 0.00,
  3269. 'SURPLUS_LS' => 0.00,
  3270. 'QY_LS' => 0.00,
  3271. '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']),
  3272. 'PERIOD_NUM' => $this->_periodNum,
  3273. 'CALC_YEAR' => $this->_calcYear,
  3274. 'CALC_MONTH' => $this->_calcYearMonth,
  3275. 'CALCULATED_AT' => Date::nowTime(),
  3276. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  3277. 'CREATED_AT' => Date::nowTime(),
  3278. ];
  3279. $resend = [];
  3280. unset($bonus, $realBonusGx, $realBonusGl, $bonusReal);
  3281. return ['result'=>$result,'resend'=>$resend];
  3282. }
  3283. /**
  3284. * 根据本有收入计算基础积分
  3285. * @param int $offset
  3286. * @return bool
  3287. */
  3288. public function calcBaseScore(int $offset=0) {
  3289. if (!$this->_isCalcMonth) {
  3290. return true;
  3291. }
  3292. echo sprintf("时间:[%s]基础积分,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3293. //查询本月的,每人的累计总收入
  3294. $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();
  3295. if ($allData) {
  3296. foreach ($allData as $everyData) {
  3297. $validTotalSum = $everyData['TOTAL_SUM'] - $everyData['BD_TOTAL_SUM'];
  3298. if( $validTotalSum <=0 ) continue;
  3299. //达不到 见习主任级别,不产生基础积分
  3300. $userBaseInfo = CalcCache::getUserInfo($everyData['USER_ID'], $this->_periodNum);
  3301. $userEmpLevel = $this->_empLevelConfig[$userBaseInfo['EMP_LV']];
  3302. $userEmpLevelSort = $userEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  3303. unset($userBaseInfo, $userEmpLevel);
  3304. if( $userEmpLevelSort < EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {
  3305. unset($userEmpLevelSort);
  3306. continue;
  3307. }
  3308. unset($userEmpLevelSort);
  3309. //根据总收入获取积分
  3310. $baseScore = $this->_getBaseScoreByTotalBonus($validTotalSum);
  3311. //根据用户的级别判断 能否得到级别积分
  3312. if( $baseScore > 0 ) {
  3313. CalcCache::nowMonthScore($everyData['USER_ID'], $this->_periodNum, [
  3314. 'BASE_SCORE' => $baseScore,
  3315. ]);
  3316. CalcCache::addHasScoreUsers($everyData['USER_ID'], $this->_periodNum);
  3317. }
  3318. unset($everyData, $validTotalSum, $baseScore);
  3319. }
  3320. unset($allData);
  3321. return $this->calcBaseScore($offset + $this->_limit);
  3322. }
  3323. unset($allData);
  3324. return true;
  3325. }
  3326. /**
  3327. * 循环积分入库
  3328. * @param int $offset
  3329. * @return bool
  3330. * @throws \yii\db\Exception
  3331. */
  3332. public function loopWriteScore(int $offset = 0) {
  3333. if(!$this->_isCalcMonth){
  3334. return true;
  3335. }
  3336. echo sprintf("时间:[%s]积分入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3337. // 从缓存列表里面从底层往上倒序获取会员
  3338. $allData = CalcCache::getHasScoreUsers($this->_periodNum, $offset, $this->_limit);
  3339. if($allData){
  3340. $insertDataMonthScore = [];
  3341. foreach($allData as $userId){
  3342. $monthScoreData = CalcCache::nowMonthScore($userId, $this->_periodNum);
  3343. $totalScore = $monthScoreData['BASE_SCORE'] + $monthScoreData['LEVEL_SCORE'] + $monthScoreData['UPGRADE_SCORE'];
  3344. if( $totalScore <= 0 ) continue;
  3345. $insertDataMonthScore[] = [
  3346. 'USER_ID' => $userId,
  3347. 'BASE_SCORE' => $monthScoreData['BASE_SCORE'],
  3348. 'LEVEL_SCORE' => $monthScoreData['LEVEL_SCORE'],
  3349. 'UPGRADE_SCORE' => $monthScoreData['UPGRADE_SCORE'],
  3350. 'TOTAL_SCORE' => $totalScore,
  3351. 'PERIOD_NUM' => $this->_periodNum,
  3352. 'CALC_MONTH' => $this->_calcYearMonth,
  3353. 'CREATED_AT' => Date::nowTime(),
  3354. ];
  3355. unset($userId, $monthScoreData, $totalScore);
  3356. }
  3357. ScoreMonth::batchInsert($insertDataMonthScore);
  3358. unset($insertDataMonthScore, $allData);
  3359. return $this->loopWriteScore($offset + $this->_limit);
  3360. }
  3361. unset($allData);
  3362. return true;
  3363. }
  3364. /**
  3365. * 根据总收入获取相应的基础积分
  3366. * @param $totalBonus
  3367. * @return int|mixed
  3368. */
  3369. private function _getBaseScoreByTotalBonus($totalBonus) {
  3370. $baseScoreConfig = Json::decode($this->_sysConfig['baseScore']['VALUE']);
  3371. $baseScore = 0;
  3372. foreach ($baseScoreConfig as $everyScoreData) {
  3373. if( $totalBonus < $everyScoreData['monthTotalPvMin'] ) continue;
  3374. if( $everyScoreData['monthTotalPvMax'] != 0 && $totalBonus >= $everyScoreData['monthTotalPvMax'] ) continue;
  3375. $baseScore = $everyScoreData['score'];
  3376. unset($everyScoreData);
  3377. break;
  3378. }
  3379. unset($baseScoreConfig, $totalBonus);
  3380. return $baseScore;
  3381. }
  3382. // 判断是否满足月最低消费
  3383. public function _isMonthPerfLimit($userId) {
  3384. $userMonthTotal = PerfMonth::find()->where(
  3385. 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  3386. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth]
  3387. )
  3388. ->asArray()
  3389. ->one();
  3390. $fxPvStatus = false;
  3391. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  3392. $fxPvStatus = true;
  3393. }
  3394. return $fxPvStatus;
  3395. }
  3396. }