BonusCalc.php 177 KB

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