BonusCalc.php 177 KB

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