BonusCalc.php 148 KB

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