BonusCalc.php 170 KB

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