BonusCalc.php 173 KB

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