BonusCalc.php 178 KB

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