BonusCalc.php 173 KB

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