BonusCalc.php 146 KB

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