BonusCalc.php 168 KB

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