BonusCalc.php 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804
  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]店服务奖金非月节点,不进行计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  769. return false;
  770. }
  771. echo sprintf("时间:[%s]店服务奖金计算--紧缩方式,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  772. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  773. if ($allData) {
  774. $insertBonusData = [];
  775. foreach ($allData as $userId) {
  776. $storeBonus = $this->verifyStorePerf($userId);
  777. if (empty($storeBonus)) continue;
  778. // 判断此店铺是否是活跃用户,如果不活跃,则往上找一个活跃的店铺获得此奖
  779. $isActive = $this->_isMonthPerfLimit($userId);
  780. // 聘级列表
  781. $empLv = EmployLevel::getIdConvertLevelSortCache();
  782. // 本期蓝星奖
  783. $bonusBs = CalcBonusBS::findUseDbCalc()->where('PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID', [':PERIOD_NUM' => $this->_periodNum, ':USER_ID' => $userId])->asArray()->one();
  784. $bonusUserId = '';
  785. // 活跃 + 主任聘级
  786. if (!$isActive || !$bonusBs || $empLv[$bonusBs['LEVEL_ID']] < 3) {
  787. // 如果此店铺不活跃,则找最近的一个店铺获得此奖
  788. $this->loopRelationParentDo($userId, function ($parent) use (&$bonusUserId, $empLv, $userId) {
  789. if ($this->_isMonthPerfLimit($parent['PARENT_UID'])) {
  790. // 判断是否是店铺
  791. $parentUser = CalcCache::getUserInfo($parent['PARENT_UID'], $this->_periodNum);
  792. if ($parentUser['IS_STUDIO'] == 1) {
  793. // 本期蓝星奖
  794. $bonusBs = CalcBonusBS::findUseDbCalc()->where('PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID', [':PERIOD_NUM' => $this->_periodNum, ':USER_ID' => $parent['PARENT_UID']])->asArray()->one();
  795. // 蓝星奖星级>=主任
  796. if ($bonusBs && $empLv[$bonusBs['LEVEL_ID']] >= 3) {
  797. $bonusUserId = $parent['PARENT_UID'];
  798. return self::LOOP_FINISH;
  799. }
  800. }
  801. }
  802. unset($parent, $bonusBs);
  803. });
  804. }
  805. if (!$isActive && !$bonusUserId) {
  806. continue;
  807. }
  808. $realBonusUserId = $bonusUserId ?: $userId;
  809. //扣除相应的复消积分和管理费
  810. $deductData = $this->deduct($realBonusUserId, $storeBonus);
  811. // 把对碰后的奖金存入缓存中
  812. CalcCache::bonus($realBonusUserId, $this->_periodNum, 'BONUS_STORE', $storeBonus, $deductData);
  813. $surplus = $deductData['surplus'];
  814. $manageTax = $deductData['manageTax'];
  815. $reConsumePoints = $deductData['reConsumePoints'];
  816. //店服务奖流水
  817. $insertBonusData[] = [
  818. 'ID' => SnowFake::instance()->generateId(),
  819. 'USER_ID' => $realBonusUserId,
  820. 'FROM_USER_ID' => $bonusUserId ? $userId : '', // 分享店铺的ID.只有紧缩且未活跃,才有此值
  821. 'AMOUNT' => $surplus,
  822. 'ORI_BONUS' => $storeBonus,
  823. 'RECONSUME_POINTS' => $reConsumePoints,
  824. 'MANAGE_TAX' => $manageTax,
  825. 'PERIOD_NUM' => $this->_periodNum,
  826. 'CALC_YEAR' => $this->_calcYear,
  827. 'CALC_MONTH' => $this->_calcYearMonth,
  828. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  829. 'CREATED_AT' => Date::nowTime()
  830. ];
  831. unset($bonusUserId, $userId, $deductData);
  832. }
  833. CalcBonusST::batchInsert($insertBonusData);
  834. unset($allData, $insertBonusData);
  835. return $this->calcStoreBonusReduce($offset + $this->_limit);
  836. }
  837. unset($allData);
  838. return true;
  839. }
  840. // 未开启紧缩方式计算店服务奖,不活跃即不给奖金
  841. public function calcStoreBonus(int $offset = 0) {
  842. if( !$this->_isCalcMonth ) {
  843. // 不是结算月,则不进行计算
  844. echo sprintf("时间:[%s]店服务奖金非月节点,不进行计算,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  845. return false;
  846. }
  847. echo sprintf("时间:[%s]店服务奖金计算--非紧缩方式,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  848. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  849. if ($allData) {
  850. $insertBonusData = [];
  851. foreach ($allData as $userId) {
  852. $storeBonus = $this->verifyStorePerf($userId);
  853. if (empty($storeBonus)) continue;
  854. // 未开启紧缩,不活跃,则不发放了
  855. $isActive = $this->_isMonthPerfLimit($userId);
  856. if (!$isActive) continue;
  857. $surplus = $storeBonus;
  858. //扣除相应的复消积分和管理费
  859. $deductData = $this->deduct($userId, $storeBonus);
  860. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_STORE', $storeBonus, $deductData);
  861. $surplus = $deductData['surplus'];
  862. $manageTax = $deductData['manageTax'];
  863. $reConsumePoints = $deductData['reConsumePoints'];
  864. //店服务奖流水
  865. $insertBonusData[] = [
  866. 'ID' => SnowFake::instance()->generateId(),
  867. 'USER_ID' => $userId,
  868. 'AMOUNT' => $surplus,
  869. 'ORI_BONUS' => $storeBonus,
  870. 'RECONSUME_POINTS' => $reConsumePoints,
  871. 'MANAGE_TAX' => $manageTax,
  872. 'PERIOD_NUM' => $this->_periodNum,
  873. 'CALC_YEAR' => $this->_calcYear,
  874. 'CALC_MONTH' => $this->_calcYearMonth,
  875. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  876. 'CREATED_AT' => Date::nowTime()
  877. ];
  878. unset($userId, $deductData);
  879. }
  880. CalcBonusST::batchInsert($insertBonusData);
  881. unset($allData, $insertBonusData);
  882. return $this->calcStoreBonus($offset + $this->_limit);
  883. }
  884. unset($allData);
  885. return true;
  886. }
  887. public function verifyStorePerfNew($userId, $groupPv) {
  888. // 业绩乘以比例为奖金
  889. $storeBonus = Tool::formatPrice($groupPv * $this->_sysConfig['storePercent']['VALUE'] / 100);
  890. if ($storeBonus <= 0) {
  891. return false;
  892. }
  893. // 获取会员的报单级别
  894. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  895. $storeBonus = $this->bonusTotalLimit($storeBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  896. if( $storeBonus <= 0 ) {
  897. return false;
  898. }
  899. unset($perfPv, $userBaseInfo, $userId);
  900. return $storeBonus;
  901. }
  902. /**
  903. * 通过用户id,校验店服务奖,此用户是否有原奖金
  904. */
  905. public function verifyStorePerf($userId) {
  906. // 从缓存中获取会员的业绩信息
  907. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  908. if( !$perfData ) {
  909. return false;
  910. }
  911. // 获取小组+自己的业绩
  912. $perfPv = $perfData['STORE_PV_GRAND'] ?? 0;
  913. if( $perfPv <= 0 ) {
  914. return false;
  915. }
  916. StorePerfLog::stGroupPv($this->_periodNum,$this->_calcYearMonth, $userId, $perfPv);
  917. // 业绩乘以比例为奖金
  918. $storeBonus = Tool::formatPrice($perfPv * $this->_sysConfig['storePercent']['VALUE'] / 100);
  919. if ($storeBonus <= 0) {
  920. return false;
  921. }
  922. // 获取会员的报单级别
  923. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  924. $storeBonus = $this->bonusTotalLimit($storeBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  925. if( $storeBonus <= 0 ) {
  926. return false;
  927. }
  928. unset($perfData, $perfPv, $userBaseInfo, $userId);
  929. return $storeBonus;
  930. }
  931. /**
  932. * 报单业绩奖
  933. * @param int $offset
  934. * @return bool
  935. * @throws \yii\db\Exception
  936. */
  937. public function calcBonusBdYJ(int $offset = 0) {
  938. $periodNum = $this->_periodNum;
  939. // 从缓存获取分页有业绩的会员信息
  940. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  941. if ($allData) {
  942. foreach ($allData as $userId) {
  943. $insertBonusData = [];
  944. // 从缓存中获取会员的业绩信息
  945. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  946. if( !$perfData ) continue;
  947. //个人业绩都算见点奖,包括报单和复消、二次购物
  948. $perfPv = $perfData['PV_PCS_ZC'] ?? 0;
  949. if( $perfPv <= 0 ) continue;
  950. //业绩奖使用报单PV
  951. // $oriPointsBonus = Tool::formatPrice($perfPv * $this->_sysConfig['pointsPercent']['VALUE'] / 100);
  952. // if ($oriPointsBonus <= 0) continue;
  953. //偶数层数
  954. $pointsEvenLayer = $this->_sysConfig['pointsLayer']['VALUE'];
  955. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  956. $userEmpLevel = $userBaseInfo['EMP_LV'];
  957. $netWorkParents = Cache::getAllNetworkParents($userId);
  958. foreach ($netWorkParents as $netWorkParent) {
  959. // $pointsBonus = $oriPointsBonus;
  960. //自已距顶端的深度减去父级距顶端的深度
  961. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  962. if( $diffLayer <= 0) continue;
  963. if( $diffLayer > 2 * $pointsEvenLayer ) continue;
  964. if( $diffLayer % 2 == 1 ) continue;
  965. $pointsPercentName = sprintf('pointsPercent_%d', $diffLayer);
  966. if( isset($this->_sysConfig[$pointsPercentName]['VALUE']) ) {
  967. $diffLayerPercent = $this->_sysConfig[$pointsPercentName]['VALUE'];
  968. }else {
  969. $diffLayerPercent = $this->_sysConfig['pointsPercent']['VALUE'];
  970. }
  971. $pointsBonus = Tool::formatPrice($perfPv * $diffLayerPercent / 100);
  972. if( $pointsBonus <= 0 ) continue;
  973. // 把对碰后的奖金存入缓存中
  974. $bonusUserId = $netWorkParent['PARENT_UID'];
  975. if( !$bonusUserId ) continue;
  976. // 获取会员的报单级别
  977. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  978. //判断上限 包括级别奖金上限和总上限
  979. // $pointsBonus = $this->declarationLevelCap($pointsBonus, $bonusUserId, $bonusUserBaseInfo['DEC_LV']);
  980. // if( $pointsBonus <= 0 ) continue;
  981. //总金额限制
  982. $pointsBonus = $this->bonusTotalLimit($pointsBonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  983. if( $pointsBonus <= 0 ) continue;
  984. //扣除相应的复消积分和管理费
  985. $deductData = $this->deduct($bonusUserId, $pointsBonus);
  986. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_YJ', $pointsBonus, $deductData, CalcCache::FROM_MEANS_BD);
  987. //业绩奖流水
  988. $insertBonusData[] = [
  989. 'ID' => SnowFake::instance()->generateId(),
  990. 'USER_ID' => $bonusUserId,
  991. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  992. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  993. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  994. 'FROM_USER_ID' => $userId,
  995. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  996. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  997. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  998. 'AMOUNT' => $deductData['surplus'],
  999. 'ORI_BONUS' => $pointsBonus,
  1000. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1001. 'MANAGE_TAX' => $deductData['manageTax'],
  1002. 'PERIOD_NUM' => $this->_periodNum,
  1003. 'CALC_YEAR' => $this->_calcYear,
  1004. 'CALC_MONTH' => $this->_calcYearMonth,
  1005. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1006. 'CREATED_AT' => Date::nowTime(),
  1007. 'LOGS' => json_encode([
  1008. 'perfPv' => $perfPv,
  1009. 'pointsLayerConfig' => $this->_sysConfig['pointsLayer']['VALUE'],
  1010. 'pointsPercentConfig' => $diffLayerPercent,
  1011. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1012. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1013. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1014. ]),
  1015. ];
  1016. unset($netWorkParent, $pointsBonus, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $deductData, $diffLayerPercent, $pointsPercentName);
  1017. }
  1018. CalcBonusYJ::batchInsert($insertBonusData);
  1019. unset($insertBonusData, $userId, $perfData, $perfPv, $oriPointsBonus, $pointsEvenLayer, $userBaseInfo, $userEmpLevel, $netWorkParents);
  1020. }
  1021. unset($allData);
  1022. return $this->calcBonusBdYJ($offset + $this->_limit);
  1023. }
  1024. unset($allData);
  1025. return true;
  1026. }
  1027. /**
  1028. * 复消业绩奖
  1029. * @param int $offset
  1030. * @return bool
  1031. * @throws \yii\db\Exception
  1032. */
  1033. public function calcBonusFxYJ(int $offset = 0) {
  1034. $periodNum = $this->_periodNum;
  1035. // 从缓存获取分页有业绩的会员信息
  1036. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1037. if ($allData) {
  1038. foreach ($allData as $userId) {
  1039. $insertBonusData = [];
  1040. // 从缓存中获取会员的业绩信息
  1041. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1042. if( !$perfData ) continue;
  1043. //个人业绩都算见点奖,包括报单和复消、二次购物
  1044. $perfPv = $perfData['PV_PCS_FX'] ?? 0;
  1045. if( $perfPv <= 0 ) continue;
  1046. //业绩奖使用报单PV
  1047. // $oriPointsBonus = Tool::formatPrice($perfPv * $this->_sysConfig['pointsPercent']['VALUE'] / 100);
  1048. // if ($oriPointsBonus <= 0) continue;
  1049. //偶数层数
  1050. $pointsEvenLayer = $this->_sysConfig['fxPointsLayer']['VALUE'];
  1051. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1052. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1053. $netWorkParents = Cache::getAllNetworkParents($userId);
  1054. foreach ($netWorkParents as $netWorkParent) {
  1055. // $pointsBonus = $oriPointsBonus;
  1056. //自已距顶端的深度减去父级距顶端的深度
  1057. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  1058. if( $diffLayer <= 0) continue;
  1059. if( $diffLayer > 2 * $pointsEvenLayer ) continue;
  1060. if( $diffLayer % 2 == 1 ) continue;
  1061. $pointsPercentName = sprintf('fxPointsPercent_%d', $diffLayer);
  1062. if( isset($this->_sysConfig[$pointsPercentName]['VALUE']) ) {
  1063. $diffLayerPercent = $this->_sysConfig[$pointsPercentName]['VALUE'];
  1064. }else {
  1065. $diffLayerPercent = $this->_sysConfig['fxPointsPercent']['VALUE'];
  1066. }
  1067. $pointsBonus = Tool::formatPrice($perfPv * $diffLayerPercent / 100);
  1068. if( $pointsBonus <= 0 ) continue;
  1069. // 把对碰后的奖金存入缓存中
  1070. $bonusUserId = $netWorkParent['PARENT_UID'];
  1071. if( !$bonusUserId ) continue;
  1072. // 获取会员的报单级别
  1073. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1074. //判断上限 包括级别奖金上限和总上限
  1075. // $pointsBonus = $this->declarationLevelCap($pointsBonus, $bonusUserId, $bonusUserBaseInfo['DEC_LV']);
  1076. // if( $pointsBonus <= 0 ) continue;
  1077. //总金额限制
  1078. $pointsBonus = $this->bonusTotalLimit($pointsBonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  1079. if( $pointsBonus <= 0 ) continue;
  1080. //扣除相应的复消积分和管理费
  1081. $deductData = $this->deduct($bonusUserId, $pointsBonus);
  1082. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_YJ', $pointsBonus, $deductData, CalcCache::FROM_MEANS_FX);
  1083. //业绩奖流水
  1084. $insertBonusData[] = [
  1085. 'ID' => SnowFake::instance()->generateId(),
  1086. 'USER_ID' => $bonusUserId,
  1087. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1088. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1089. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1090. 'FROM_USER_ID' => $userId,
  1091. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1092. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1093. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1094. 'AMOUNT' => $deductData['surplus'],
  1095. 'ORI_BONUS' => $pointsBonus,
  1096. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1097. 'MANAGE_TAX' => $deductData['manageTax'],
  1098. 'PERIOD_NUM' => $this->_periodNum,
  1099. 'CALC_YEAR' => $this->_calcYear,
  1100. 'CALC_MONTH' => $this->_calcYearMonth,
  1101. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1102. 'CREATED_AT' => Date::nowTime(),
  1103. 'LOGS' => json_encode([
  1104. 'perfPv' => $perfPv,
  1105. 'pointsLayerConfig' => $this->_sysConfig['fxPointsLayer']['VALUE'],
  1106. 'pointsPercentConfig' => $diffLayerPercent,
  1107. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1108. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1109. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1110. ]),
  1111. ];
  1112. unset($netWorkParent, $pointsBonus, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $deductData, $diffLayerPercent, $pointsPercentName);
  1113. }
  1114. CalcBonusYJ::batchInsert($insertBonusData);
  1115. unset($insertBonusData, $userId, $perfData, $perfPv, $oriPointsBonus, $pointsEvenLayer, $userBaseInfo, $userEmpLevel, $netWorkParents);
  1116. }
  1117. unset($allData);
  1118. return $this->calcBonusFxYJ($offset + $this->_limit);
  1119. }
  1120. unset($allData);
  1121. return true;
  1122. }
  1123. /**
  1124. * 团队奖
  1125. * @param int $offset
  1126. * @return bool
  1127. * @throws \yii\db\Exception
  1128. */
  1129. public function calcBonusQY(int $offset = 0) {
  1130. echo sprintf("时间:[%s]团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1131. $periodNum = $this->_periodNum;
  1132. // 从缓存获取分页有业绩的会员信息
  1133. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1134. if ($allData) {
  1135. $insertBonusData = [];
  1136. foreach ($allData as $userId) {
  1137. // 从缓存中获取会员的业绩信息
  1138. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1139. // 从缓存中获取会员的上期结余业绩信息
  1140. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1141. // 本期 + 上期结余
  1142. $perfArr = [
  1143. 'SURPLUS_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  1144. 'SURPLUS_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  1145. 'SURPLUS_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  1146. 'SURPLUS_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  1147. 'SURPLUS_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  1148. ];
  1149. $oriPerfArr = [
  1150. 'perfArr' => $perfArr,
  1151. 'touchBonus' => 0,
  1152. ];
  1153. // 获取会员的报单级别
  1154. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1155. $decLevelConfig = $this->_decLevelConfig;
  1156. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1157. // 对碰
  1158. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1159. $touchPerfArr = [];
  1160. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1161. $touchPerfArr[$keyR] = $perfR;
  1162. }
  1163. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1164. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1165. //更新数据库
  1166. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1167. 'USER_ID' => $userId,
  1168. 'PERIOD_NUM' => $periodNum,
  1169. ]);
  1170. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1171. $teamBonus = $touchBonusArr['touchBonus'];
  1172. //判断级别上限
  1173. $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1174. if( $teamBonus <= 0 ) continue;
  1175. $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1176. if( $teamBonus <= 0 ) continue;
  1177. //扣除相应的复消积分和管理费
  1178. $deductData = $this->deduct($userId, $teamBonus);
  1179. // 把对碰后的奖金存入缓存中
  1180. CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus, $deductData);
  1181. //团队奖流水
  1182. $insertBonusData[] = [
  1183. 'ID' => SnowFake::instance()->generateId(),
  1184. 'USER_ID' => $userId,
  1185. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  1186. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  1187. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  1188. 'AMOUNT' => $deductData['surplus'],
  1189. 'ORI_BONUS' => $teamBonus,
  1190. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1191. 'MANAGE_TAX' => $deductData['manageTax'],
  1192. 'PERIOD_NUM' => $this->_periodNum,
  1193. 'CALC_YEAR' => $this->_calcYear,
  1194. 'CALC_MONTH' => $this->_calcYearMonth,
  1195. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1196. 'CREATED_AT' => Date::nowTime(),
  1197. 'LOGS' => json_encode([
  1198. 'perfArr' => $perfArr,
  1199. 'touchPerfArrOri' => $touchBonusArr['perfArr'],
  1200. 'touchPerfArr' => $touchPerfArr,
  1201. 'nowDecLevelConfig' => $nowDecLevelConfig,
  1202. 'decLevel' => $userBaseInfo['DEC_LV'],
  1203. ]),
  1204. ];
  1205. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1206. }
  1207. CalcBonusQY::batchInsert($insertBonusData);
  1208. unset($allData, $insertBonusData);
  1209. return $this->calcBonusQY($offset + $this->_limit);
  1210. }
  1211. unset($allData);
  1212. return true;
  1213. }
  1214. /**
  1215. * 报单团队奖
  1216. * @param int $offset
  1217. * @return bool
  1218. * @throws \yii\db\Exception
  1219. */
  1220. public function calcBonusBdQY(int $offset = 0) {
  1221. echo sprintf("时间:[%s]报单团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1222. $periodNum = $this->_periodNum;
  1223. // 从缓存获取分页有业绩的会员信息
  1224. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1225. if ($allData) {
  1226. $insertBonusData = [];
  1227. foreach ($allData as $userId) {
  1228. // 从缓存中获取会员的业绩信息
  1229. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1230. // 从缓存中获取会员的上期结余业绩信息
  1231. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1232. // 本期 + 上期结余
  1233. $perfArr = [
  1234. 'SURPLUS_1L_ZC' => $perfData['PV_1L_ZC'] + $pervSurplusPerf['SURPLUS_1L_ZC'],
  1235. 'SURPLUS_2L_ZC' => $perfData['PV_2L_ZC'] + $pervSurplusPerf['SURPLUS_2L_ZC'],
  1236. 'SURPLUS_3L_ZC' => $perfData['PV_3L_ZC'] + $pervSurplusPerf['SURPLUS_3L_ZC'],
  1237. 'SURPLUS_4L_ZC' => $perfData['PV_4L_ZC'] + $pervSurplusPerf['SURPLUS_4L_ZC'],
  1238. 'SURPLUS_5L_ZC' => $perfData['PV_5L_ZC'] + $pervSurplusPerf['SURPLUS_5L_ZC'],
  1239. ];
  1240. $oriPerfArr = [
  1241. 'perfArr' => $perfArr,
  1242. 'touchBonus' => 0,
  1243. ];
  1244. // 获取会员的报单级别
  1245. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1246. $decLevelConfig = $this->_decLevelConfig;
  1247. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1248. // 对碰
  1249. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1250. $touchPerfArr = [];
  1251. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1252. $touchPerfArr[$keyR] = $perfR;
  1253. }
  1254. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1255. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1256. //更新数据库
  1257. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1258. 'USER_ID' => $userId,
  1259. 'PERIOD_NUM' => $periodNum,
  1260. ]);
  1261. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1262. $teamBonus = $touchBonusArr['touchBonus'];
  1263. //判断级别上限
  1264. // $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1265. // if( $teamBonus <= 0 ) continue;
  1266. // $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1267. // if( $teamBonus <= 0 ) continue;
  1268. //扣除相应的复消积分和管理费
  1269. // $deductData = $this->deduct($userId, $teamBonus);
  1270. // 把对碰后的奖金存入缓存中
  1271. CalcCache::bonus($userId, $periodNum, 'BONUS_QY_BD', $teamBonus);
  1272. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1273. }
  1274. unset($allData, $insertBonusData);
  1275. return $this->calcBonusBdQY($offset + $this->_limit);
  1276. }
  1277. unset($allData);
  1278. return true;
  1279. }
  1280. /**
  1281. * 复消团队奖
  1282. * @param int $offset
  1283. * @return bool
  1284. * @throws \yii\db\Exception
  1285. */
  1286. public function calcBonusFxQY(int $offset = 0) {
  1287. echo sprintf("时间:[%s]复消团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1288. $periodNum = $this->_periodNum;
  1289. // 从缓存获取分页有业绩的会员信息
  1290. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  1291. if ($allData) {
  1292. $insertBonusData = [];
  1293. foreach ($allData as $userId) {
  1294. // 从缓存中获取会员的业绩信息
  1295. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  1296. // 从缓存中获取会员的上期结余业绩信息
  1297. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  1298. // 本期 + 上期结余
  1299. $perfArr = [
  1300. 'SURPLUS_1L_FX' => $perfData['PV_1L_FX'] + $pervSurplusPerf['SURPLUS_1L_FX'],
  1301. 'SURPLUS_2L_FX' => $perfData['PV_2L_FX'] + $pervSurplusPerf['SURPLUS_2L_FX'],
  1302. 'SURPLUS_3L_FX' => $perfData['PV_3L_FX'] + $pervSurplusPerf['SURPLUS_3L_FX'],
  1303. 'SURPLUS_4L_FX' => $perfData['PV_4L_FX'] + $pervSurplusPerf['SURPLUS_4L_FX'],
  1304. 'SURPLUS_5L_FX' => $perfData['PV_5L_FX'] + $pervSurplusPerf['SURPLUS_5L_FX'],
  1305. ];
  1306. $oriPerfArr = [
  1307. 'perfArr' => $perfArr,
  1308. 'touchBonus' => 0,
  1309. ];
  1310. // 获取会员的报单级别
  1311. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1312. $decLevelConfig = $this->_decLevelConfig;
  1313. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  1314. // 对碰
  1315. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  1316. //大区封顶
  1317. $touchPerfArr = [];
  1318. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  1319. // if ($perfR > $nowDecLevelConfig['QY_BIG_CAP']) $perfR = $nowDecLevelConfig['QY_BIG_CAP'];
  1320. $touchPerfArr[$keyR] = $perfR;
  1321. }
  1322. // 对碰完成后把结余的业绩存入本期业绩缓存中
  1323. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  1324. //更新数据库
  1325. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  1326. 'USER_ID' => $userId,
  1327. 'PERIOD_NUM' => $periodNum,
  1328. ]);
  1329. if ($touchBonusArr['touchBonus'] <= 0) continue;
  1330. $teamBonus = $touchBonusArr['touchBonus'];
  1331. //判断级别上限
  1332. // $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  1333. // if( $teamBonus <= 0 ) continue;
  1334. //
  1335. // $teamBonus = $this->bonusTotalLimit($teamBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1336. // if( $teamBonus <= 0 ) continue;
  1337. //扣除相应的复消积分和管理费
  1338. // $deductData = $this->deduct($userId, $teamBonus);
  1339. // 把对碰后的奖金存入缓存中
  1340. CalcCache::bonus($userId, $periodNum, 'BONUS_QY_FX', $teamBonus);
  1341. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  1342. }
  1343. unset($allData, $insertBonusData);
  1344. return $this->calcBonusFxQY($offset + $this->_limit);
  1345. }
  1346. unset($allData);
  1347. return true;
  1348. }
  1349. /**
  1350. * 计算共享奖之前的操作
  1351. * @param int $offset
  1352. * @return bool
  1353. */
  1354. public function calcBonusGXBefore(int $offset = 0) {
  1355. //获取所有激活会员
  1356. $allData = CalcCache::getActiveUsers($this->_periodNum, $offset, $this->_limit);
  1357. if ($allData) {
  1358. echo sprintf("时间:[%s]共享奖之前,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1359. foreach ($allData as $userId) {
  1360. //判断$userId 能否得共享奖
  1361. // //判断 是否本期小市场(安置关系)新增980pv 能否得奖
  1362. // if ( !$this->checkSmallMarketPerf($userId) ) {
  1363. // unset($userId);
  1364. // continue;
  1365. // }
  1366. // 20220407 小市场980判断->改成判断此期用户是否有原报单团队奖金
  1367. if ( !$this->checkHasOriBonusQyBd($userId) ) {
  1368. unset($userId);
  1369. continue;
  1370. }
  1371. //获取当前会员所有的推荐上级, 并查看是否有首单团队奖
  1372. $validDeep = 0;
  1373. $this->loopRelationParentDo($userId, function ($parent) use ($userId, &$validDeep) {
  1374. //判断$parent是否有首单团队奖
  1375. $parentBonus = CalcCache::bonus($parent['PARENT_UID'], $this->_periodNum);
  1376. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {
  1377. $validDeep += 1;
  1378. //记录得奖缓存
  1379. CalcCache::addShareBonusOneRelation($userId, $parent['PARENT_UID'], $this->_periodNum, $validDeep);
  1380. }
  1381. unset($parent, $parentBonus);
  1382. if ( $validDeep >= 2 ) {
  1383. return self::LOOP_FINISH;
  1384. }
  1385. });
  1386. unset($userId, $validDeep);
  1387. }
  1388. unset($allData);
  1389. return $this->calcBonusGXBefore($offset + $this->_limit);
  1390. }
  1391. unset($allData);
  1392. return true;
  1393. }
  1394. /**
  1395. * 共享奖
  1396. * @param int $offset
  1397. * @return bool
  1398. * @throws \yii\db\Exception
  1399. */
  1400. public function calcBonusGX(int $offset = 0) {
  1401. // 从缓存获取分页有的会员信息
  1402. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_gxLimit);
  1403. if ($allData) {
  1404. echo sprintf("时间:[%s]共享奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1405. $insertBonusData = [];
  1406. // 获取线上找几层和是否开启找几层的限制
  1407. $configsGx = Config::find()->where(
  1408. 'CONFIG_NAME="gxNoPvFindLimitSwitch" OR CONFIG_NAME="gxNoPvFindLimitTimes"'
  1409. )->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  1410. $openFindLimit = isset($configsGx['gxNoPvFindLimitSwitch']) ? $configsGx['gxNoPvFindLimitSwitch']['VALUE'] : 1; // 是否开放了 得奖人不满足980就找一个满足980的人来得这个奖 的最大向上循环次数
  1411. $findLimitTimes = isset($configsGx['gxNoPvFindLimitTimes']) ? $configsGx['gxNoPvFindLimitTimes']['VALUE'] : 5 ; // 开放的限制层数
  1412. $shareOutTimes = Config::find()
  1413. ->where(
  1414. 'CONFIG_NAME="shareOutTimes"'
  1415. )
  1416. ->indexBy('CONFIG_NAME')
  1417. ->asArray()
  1418. ->all();// 获取向上找五次超过次数限制
  1419. $shareOutTimes = isset($shareOutTimes['shareOutTimes']) ? $shareOutTimes['shareOutTimes']['VALUE'] : 5;
  1420. foreach ($allData as $userId) {
  1421. $bonusUserData = [];
  1422. $bonusPercentOne = $this->_sysConfig['shareRecPercent']['VALUE'] / 100;
  1423. $bonusPercentTwo = $this->_sysConfig['sharePercent']['VALUE'] / 100;
  1424. // 从缓存中获取会员的收入信息
  1425. $incomeBonus = CalcCache::bonus($userId, $this->_periodNum);
  1426. $lastTwoIncome = $incomeBonus['ORI_BONUS_QY_BD'] ?? 0;
  1427. $shareBonusOne = $lastTwoIncome * $bonusPercentOne;
  1428. unset($lastTwoIncome);
  1429. //找到上2代 新增个人+推荐团队业绩 加权平均分配 这个奖
  1430. if( $shareBonusOne > 0) {
  1431. $bonusOneData = $this->gxLastTwoBonusData($userId, 1, $shareBonusOne);
  1432. $bonusTwoData = $this->gxLastTwoBonusData($userId, 2, $shareBonusOne);
  1433. $bonusUserData = array_merge($bonusUserData, $bonusOneData);
  1434. $bonusUserData = array_merge($bonusUserData, $bonusTwoData);
  1435. unset($bonusOneData, $bonusTwoData);
  1436. }
  1437. $nextFiveIncome = $incomeBonus['ORI_BONUS_QY_BD'] ?? 0;
  1438. $shareBonusTwo = $nextFiveIncome * $bonusPercentTwo;
  1439. unset($nextFiveIncome);
  1440. //找到下5代(安置网) 紧缩 相对偶数层2、4、6、8、10层
  1441. if( $shareBonusTwo > 0 ) {
  1442. $validDeep = 1;
  1443. $periodNum = $this->_periodNum;
  1444. $this->loopNetworkParentDo($userId, function ($parent) use ($shareOutTimes, $periodNum, $userId, $shareBonusTwo, &$validDeep, &$bonusUserData) {
  1445. if ( $validDeep % 2 === 0 ) {
  1446. $shareTimes = CalcCache::gxShareTimes($userId, $periodNum);
  1447. // 增加判断,是否分共享奖奖金次数超过了五次,如果超过五次则不进行循环了
  1448. if ($shareTimes > $shareOutTimes) {
  1449. return self::LOOP_FINISH;
  1450. }
  1451. $bonusUserData[] = [
  1452. 'bonusUid' => $parent['PARENT_UID'],
  1453. 'bonus' => $shareBonusTwo,
  1454. 'validMinPv' => true,
  1455. ];
  1456. }
  1457. //判断$parent是否有首单团队奖
  1458. $parentBonus = CalcCache::bonus($parent['PARENT_UID'], $this->_periodNum);
  1459. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {//有效层
  1460. $validDeep += 1;
  1461. }
  1462. unset($parent, $parentBonus);
  1463. if ( $validDeep >= 11 ) {
  1464. return self::LOOP_FINISH;
  1465. }
  1466. });
  1467. unset($validDeep);
  1468. }
  1469. if ( !$bonusUserData ) continue;
  1470. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1471. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1472. foreach ($bonusUserData as $bonusUserDataEvery) {
  1473. $bonusUserId = $bonusUserDataEvery['bonusUid'];
  1474. //判断 是否本期小市场(安置关系)新增980pv 能否得奖->20220407改成判断得奖人此期是否有报单团队奖金
  1475. // if ( $bonusUserDataEvery['validMinPv'] && !$this->checkSmallMarketPerf($bonusUserId) ) { 20220407
  1476. if ( $bonusUserDataEvery['validMinPv'] && !$this->checkHasOriBonusQyBd($bonusUserId) ) {
  1477. // unset($bonusUserDataEvery, $bonusUserId);
  1478. // continue;
  1479. //得奖人不满足980就找一个满足980的人来得这个奖
  1480. // 20220407改成判断得奖人此期是否有报单团队奖金,如果没有则找一个此期有报单团队奖金得人
  1481. $bonusUserId = $this->getMinBdPvNetworkParent($bonusUserId, $openFindLimit, $findLimitTimes);
  1482. if ( !$bonusUserId ) {
  1483. unset($bonusUserDataEvery, $bonusUserId);
  1484. continue;
  1485. }
  1486. }
  1487. $bonus = Tool::formatPrice($bonusUserDataEvery['bonus']);
  1488. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1489. //总金额限制
  1490. $bonus = $this->bonusTotalLimit($bonus, $bonusUserId, $bonusUserBaseInfo['REC_NUM'], $bonusUserBaseInfo['ZC_AMOUNT']);
  1491. if( $bonus <= 0 ) continue;
  1492. // //扣除相应的复消积分和管理费
  1493. // 共享奖调整,周共享奖信息,不扣除管理费和复消费积分
  1494. // $deductData = $this->deduct($bonusUserId, $bonus);
  1495. // CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GX', $bonus, $deductData,'',true);
  1496. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GX', $bonus);
  1497. CalcCache::addHasMonthBonusUsers($bonusUserId, $this->_periodNum);
  1498. //共享奖流水 rm1820 月总扣除复消积分和管理费,流水中不扣除.
  1499. $insertBonusData[] = [
  1500. 'ID' => SnowFake::instance()->generateId(),
  1501. 'USER_ID' => $bonusUserId,
  1502. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1503. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1504. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1505. 'FROM_USER_ID' => $userId,
  1506. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1507. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1508. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1509. // 'AMOUNT' => 0,
  1510. 'ORI_BONUS' => $bonus,
  1511. // 'RECONSUME_POINTS' => 0,
  1512. // 'MANAGE_TAX' => 0,
  1513. 'PERIOD_NUM' => $this->_periodNum,
  1514. 'CALC_YEAR' => $this->_calcYear,
  1515. 'CALC_MONTH' => $this->_calcYearMonth,
  1516. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1517. 'CREATED_AT' => Date::nowTime(),
  1518. 'LOGS' => json_encode([
  1519. 'sharePercent' => $bonusPercentOne,
  1520. 'incomeBonus' => $incomeBonus,
  1521. 'recNum' => $userBaseInfo['REC_NUM'],
  1522. 'decAmount' => $userBaseInfo['ZC_AMOUNT'],
  1523. ]),
  1524. ];
  1525. unset($bonusUserDataEvery, $bonusUserId, $bonus, $bonusUserBaseInfo);
  1526. }
  1527. unset($userId, $bonusUserData, $userBaseInfo, $userEmpLevel, $bonusPercentOne, $bonusPercentTwo, $incomeBonus, $sharePerson);
  1528. // echo ('内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  1529. }
  1530. if($insertBonusData) CalcBonusGX::batchInsert($insertBonusData);
  1531. unset($allData, $insertBonusData);
  1532. return $this->calcBonusGX($offset + $this->_gxLimit);
  1533. }
  1534. unset($allData);
  1535. return true;
  1536. }
  1537. // 执行蓝星管理奖金的存储过程
  1538. public function calcBsProcedure() {
  1539. if( !$this->_isCalcMonth ) {
  1540. // 不是结算月,则不进行计算
  1541. return false;
  1542. }
  1543. $result = \Yii::$app->db->createCommand("CALL CALCBLUE(:periodNum)")
  1544. ->bindValue(':periodNum' , $this->_periodNum )
  1545. ->execute();
  1546. return $result;
  1547. }
  1548. /**
  1549. * 蓝星管理奖金未拆分
  1550. * @param int $offset
  1551. * @return bool
  1552. * @throws \yii\db\Exception
  1553. */
  1554. public function calcBonusBsGL(int $offset = 0) {
  1555. if( !$this->_isCalcMonth ) {
  1556. // 不是结算月,则不进行计算
  1557. return false;
  1558. }
  1559. // 从缓存获取分页有收入的会员信息
  1560. $allData = CalcBonusBS::findUseDbCalc()
  1561. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1562. ->groupBy('USER_ID')
  1563. ->offset($offset)
  1564. ->limit($this->_limit)
  1565. ->asArray()
  1566. ->all();
  1567. if ($allData) {
  1568. foreach ($allData as $user) {
  1569. // 添加到有奖励信息的数据中
  1570. CalcCache::addHasBonusUsers($user['USER_ID'], $this->_periodNum);
  1571. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  1572. if ($fxStatus) {
  1573. //扣除相应的复消积分和管理费
  1574. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS']);
  1575. // 把对碰后的奖金存入缓存中
  1576. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS'], $deductData);
  1577. //CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  1578. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1579. $manageTax = $deductData['manageTax']; // 管理费
  1580. $point = $deductData['reConsumePoints'];// 复消积分
  1581. } else {
  1582. // 如果未满足月消费,则不扣除管理费和复消积分了
  1583. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  1584. $realBonusBs = $user['ORI_BONUS'];
  1585. $manageTax = 0; // 管理费
  1586. $point = 0;// 复消积分
  1587. }
  1588. // 更新蓝星奖金存储过程的实发金额数据
  1589. CalcBonusBS::updateAll(['AMOUNT' => $realBonusBs, 'MANAGE_TAX' => $manageTax, 'RECONSUME_POINTS' => $point],
  1590. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1591. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1592. }
  1593. return $this->calcBonusBsGL($offset + $this->_limit);
  1594. }
  1595. unset($allData);
  1596. return true;
  1597. }
  1598. /**
  1599. * 蓝星管理奖金
  1600. * @param int $offset
  1601. * @return bool
  1602. * @throws \yii\db\Exception
  1603. */
  1604. public function calcBonusBsGLCF(int $offset = 0) {
  1605. if( !$this->_isCalcMonth ) {
  1606. // 不是结算月,则不进行计算
  1607. return false;
  1608. }
  1609. // 从缓存获取分页有收入的会员信息
  1610. $allData = CalcBonusBS::findUseDbCalc()
  1611. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1612. ->groupBy('USER_ID')
  1613. ->offset($offset)
  1614. ->limit($this->_limit)
  1615. ->asArray()
  1616. ->all();
  1617. if ($allData) {
  1618. foreach ($allData as $user) {
  1619. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  1620. if ($fxStatus) {
  1621. //扣除相应的复消积分和管理费
  1622. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_MNT']);
  1623. // 把对碰后的奖金存入缓存中
  1624. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_MNT', $user['ORI_BONUS_MNT']);
  1625. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1626. $manageTax = $deductData['manageTax']; // 管理费
  1627. $point = $deductData['reConsumePoints'];// 复消积分
  1628. } else {
  1629. // 如果未满足月消费,则不扣除管理费和复消积分了
  1630. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_MNT', $user['ORI_BONUS_MNT']);
  1631. $realBonusBs = $user['ORI_BONUS_MNT'];
  1632. $manageTax = 0; // 管理费
  1633. $point = 0;// 复消积分
  1634. }
  1635. // 更新蓝星奖金存储过程的实发金额数据
  1636. CalcBonusBS::updateAll(['AMOUNT_MNT' => $realBonusBs, 'MANAGE_TAX_MNT' => $manageTax, 'MNT_RECONSUME_POINTS' => $point],
  1637. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1638. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1639. }
  1640. return $this->calcBonusBsGLCF($offset + $this->_limit);
  1641. }
  1642. unset($allData);
  1643. return true;
  1644. }
  1645. /**
  1646. * 蓝星业绩奖金
  1647. * @param int $offset
  1648. * @return bool
  1649. * @throws \yii\db\Exception
  1650. */
  1651. public function calcBonusBsYJCF(int $offset = 0) {
  1652. if( !$this->_isCalcMonth ) {
  1653. // 不是结算月,则不进行计算
  1654. return false;
  1655. }
  1656. // 从缓存获取分页有收入的会员信息
  1657. $allData = CalcBonusBS::findUseDbCalc()
  1658. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1659. ->groupBy('USER_ID')
  1660. ->offset($offset)
  1661. ->limit($this->_limit)
  1662. ->asArray()
  1663. ->all();
  1664. if ($allData) {
  1665. foreach ($allData as $user) {
  1666. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  1667. if ($fxStatus) {
  1668. //扣除相应的复消积分和管理费
  1669. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']);
  1670. // 把对碰后的奖金存入缓存中
  1671. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_ABBR', $user['ORI_BONUS_ABBR']);
  1672. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1673. $manageTax = $deductData['manageTax']; // 管理费
  1674. $point = $deductData['reConsumePoints'];// 复消积分
  1675. } else {
  1676. // 如果未满足月消费,则不扣除管理费和复消积分了
  1677. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_ABBR', $user['ORI_BONUS_ABBR']);
  1678. $realBonusBs = $user['ORI_BONUS_ABBR'];
  1679. $manageTax = 0; // 管理费
  1680. $point = 0;// 复消积分
  1681. }
  1682. // 更新蓝星奖金存储过程的实发金额数据
  1683. CalcBonusBS::updateAll(['AMOUNT_ABBR' => $realBonusBs, 'MANAGE_TAX_ABBR' => $manageTax, 'ABBR_RECONSUME_POINTS' => $point],
  1684. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1685. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1686. }
  1687. return $this->calcBonusBsYJCF($offset + $this->_limit);
  1688. }
  1689. unset($allData);
  1690. return true;
  1691. }
  1692. /**
  1693. * 报单管理奖
  1694. * @param int $offset
  1695. * @return bool
  1696. * @throws \yii\db\Exception
  1697. */
  1698. public function calcBonusBdGL(int $offset = 0) {
  1699. // 从缓存获取分页有收入的会员信息
  1700. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1701. if ($allData) {
  1702. $insertBonusData = [];
  1703. foreach ($allData as $userId) {
  1704. $bonusUserData = [];
  1705. //会员信息
  1706. $validDeep = 1;
  1707. $this->loopRelationParentDo($userId, function ($parent) use ($userId, &$validDeep, &$bonusUserData) {
  1708. //判断$parent是否有首单团队奖
  1709. $bonusUserId = $parent['PARENT_UID'];
  1710. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1711. $nowDecLevelConfig = $this->_decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1712. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1713. if( $recNum <= 0 ) {
  1714. $maxOddLayer = 0;
  1715. }else if( $recNum == 1 ) {
  1716. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'] ?? 0;
  1717. }else if( $recNum == 2 ) {
  1718. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'] ?? 0;
  1719. }else {
  1720. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'] ?? 0;
  1721. }
  1722. if ( $validDeep < 2 * $maxOddLayer && $validDeep % 2 === 1 ) {//奇数层
  1723. $bonusUserData[] = [
  1724. 'bonusUid' => $parent['PARENT_UID'],
  1725. 'validDeep' => $validDeep
  1726. ];
  1727. }
  1728. $parentBonus = CalcCache::bonus($bonusUserId, $this->_periodNum);
  1729. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {//有效层
  1730. $validDeep += 1;
  1731. }
  1732. unset($parent, $parentBonus, $bonusUserId, $bonusUserBaseInfo, $nowDecLevelConfig, $recNum, $maxOddLayer);
  1733. //超过代数上限则结束
  1734. if ( $validDeep >= 18 ) {
  1735. return self::LOOP_FINISH;
  1736. }
  1737. });
  1738. //发奖
  1739. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1740. $userBonus = CalcCache::bonus($userId, $this->_periodNum);
  1741. foreach ($bonusUserData as $everyData) {
  1742. $bonusUserId = $everyData['bonusUid'];
  1743. //判断 是否本期小市场(安置关系)新增980pv 能否得奖
  1744. if ( !$this->checkSmallMarketPerf($bonusUserId) ) {
  1745. // unset($bonusUserId);
  1746. // continue;
  1747. //得奖人不满足980就找一个满足980的人来得这个奖
  1748. $bonusUserId = $this->getMinBdPvRelationParent($bonusUserId);
  1749. if ( !$bonusUserId ) {
  1750. unset($bonusUserId);
  1751. continue;
  1752. }
  1753. }
  1754. $parentOddPercentValue = floor($everyData['validDeep']/6);
  1755. $parentOddPercentName = sprintf('parentOddPercent_%s', $parentOddPercentValue);
  1756. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1757. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1758. $manageBonus = Tool::formatPrice($userBonus['ORI_BONUS_QY_BD'] * $bonusPercent / 100);
  1759. if ($manageBonus <= 0) continue;
  1760. //总金额限制
  1761. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1762. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1763. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1764. if( $manageBonus <= 0 ) continue;
  1765. //扣除相应的复消积分和管理费
  1766. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1767. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_BD);
  1768. CalcCache::addHasMonthBonusUsers($bonusUserId, $this->_periodNum);
  1769. //管理奖流水
  1770. $insertBonusData[] = [
  1771. 'ID' => SnowFake::instance()->generateId(),
  1772. 'USER_ID' => $bonusUserId,
  1773. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1774. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1775. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1776. 'FROM_USER_ID' => $userId,
  1777. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1778. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  1779. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1780. 'AMOUNT' => $deductData['surplus'],
  1781. 'ORI_BONUS' => $manageBonus,
  1782. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1783. 'MANAGE_TAX' => $deductData['manageTax'],
  1784. 'PERIOD_NUM' => $this->_periodNum,
  1785. 'CALC_YEAR' => $this->_calcYear,
  1786. 'CALC_MONTH' => $this->_calcYearMonth,
  1787. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1788. 'CREATED_AT' => Date::nowTime(),
  1789. 'LOGS' => json_encode([
  1790. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1791. 'validDeep' => $everyData['validDeep'],
  1792. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1793. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1794. 'fromUserId' => $userId,
  1795. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1796. ]),
  1797. ];
  1798. unset($everyData, $bonusUserId, $parentOddPercentValue, $parentOddPercentName, $bonusPercent, $manageBonus, $deductData);
  1799. }
  1800. unset($userId, $validDeep, $userBaseInfo, $userBonus, $bonusUserData);
  1801. }
  1802. if($insertBonusData) CalcBonusGL::batchInsert($insertBonusData);
  1803. unset($allData, $insertBonusData);
  1804. return $this->calcBonusBdGL($offset + $this->_limit);
  1805. }
  1806. unset($allData);
  1807. return true;
  1808. }
  1809. /**
  1810. * 复消管理奖
  1811. * @param int $offset
  1812. * @return bool
  1813. * @throws \yii\db\Exception
  1814. */
  1815. public function calcBonusFxGL(int $offset = 0) {
  1816. $periodNum = $this->_periodNum;
  1817. // 从缓存获取分页有收入的会员信息
  1818. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1819. if ($allData) {
  1820. $insertBonusData = [];
  1821. foreach ($allData as $userId) {
  1822. //会员信息
  1823. $decLevelConfig = $this->_decLevelConfig;
  1824. $netWorkParents = Cache::getAllRelationParents($userId);
  1825. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1826. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1827. foreach ($netWorkParents as $netWorkParent) {
  1828. //自已距顶端的深度减去父级距顶端的深度
  1829. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  1830. if( $diffLayer <= 0) continue;
  1831. //@todo 超过最大代数就跳出本次循环
  1832. //偶数代跳过
  1833. if( $diffLayer % 2 == 0 ) continue;
  1834. // 获取会员的报单级别
  1835. $bonusUserId = $netWorkParent['PARENT_UID'];
  1836. if( !$bonusUserId ) continue;
  1837. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1838. $nowDecLevelConfig = $decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1839. if( !$nowDecLevelConfig ) continue;
  1840. //推荐人数
  1841. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1842. if( $recNum <= 0 ) continue;
  1843. if( $recNum == 1 ) {
  1844. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'];
  1845. }else if( $recNum == 2 ) {
  1846. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'];
  1847. }else {
  1848. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'];
  1849. }
  1850. //超过代数上限也跳过
  1851. if( $diffLayer >= 2 * $maxOddLayer ) continue;
  1852. // $everyLayer = floor(2 * $nowDecLevelConfig['GL_ODD_DEEP_THREE'] / $nowDecLevelConfig['SORT']);
  1853. //多少代
  1854. // $theLayer = $diffLayer % $everyLayer;
  1855. // $theLayerLevel = floor($diffLayer/$everyLayer);
  1856. $theLayerLevel = floor($diffLayer/6);
  1857. $parentOddPercentName = sprintf('parentOddPercent_%s', $theLayerLevel);
  1858. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1859. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1860. // 从缓存中获取会员的收入信息
  1861. $incomeBonus = CalcCache::bonus($userId, $periodNum);
  1862. $parentOddIncomeFromName = sprintf('parentOddIncomeFrom_%s', $theLayerLevel);
  1863. $parentOddIncomeFrom = explode(',', $this->_sysConfig[$parentOddIncomeFromName]['VALUE']);
  1864. $parentOddIncome = 0;
  1865. foreach ($parentOddIncomeFrom as $incomeType) {
  1866. if( $incomeType === 'XF' ) {
  1867. $incomeTypeName = sprintf('ORI_BONUS_%s', $incomeType);
  1868. }else {
  1869. $incomeTypeName = sprintf('ORI_BONUS_%s_FX', $incomeType);
  1870. }
  1871. $incomeTypeValue = $incomeBonus[$incomeTypeName] ?? 0;
  1872. $parentOddIncome += $incomeTypeValue;
  1873. unset($incomeType, $incomeTypeName, $incomeTypeValue);
  1874. }
  1875. unset($parentOddIncomeFrom);
  1876. $manageBonus = Tool::formatPrice($parentOddIncome * $bonusPercent / 100);
  1877. if ($manageBonus <= 0) continue;
  1878. //总金额限制
  1879. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1880. if( $manageBonus <= 0 ) continue;
  1881. //扣除相应的复消积分和管理费
  1882. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1883. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_FX);
  1884. //管理奖流水
  1885. $insertBonusData[] = [
  1886. 'ID' => SnowFake::instance()->generateId(),
  1887. 'USER_ID' => $bonusUserId,
  1888. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1889. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1890. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1891. 'FROM_USER_ID' => $userId,
  1892. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1893. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1894. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1895. 'AMOUNT' => $deductData['surplus'],
  1896. 'ORI_BONUS' => $manageBonus,
  1897. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1898. 'MANAGE_TAX' => $deductData['manageTax'],
  1899. 'PERIOD_NUM' => $this->_periodNum,
  1900. 'CALC_YEAR' => $this->_calcYear,
  1901. 'CALC_MONTH' => $this->_calcYearMonth,
  1902. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1903. 'CREATED_AT' => Date::nowTime(),
  1904. 'LOGS' => json_encode([
  1905. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1906. $parentOddIncomeFromName => $this->_sysConfig[$parentOddIncomeFromName]['VALUE'],
  1907. 'decLevelConfig' => $nowDecLevelConfig,
  1908. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1909. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1910. 'fromUserId' => $userId,
  1911. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1912. ]),
  1913. ];
  1914. unset($netWorkParent, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $bonusPercent, $nowDecLevelConfig, $manageBonus, $incomeBonus, $maxOddLayer, $theLayerLevel, $recNum, $parentOddPercentName, $parentOddIncomeFromName, $deductData);
  1915. }
  1916. unset($userId, $userBaseInfo, $userEmpLevel, $netWorkParents, $decLevelConfig);
  1917. }
  1918. CalcBonusGL::batchInsert($insertBonusData);
  1919. unset($allData, $insertBonusData);
  1920. return $this->calcBonusFxGL($offset + $this->_limit);
  1921. }
  1922. unset($allData);
  1923. return true;
  1924. }
  1925. /**
  1926. * 结算荣衔奖第一步
  1927. * @param int $offset
  1928. * @return bool
  1929. * @throws \yii\db\Exception
  1930. * @throws \Exception
  1931. */
  1932. public function calcBonusYCStepOne(int $offset = 0) {
  1933. // 月结,如果不是月结点,则直接退出
  1934. if (!$this->_isCalcMonth) {
  1935. return true;
  1936. }
  1937. echo sprintf("时间:[%s]荣衔奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1938. //从缓存获取分页有本月业绩的会员
  1939. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  1940. if ($allData) {
  1941. $maxLevelId = EmployLevel::SHX_ZJ_LEVEL_ID;
  1942. $maxLevel = $this->_empLevelConfig[$maxLevelId];
  1943. $maxLevelPercent = $maxLevel['RX_PERCENT'] ?? 0;
  1944. unset($maxLevelId, $maxLevel);
  1945. foreach ($allData as $userId) {
  1946. // echo sprintf("时间:[%s]荣衔奖,当前用户ID为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $userId);
  1947. $nowMonthPerf = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1948. $monthPvPcs = $nowMonthPerf['PV_PCS'] ?? 0;
  1949. if( $monthPvPcs <= 0 ) continue;
  1950. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1951. //个人业绩的奖金
  1952. $empLevelId = $userBaseInfo['EMP_LV'];
  1953. $empLevel = $this->_empLevelConfig[$empLevelId];
  1954. $rxPercent = $empLevel['RX_PERCENT'] ?? 0;
  1955. $empBonus = $monthPvPcs * $rxPercent / 100;
  1956. if( $empBonus > 0 ) {
  1957. //给本人添加聘级比例
  1958. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $rxPercent);
  1959. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1960. CalcCache::saveYCBonusList($userId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1961. CalcCache::addHasYcBonusUsers($userId, $this->_periodNum);
  1962. //达到最大的比例其它人就得不到了
  1963. if( $rxPercent >= $maxLevelPercent ) continue;
  1964. }
  1965. unset($empLevelId, $empLevel, $rxPercent, $empBonus);
  1966. //获取所有的父级
  1967. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $monthPvPcs, $maxLevelPercent) {
  1968. $bonusUserId = $parent['PARENT_UID'];
  1969. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  1970. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1971. $parentEmpLevelId = $bonusUserInfo['EMP_LV'];
  1972. $parentEmpLevel = $this->_empLevelConfig[$parentEmpLevelId];
  1973. $parentRxPercent = $parentEmpLevel['RX_PERCENT'] ?? 0;
  1974. $cacheMaxPercent = CalcCache::ycMaxBonusPercent($userId, $this->_periodNum);
  1975. $diffPercent = $parentRxPercent - $cacheMaxPercent;
  1976. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  1977. $empBonus = $monthPvPcs * $diffPercent / 100;
  1978. if( $empBonus <= 0 ) return self::LOOP_CONTINUE;
  1979. //给本人添加聘级比例
  1980. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $parentRxPercent);
  1981. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1982. CalcCache::saveYCBonusList($bonusUserId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1983. CalcCache::addHasYcBonusUsers($bonusUserId, $this->_periodNum);
  1984. unset($cacheMaxPercent, $diffPercent, $parentEmpLevel, $bonusUserId, $bonusUserInfo, $parentEmpLevelId, $empBonus);
  1985. //达到最大的比例就不在向上找了
  1986. if( $parentRxPercent >= $maxLevelPercent ) return self::LOOP_FINISH;
  1987. unset($parentRxPercent);
  1988. });
  1989. unset($nowMonthPerf, $monthPvPcs);
  1990. }
  1991. unset($allData, $maxLevelPercent);
  1992. $this->calcBonusYCStepOne($offset + $this->_limit);
  1993. }
  1994. unset($periodNum, $allData);
  1995. return true;
  1996. }
  1997. /**
  1998. * 结算荣衔奖第二步
  1999. * @param int $offset
  2000. * @return bool
  2001. * @throws \yii\db\Exception
  2002. */
  2003. public function calcBonusYCStepTwo(int $offset = 0) {
  2004. // 月结,如果不是月结点,则直接退出
  2005. if (!$this->_isCalcMonth) {
  2006. return true;
  2007. }
  2008. echo sprintf("时间:[%s]荣衔奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2009. $allData = CalcCache::getHasYcBonusUsers($this->_periodNum, $offset, $this->_limit);
  2010. if ($allData) {
  2011. $insertBonusData = [];
  2012. foreach ($allData as $userId) {
  2013. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  2014. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2015. unset($monthPerfData);
  2016. if( !$fxPvStatus ) continue;
  2017. unset($fxPvStatus);
  2018. $ycBonusData = CalcCache::getYCBonusList($userId, $this->_periodNum);
  2019. if( !$ycBonusData ) continue;
  2020. $empBonus = $ycBonusData['empBonus'] ?? 0;
  2021. if( $empBonus <=0 ) continue;
  2022. //总金额限制
  2023. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2024. $empBonus = $this->bonusTotalLimit($empBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  2025. if( $empBonus <= 0 ) continue;
  2026. //扣除相应的复消积分和管理费
  2027. $empBonus = Tool::formatPrice($empBonus);
  2028. $deductData = $this->deduct($userId, $empBonus);
  2029. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_YC', $empBonus, $deductData);
  2030. $empLevelId = $userBaseInfo['EMP_LV'];
  2031. $insertBonusData[] = [
  2032. 'ID' => SnowFake::instance()->generateId(),
  2033. 'USER_ID' => $userId,
  2034. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2035. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2036. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2037. 'FROM_USER_ID' => $userId,
  2038. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2039. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2040. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2041. 'DEEP' => 0,
  2042. 'AMOUNT' => $deductData['surplus'],
  2043. 'ORI_BONUS' => $empBonus,
  2044. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2045. 'MANAGE_TAX' => $deductData['manageTax'],
  2046. 'BONUS_TYPE' => CalcBonusYC::BONUS_TYPE_YC,
  2047. 'PERIOD_NUM' => $this->_periodNum,
  2048. 'CALC_MONTH' => $this->_calcYearMonth,
  2049. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2050. 'CREATED_AT' => Date::nowTime(),
  2051. 'LOGS' => json_encode([
  2052. 'empLevelId' => $empLevelId,
  2053. ])
  2054. // 'FROM_DATA' => json_encode($ycBonusData['fromData']),
  2055. ];
  2056. unset($ycBonusData, $deductData);
  2057. //如果是总监以上拿平级2代
  2058. $empLevel = $this->_empLevelConfig[$empLevelId];
  2059. $userEmpLevelSort = $empLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  2060. // if( $userEmpLevelSort >= EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {}
  2061. $newInsertBonusData = $this->calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $empBonus);
  2062. $insertBonusData = array_merge($insertBonusData, $newInsertBonusData);
  2063. unset($newInsertBonusData);
  2064. unset($userId, $userBaseInfo, $empLevel, $empLevelId, $userEmpLevelSort, $empBonus);
  2065. }
  2066. CalcBonusYC::batchInsert($insertBonusData);
  2067. unset($insertBonusData, $allData);
  2068. $this->calcBonusYCStepTwo($offset + $this->_limit);
  2069. }
  2070. unset($allData);
  2071. return true;
  2072. }
  2073. /**
  2074. * VIP奖
  2075. * @param int $offset
  2076. * @return bool
  2077. * @throws \yii\db\Exception
  2078. */
  2079. public function calcBonusVIP(int $offset = 0)
  2080. {
  2081. // 月结,如果不是月结点,则直接退出
  2082. if (!$this->_isCalcMonth) {
  2083. return true;
  2084. }
  2085. echo sprintf("时间:[%s]VIP奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2086. //从缓存获取分页有本月业绩的会员
  2087. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2088. if ($allData) {
  2089. $insertBonusData = [];
  2090. foreach ($allData as $userId) {
  2091. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2092. //条件一:级别必须为VIP
  2093. $isVip = false;
  2094. if( $userBaseInfo['DEC_LV'] === DeclarationLevel::VIP_LEVEL_ID ) {
  2095. $isVip= true;
  2096. }
  2097. if( $this->_sysConfig['vipBonusGoldDecLevel']['VALUE'] && $userBaseInfo['DEC_LV'] === DeclarationLevel::JIN_ZUAN_LEVEL_ID ) {
  2098. $isVip = true;
  2099. }
  2100. if ( !$isVip ) {
  2101. unset($userId, $userBaseInfo);
  2102. continue;
  2103. }
  2104. //判断其它两个条件,满足则计算月团队奖,不满足则留下大区剩余业绩,清除其它区的剩余业绩
  2105. //获取本月的业绩
  2106. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  2107. // 从缓存中获取会员的上期结余业绩信息
  2108. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2109. // 本期 + 上期结余
  2110. $perfArr = [
  2111. 'VIP_SURPLUS_1L_ZC' => $monthPerfData['VIP_PV_1L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_1L_ZC'],
  2112. 'VIP_SURPLUS_2L_ZC' => $monthPerfData['VIP_PV_2L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_2L_ZC'],
  2113. 'VIP_SURPLUS_3L_ZC' => $monthPerfData['VIP_PV_3L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_3L_ZC'],
  2114. 'VIP_SURPLUS_4L_ZC' => $monthPerfData['VIP_PV_4L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_4L_ZC'],
  2115. 'VIP_SURPLUS_5L_ZC' => $monthPerfData['VIP_PV_5L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_5L_ZC'],
  2116. ];
  2117. //条件二:本月复消金额达到300元
  2118. //fix-2021-03-26修改 条件二:本月个人复消PV300
  2119. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2120. unset($monthPerfData, $pervSurplusPerf);
  2121. if( !$fxPvStatus ) {
  2122. $this->dealWithMaxPerf($userId, $perfArr);
  2123. unset($userBaseInfo, $userId, $perfArr, $fxPvStatus);
  2124. continue;
  2125. }
  2126. //条件三:其它市场业绩
  2127. $isCanOtherDepartPerf = $this->_isCanVipBonusOtherAreaPerf($perfArr);
  2128. if ( !$isCanOtherDepartPerf ) {
  2129. $this->dealWithMaxPerf($userId, $perfArr);
  2130. unset($userBaseInfo, $userId, $perfArr, $isCanOtherDepartPerf);
  2131. continue;
  2132. }
  2133. $oriPerfArr = [
  2134. 'perfArr' => $perfArr,
  2135. 'touchBonus' => 0,
  2136. ];
  2137. // 对碰
  2138. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $this->_sysConfig['vipBonusPercent']['VALUE']/100);
  2139. unset($oriPerfArr, $perfArr);
  2140. $touchPerfArr = [];
  2141. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  2142. $touchPerfArr[$keyR] = $perfR;
  2143. }
  2144. // 对碰完成后把结余的业绩存入本期业绩缓存中
  2145. // CalcCache::nowPeriodPerf($userId, $this->_periodNum, $touchPerfArr);
  2146. //更新数据库
  2147. PerfMonth::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2148. 'USER_ID' => $userId,
  2149. 'CALC_MONTH' => $this->_calcYearMonth,
  2150. ]);
  2151. if ($touchBonusArr['touchBonus'] <= 0) continue;
  2152. $vipBonus = $touchBonusArr['touchBonus'];
  2153. //判断级别上限
  2154. $vipBonus = $this->declarationLevelCap($vipBonus, $userId, $userBaseInfo['DEC_LV']);
  2155. if( $vipBonus <= 0 ) continue;
  2156. $vipBonus = $this->bonusTotalLimit($vipBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  2157. if( $vipBonus <= 0 ) continue;
  2158. //扣除相应的复消积分和管理费
  2159. $deductData = $this->deduct($userId, $vipBonus);
  2160. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_VIP', $vipBonus, $deductData);
  2161. $insertBonusData[] = [
  2162. 'ID' => SnowFake::instance()->generateId(),
  2163. 'USER_ID' => $userId,
  2164. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2165. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2166. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2167. 'FROM_USER_ID' => $userId,
  2168. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2169. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2170. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2171. 'AMOUNT' => $deductData['surplus'],
  2172. 'ORI_BONUS' => $vipBonus,
  2173. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2174. 'MANAGE_TAX' => $deductData['manageTax'],
  2175. 'PERIOD_NUM' => $this->_periodNum,
  2176. 'CALC_YEAR' => $this->_calcYear,
  2177. 'CALC_MONTH' => $this->_calcYearMonth,
  2178. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2179. 'CREATED_AT' => Date::nowTime(),
  2180. 'LOGS' => json_encode($touchPerfArr)
  2181. ];
  2182. unset($userBaseInfo, $userId, $perfArr, $fxAmountStatus, $isCanOtherDepartPerf, $touchBonusArr, $touchPerfArr, $vipBonus, $deductData);
  2183. }
  2184. if( $insertBonusData ) CalcBonusVIP::batchInsert($insertBonusData);
  2185. unset($insertBonusData, $allData);
  2186. $this->calcBonusVIP($offset + $this->_limit);
  2187. }
  2188. unset($allData);
  2189. return true;
  2190. }
  2191. /**
  2192. * 见习达标奖
  2193. * @param int $offset
  2194. * @return bool
  2195. * @throws \yii\db\Exception
  2196. */
  2197. public function calcBonusStandard(int $offset = 0)
  2198. {
  2199. // 月结,如果不是月结点,则直接退出
  2200. if (!$this->_isCalcMonth) {
  2201. return true;
  2202. }
  2203. echo sprintf("时间:[%s]见习达标奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2204. //从缓存获取分页有本月业绩的会员
  2205. $allData = CalcCache::getHasStandardMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2206. if ($allData) {
  2207. $minSaleCondition = $this->_jxStandardMinSaleCondition();
  2208. $insertBonusData = [];
  2209. foreach ($allData as $userId) {
  2210. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2211. //条件一:聘级必须为见习主任
  2212. if( $userBaseInfo['EMP_LV'] != EmployLevel::JX_ZR_LEVEL_ID ) {
  2213. unset($userId, $userBaseInfo);
  2214. continue;
  2215. }
  2216. $standardMonthPerf = CalcCache::nowStandardMonthPerf($userId, $this->_periodNum);
  2217. $totalAmount = $standardMonthPerf['AMOUNT_PCS'] + $standardMonthPerf['AMOUNT_PSS'];
  2218. if( $totalAmount < $minSaleCondition ) {
  2219. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2220. continue;
  2221. }
  2222. $standardBonus = $this->_jxStandardAwardAmount($totalAmount);
  2223. if( $standardBonus <= 0 ) {
  2224. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount, $standardBonus);
  2225. continue;
  2226. }
  2227. CalcCache::standardBonus($userId, $this->_periodNum, $standardBonus);
  2228. $insertBonusData[] = [
  2229. 'ID' => SnowFake::instance()->generateId(),
  2230. 'USER_ID' => $userId,
  2231. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2232. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2233. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2234. 'FROM_USER_ID' => $userId,
  2235. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2236. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2237. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2238. 'AMOUNT' => $standardBonus,
  2239. 'ORI_BONUS' => $standardBonus,
  2240. 'RECONSUME_POINTS' => 0,
  2241. 'MANAGE_TAX' => 0,
  2242. 'PERIOD_NUM' => $this->_periodNum,
  2243. 'CALC_YEAR' => $this->_calcYear,
  2244. 'CALC_MONTH' => $this->_calcYearMonth,
  2245. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2246. 'CREATED_AT' => Date::nowTime(),
  2247. 'LOGS' => json_encode($standardMonthPerf)
  2248. ];
  2249. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2250. }
  2251. if( $insertBonusData ) CalcBonusStandard::batchInsert($insertBonusData);
  2252. unset($insertBonusData, $minSaleCondition, $insertBonusData, $allData);
  2253. $this->calcBonusStandard($offset + $this->_limit);
  2254. }
  2255. unset($allData);
  2256. return true;
  2257. }
  2258. /**
  2259. * === 运算相关的辅助功能开始 ===
  2260. */
  2261. /**
  2262. * 获取上2代的得奖人信息
  2263. * @param $userId
  2264. * @param $validDeep
  2265. * @param $shareBonusOne
  2266. * @return array
  2267. */
  2268. protected function gxLastTwoBonusData($userId, $validDeep, $shareBonusOne) {
  2269. $bonusUserList = CalcCache::getShareBonusOneRelation($userId, $this->_periodNum, $validDeep);
  2270. if( !$bonusUserList ) {
  2271. unset($bonusUserList);
  2272. return [];
  2273. }
  2274. $bonusUserData = [];
  2275. $bonusUserTotalPerf = 0;
  2276. $bonusUserRealList = [];
  2277. foreach ($bonusUserList as $bonusUserId) {
  2278. //查看该用户的个人业绩和团队新增业绩总和
  2279. $nowPeriodData = CalcCache::nowPeriodPerf($bonusUserId, $this->_periodNum);
  2280. //本期新增业绩
  2281. $bonusUserPerf = $nowPeriodData['PV_PCS'] + $nowPeriodData['PV_PSS'];
  2282. if ($bonusUserPerf <= 0) {
  2283. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2284. continue;
  2285. }
  2286. $bonusUserTotalPerf += $bonusUserPerf;
  2287. $bonusUserRealList[] = [
  2288. 'bonusUserId' => $bonusUserId,
  2289. 'bonusUserPerf' => $bonusUserPerf,
  2290. ];
  2291. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2292. }
  2293. unset($bonusUserList);
  2294. foreach ($bonusUserRealList as $everyData) {
  2295. $bonusUserData[] = [
  2296. 'bonusUid' => $everyData['bonusUserId'],
  2297. 'bonus' => $shareBonusOne * $everyData['bonusUserPerf'] / $bonusUserTotalPerf,
  2298. 'validMinPv' => false
  2299. ];
  2300. unset($everyData);
  2301. }
  2302. unset($bonusUserRealList);
  2303. return $bonusUserData;
  2304. }
  2305. /**
  2306. * 获取达标奖的金额
  2307. * @param $amount
  2308. * @return int|mixed
  2309. */
  2310. private function _jxStandardAwardAmount($amount) {
  2311. // $jxStandardConfig = Json::decode($this->_sysConfig['jxStandard']['VALUE']);
  2312. // $awardAmount = 0;
  2313. // foreach ($jxStandardConfig as $everyData) {
  2314. // if( $amount >= $everyData['salesCondition'] ) {
  2315. // $awardAmount = $everyData['awardAmount'];
  2316. // }
  2317. // unset($everyData);
  2318. // }
  2319. //
  2320. // return $awardAmount;
  2321. $realTimes = floor($amount/self::JX_STANDARD_BASE_AMOUNT);
  2322. $calcTimes = min($realTimes, self::JX_STANDARD_MAX_TIMES);
  2323. $awardAmount = $calcTimes * self::JX_STANDARD_BASE_AMOUNT * self::JX_STANDARD_BONUS_PERCENT / 100;
  2324. unset($amount, $realTimes, $calcTimes);
  2325. return $awardAmount;
  2326. }
  2327. /**
  2328. * 获取达标奖的最小条件
  2329. * @return int|mixed
  2330. */
  2331. private function _jxStandardMinSaleCondition() {
  2332. return self::JX_STANDARD_BASE_AMOUNT;
  2333. }
  2334. /**
  2335. * 处理只留最大业绩的逻辑
  2336. * @param $userId
  2337. * @param $perfArr
  2338. */
  2339. private function dealWithMaxPerf($userId, $perfArr) {
  2340. $maxPerf = max($perfArr);
  2341. $hitMaxPerf = false;
  2342. foreach ($perfArr as $key=>$everyPerf) {
  2343. if( !$hitMaxPerf && $everyPerf == $maxPerf ) {
  2344. $hitMaxPerf = true;
  2345. }else {
  2346. $perfArr[$key] = 0;
  2347. }
  2348. unset($key, $everyPerf);
  2349. }
  2350. //更新数据库
  2351. PerfMonth::updateAll($perfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2352. 'USER_ID' => $userId,
  2353. 'CALC_MONTH' => $this->_calcYearMonth
  2354. ]);
  2355. unset($userId, $maxPerf, $hitMaxPerf, $perfArr);
  2356. }
  2357. /**
  2358. * VIP奖其它市场的报单业绩是否能满足
  2359. * @param $perfArr
  2360. * @return bool
  2361. */
  2362. private function _isCanVipBonusOtherAreaPerf($perfArr) {
  2363. $maxAreaPerf = max($perfArr);
  2364. $totalAreaPerf = array_sum($perfArr);
  2365. $diffAreaPerf = strval($totalAreaPerf - $maxAreaPerf);
  2366. if( $diffAreaPerf >= $this->_sysConfig['vipBonusOtherDepartPvCondition']['VALUE'] ) {
  2367. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2368. return true;
  2369. }else {
  2370. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2371. return false;
  2372. }
  2373. }
  2374. /**
  2375. * 给父级计算的荣衔奖
  2376. * @param $userId
  2377. * @param $userBaseInfo
  2378. * @param $userEmpLevelSort
  2379. * @param $qyBonus
  2380. * @return array
  2381. */
  2382. public function calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus) {
  2383. if( $userEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return [];
  2384. $validDeep = 0;
  2385. $bonusDataList = [];
  2386. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $userEmpLevelSort, &$validDeep, &$bonusDataList) {
  2387. $bonusUserId = $parent['PARENT_UID'];
  2388. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2389. $bonusUserEmpLevel = $this->_empLevelConfig[$bonusUserInfo['EMP_LV']];
  2390. $bonusUserEmpLevelSort = $bonusUserEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  2391. unset($bonusUserInfo, $bonusUserEmpLevel);
  2392. //必须是有聘级的用户
  2393. if( $bonusUserEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return self::LOOP_CONTINUE;
  2394. //超过级别就停止
  2395. if( $bonusUserEmpLevelSort > $userEmpLevelSort) return self::LOOP_FINISH;
  2396. //级别小于就跳过
  2397. if( $bonusUserEmpLevelSort < $userEmpLevelSort ) return self::LOOP_CONTINUE;
  2398. $validDeep++;
  2399. array_push($bonusDataList, [
  2400. 'bonusUserId' => $bonusUserId,
  2401. 'bonusType' => $validDeep,
  2402. ]);
  2403. if( $validDeep >= 2 ) return self::LOOP_FINISH;
  2404. unset($bonusUserId, $bonusUserEmpLevelSort);
  2405. });
  2406. $insertBonusData = [];
  2407. foreach ($bonusDataList as $bonusUserData) {
  2408. $bonusUserId = $bonusUserData['bonusUserId'];
  2409. $bonusMonthPerfData = CalcCache::nowMonthPerf($bonusUserId, $this->_periodNum);
  2410. $fxPvStatus = $bonusMonthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2411. unset($bonusMonthPerfData);
  2412. if( !$fxPvStatus ) continue;
  2413. unset($fxPvStatus);
  2414. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2415. $bonusEmpLevelId = $bonusUserInfo['EMP_LV'];
  2416. $newEmpBonus = Tool::formatPrice($qyBonus * $this->_sysConfig['sameEmpLevelPercent']['VALUE'] / 100);
  2417. //总金额限制
  2418. $newEmpBonus = $this->bonusTotalLimit($newEmpBonus, $bonusUserId, $bonusUserInfo['REC_NUM'], $bonusUserInfo['ZC_AMOUNT']);
  2419. if( $newEmpBonus <= 0 ) continue;
  2420. //扣除相应的复消积分和管理费
  2421. $newEmpBonus = Tool::formatPrice($newEmpBonus);
  2422. $deductData = $this->deduct($bonusUserId, $newEmpBonus);
  2423. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_YC_EXTRA', $newEmpBonus, $deductData);
  2424. // 把育成津贴追加到育成津贴结算表,以便奖金追溯育成津贴用
  2425. $insertBonusData[] = [
  2426. 'ID' => SnowFake::instance()->generateId(),
  2427. 'USER_ID' => $bonusUserId,
  2428. 'LAST_DEC_LV' => $bonusUserInfo['DEC_LV'],
  2429. 'LAST_EMP_LV' => $bonusUserInfo['EMP_LV'],
  2430. 'LAST_STATUS' => $bonusUserInfo['STATUS'],
  2431. 'FROM_USER_ID' => $userId,
  2432. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2433. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2434. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2435. 'DEEP' => 1,
  2436. 'AMOUNT' => $deductData['surplus'],
  2437. 'ORI_BONUS' => $newEmpBonus,
  2438. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2439. 'MANAGE_TAX' => $deductData['manageTax'],
  2440. 'BONUS_TYPE' => $bonusUserData['bonusType'],
  2441. 'PERIOD_NUM' => $this->_periodNum,
  2442. 'CALC_MONTH' => $this->_calcYearMonth,
  2443. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2444. 'CREATED_AT' => Date::nowTime(),
  2445. 'LOGS' => json_encode([
  2446. 'empLevelId' => $bonusEmpLevelId,
  2447. 'qyBonus' => $qyBonus,
  2448. 'sameEmpLevelPercent' => $this->_sysConfig['sameEmpLevelPercent']['VALUE'],
  2449. ]),
  2450. ];
  2451. unset($newEmpBonus, $bonusEmpLevelId, $bonusUserId, $bonusUserInfo, $deductData);
  2452. }
  2453. unset($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus, $bonusDataList);
  2454. return $insertBonusData;
  2455. }
  2456. /**
  2457. * 对碰
  2458. * @param array $oriPerfArr
  2459. * @param array $perfArr
  2460. * @param $percent
  2461. * @param $loopTimes
  2462. * @return array
  2463. */
  2464. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  2465. $resultArr = $oriPerfArr;
  2466. foreach ($perfArr as $keyT => $perfT) {
  2467. if ($perfT <= 0) {
  2468. unset($perfArr[$keyT]);
  2469. }
  2470. }
  2471. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2472. arsort($perfArr, SORT_NUMERIC);
  2473. $onePerf = null;
  2474. $oneKey = null;
  2475. // $touchSurplusAmount = 0;
  2476. $touchAmount = 0;
  2477. // 前两个进行对碰
  2478. foreach ($perfArr as $key => $perf) {
  2479. if ($onePerf === null) {
  2480. $oneKey = $key;
  2481. $onePerf = $perf;
  2482. } else {
  2483. $touchSurplusAmount = $perf - $onePerf;
  2484. if ($touchSurplusAmount > 0) {
  2485. unset($perfArr[$oneKey]);
  2486. $resultArr['perfArr'][$oneKey] = 0;
  2487. $perfArr[$key] = $touchSurplusAmount;
  2488. $touchAmount = $onePerf;
  2489. } elseif ($touchSurplusAmount < 0) {
  2490. unset($perfArr[$key]);
  2491. $resultArr['perfArr'][$key] = 0;
  2492. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2493. $touchAmount = $perf;
  2494. } else {
  2495. unset($perfArr[$oneKey], $perfArr[$key]);
  2496. $resultArr['perfArr'][$oneKey] = 0;
  2497. $resultArr['perfArr'][$key] = 0;
  2498. $touchAmount = $perf;
  2499. }
  2500. break;
  2501. }
  2502. }
  2503. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2504. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2505. }*/
  2506. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  2507. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2508. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2509. // } else {
  2510. // $resultArr['touchBonus'] += $touchBonus;
  2511. // }
  2512. $resultArr['touchBonus'] += $touchBonus;
  2513. foreach ($perfArr as $keyR => $perfR) {
  2514. $resultArr['perfArr'][$keyR] = $perfR;
  2515. }
  2516. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  2517. }
  2518. return $resultArr;
  2519. }
  2520. /**
  2521. * 对碰
  2522. * @param array $oriPerfArr
  2523. * @param array $perfArr
  2524. * @param $decLevel
  2525. * @param $decLevelConfig
  2526. * @param $loopTimes
  2527. * @return array
  2528. */
  2529. public function touchPerfBefore(array $oriPerfArr, array $perfArr, $decLevel, $decLevelConfig, $loopTimes=1) {
  2530. $resultArr = $oriPerfArr;
  2531. foreach ($perfArr as $keyT => $perfT) {
  2532. if ($perfT <= 0) {
  2533. unset($perfArr[$keyT]);
  2534. }
  2535. }
  2536. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2537. $nowDecLevelConfig = $decLevelConfig[$decLevel];
  2538. arsort($perfArr, SORT_NUMERIC);
  2539. $onePerf = null;
  2540. $oneKey = null;
  2541. // $touchSurplusAmount = 0;
  2542. $touchAmount = 0;
  2543. // 前两个进行对碰
  2544. foreach ($perfArr as $key => $perf) {
  2545. if ($onePerf === null) {
  2546. $oneKey = $key;
  2547. $onePerf = $perf;
  2548. } else {
  2549. $touchSurplusAmount = $perf - $onePerf;
  2550. if ($touchSurplusAmount > 0) {
  2551. unset($perfArr[$oneKey]);
  2552. $resultArr['perfArr'][$oneKey] = 0;
  2553. $perfArr[$key] = $touchSurplusAmount;
  2554. $touchAmount = $onePerf;
  2555. } elseif ($touchSurplusAmount < 0) {
  2556. unset($perfArr[$key]);
  2557. $resultArr['perfArr'][$key] = 0;
  2558. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2559. $touchAmount = $perf;
  2560. } else {
  2561. unset($perfArr[$oneKey], $perfArr[$key]);
  2562. $resultArr['perfArr'][$oneKey] = 0;
  2563. $resultArr['perfArr'][$key] = 0;
  2564. $touchAmount = $perf;
  2565. }
  2566. break;
  2567. }
  2568. }
  2569. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2570. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2571. }*/
  2572. $touchBonus = Tool::formatPrice($touchAmount * 2 / 100);
  2573. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2574. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2575. // } else {
  2576. // $resultArr['touchBonus'] += $touchBonus;
  2577. // }
  2578. $resultArr['touchBonus'] += $touchBonus;
  2579. foreach ($perfArr as $keyR => $perfR) {
  2580. $resultArr['perfArr'][$keyR] = $perfR;
  2581. }
  2582. return $this->touchPerfBefore($resultArr, $perfArr, $decLevel, $decLevelConfig, $loopTimes+1);
  2583. }
  2584. return $resultArr;
  2585. }
  2586. /**
  2587. * 循环父级并执行回调函数
  2588. * @param $userId
  2589. * @param callable $callbackFunc
  2590. * @param int $offset
  2591. * @return bool
  2592. */
  2593. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2594. $allParents = Cache::getAllNetworkParents($userId);
  2595. $allData = array_slice($allParents, $offset, $this->_limit);
  2596. unset($allParents);
  2597. if ($allData) {
  2598. foreach ($allData as $data) {
  2599. $funcResult = $callbackFunc($data);
  2600. if ($funcResult === self::LOOP_FINISH) {
  2601. return true;
  2602. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2603. continue;
  2604. }
  2605. unset($data, $funcResult);
  2606. }
  2607. unset($allData);
  2608. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2609. }
  2610. return true;
  2611. }
  2612. /**
  2613. * 循环推荐网络的父级
  2614. * @param $userId
  2615. * @param callable $callbackFunc
  2616. * @param int $offset
  2617. * @return bool
  2618. */
  2619. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2620. $allParents = Cache::getAllRelationParents($userId);
  2621. $allData = array_slice($allParents, $offset, $this->_limit);
  2622. unset($allParents);
  2623. if ($allData) {
  2624. foreach ($allData as $data) {
  2625. $funcResult = $callbackFunc($data);
  2626. if ($funcResult === self::LOOP_FINISH) {
  2627. return true;
  2628. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2629. continue;
  2630. }
  2631. unset($data, $funcResult);
  2632. }
  2633. unset($allData);
  2634. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2635. }
  2636. return true;
  2637. }
  2638. /**
  2639. * 共享奖,判断当期此用户,是否有原报单团队奖金
  2640. */
  2641. public function checkHasOriBonusQyBd($userId) {
  2642. //判断$parent是否有首单团队奖
  2643. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  2644. if( isset($bonus['ORI_BONUS_QY_BD']) && $bonus['ORI_BONUS_QY_BD'] > 0 ) {
  2645. return true;
  2646. }
  2647. return false;
  2648. }
  2649. /**
  2650. * 判断除大市场外 其它市场的报单业绩
  2651. * @param $userId
  2652. * @return bool
  2653. * @throws \yii\db\Exception
  2654. */
  2655. public function checkSmallMarketPerf($userId) {
  2656. // 从缓存中获取会员的上期结余业绩信息
  2657. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2658. //判断出大市场
  2659. $areaNums = [1, 2, 3];
  2660. $maxArea = 0;
  2661. $maxSurplusPerf = 0;
  2662. foreach ($areaNums as $area) {
  2663. $areaKey = sprintf('SURPLUS_%dL', $area);
  2664. if ( isset($pervSurplusPerf[$areaKey]) && $pervSurplusPerf[$areaKey] > $maxSurplusPerf ) {
  2665. $maxArea = $area;
  2666. $maxSurplusPerf = $pervSurplusPerf[$areaKey];
  2667. }
  2668. unset($area, $areaKey);
  2669. }
  2670. unset($pervSurplusPerf, $maxSurplusPerf);
  2671. $otherAreaZcPv = 0;
  2672. $nowPeriodPerf = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  2673. foreach ($areaNums as $area) {
  2674. if( $maxArea > 0 && $area === $maxArea ) continue;
  2675. $areaKey = sprintf('PV_%dL_ZC', $area);
  2676. if ( isset($nowPeriodPerf[$areaKey]) && $nowPeriodPerf[$areaKey] > 0 ) {
  2677. $otherAreaZcPv += $nowPeriodPerf[$areaKey];
  2678. }
  2679. unset($area, $areaKey);
  2680. }
  2681. unset($nowPeriodPerf, $areaNums, $maxArea);
  2682. return $otherAreaZcPv >= self::MIN_BD_PV;
  2683. }
  2684. /**
  2685. * 获取有最小报单PV的用效父级
  2686. * @param $userId
  2687. * @return string
  2688. */
  2689. public function getMinBdPvRelationParent($userId) {
  2690. $validParentId = '';
  2691. $this->loopRelationParentDo($userId, function ($parent) use(&$validParentId) {
  2692. if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2693. $validParentId = $parent['PARENT_UID'];
  2694. unset($parent);
  2695. return self::LOOP_FINISH;
  2696. }
  2697. unset($parent);
  2698. });
  2699. return$validParentId;
  2700. }
  2701. /**
  2702. * 获取有最小报单PV的用效父级 20220407修改成是否此期此用户有原报单团队奖金
  2703. * @param $userId
  2704. * @return string
  2705. */
  2706. public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2707. $validParentId = '';
  2708. $validDeep = 1;
  2709. $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2710. if ( $this->checkHasOriBonusQyBd($parent['PARENT_UID']) ) {
  2711. $validParentId = $parent['PARENT_UID'];
  2712. unset($parent);
  2713. return self::LOOP_FINISH;
  2714. }
  2715. // 只找5层
  2716. if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2717. unset($parent);
  2718. return self::LOOP_FINISH;
  2719. }
  2720. $validDeep += 1;
  2721. unset($parent);
  2722. });
  2723. return$validParentId;
  2724. }
  2725. // public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2726. // $validParentId = '';
  2727. // $validDeep = 1;
  2728. // $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2729. // if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2730. // $validParentId = $parent['PARENT_UID'];
  2731. // unset($parent);
  2732. // return self::LOOP_FINISH;
  2733. // }
  2734. // // 只找5层
  2735. // if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2736. // unset($parent);
  2737. // return self::LOOP_FINISH;
  2738. // }
  2739. // $validDeep += 1;
  2740. // unset($parent);
  2741. // });
  2742. // return$validParentId;
  2743. // }
  2744. /**
  2745. * 按级别的收入上限
  2746. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  2747. * @param $bonus
  2748. * @param $userId
  2749. * @param $declarationLevel
  2750. * @return float
  2751. */
  2752. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2753. $decLevelConfig = $this->_decLevelConfig;
  2754. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2755. unset($decLevelConfig);
  2756. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  2757. if( $bonus <= $maxGetBonus) {
  2758. return $bonus;
  2759. }else {
  2760. return $maxGetBonus;
  2761. }
  2762. }
  2763. // public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2764. // $decLevelConfig = $this->_decLevelConfig;
  2765. // $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2766. // unset($decLevelConfig);
  2767. // //本月往期奖金 这里执行速度可能慢一些,但需求如此没有办法
  2768. // $lastData = CalcCache::lastPeriodMonthCalcBonus($userId, $this->_periodNum, $this->_calcMonth);
  2769. // $lastTotal = $lastData['ORI_BONUS_QY_SUM'] + $lastData['ORI_BONUS_VIP_SUM'];
  2770. // // 从缓存中获取用户的奖金
  2771. // $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2772. // $thisTotal = $bonusData['ORI_BONUS_QY'] + $bonusData['ORI_BONUS_VIP'];
  2773. // $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'] * $this->_calcMonthPeriodNumCount - $lastTotal - $thisTotal;
  2774. // unset($lastData, $lastTotal, $bonusData, $thisTotal);
  2775. // if( $maxGetBonus <= 0 ) return 0.00;
  2776. // if( $bonus <= $maxGetBonus) {
  2777. // return $bonus;
  2778. // }else {
  2779. // return $maxGetBonus;
  2780. // }
  2781. // }
  2782. /**
  2783. * 总奖金限制
  2784. * @param $bonus
  2785. * @param $userId
  2786. * @param $recNum
  2787. * @param $decAmount
  2788. * @return float
  2789. */
  2790. public function bonusTotalLimit($bonus, $userId, $recNum, $decAmount) {
  2791. //@todo 临时不做总奖金限制
  2792. return $bonus;
  2793. if( $recNum >= 3 ) return $bonus;
  2794. $limitAmount = 0;
  2795. switch ($recNum) {
  2796. case 0:
  2797. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalZeroLimit']['VALUE'];
  2798. break;
  2799. case 1:
  2800. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalOneLimit']['VALUE'];
  2801. break;
  2802. case 2:
  2803. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalTwoLimit']['VALUE'];
  2804. break;
  2805. default:
  2806. break;
  2807. }
  2808. // 从缓存中获取用户的奖金
  2809. $lastUserBonus = CalcCache::LastUserBonus($userId, $this->_periodNum);//往期的总金额
  2810. $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2811. $bonusTotal = $bonusData['BONUS_TOTAL'] + $lastUserBonus['BONUS_TOTAL'];
  2812. unset($lastUserBonus, $userId, $recNum, $decAmount, $bonusData);
  2813. if( $bonusTotal >= $limitAmount ) return 0.00;
  2814. if( $bonusTotal + $bonus <= $limitAmount) {
  2815. return $bonus;
  2816. }else {
  2817. return $limitAmount - $bonusTotal;
  2818. }
  2819. }
  2820. /**
  2821. * 扣除复消积分和管理费
  2822. * @param $userId
  2823. * @param $bonus
  2824. * @return array
  2825. * @throws \yii\db\Exception
  2826. */
  2827. public function deductNoReconsumePoints($userId, $bonus): array
  2828. {
  2829. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2830. $surplus = $bonus - $manageTax;
  2831. return [
  2832. 'reConsumePoints' => 0.00,
  2833. 'manageTax' => Tool::formatPrice($manageTax),
  2834. 'surplus' => Tool::formatPrice($surplus),
  2835. ];
  2836. }
  2837. /**
  2838. * 扣除复消积分和管理费
  2839. * @param $userId
  2840. * @param $bonus
  2841. * @return array
  2842. * @throws \yii\db\Exception
  2843. */
  2844. public function deduct($userId, $bonus, $isGx = false) {
  2845. // 如果是共享奖金,则只有最后一期,才扣除管理费?其他三期不扣除管理费.
  2846. //判断是否达到了本月扣除复消的上限
  2847. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  2848. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  2849. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $cacheData['RECONSUME_POINTS_SUM'];
  2850. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  2851. unset($cacheData, $bonusCache);
  2852. $reConsumePoints = 0;
  2853. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  2854. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  2855. // 2022/06/07上限改成10000000 奖金乘以比例 上限1000 // 当月总复消积分 前三期加当前的
  2856. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  2857. }
  2858. unset($reConsumePointsTotal, $reConsumePointsCap);
  2859. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2860. $surplus = $bonus - $reConsumePoints - $manageTax;
  2861. return [
  2862. 'reConsumePoints' => Tool::formatPrice($reConsumePoints),
  2863. 'manageTax' => Tool::formatPrice($manageTax),
  2864. 'surplus' => Tool::formatPrice($surplus),
  2865. ];
  2866. }
  2867. /**
  2868. * 更新百分比并发送
  2869. * @param $percent
  2870. */
  2871. private function _updatePercent($percent) {
  2872. // 把数据写入数据库中
  2873. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  2874. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  2875. }
  2876. /**
  2877. * 把往期有月奖金的会员加到本期有奖会员缓存列表中
  2878. * @param int $offset
  2879. * @return bool
  2880. */
  2881. public function loopMonthBonusUserFromDbToCache($offset = 0) {
  2882. if (!$this->_isCalcMonth) {
  2883. return true;
  2884. }
  2885. $allData = CalcMonthBonusUser::findUseDbCalc()->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->asArray()->all();
  2886. if ( $allData ) {
  2887. foreach ($allData as $everyData) {
  2888. CalcCache::addHasBonusUsers($everyData['USER_ID'], $this->_periodNum);
  2889. unset($everyData);
  2890. }
  2891. unset($allData);
  2892. $this->loopMonthBonusUserFromDbToCache($offset + $this->_limit);
  2893. }
  2894. unset($allData);
  2895. return true;
  2896. }
  2897. /**
  2898. * 把缓存中的月奖用户信息存到数据库
  2899. * @param int $offset
  2900. * @return bool
  2901. * @throws \yii\db\Exception
  2902. */
  2903. public function loopMonthBonusUserToDb($offset = 0) {
  2904. echo sprintf("时间:[%s]月奖会员入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2905. $allData = CalcCache::getHasMonthBonusUsers($this->_periodNum, $offset, $this->_limit);
  2906. if($allData) {
  2907. $insertDataBonusUser = [];
  2908. foreach($allData as $userId){
  2909. $insertDataBonusUser[] = [
  2910. 'ID' => SnowFake::instance()->generateId(),
  2911. 'USER_ID' => $userId,
  2912. 'CALC_MONTH' => $this->_calcYearMonth,
  2913. 'PERIOD_NUM' => $this->_periodNum,
  2914. 'CREATED_AT' => Date::nowTime()
  2915. ];
  2916. unset($userId);
  2917. }
  2918. if( $insertDataBonusUser ) CalcMonthBonusUser::batchInsert($insertDataBonusUser);
  2919. unset($insertDataBonus, $allData);
  2920. return $this->loopMonthBonusUserToDb($offset + $this->_limit);
  2921. }
  2922. unset($allData);
  2923. return true;
  2924. }
  2925. //奖金入库
  2926. /**
  2927. * 循环奖服务奖金会员,并入库
  2928. * @param int $offset
  2929. * @return bool
  2930. * @throws \yii\db\Exception
  2931. */
  2932. public function loopBonusUsers($offset = 0) {
  2933. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2934. // 从缓存列表里面从底层往上倒序获取会员
  2935. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  2936. if($allData){
  2937. $insertDataBonus = [];
  2938. foreach($allData as $userId){
  2939. $tempBonusData = $this->bonusData($userId);
  2940. if(!empty($tempBonusData['result'])){
  2941. $insertDataBonus[] = $tempBonusData['result'];
  2942. }
  2943. unset($userId, $tempBonusData);
  2944. }
  2945. // echo json_encode($insertDataBonus);
  2946. // exit();
  2947. CalcBonus::batchInsert($insertDataBonus);
  2948. unset($insertDataBonus, $allData);
  2949. return $this->loopBonusUsers($offset + $this->_limit);
  2950. }
  2951. return true;
  2952. }
  2953. // 奖金入库完成,将各个奖金计算流水会员聘级,更新成蓝星奖当时计算的聘级
  2954. public function loopCalcBlueEmpLv($offset = 0) {
  2955. $allData = CalcBonus::findUseDbCalc()
  2956. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  2957. ->offset($offset)
  2958. ->limit($this->_limit)
  2959. ->asArray()
  2960. ->all();
  2961. if ($allData) {
  2962. foreach ($allData as $data) {
  2963. // 会员的最新级别,查看上一期,如果无数据,则就是无级别。 上一期到下一期结算月之间,都显示上一期的最新级别。
  2964. if ($this->_isCalcMonth) {
  2965. // 如果是结算月,则直接获取当期结算结果
  2966. $blueBsInfo = CalcBonusBS::findOneAsArray(
  2967. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  2968. [':PERIOD_NUM'=>$this->_periodNum, ':USER_ID'=>$data['USER_ID']]
  2969. );
  2970. if (!empty($blueBsInfo)) {
  2971. $nowBsEmpLv = $blueBsInfo['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
  2972. } else {
  2973. $nowBsEmpLv = EmployLevel::NO_LEVEL_ID;
  2974. }
  2975. } else {
  2976. // 如果不是结算月,则找上一期的结算月数据
  2977. $forwardMonthPeriod = Period::find()
  2978. ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM'=>$this->_periodNum])
  2979. ->orderBy('PERIOD_NUM DESC')
  2980. ->asArray()
  2981. ->one();
  2982. $forwardPeriodNum = $forwardMonthPeriod['PERIOD_NUM'];
  2983. $blueBsInfo = CalcBonusBS::findOneAsArray(
  2984. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  2985. [':PERIOD_NUM'=>$forwardPeriodNum, ':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. }
  2993. // 修改AR_CALC_BONUS的LAST_EMP_LV聘级,修改奖金流水的聘级AR_FLOW_BONUS表的LAST_EMP_LV
  2994. // 期结算结果
  2995. CalcBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2996. [
  2997. ':USER_ID' => $data['USER_ID'],
  2998. ':PERIOD_NUM' => $this->_periodNum
  2999. ]
  3000. );
  3001. // // 奖金流水
  3002. // FlowBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3003. // [
  3004. // ':USER_ID' => $data['USER_ID'],
  3005. // ':PERIOD_NUM' => $this->_periodNum
  3006. // ]
  3007. // );
  3008. // 共享奖流水
  3009. CalcBonusGX::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3010. [
  3011. ':USER_ID' => $data['USER_ID'],
  3012. ':PERIOD_NUM' => $this->_periodNum
  3013. ]
  3014. );
  3015. // 推广奖流水
  3016. CalcBonusTG::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3017. [
  3018. ':USER_ID' => $data['USER_ID'],
  3019. ':PERIOD_NUM' => $this->_periodNum
  3020. ]
  3021. );
  3022. // 团队奖流水
  3023. CalcBonusQY::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3024. [
  3025. ':USER_ID' => $data['USER_ID'],
  3026. ':PERIOD_NUM' => $this->_periodNum
  3027. ]
  3028. );
  3029. // 服务奖流水
  3030. CalcBonusBD::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3031. [
  3032. ':USER_ID' => $data['USER_ID'],
  3033. ':PERIOD_NUM' => $this->_periodNum
  3034. ]
  3035. );
  3036. // 更新form的聘级
  3037. CalcBonusGX::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3038. [
  3039. ':FROM_USER_ID' => $data['USER_ID'],
  3040. ':PERIOD_NUM' => $this->_periodNum
  3041. ]
  3042. );
  3043. CalcBonusTG::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3044. [
  3045. ':FROM_USER_ID' => $data['USER_ID'],
  3046. ':PERIOD_NUM' => $this->_periodNum
  3047. ]
  3048. );
  3049. CalcBonusBD::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  3050. [
  3051. ':FROM_USER_ID' => $data['USER_ID'],
  3052. ':PERIOD_NUM' => $this->_periodNum
  3053. ]
  3054. );
  3055. }
  3056. unset($allData);
  3057. return $this->loopCalcBlueEmpLv($offset + $this->_limit);
  3058. }
  3059. unset($allData);
  3060. return true;
  3061. }
  3062. /**
  3063. * 奖金
  3064. * @param $userId
  3065. * @return array
  3066. * @throws \yii\db\Exception
  3067. */
  3068. public function bonusData($userId) {
  3069. // 从缓存中获取用户的奖金
  3070. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  3071. $standardBonus = CalcCache::standardBonus($userId, $this->_periodNum);
  3072. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  3073. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  3074. $empLv = $baseInfo['EMP_LV'];
  3075. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  3076. //没有共享和管理奖
  3077. $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;
  3078. $realBonusGx = 0;
  3079. $realBonusGl = 0;
  3080. $realBonusBs = 0; // 蓝星管理奖. BlueStar
  3081. $blueStartAmount = 0;
  3082. $buleStartOriBonus = 0;
  3083. $buleStartManageTax = 0;
  3084. $exchangePoints = 0; // 蓝星奖管理奖. 产生的兑换积分
  3085. $blueBonusGL = 0; //拆分蓝星管理奖,原奖金
  3086. $blueBonusYJ = 0; //拆分蓝星业绩奖,原奖金
  3087. $blueBonusGLSF = 0;//拆分蓝星管理奖,实发
  3088. $blueBonusYJSF = 0;//拆分蓝星业绩奖,实发
  3089. // rm1820 共享奖周算且不计入总原奖金中,所以需要在总原奖金中去掉
  3090. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $bonus['ORI_BONUS_GX'];
  3091. $oriGxMonthBonus = 0; // 月共享奖金,结算月之和原总金额
  3092. $monthGxReal = $monthGxManageTax = $monthGxpoint = 0;
  3093. $bonus['BONUS_GX'] = 0; // 以前的周共享奖实发金额,新版本每周不发,不扣除复消积分管理费,所以这里给0
  3094. if( $this->_isCalcMonth ) {
  3095. $fxPvStatus = $this->_isMonthPerfLimit($userId);
  3096. // BONUS_REAL 字段是发到用户的真实奖金
  3097. if ( $fxPvStatus ) {
  3098. // 月节点,获取蓝星管理奖数据
  3099. $userBS = CalcBonusBS::find()
  3100. ->where(
  3101. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  3102. [
  3103. ':PERIOD_NUM' => $this->_periodNum,
  3104. ':USER_ID' => $userId
  3105. ]
  3106. )
  3107. ->select('AMOUNT,ORI_BONUS,MANAGE_TAX,LEVEL_ID,PRODUCT_POINT,ORI_BONUS_ABBR,AMOUNT_ABBR,ORI_BONUS_MNT,AMOUNT_MNT')
  3108. ->limit(1)
  3109. ->orderBy('CREATED_AT DESC')
  3110. ->asArray()
  3111. ->all();
  3112. $userBS = is_array($userBS) ? reset($userBS) : [];
  3113. $blueStartAmount = isset($userBS['AMOUNT']) && !empty($userBS['AMOUNT']) ? $userBS['AMOUNT'] : 0; // 奖金
  3114. $buleStartOriBonus = isset($userBS['ORI_BONUS']) && !empty($userBS['ORI_BONUS']) ? $userBS['ORI_BONUS'] : 0; // 原奖金
  3115. $blueBonusGL = isset($userBS['ORI_BONUS_MNT']) && !empty($userBS['ORI_BONUS_MNT']) ? $userBS['ORI_BONUS_MNT'] : 0; // 拆分管理奖原奖金
  3116. $blueBonusYJ = isset($userBS['ORI_BONUS_ABBR']) && !empty($userBS['ORI_BONUS_ABBR']) ? $userBS['ORI_BONUS_ABBR'] : 0; // 拆分业绩奖原奖金
  3117. $blueBonusGLSF = isset($userBS['AMOUNT_MNT']) && !empty($userBS['AMOUNT_MNT']) ? $userBS['AMOUNT_MNT'] : 0; // 拆分管理奖 实发
  3118. $blueBonusYJSF = isset($userBS['AMOUNT_ABBR']) && !empty($userBS['AMOUNT_ABBR']) ? $userBS['AMOUNT_ABBR'] : 0; // 拆分业绩奖 实发
  3119. //$blueStartAmount = $blueBonusGLSF + $blueBonusYJSF; // 总实发,管理奖实发+业绩奖实发
  3120. #rm1820 共享奖金月节点合起来原奖金一起扣除管理费和复消积分
  3121. $monthSumData = CalcBonus::findUseSlaves()
  3122. ->select('SUM(ORI_BONUS_GX) AS BONUS_GX_SUM, SUM(BONUS_GL) AS BONUS_GL_SUM')
  3123. ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  3124. [
  3125. 'USER_ID'=>$userId,
  3126. 'CALC_MONTH'=>$this->_calcYearMonth
  3127. ]
  3128. )
  3129. ->asArray()
  3130. ->one();
  3131. $bonusGxSum = $monthSumData['BONUS_GX_SUM'] ?? 0;
  3132. $bonusGxSum = $bonus['ORI_BONUS_GX'] + $bonusGxSum;
  3133. $oriGxMonthBonus = $bonusGxSum;
  3134. if ($bonusGxSum > 0) {
  3135. // 扣除管理费和复消积分
  3136. //扣除相应的复消积分和管理费
  3137. $deductData = $this->deduct($userId, $bonusGxSum);
  3138. $monthGxReal = $deductData['surplus']; // 扣除管理费和复消积分后的实发金额
  3139. $monthGxManageTax = $deductData['manageTax']; // 管理费
  3140. $monthGxpoint = $deductData['reConsumePoints'];// 复消积分
  3141. }
  3142. $realBonusGx = $monthGxReal;
  3143. $bonusGlSum = $monthSumData['BONUS_GL_SUM'] ?? 0;
  3144. $bonus['RECONSUME_POINTS'] = $bonus['RECONSUME_POINTS'] + $monthGxpoint;
  3145. $bonus['MANAGE_TAX'] = $bonus['MANAGE_TAX'] + $monthGxManageTax;
  3146. $realBonusGl += $bonusGlSum + $bonus['BONUS_GL'];
  3147. $realBonusBs = $blueStartAmount;
  3148. $bonusReal += $realBonusGx + $realBonusBs + $realBonusGl + $bonus['BONUS_STORE']; // 再加上店服务奖的实发
  3149. $exchangePoints = isset($userBS['PRODUCT_POINT']) && !empty($userBS['PRODUCT_POINT']) ? $userBS['PRODUCT_POINT'] : 0; // 兑换积分
  3150. unset($monthSumData, $bonusGxSum, $bonusGlSum);
  3151. $oriBonusBs = isset($bonus['ORI_BONUS_BS']) && $bonus['ORI_BONUS_BS'] > 0 ? $bonus['ORI_BONUS_BS'] : 0;
  3152. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $blueBonusGL - $blueBonusYJ;
  3153. // 共享奖最后一起算,所以总原奖金中,需要加上整个月的数据
  3154. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] + $oriGxMonthBonus;
  3155. $empLv = $userBS['LEVEL_ID'] ?? $empLv;
  3156. } else {
  3157. // 蓝星奖原奖金在扣管理费的时候被加入到了BONUS_TOTAL,需要再减去原奖金
  3158. $oriBonusBs = isset($bonus['ORI_BONUS_BS']) && $bonus['ORI_BONUS_BS'] > 0 ? $bonus['ORI_BONUS_BS'] : 0;
  3159. // 如果未满足,则复消积分,应该不能发放
  3160. $oriBonusBsYJCF = isset($bonus['ORI_BONUS_ABBR']) && $bonus['ORI_BONUS_ABBR'] > 0 ? $bonus['ORI_BONUS_ABBR'] : 0;
  3161. $oriBonusBsGLCF = isset($bonus['ORI_BONUS_MNT']) && $bonus['ORI_BONUS_MNT'] > 0 ? $bonus['ORI_BONUS_MNT'] : 0;
  3162. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $oriBonusBs - $oriBonusBsYJCF - $oriBonusBsGLCF;
  3163. $empLv = EmployLevel::getDefaultLevelId();
  3164. }
  3165. }
  3166. $result = [
  3167. 'USER_ID' => $userId,
  3168. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  3169. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  3170. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  3171. 'LAST_EMP_LV' => $empLv,
  3172. 'LAST_STATUS' => $baseInfo['STATUS'],
  3173. 'LAST_MOBILE' => $baseInfo['MOBILE'],
  3174. 'LAST_PERIOD_AT' => $baseInfo['PERIOD_NUM'],
  3175. 'LAST_CREATED_AT' => $baseInfo['CREATED_AT'],
  3176. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  3177. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  3178. 'LAST_CITY' => $baseInfo['CITY'],
  3179. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  3180. 'LAST_SYSTEM_ID' => $baseInfo['SYSTEM_ID'] ? $baseInfo['SYSTEM_ID'] : '',
  3181. 'LAST_IS_DIRECT_SELLER' => $baseInfo['IS_DIRECT_SELLER'],
  3182. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  3183. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  3184. 'LAST_CON_USER_NAME' => $baseInfo['CON_USER_NAME'],
  3185. 'LAST_CON_REAL_NAME' => $baseInfo['CON_REAL_NAME'],
  3186. 'EXCHANGE_POINTS' => $exchangePoints, // 兑换积分
  3187. // 'LAST_LOCATION' => $baseInfo['LOCATION'] ? $baseInfo['LOCATION'] : 1,
  3188. //@todo
  3189. 'LAST_LOCATION' => 1,
  3190. 'BONUS_BD' => $bonus['BONUS_BD'],
  3191. 'BONUS_TG' => $bonus['BONUS_TG'],
  3192. 'BONUS_XF' => $bonus['BONUS_XF'],
  3193. 'BONUS_YJ' => $bonus['BONUS_YJ'],
  3194. 'BONUS_GX' => $bonus['BONUS_GX'],
  3195. 'BONUS_GL' => $bonus['BONUS_GL'],
  3196. 'BONUS_QY' => $bonus['BONUS_QY'],
  3197. 'BONUS_YC' => $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'],
  3198. 'BONUS_VIP' => $bonus['BONUS_VIP'],
  3199. 'RECONSUME_POINTS' => $bonus['RECONSUME_POINTS'],
  3200. 'MANAGE_TAX' => $bonus['MANAGE_TAX'],
  3201. 'BONUS_INCOME'=>$bonus['INCOME_TOTAL'],
  3202. 'BONUS_REAL'=> $bonusReal,
  3203. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  3204. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  3205. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  3206. 'ORI_BONUS_XF' => $bonus['ORI_BONUS_XF'],
  3207. 'ORI_BONUS_YJ' => $bonus['ORI_BONUS_YJ'],
  3208. 'ORI_BONUS_YJ_BD' => $bonus['ORI_BONUS_YJ_BD'],
  3209. 'ORI_BONUS_YJ_FX' => $bonus['ORI_BONUS_YJ_FX'],
  3210. 'ORI_BONUS_GX' => $bonus['ORI_BONUS_GX'],
  3211. 'ORI_MONTH_GX_BONUS' => $oriGxMonthBonus,
  3212. 'GX_MANAGE_TAX' => $monthGxManageTax, //月共享奖管理费
  3213. 'GX_RECONSUME_POINTS' => $monthGxpoint, //月共享奖复消积分
  3214. 'REAL_BONUS_GX' => $realBonusGx,
  3215. 'ORI_BONUS_GL' => $bonus['ORI_BONUS_GL'],
  3216. 'REAL_BONUS_GL' => $realBonusGl,
  3217. 'BONUS_BS' => $realBonusBs, // 新的管理奖金,即蓝星管理奖
  3218. 'ORI_BONUS_BS' => $buleStartOriBonus, // 蓝星管理奖金原奖金,即包含管理费
  3219. 'REAL_BONUS_BS' => $realBonusBs, // 实发蓝星管理奖金
  3220. 'ORI_BONUS_GL_BD' => $bonus['ORI_BONUS_GL_BD'],
  3221. 'ORI_BONUS_GL_FX' => $bonus['ORI_BONUS_GL_FX'],
  3222. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  3223. 'ORI_BONUS_QY_BD' => $bonus['ORI_BONUS_QY_BD'],
  3224. 'ORI_BONUS_QY_FX' => $bonus['ORI_BONUS_QY_FX'],
  3225. 'ORI_BONUS_YC' => $bonus['ORI_BONUS_YC'] + $bonus['ORI_BONUS_YC_EXTRA'],
  3226. 'ORI_BONUS_VIP' => $bonus['ORI_BONUS_VIP'],
  3227. 'ORI_BONUS_STANDARD' => $standardBonus,
  3228. 'BONUS_ST' => $bonus['BONUS_STORE'], // 店服务奖实发
  3229. 'ORI_BONUS_ST' => $bonus['ORI_BONUS_STORE'], // 店服务奖原奖金
  3230. 'BONUS_MNT' => $blueBonusGLSF,
  3231. 'ORI_BONUS_MNT' => $blueBonusGL,
  3232. 'ORI_BONUS_ABBR' => $blueBonusYJ,
  3233. 'BONUS_ABBR' => $blueBonusYJSF,
  3234. //以下没有用
  3235. 'BONUS_FW' => 0,
  3236. 'BONUS_FX' => $bonus['BONUS_FX'],
  3237. 'BONUS_LS' => $bonus['BONUS_LS'],
  3238. 'BONUS_BT' => $bonus['BONUS_BT'],
  3239. 'BONUS_BT_PROD' => $bonus['BONUS_BT_PROD'],
  3240. 'BONUS_BT_TOOL' => $bonus['BONUS_BT_TOOL'],
  3241. 'DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3242. 'BONUS_FL' => $bonus['BONUS_FL'],
  3243. 'BONUS_CF' => $bonus['BONUS_CF'],
  3244. 'BONUS_LX' => $bonus['BONUS_LX'],
  3245. 'SHOULD_QY' => $bonus['BONUS_QY'],
  3246. 'SHOULD_DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3247. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  3248. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  3249. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  3250. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  3251. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  3252. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  3253. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  3254. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  3255. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  3256. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  3257. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  3258. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  3259. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  3260. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  3261. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  3262. 'PV_PCS' => $perfData['PV_PCS'],
  3263. 'PV_LS_TOUCH' => 0.00,
  3264. 'SURPLUS_LS' => 0.00,
  3265. 'QY_LS' => 0.00,
  3266. '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']),
  3267. 'PERIOD_NUM' => $this->_periodNum,
  3268. 'CALC_YEAR' => $this->_calcYear,
  3269. 'CALC_MONTH' => $this->_calcYearMonth,
  3270. 'CALCULATED_AT' => Date::nowTime(),
  3271. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  3272. 'CREATED_AT' => Date::nowTime(),
  3273. ];
  3274. $resend = [];
  3275. unset($bonus, $realBonusGx, $realBonusGl, $bonusReal);
  3276. return ['result'=>$result,'resend'=>$resend];
  3277. }
  3278. /**
  3279. * 根据本有收入计算基础积分
  3280. * @param int $offset
  3281. * @return bool
  3282. */
  3283. public function calcBaseScore(int $offset=0) {
  3284. if (!$this->_isCalcMonth) {
  3285. return true;
  3286. }
  3287. echo sprintf("时间:[%s]基础积分,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3288. //查询本月的,每人的累计总收入
  3289. $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();
  3290. if ($allData) {
  3291. foreach ($allData as $everyData) {
  3292. $validTotalSum = $everyData['TOTAL_SUM'] - $everyData['BD_TOTAL_SUM'];
  3293. if( $validTotalSum <=0 ) continue;
  3294. //达不到 见习主任级别,不产生基础积分
  3295. $userBaseInfo = CalcCache::getUserInfo($everyData['USER_ID'], $this->_periodNum);
  3296. $userEmpLevel = $this->_empLevelConfig[$userBaseInfo['EMP_LV']];
  3297. $userEmpLevelSort = $userEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  3298. unset($userBaseInfo, $userEmpLevel);
  3299. if( $userEmpLevelSort < EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {
  3300. unset($userEmpLevelSort);
  3301. continue;
  3302. }
  3303. unset($userEmpLevelSort);
  3304. //根据总收入获取积分
  3305. $baseScore = $this->_getBaseScoreByTotalBonus($validTotalSum);
  3306. //根据用户的级别判断 能否得到级别积分
  3307. if( $baseScore > 0 ) {
  3308. CalcCache::nowMonthScore($everyData['USER_ID'], $this->_periodNum, [
  3309. 'BASE_SCORE' => $baseScore,
  3310. ]);
  3311. CalcCache::addHasScoreUsers($everyData['USER_ID'], $this->_periodNum);
  3312. }
  3313. unset($everyData, $validTotalSum, $baseScore);
  3314. }
  3315. unset($allData);
  3316. return $this->calcBaseScore($offset + $this->_limit);
  3317. }
  3318. unset($allData);
  3319. return true;
  3320. }
  3321. /**
  3322. * 循环积分入库
  3323. * @param int $offset
  3324. * @return bool
  3325. * @throws \yii\db\Exception
  3326. */
  3327. public function loopWriteScore(int $offset = 0) {
  3328. if(!$this->_isCalcMonth){
  3329. return true;
  3330. }
  3331. echo sprintf("时间:[%s]积分入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3332. // 从缓存列表里面从底层往上倒序获取会员
  3333. $allData = CalcCache::getHasScoreUsers($this->_periodNum, $offset, $this->_limit);
  3334. if($allData){
  3335. $insertDataMonthScore = [];
  3336. foreach($allData as $userId){
  3337. $monthScoreData = CalcCache::nowMonthScore($userId, $this->_periodNum);
  3338. $totalScore = $monthScoreData['BASE_SCORE'] + $monthScoreData['LEVEL_SCORE'] + $monthScoreData['UPGRADE_SCORE'];
  3339. if( $totalScore <= 0 ) continue;
  3340. $insertDataMonthScore[] = [
  3341. 'USER_ID' => $userId,
  3342. 'BASE_SCORE' => $monthScoreData['BASE_SCORE'],
  3343. 'LEVEL_SCORE' => $monthScoreData['LEVEL_SCORE'],
  3344. 'UPGRADE_SCORE' => $monthScoreData['UPGRADE_SCORE'],
  3345. 'TOTAL_SCORE' => $totalScore,
  3346. 'PERIOD_NUM' => $this->_periodNum,
  3347. 'CALC_MONTH' => $this->_calcYearMonth,
  3348. 'CREATED_AT' => Date::nowTime(),
  3349. ];
  3350. unset($userId, $monthScoreData, $totalScore);
  3351. }
  3352. ScoreMonth::batchInsert($insertDataMonthScore);
  3353. unset($insertDataMonthScore, $allData);
  3354. return $this->loopWriteScore($offset + $this->_limit);
  3355. }
  3356. unset($allData);
  3357. return true;
  3358. }
  3359. /**
  3360. * 根据总收入获取相应的基础积分
  3361. * @param $totalBonus
  3362. * @return int|mixed
  3363. */
  3364. private function _getBaseScoreByTotalBonus($totalBonus) {
  3365. $baseScoreConfig = Json::decode($this->_sysConfig['baseScore']['VALUE']);
  3366. $baseScore = 0;
  3367. foreach ($baseScoreConfig as $everyScoreData) {
  3368. if( $totalBonus < $everyScoreData['monthTotalPvMin'] ) continue;
  3369. if( $everyScoreData['monthTotalPvMax'] != 0 && $totalBonus >= $everyScoreData['monthTotalPvMax'] ) continue;
  3370. $baseScore = $everyScoreData['score'];
  3371. unset($everyScoreData);
  3372. break;
  3373. }
  3374. unset($baseScoreConfig, $totalBonus);
  3375. return $baseScore;
  3376. }
  3377. // 判断是否满足月最低消费
  3378. public function _isMonthPerfLimit($userId) {
  3379. $userMonthTotal = PerfMonth::find()->where(
  3380. 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  3381. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth]
  3382. )
  3383. ->asArray()
  3384. ->one();
  3385. $fxPvStatus = false;
  3386. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  3387. $fxPvStatus = true;
  3388. }
  3389. return $fxPvStatus;
  3390. }
  3391. }