BonusCalc.php 144 KB

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