BonusCalc.php 169 KB

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