BonusCalc.php 176 KB

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