BonusCalc.php 168 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694
  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_MNT', $user['ORI_BONUS_MNT']);
  1537. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1538. $manageTax = $deductData['manageTax']; // 管理费
  1539. $point = $deductData['reConsumePoints'];// 复消积分
  1540. } else {
  1541. // 如果未满足月消费,则不扣除管理费和复消积分了
  1542. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_MNT', $user['ORI_BONUS_MNT']);
  1543. $realBonusBs = $user['ORI_BONUS_MNT'];
  1544. $manageTax = 0; // 管理费
  1545. $point = 0;// 复消积分
  1546. }
  1547. // 更新蓝星奖金存储过程的实发金额数据
  1548. CalcBonusBS::updateAll(['AMOUNT_MNT' => $realBonusBs, 'MANAGE_TAX_MNT' => $manageTax, 'MNT_RECONSUME_POINTS' => $point],
  1549. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1550. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1551. }
  1552. return $this->calcBonusBsGLCF($offset + $this->_limit);
  1553. }
  1554. unset($allData);
  1555. return true;
  1556. }
  1557. /**
  1558. * 蓝星业绩奖金
  1559. * @param int $offset
  1560. * @return bool
  1561. * @throws \yii\db\Exception
  1562. */
  1563. public function calcBonusBsYJCF(int $offset = 0) {
  1564. if( !$this->_isCalcMonth ) {
  1565. // 不是结算月,则不进行计算
  1566. return false;
  1567. }
  1568. // 从缓存获取分页有收入的会员信息
  1569. $allData = CalcBonusBS::findUseDbCalc()
  1570. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  1571. ->groupBy('USER_ID')
  1572. ->offset($offset)
  1573. ->limit($this->_limit)
  1574. ->asArray()
  1575. ->all();
  1576. if ($allData) {
  1577. foreach ($allData as $user) {
  1578. $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']);
  1579. if ($fxStatus) {
  1580. //扣除相应的复消积分和管理费
  1581. $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']);
  1582. // 把对碰后的奖金存入缓存中
  1583. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_ABBR', $user['ORI_BONUS_ABBR']);
  1584. $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金
  1585. $manageTax = $deductData['manageTax']; // 管理费
  1586. $point = $deductData['reConsumePoints'];// 复消积分
  1587. } else {
  1588. // 如果未满足月消费,则不扣除管理费和复消积分了
  1589. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_ABBR', $user['ORI_BONUS_ABBR']);
  1590. $realBonusBs = $user['ORI_BONUS_ABBR'];
  1591. $manageTax = 0; // 管理费
  1592. $point = 0;// 复消积分
  1593. }
  1594. // 更新蓝星奖金存储过程的实发金额数据
  1595. CalcBonusBS::updateAll(['AMOUNT_ABBR' => $realBonusBs, 'MANAGE_TAX_ABBR' => $manageTax, 'ABBR_RECONSUME_POINTS' => $point],
  1596. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  1597. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  1598. }
  1599. return $this->calcBonusBsYJCF($offset + $this->_limit);
  1600. }
  1601. unset($allData);
  1602. return true;
  1603. }
  1604. /**
  1605. * 报单管理奖
  1606. * @param int $offset
  1607. * @return bool
  1608. * @throws \yii\db\Exception
  1609. */
  1610. public function calcBonusBdGL(int $offset = 0) {
  1611. // 从缓存获取分页有收入的会员信息
  1612. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1613. if ($allData) {
  1614. $insertBonusData = [];
  1615. foreach ($allData as $userId) {
  1616. $bonusUserData = [];
  1617. //会员信息
  1618. $validDeep = 1;
  1619. $this->loopRelationParentDo($userId, function ($parent) use ($userId, &$validDeep, &$bonusUserData) {
  1620. //判断$parent是否有首单团队奖
  1621. $bonusUserId = $parent['PARENT_UID'];
  1622. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1623. $nowDecLevelConfig = $this->_decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1624. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1625. if( $recNum <= 0 ) {
  1626. $maxOddLayer = 0;
  1627. }else if( $recNum == 1 ) {
  1628. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'] ?? 0;
  1629. }else if( $recNum == 2 ) {
  1630. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'] ?? 0;
  1631. }else {
  1632. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'] ?? 0;
  1633. }
  1634. if ( $validDeep < 2 * $maxOddLayer && $validDeep % 2 === 1 ) {//奇数层
  1635. $bonusUserData[] = [
  1636. 'bonusUid' => $parent['PARENT_UID'],
  1637. 'validDeep' => $validDeep
  1638. ];
  1639. }
  1640. $parentBonus = CalcCache::bonus($bonusUserId, $this->_periodNum);
  1641. if( $parentBonus['ORI_BONUS_QY_BD'] > 0 ) {//有效层
  1642. $validDeep += 1;
  1643. }
  1644. unset($parent, $parentBonus, $bonusUserId, $bonusUserBaseInfo, $nowDecLevelConfig, $recNum, $maxOddLayer);
  1645. //超过代数上限则结束
  1646. if ( $validDeep >= 18 ) {
  1647. return self::LOOP_FINISH;
  1648. }
  1649. });
  1650. //发奖
  1651. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1652. $userBonus = CalcCache::bonus($userId, $this->_periodNum);
  1653. foreach ($bonusUserData as $everyData) {
  1654. $bonusUserId = $everyData['bonusUid'];
  1655. //判断 是否本期小市场(安置关系)新增980pv 能否得奖
  1656. if ( !$this->checkSmallMarketPerf($bonusUserId) ) {
  1657. // unset($bonusUserId);
  1658. // continue;
  1659. //得奖人不满足980就找一个满足980的人来得这个奖
  1660. $bonusUserId = $this->getMinBdPvRelationParent($bonusUserId);
  1661. if ( !$bonusUserId ) {
  1662. unset($bonusUserId);
  1663. continue;
  1664. }
  1665. }
  1666. $parentOddPercentValue = floor($everyData['validDeep']/6);
  1667. $parentOddPercentName = sprintf('parentOddPercent_%s', $parentOddPercentValue);
  1668. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1669. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1670. $manageBonus = Tool::formatPrice($userBonus['ORI_BONUS_QY_BD'] * $bonusPercent / 100);
  1671. if ($manageBonus <= 0) continue;
  1672. //总金额限制
  1673. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1674. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1675. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1676. if( $manageBonus <= 0 ) continue;
  1677. //扣除相应的复消积分和管理费
  1678. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1679. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_BD);
  1680. CalcCache::addHasMonthBonusUsers($bonusUserId, $this->_periodNum);
  1681. //管理奖流水
  1682. $insertBonusData[] = [
  1683. 'ID' => SnowFake::instance()->generateId(),
  1684. 'USER_ID' => $bonusUserId,
  1685. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1686. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1687. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1688. 'FROM_USER_ID' => $userId,
  1689. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1690. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  1691. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1692. 'AMOUNT' => $deductData['surplus'],
  1693. 'ORI_BONUS' => $manageBonus,
  1694. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1695. 'MANAGE_TAX' => $deductData['manageTax'],
  1696. 'PERIOD_NUM' => $this->_periodNum,
  1697. 'CALC_YEAR' => $this->_calcYear,
  1698. 'CALC_MONTH' => $this->_calcYearMonth,
  1699. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1700. 'CREATED_AT' => Date::nowTime(),
  1701. 'LOGS' => json_encode([
  1702. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1703. 'validDeep' => $everyData['validDeep'],
  1704. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1705. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1706. 'fromUserId' => $userId,
  1707. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1708. 'bonusTotalLimit' => [
  1709. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  1710. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  1711. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  1712. ],
  1713. ]),
  1714. ];
  1715. unset($everyData, $bonusUserId, $parentOddPercentValue, $parentOddPercentName, $bonusPercent, $manageBonus, $deductData);
  1716. }
  1717. unset($userId, $validDeep, $userBaseInfo, $userBonus, $bonusUserData);
  1718. }
  1719. if($insertBonusData) CalcBonusGL::batchInsert($insertBonusData);
  1720. unset($allData, $insertBonusData);
  1721. return $this->calcBonusBdGL($offset + $this->_limit);
  1722. }
  1723. unset($allData);
  1724. return true;
  1725. }
  1726. /**
  1727. * 复消管理奖
  1728. * @param int $offset
  1729. * @return bool
  1730. * @throws \yii\db\Exception
  1731. */
  1732. public function calcBonusFxGL(int $offset = 0) {
  1733. $periodNum = $this->_periodNum;
  1734. // 从缓存获取分页有收入的会员信息
  1735. $allData = CalcCache::getHasIncomeUsers($this->_periodNum, $offset, $this->_limit);
  1736. if ($allData) {
  1737. $insertBonusData = [];
  1738. foreach ($allData as $userId) {
  1739. //会员信息
  1740. $decLevelConfig = $this->_decLevelConfig;
  1741. $netWorkParents = Cache::getAllRelationParents($userId);
  1742. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1743. $userEmpLevel = $userBaseInfo['EMP_LV'];
  1744. foreach ($netWorkParents as $netWorkParent) {
  1745. //自已距顶端的深度减去父级距顶端的深度
  1746. $diffLayer = $netWorkParent['TOP_DEEP'] - $netWorkParent['PARENT_DEEP'];
  1747. if( $diffLayer <= 0) continue;
  1748. //@todo 超过最大代数就跳出本次循环
  1749. //偶数代跳过
  1750. if( $diffLayer % 2 == 0 ) continue;
  1751. // 获取会员的报单级别
  1752. $bonusUserId = $netWorkParent['PARENT_UID'];
  1753. if( !$bonusUserId ) continue;
  1754. $bonusUserBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1755. $nowDecLevelConfig = $decLevelConfig[$bonusUserBaseInfo['DEC_LV']];
  1756. if( !$nowDecLevelConfig ) continue;
  1757. //推荐人数
  1758. $recNum = $bonusUserBaseInfo['REC_NUM'];
  1759. if( $recNum <= 0 ) continue;
  1760. if( $recNum == 1 ) {
  1761. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_ONE'];
  1762. }else if( $recNum == 2 ) {
  1763. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_TWO'];
  1764. }else {
  1765. $maxOddLayer = $nowDecLevelConfig['GL_ODD_DEEP_THREE'];
  1766. }
  1767. //超过代数上限也跳过
  1768. if( $diffLayer >= 2 * $maxOddLayer ) continue;
  1769. // $everyLayer = floor(2 * $nowDecLevelConfig['GL_ODD_DEEP_THREE'] / $nowDecLevelConfig['SORT']);
  1770. //多少代
  1771. // $theLayer = $diffLayer % $everyLayer;
  1772. // $theLayerLevel = floor($diffLayer/$everyLayer);
  1773. $theLayerLevel = floor($diffLayer/6);
  1774. $parentOddPercentName = sprintf('parentOddPercent_%s', $theLayerLevel);
  1775. if( !isset($this->_sysConfig[$parentOddPercentName]) ) continue;
  1776. $bonusPercent = $this->_sysConfig[$parentOddPercentName]['VALUE'];
  1777. // 从缓存中获取会员的收入信息
  1778. $incomeBonus = CalcCache::bonus($userId, $periodNum);
  1779. $parentOddIncomeFromName = sprintf('parentOddIncomeFrom_%s', $theLayerLevel);
  1780. $parentOddIncomeFrom = explode(',', $this->_sysConfig[$parentOddIncomeFromName]['VALUE']);
  1781. $parentOddIncome = 0;
  1782. foreach ($parentOddIncomeFrom as $incomeType) {
  1783. if( $incomeType === 'XF' ) {
  1784. $incomeTypeName = sprintf('ORI_BONUS_%s', $incomeType);
  1785. }else {
  1786. $incomeTypeName = sprintf('ORI_BONUS_%s_FX', $incomeType);
  1787. }
  1788. $incomeTypeValue = $incomeBonus[$incomeTypeName] ?? 0;
  1789. $parentOddIncome += $incomeTypeValue;
  1790. unset($incomeType, $incomeTypeName, $incomeTypeValue);
  1791. }
  1792. unset($parentOddIncomeFrom);
  1793. $manageBonus = Tool::formatPrice($parentOddIncome * $bonusPercent / 100);
  1794. if ($manageBonus <= 0) continue;
  1795. //总金额限制
  1796. $manageBonus = $this->bonusTotalLimit($manageBonus, $bonusUserId, $recNum, $bonusUserBaseInfo['ZC_AMOUNT']);
  1797. if( $manageBonus <= 0 ) continue;
  1798. //扣除相应的复消积分和管理费
  1799. $deductData = $this->deduct($bonusUserId, $manageBonus);
  1800. CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_GL', $manageBonus, $deductData, CalcCache::FROM_MEANS_FX);
  1801. //管理奖流水
  1802. $insertBonusData[] = [
  1803. 'ID' => SnowFake::instance()->generateId(),
  1804. 'USER_ID' => $bonusUserId,
  1805. 'LAST_DEC_LV' => $bonusUserBaseInfo['DEC_LV'],
  1806. 'LAST_EMP_LV' => $bonusUserBaseInfo['EMP_LV'],
  1807. 'LAST_STATUS' => $bonusUserBaseInfo['STATUS'],
  1808. 'FROM_USER_ID' => $userId,
  1809. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1810. 'LAST_FROM_EMP_LV' => $userEmpLevel,
  1811. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1812. 'AMOUNT' => $deductData['surplus'],
  1813. 'ORI_BONUS' => $manageBonus,
  1814. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1815. 'MANAGE_TAX' => $deductData['manageTax'],
  1816. 'PERIOD_NUM' => $this->_periodNum,
  1817. 'CALC_YEAR' => $this->_calcYear,
  1818. 'CALC_MONTH' => $this->_calcYearMonth,
  1819. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1820. 'CREATED_AT' => Date::nowTime(),
  1821. 'LOGS' => json_encode([
  1822. $parentOddPercentName => $this->_sysConfig[$parentOddPercentName]['VALUE'],
  1823. $parentOddIncomeFromName => $this->_sysConfig[$parentOddIncomeFromName]['VALUE'],
  1824. 'decLevelConfig' => $nowDecLevelConfig,
  1825. 'recNum' => $bonusUserBaseInfo['REC_NUM'],
  1826. 'decAmount' => $bonusUserBaseInfo['ZC_AMOUNT'],
  1827. 'fromUserId' => $userId,
  1828. 'decLevel' => $bonusUserBaseInfo['DEC_LV'],
  1829. 'bonusTotalLimit' => [
  1830. $this->_sysConfig['bonusTotalZeroLimit']['VALUE'],
  1831. $this->_sysConfig['bonusTotalOneLimit']['VALUE'],
  1832. $this->_sysConfig['bonusTotalTwoLimit']['VALUE'],
  1833. ],
  1834. ]),
  1835. ];
  1836. unset($netWorkParent, $diffLayer, $bonusUserId, $bonusUserBaseInfo, $bonusPercent, $nowDecLevelConfig, $manageBonus, $incomeBonus, $maxOddLayer, $theLayerLevel, $recNum, $parentOddPercentName, $parentOddIncomeFromName, $deductData);
  1837. }
  1838. unset($userId, $userBaseInfo, $userEmpLevel, $netWorkParents, $decLevelConfig);
  1839. }
  1840. CalcBonusGL::batchInsert($insertBonusData);
  1841. unset($allData, $insertBonusData);
  1842. return $this->calcBonusFxGL($offset + $this->_limit);
  1843. }
  1844. unset($allData);
  1845. return true;
  1846. }
  1847. /**
  1848. * 结算荣衔奖第一步
  1849. * @param int $offset
  1850. * @return bool
  1851. * @throws \yii\db\Exception
  1852. * @throws \Exception
  1853. */
  1854. public function calcBonusYCStepOne(int $offset = 0) {
  1855. // 月结,如果不是月结点,则直接退出
  1856. if (!$this->_isCalcMonth) {
  1857. return true;
  1858. }
  1859. echo sprintf("时间:[%s]荣衔奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1860. //从缓存获取分页有本月业绩的会员
  1861. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  1862. if ($allData) {
  1863. $maxLevelId = EmployLevel::SHX_ZJ_LEVEL_ID;
  1864. $maxLevel = $this->_empLevelConfig[$maxLevelId];
  1865. $maxLevelPercent = $maxLevel['RX_PERCENT'] ?? 0;
  1866. unset($maxLevelId, $maxLevel);
  1867. foreach ($allData as $userId) {
  1868. // echo sprintf("时间:[%s]荣衔奖,当前用户ID为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $userId);
  1869. $nowMonthPerf = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1870. $monthPvPcs = $nowMonthPerf['PV_PCS'] ?? 0;
  1871. if( $monthPvPcs <= 0 ) continue;
  1872. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1873. //个人业绩的奖金
  1874. $empLevelId = $userBaseInfo['EMP_LV'];
  1875. $empLevel = $this->_empLevelConfig[$empLevelId];
  1876. $rxPercent = $empLevel['RX_PERCENT'] ?? 0;
  1877. $empBonus = $monthPvPcs * $rxPercent / 100;
  1878. if( $empBonus > 0 ) {
  1879. //给本人添加聘级比例
  1880. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $rxPercent);
  1881. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1882. CalcCache::saveYCBonusList($userId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1883. CalcCache::addHasYcBonusUsers($userId, $this->_periodNum);
  1884. //达到最大的比例其它人就得不到了
  1885. if( $rxPercent >= $maxLevelPercent ) continue;
  1886. }
  1887. unset($empLevelId, $empLevel, $rxPercent, $empBonus);
  1888. //获取所有的父级
  1889. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $monthPvPcs, $maxLevelPercent) {
  1890. $bonusUserId = $parent['PARENT_UID'];
  1891. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  1892. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  1893. $parentEmpLevelId = $bonusUserInfo['EMP_LV'];
  1894. $parentEmpLevel = $this->_empLevelConfig[$parentEmpLevelId];
  1895. $parentRxPercent = $parentEmpLevel['RX_PERCENT'] ?? 0;
  1896. $cacheMaxPercent = CalcCache::ycMaxBonusPercent($userId, $this->_periodNum);
  1897. $diffPercent = $parentRxPercent - $cacheMaxPercent;
  1898. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  1899. $empBonus = $monthPvPcs * $diffPercent / 100;
  1900. if( $empBonus <= 0 ) return self::LOOP_CONTINUE;
  1901. //给本人添加聘级比例
  1902. CalcCache::ycMaxBonusPercent($userId, $this->_periodNum, $parentRxPercent);
  1903. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  1904. CalcCache::saveYCBonusList($bonusUserId, $this->_periodNum, $empBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$monthPvPcs]);
  1905. CalcCache::addHasYcBonusUsers($bonusUserId, $this->_periodNum);
  1906. unset($cacheMaxPercent, $diffPercent, $parentEmpLevel, $bonusUserId, $bonusUserInfo, $parentEmpLevelId, $empBonus);
  1907. //达到最大的比例就不在向上找了
  1908. if( $parentRxPercent >= $maxLevelPercent ) return self::LOOP_FINISH;
  1909. unset($parentRxPercent);
  1910. });
  1911. unset($nowMonthPerf, $monthPvPcs);
  1912. }
  1913. unset($allData, $maxLevelPercent);
  1914. $this->calcBonusYCStepOne($offset + $this->_limit);
  1915. }
  1916. unset($periodNum, $allData);
  1917. return true;
  1918. }
  1919. /**
  1920. * 结算荣衔奖第二步
  1921. * @param int $offset
  1922. * @return bool
  1923. * @throws \yii\db\Exception
  1924. */
  1925. public function calcBonusYCStepTwo(int $offset = 0) {
  1926. // 月结,如果不是月结点,则直接退出
  1927. if (!$this->_isCalcMonth) {
  1928. return true;
  1929. }
  1930. echo sprintf("时间:[%s]荣衔奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1931. $allData = CalcCache::getHasYcBonusUsers($this->_periodNum, $offset, $this->_limit);
  1932. if ($allData) {
  1933. $insertBonusData = [];
  1934. foreach ($allData as $userId) {
  1935. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  1936. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  1937. unset($monthPerfData);
  1938. if( !$fxPvStatus ) continue;
  1939. unset($fxPvStatus);
  1940. $ycBonusData = CalcCache::getYCBonusList($userId, $this->_periodNum);
  1941. if( !$ycBonusData ) continue;
  1942. $empBonus = $ycBonusData['empBonus'] ?? 0;
  1943. if( $empBonus <=0 ) continue;
  1944. //总金额限制
  1945. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1946. $empBonus = $this->bonusTotalLimit($empBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  1947. if( $empBonus <= 0 ) continue;
  1948. //扣除相应的复消积分和管理费
  1949. $empBonus = Tool::formatPrice($empBonus);
  1950. $deductData = $this->deduct($userId, $empBonus);
  1951. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_YC', $empBonus, $deductData);
  1952. $empLevelId = $userBaseInfo['EMP_LV'];
  1953. $insertBonusData[] = [
  1954. 'ID' => SnowFake::instance()->generateId(),
  1955. 'USER_ID' => $userId,
  1956. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  1957. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  1958. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  1959. 'FROM_USER_ID' => $userId,
  1960. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  1961. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  1962. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  1963. 'DEEP' => 0,
  1964. 'AMOUNT' => $deductData['surplus'],
  1965. 'ORI_BONUS' => $empBonus,
  1966. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  1967. 'MANAGE_TAX' => $deductData['manageTax'],
  1968. 'BONUS_TYPE' => CalcBonusYC::BONUS_TYPE_YC,
  1969. 'PERIOD_NUM' => $this->_periodNum,
  1970. 'CALC_MONTH' => $this->_calcYearMonth,
  1971. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  1972. 'CREATED_AT' => Date::nowTime(),
  1973. 'LOGS' => json_encode([
  1974. 'empLevelId' => $empLevelId,
  1975. ])
  1976. // 'FROM_DATA' => json_encode($ycBonusData['fromData']),
  1977. ];
  1978. unset($ycBonusData, $deductData);
  1979. //如果是总监以上拿平级2代
  1980. $empLevel = $this->_empLevelConfig[$empLevelId];
  1981. $userEmpLevelSort = $empLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  1982. // if( $userEmpLevelSort >= EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {}
  1983. $newInsertBonusData = $this->calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $empBonus);
  1984. $insertBonusData = array_merge($insertBonusData, $newInsertBonusData);
  1985. unset($newInsertBonusData);
  1986. unset($userId, $userBaseInfo, $empLevel, $empLevelId, $userEmpLevelSort, $empBonus);
  1987. }
  1988. CalcBonusYC::batchInsert($insertBonusData);
  1989. unset($insertBonusData, $allData);
  1990. $this->calcBonusYCStepTwo($offset + $this->_limit);
  1991. }
  1992. unset($allData);
  1993. return true;
  1994. }
  1995. /**
  1996. * VIP奖
  1997. * @param int $offset
  1998. * @return bool
  1999. * @throws \yii\db\Exception
  2000. */
  2001. public function calcBonusVIP(int $offset = 0)
  2002. {
  2003. // 月结,如果不是月结点,则直接退出
  2004. if (!$this->_isCalcMonth) {
  2005. return true;
  2006. }
  2007. echo sprintf("时间:[%s]VIP奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2008. //从缓存获取分页有本月业绩的会员
  2009. $allData = CalcCache::getHasMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2010. if ($allData) {
  2011. $insertBonusData = [];
  2012. foreach ($allData as $userId) {
  2013. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2014. //条件一:级别必须为VIP
  2015. $isVip = false;
  2016. if( $userBaseInfo['DEC_LV'] === DeclarationLevel::VIP_LEVEL_ID ) {
  2017. $isVip= true;
  2018. }
  2019. if( $this->_sysConfig['vipBonusGoldDecLevel']['VALUE'] && $userBaseInfo['DEC_LV'] === DeclarationLevel::JIN_ZUAN_LEVEL_ID ) {
  2020. $isVip = true;
  2021. }
  2022. if ( !$isVip ) {
  2023. unset($userId, $userBaseInfo);
  2024. continue;
  2025. }
  2026. //判断其它两个条件,满足则计算月团队奖,不满足则留下大区剩余业绩,清除其它区的剩余业绩
  2027. //获取本月的业绩
  2028. $monthPerfData = CalcCache::nowMonthPerf($userId, $this->_periodNum);
  2029. // 从缓存中获取会员的上期结余业绩信息
  2030. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2031. // 本期 + 上期结余
  2032. $perfArr = [
  2033. 'VIP_SURPLUS_1L_ZC' => $monthPerfData['VIP_PV_1L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_1L_ZC'],
  2034. 'VIP_SURPLUS_2L_ZC' => $monthPerfData['VIP_PV_2L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_2L_ZC'],
  2035. 'VIP_SURPLUS_3L_ZC' => $monthPerfData['VIP_PV_3L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_3L_ZC'],
  2036. 'VIP_SURPLUS_4L_ZC' => $monthPerfData['VIP_PV_4L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_4L_ZC'],
  2037. 'VIP_SURPLUS_5L_ZC' => $monthPerfData['VIP_PV_5L_ZC'] + $pervSurplusPerf['VIP_SURPLUS_5L_ZC'],
  2038. ];
  2039. //条件二:本月复消金额达到300元
  2040. //fix-2021-03-26修改 条件二:本月个人复消PV300
  2041. $fxPvStatus = $monthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2042. unset($monthPerfData, $pervSurplusPerf);
  2043. if( !$fxPvStatus ) {
  2044. $this->dealWithMaxPerf($userId, $perfArr);
  2045. unset($userBaseInfo, $userId, $perfArr, $fxPvStatus);
  2046. continue;
  2047. }
  2048. //条件三:其它市场业绩
  2049. $isCanOtherDepartPerf = $this->_isCanVipBonusOtherAreaPerf($perfArr);
  2050. if ( !$isCanOtherDepartPerf ) {
  2051. $this->dealWithMaxPerf($userId, $perfArr);
  2052. unset($userBaseInfo, $userId, $perfArr, $isCanOtherDepartPerf);
  2053. continue;
  2054. }
  2055. $oriPerfArr = [
  2056. 'perfArr' => $perfArr,
  2057. 'touchBonus' => 0,
  2058. ];
  2059. // 对碰
  2060. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $this->_sysConfig['vipBonusPercent']['VALUE']/100);
  2061. unset($oriPerfArr, $perfArr);
  2062. $touchPerfArr = [];
  2063. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  2064. $touchPerfArr[$keyR] = $perfR;
  2065. }
  2066. // 对碰完成后把结余的业绩存入本期业绩缓存中
  2067. // CalcCache::nowPeriodPerf($userId, $this->_periodNum, $touchPerfArr);
  2068. //更新数据库
  2069. PerfMonth::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2070. 'USER_ID' => $userId,
  2071. 'CALC_MONTH' => $this->_calcYearMonth,
  2072. ]);
  2073. if ($touchBonusArr['touchBonus'] <= 0) continue;
  2074. $vipBonus = $touchBonusArr['touchBonus'];
  2075. //判断级别上限
  2076. $vipBonus = $this->declarationLevelCap($vipBonus, $userId, $userBaseInfo['DEC_LV']);
  2077. if( $vipBonus <= 0 ) continue;
  2078. $vipBonus = $this->bonusTotalLimit($vipBonus, $userId, $userBaseInfo['REC_NUM'], $userBaseInfo['ZC_AMOUNT']);
  2079. if( $vipBonus <= 0 ) continue;
  2080. //扣除相应的复消积分和管理费
  2081. $deductData = $this->deduct($userId, $vipBonus);
  2082. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_VIP', $vipBonus, $deductData);
  2083. $insertBonusData[] = [
  2084. 'ID' => SnowFake::instance()->generateId(),
  2085. 'USER_ID' => $userId,
  2086. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2087. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2088. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2089. 'FROM_USER_ID' => $userId,
  2090. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2091. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2092. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2093. 'AMOUNT' => $deductData['surplus'],
  2094. 'ORI_BONUS' => $vipBonus,
  2095. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2096. 'MANAGE_TAX' => $deductData['manageTax'],
  2097. 'PERIOD_NUM' => $this->_periodNum,
  2098. 'CALC_YEAR' => $this->_calcYear,
  2099. 'CALC_MONTH' => $this->_calcYearMonth,
  2100. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2101. 'CREATED_AT' => Date::nowTime(),
  2102. 'LOGS' => json_encode($touchPerfArr)
  2103. ];
  2104. unset($userBaseInfo, $userId, $perfArr, $fxAmountStatus, $isCanOtherDepartPerf, $touchBonusArr, $touchPerfArr, $vipBonus, $deductData);
  2105. }
  2106. if( $insertBonusData ) CalcBonusVIP::batchInsert($insertBonusData);
  2107. unset($insertBonusData, $allData);
  2108. $this->calcBonusVIP($offset + $this->_limit);
  2109. }
  2110. unset($allData);
  2111. return true;
  2112. }
  2113. /**
  2114. * 见习达标奖
  2115. * @param int $offset
  2116. * @return bool
  2117. * @throws \yii\db\Exception
  2118. */
  2119. public function calcBonusStandard(int $offset = 0)
  2120. {
  2121. // 月结,如果不是月结点,则直接退出
  2122. if (!$this->_isCalcMonth) {
  2123. return true;
  2124. }
  2125. echo sprintf("时间:[%s]见习达标奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()), $offset);
  2126. //从缓存获取分页有本月业绩的会员
  2127. $allData = CalcCache::getHasStandardMonthPerfUsers($this->_periodNum, $offset, $this->_limit);
  2128. if ($allData) {
  2129. $minSaleCondition = $this->_jxStandardMinSaleCondition();
  2130. $insertBonusData = [];
  2131. foreach ($allData as $userId) {
  2132. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2133. //条件一:聘级必须为见习主任
  2134. if( $userBaseInfo['EMP_LV'] != EmployLevel::JX_ZR_LEVEL_ID ) {
  2135. unset($userId, $userBaseInfo);
  2136. continue;
  2137. }
  2138. $standardMonthPerf = CalcCache::nowStandardMonthPerf($userId, $this->_periodNum);
  2139. $totalAmount = $standardMonthPerf['AMOUNT_PCS'] + $standardMonthPerf['AMOUNT_PSS'];
  2140. if( $totalAmount < $minSaleCondition ) {
  2141. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2142. continue;
  2143. }
  2144. $standardBonus = $this->_jxStandardAwardAmount($totalAmount);
  2145. if( $standardBonus <= 0 ) {
  2146. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount, $standardBonus);
  2147. continue;
  2148. }
  2149. CalcCache::standardBonus($userId, $this->_periodNum, $standardBonus);
  2150. $insertBonusData[] = [
  2151. 'ID' => SnowFake::instance()->generateId(),
  2152. 'USER_ID' => $userId,
  2153. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  2154. 'LAST_EMP_LV' => $userBaseInfo['EMP_LV'],
  2155. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  2156. 'FROM_USER_ID' => $userId,
  2157. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2158. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2159. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2160. 'AMOUNT' => $standardBonus,
  2161. 'ORI_BONUS' => $standardBonus,
  2162. 'RECONSUME_POINTS' => 0,
  2163. 'MANAGE_TAX' => 0,
  2164. 'PERIOD_NUM' => $this->_periodNum,
  2165. 'CALC_YEAR' => $this->_calcYear,
  2166. 'CALC_MONTH' => $this->_calcYearMonth,
  2167. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2168. 'CREATED_AT' => Date::nowTime(),
  2169. 'LOGS' => json_encode($standardMonthPerf)
  2170. ];
  2171. unset($userId, $userBaseInfo, $standardMonthPerf, $totalAmount);
  2172. }
  2173. if( $insertBonusData ) CalcBonusStandard::batchInsert($insertBonusData);
  2174. unset($insertBonusData, $minSaleCondition, $insertBonusData, $allData);
  2175. $this->calcBonusStandard($offset + $this->_limit);
  2176. }
  2177. unset($allData);
  2178. return true;
  2179. }
  2180. /**
  2181. * === 运算相关的辅助功能开始 ===
  2182. */
  2183. /**
  2184. * 获取上2代的得奖人信息
  2185. * @param $userId
  2186. * @param $validDeep
  2187. * @param $shareBonusOne
  2188. * @return array
  2189. */
  2190. protected function gxLastTwoBonusData($userId, $validDeep, $shareBonusOne) {
  2191. $bonusUserList = CalcCache::getShareBonusOneRelation($userId, $this->_periodNum, $validDeep);
  2192. if( !$bonusUserList ) {
  2193. unset($bonusUserList);
  2194. return [];
  2195. }
  2196. $bonusUserData = [];
  2197. $bonusUserTotalPerf = 0;
  2198. $bonusUserRealList = [];
  2199. foreach ($bonusUserList as $bonusUserId) {
  2200. //查看该用户的个人业绩和团队新增业绩总和
  2201. $nowPeriodData = CalcCache::nowPeriodPerf($bonusUserId, $this->_periodNum);
  2202. //本期新增业绩
  2203. $bonusUserPerf = $nowPeriodData['PV_PCS'] + $nowPeriodData['PV_PSS'];
  2204. if ($bonusUserPerf <= 0) {
  2205. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2206. continue;
  2207. }
  2208. $bonusUserTotalPerf += $bonusUserPerf;
  2209. $bonusUserRealList[] = [
  2210. 'bonusUserId' => $bonusUserId,
  2211. 'bonusUserPerf' => $bonusUserPerf,
  2212. ];
  2213. unset($bonusUserId, $nowPeriodData, $bonusUserPerf);
  2214. }
  2215. unset($bonusUserList);
  2216. foreach ($bonusUserRealList as $everyData) {
  2217. $bonusUserData[] = [
  2218. 'bonusUid' => $everyData['bonusUserId'],
  2219. 'bonus' => $shareBonusOne * $everyData['bonusUserPerf'] / $bonusUserTotalPerf,
  2220. 'validMinPv' => false
  2221. ];
  2222. unset($everyData);
  2223. }
  2224. unset($bonusUserRealList);
  2225. return $bonusUserData;
  2226. }
  2227. /**
  2228. * 获取达标奖的金额
  2229. * @param $amount
  2230. * @return int|mixed
  2231. */
  2232. private function _jxStandardAwardAmount($amount) {
  2233. // $jxStandardConfig = Json::decode($this->_sysConfig['jxStandard']['VALUE']);
  2234. // $awardAmount = 0;
  2235. // foreach ($jxStandardConfig as $everyData) {
  2236. // if( $amount >= $everyData['salesCondition'] ) {
  2237. // $awardAmount = $everyData['awardAmount'];
  2238. // }
  2239. // unset($everyData);
  2240. // }
  2241. //
  2242. // return $awardAmount;
  2243. $realTimes = floor($amount/self::JX_STANDARD_BASE_AMOUNT);
  2244. $calcTimes = min($realTimes, self::JX_STANDARD_MAX_TIMES);
  2245. $awardAmount = $calcTimes * self::JX_STANDARD_BASE_AMOUNT * self::JX_STANDARD_BONUS_PERCENT / 100;
  2246. unset($amount, $realTimes, $calcTimes);
  2247. return $awardAmount;
  2248. }
  2249. /**
  2250. * 获取达标奖的最小条件
  2251. * @return int|mixed
  2252. */
  2253. private function _jxStandardMinSaleCondition() {
  2254. return self::JX_STANDARD_BASE_AMOUNT;
  2255. }
  2256. /**
  2257. * 处理只留最大业绩的逻辑
  2258. * @param $userId
  2259. * @param $perfArr
  2260. */
  2261. private function dealWithMaxPerf($userId, $perfArr) {
  2262. $maxPerf = max($perfArr);
  2263. $hitMaxPerf = false;
  2264. foreach ($perfArr as $key=>$everyPerf) {
  2265. if( !$hitMaxPerf && $everyPerf == $maxPerf ) {
  2266. $hitMaxPerf = true;
  2267. }else {
  2268. $perfArr[$key] = 0;
  2269. }
  2270. unset($key, $everyPerf);
  2271. }
  2272. //更新数据库
  2273. PerfMonth::updateAll($perfArr, 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  2274. 'USER_ID' => $userId,
  2275. 'CALC_MONTH' => $this->_calcYearMonth
  2276. ]);
  2277. unset($userId, $maxPerf, $hitMaxPerf, $perfArr);
  2278. }
  2279. /**
  2280. * VIP奖其它市场的报单业绩是否能满足
  2281. * @param $perfArr
  2282. * @return bool
  2283. */
  2284. private function _isCanVipBonusOtherAreaPerf($perfArr) {
  2285. $maxAreaPerf = max($perfArr);
  2286. $totalAreaPerf = array_sum($perfArr);
  2287. $diffAreaPerf = strval($totalAreaPerf - $maxAreaPerf);
  2288. if( $diffAreaPerf >= $this->_sysConfig['vipBonusOtherDepartPvCondition']['VALUE'] ) {
  2289. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2290. return true;
  2291. }else {
  2292. unset($maxAreaPerf, $totalAreaPerf, $perfArr);
  2293. return false;
  2294. }
  2295. }
  2296. /**
  2297. * 给父级计算的荣衔奖
  2298. * @param $userId
  2299. * @param $userBaseInfo
  2300. * @param $userEmpLevelSort
  2301. * @param $qyBonus
  2302. * @return array
  2303. */
  2304. public function calcParentYC($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus) {
  2305. if( $userEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return [];
  2306. $validDeep = 0;
  2307. $bonusDataList = [];
  2308. $this->loopRelationParentDo($userId, function ($parent) use ($userId, $userEmpLevelSort, &$validDeep, &$bonusDataList) {
  2309. $bonusUserId = $parent['PARENT_UID'];
  2310. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2311. $bonusUserEmpLevel = $this->_empLevelConfig[$bonusUserInfo['EMP_LV']];
  2312. $bonusUserEmpLevelSort = $bonusUserEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  2313. unset($bonusUserInfo, $bonusUserEmpLevel);
  2314. //必须是有聘级的用户
  2315. if( $bonusUserEmpLevelSort <= EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'] ) return self::LOOP_CONTINUE;
  2316. //超过级别就停止
  2317. if( $bonusUserEmpLevelSort > $userEmpLevelSort) return self::LOOP_FINISH;
  2318. //级别小于就跳过
  2319. if( $bonusUserEmpLevelSort < $userEmpLevelSort ) return self::LOOP_CONTINUE;
  2320. $validDeep++;
  2321. array_push($bonusDataList, [
  2322. 'bonusUserId' => $bonusUserId,
  2323. 'bonusType' => $validDeep,
  2324. ]);
  2325. if( $validDeep >= 2 ) return self::LOOP_FINISH;
  2326. unset($bonusUserId, $bonusUserEmpLevelSort);
  2327. });
  2328. $insertBonusData = [];
  2329. foreach ($bonusDataList as $bonusUserData) {
  2330. $bonusUserId = $bonusUserData['bonusUserId'];
  2331. $bonusMonthPerfData = CalcCache::nowMonthPerf($bonusUserId, $this->_periodNum);
  2332. $fxPvStatus = $bonusMonthPerfData['PV_PCS_FX'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  2333. unset($bonusMonthPerfData);
  2334. if( !$fxPvStatus ) continue;
  2335. unset($fxPvStatus);
  2336. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  2337. $bonusEmpLevelId = $bonusUserInfo['EMP_LV'];
  2338. $newEmpBonus = Tool::formatPrice($qyBonus * $this->_sysConfig['sameEmpLevelPercent']['VALUE'] / 100);
  2339. //总金额限制
  2340. $newEmpBonus = $this->bonusTotalLimit($newEmpBonus, $bonusUserId, $bonusUserInfo['REC_NUM'], $bonusUserInfo['ZC_AMOUNT']);
  2341. if( $newEmpBonus <= 0 ) continue;
  2342. //扣除相应的复消积分和管理费
  2343. $newEmpBonus = Tool::formatPrice($newEmpBonus);
  2344. $deductData = $this->deduct($bonusUserId, $newEmpBonus);
  2345. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_YC_EXTRA', $newEmpBonus, $deductData);
  2346. // 把育成津贴追加到育成津贴结算表,以便奖金追溯育成津贴用
  2347. $insertBonusData[] = [
  2348. 'ID' => SnowFake::instance()->generateId(),
  2349. 'USER_ID' => $bonusUserId,
  2350. 'LAST_DEC_LV' => $bonusUserInfo['DEC_LV'],
  2351. 'LAST_EMP_LV' => $bonusUserInfo['EMP_LV'],
  2352. 'LAST_STATUS' => $bonusUserInfo['STATUS'],
  2353. 'FROM_USER_ID' => $userId,
  2354. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  2355. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  2356. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  2357. 'DEEP' => 1,
  2358. 'AMOUNT' => $deductData['surplus'],
  2359. 'ORI_BONUS' => $newEmpBonus,
  2360. 'RECONSUME_POINTS' => $deductData['reConsumePoints'],
  2361. 'MANAGE_TAX' => $deductData['manageTax'],
  2362. 'BONUS_TYPE' => $bonusUserData['bonusType'],
  2363. 'PERIOD_NUM' => $this->_periodNum,
  2364. 'CALC_MONTH' => $this->_calcYearMonth,
  2365. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  2366. 'CREATED_AT' => Date::nowTime(),
  2367. 'LOGS' => json_encode([
  2368. 'empLevelId' => $bonusEmpLevelId,
  2369. 'qyBonus' => $qyBonus,
  2370. 'sameEmpLevelPercent' => $this->_sysConfig['sameEmpLevelPercent']['VALUE'],
  2371. ]),
  2372. ];
  2373. unset($newEmpBonus, $bonusEmpLevelId, $bonusUserId, $bonusUserInfo, $deductData);
  2374. }
  2375. unset($userId, $userBaseInfo, $userEmpLevelSort, $qyBonus, $bonusDataList);
  2376. return $insertBonusData;
  2377. }
  2378. /**
  2379. * 对碰
  2380. * @param array $oriPerfArr
  2381. * @param array $perfArr
  2382. * @param $percent
  2383. * @param $loopTimes
  2384. * @return array
  2385. */
  2386. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  2387. $resultArr = $oriPerfArr;
  2388. foreach ($perfArr as $keyT => $perfT) {
  2389. if ($perfT <= 0) {
  2390. unset($perfArr[$keyT]);
  2391. }
  2392. }
  2393. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2394. arsort($perfArr, SORT_NUMERIC);
  2395. $onePerf = null;
  2396. $oneKey = null;
  2397. // $touchSurplusAmount = 0;
  2398. $touchAmount = 0;
  2399. // 前两个进行对碰
  2400. foreach ($perfArr as $key => $perf) {
  2401. if ($onePerf === null) {
  2402. $oneKey = $key;
  2403. $onePerf = $perf;
  2404. } else {
  2405. $touchSurplusAmount = $perf - $onePerf;
  2406. if ($touchSurplusAmount > 0) {
  2407. unset($perfArr[$oneKey]);
  2408. $resultArr['perfArr'][$oneKey] = 0;
  2409. $perfArr[$key] = $touchSurplusAmount;
  2410. $touchAmount = $onePerf;
  2411. } elseif ($touchSurplusAmount < 0) {
  2412. unset($perfArr[$key]);
  2413. $resultArr['perfArr'][$key] = 0;
  2414. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2415. $touchAmount = $perf;
  2416. } else {
  2417. unset($perfArr[$oneKey], $perfArr[$key]);
  2418. $resultArr['perfArr'][$oneKey] = 0;
  2419. $resultArr['perfArr'][$key] = 0;
  2420. $touchAmount = $perf;
  2421. }
  2422. break;
  2423. }
  2424. }
  2425. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2426. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2427. }*/
  2428. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  2429. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2430. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2431. // } else {
  2432. // $resultArr['touchBonus'] += $touchBonus;
  2433. // }
  2434. $resultArr['touchBonus'] += $touchBonus;
  2435. foreach ($perfArr as $keyR => $perfR) {
  2436. $resultArr['perfArr'][$keyR] = $perfR;
  2437. }
  2438. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  2439. }
  2440. return $resultArr;
  2441. }
  2442. /**
  2443. * 对碰
  2444. * @param array $oriPerfArr
  2445. * @param array $perfArr
  2446. * @param $decLevel
  2447. * @param $decLevelConfig
  2448. * @param $loopTimes
  2449. * @return array
  2450. */
  2451. public function touchPerfBefore(array $oriPerfArr, array $perfArr, $decLevel, $decLevelConfig, $loopTimes=1) {
  2452. $resultArr = $oriPerfArr;
  2453. foreach ($perfArr as $keyT => $perfT) {
  2454. if ($perfT <= 0) {
  2455. unset($perfArr[$keyT]);
  2456. }
  2457. }
  2458. if (count($perfArr) >= 2 && $loopTimes < 6) {
  2459. $nowDecLevelConfig = $decLevelConfig[$decLevel];
  2460. arsort($perfArr, SORT_NUMERIC);
  2461. $onePerf = null;
  2462. $oneKey = null;
  2463. // $touchSurplusAmount = 0;
  2464. $touchAmount = 0;
  2465. // 前两个进行对碰
  2466. foreach ($perfArr as $key => $perf) {
  2467. if ($onePerf === null) {
  2468. $oneKey = $key;
  2469. $onePerf = $perf;
  2470. } else {
  2471. $touchSurplusAmount = $perf - $onePerf;
  2472. if ($touchSurplusAmount > 0) {
  2473. unset($perfArr[$oneKey]);
  2474. $resultArr['perfArr'][$oneKey] = 0;
  2475. $perfArr[$key] = $touchSurplusAmount;
  2476. $touchAmount = $onePerf;
  2477. } elseif ($touchSurplusAmount < 0) {
  2478. unset($perfArr[$key]);
  2479. $resultArr['perfArr'][$key] = 0;
  2480. $perfArr[$oneKey] = abs($touchSurplusAmount);
  2481. $touchAmount = $perf;
  2482. } else {
  2483. unset($perfArr[$oneKey], $perfArr[$key]);
  2484. $resultArr['perfArr'][$oneKey] = 0;
  2485. $resultArr['perfArr'][$key] = 0;
  2486. $touchAmount = $perf;
  2487. }
  2488. break;
  2489. }
  2490. }
  2491. /*if ($touchAmount > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2492. $touchAmount = $nowDecLevelConfig['QY_TOUCH_CAP'];
  2493. }*/
  2494. $touchBonus = Tool::formatPrice($touchAmount * 2 / 100);
  2495. // if ($touchBonus > $nowDecLevelConfig['QY_TOUCH_CAP']) {
  2496. // $resultArr['touchBonus'] += $nowDecLevelConfig['QY_TOUCH_CAP'];
  2497. // } else {
  2498. // $resultArr['touchBonus'] += $touchBonus;
  2499. // }
  2500. $resultArr['touchBonus'] += $touchBonus;
  2501. foreach ($perfArr as $keyR => $perfR) {
  2502. $resultArr['perfArr'][$keyR] = $perfR;
  2503. }
  2504. return $this->touchPerfBefore($resultArr, $perfArr, $decLevel, $decLevelConfig, $loopTimes+1);
  2505. }
  2506. return $resultArr;
  2507. }
  2508. /**
  2509. * 循环父级并执行回调函数
  2510. * @param $userId
  2511. * @param callable $callbackFunc
  2512. * @param int $offset
  2513. * @return bool
  2514. */
  2515. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2516. $allParents = Cache::getAllNetworkParents($userId);
  2517. $allData = array_slice($allParents, $offset, $this->_limit);
  2518. unset($allParents);
  2519. if ($allData) {
  2520. foreach ($allData as $data) {
  2521. $funcResult = $callbackFunc($data);
  2522. if ($funcResult === self::LOOP_FINISH) {
  2523. return true;
  2524. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2525. continue;
  2526. }
  2527. unset($data, $funcResult);
  2528. }
  2529. unset($allData);
  2530. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2531. }
  2532. return true;
  2533. }
  2534. /**
  2535. * 循环推荐网络的父级
  2536. * @param $userId
  2537. * @param callable $callbackFunc
  2538. * @param int $offset
  2539. * @return bool
  2540. */
  2541. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  2542. $allParents = Cache::getAllRelationParents($userId);
  2543. $allData = array_slice($allParents, $offset, $this->_limit);
  2544. unset($allParents);
  2545. if ($allData) {
  2546. foreach ($allData as $data) {
  2547. $funcResult = $callbackFunc($data);
  2548. if ($funcResult === self::LOOP_FINISH) {
  2549. return true;
  2550. } elseif ($funcResult === self::LOOP_CONTINUE) {
  2551. continue;
  2552. }
  2553. unset($data, $funcResult);
  2554. }
  2555. unset($allData);
  2556. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  2557. }
  2558. return true;
  2559. }
  2560. /**
  2561. * 共享奖,判断当期此用户,是否有原报单团队奖金
  2562. */
  2563. public function checkHasOriBonusQyBd($userId) {
  2564. //判断$parent是否有首单团队奖
  2565. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  2566. if( isset($bonus['ORI_BONUS_QY_BD']) && $bonus['ORI_BONUS_QY_BD'] > 0 ) {
  2567. return true;
  2568. }
  2569. return false;
  2570. }
  2571. /**
  2572. * 判断除大市场外 其它市场的报单业绩
  2573. * @param $userId
  2574. * @return bool
  2575. * @throws \yii\db\Exception
  2576. */
  2577. public function checkSmallMarketPerf($userId) {
  2578. // 从缓存中获取会员的上期结余业绩信息
  2579. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2580. //判断出大市场
  2581. $areaNums = [1, 2, 3];
  2582. $maxArea = 0;
  2583. $maxSurplusPerf = 0;
  2584. foreach ($areaNums as $area) {
  2585. $areaKey = sprintf('SURPLUS_%dL', $area);
  2586. if ( isset($pervSurplusPerf[$areaKey]) && $pervSurplusPerf[$areaKey] > $maxSurplusPerf ) {
  2587. $maxArea = $area;
  2588. $maxSurplusPerf = $pervSurplusPerf[$areaKey];
  2589. }
  2590. unset($area, $areaKey);
  2591. }
  2592. unset($pervSurplusPerf, $maxSurplusPerf);
  2593. $otherAreaZcPv = 0;
  2594. $nowPeriodPerf = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  2595. foreach ($areaNums as $area) {
  2596. if( $maxArea > 0 && $area === $maxArea ) continue;
  2597. $areaKey = sprintf('PV_%dL_ZC', $area);
  2598. if ( isset($nowPeriodPerf[$areaKey]) && $nowPeriodPerf[$areaKey] > 0 ) {
  2599. $otherAreaZcPv += $nowPeriodPerf[$areaKey];
  2600. }
  2601. unset($area, $areaKey);
  2602. }
  2603. unset($nowPeriodPerf, $areaNums, $maxArea);
  2604. return $otherAreaZcPv >= self::MIN_BD_PV;
  2605. }
  2606. /**
  2607. * 获取有最小报单PV的用效父级
  2608. * @param $userId
  2609. * @return string
  2610. */
  2611. public function getMinBdPvRelationParent($userId) {
  2612. $validParentId = '';
  2613. $this->loopRelationParentDo($userId, function ($parent) use(&$validParentId) {
  2614. if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2615. $validParentId = $parent['PARENT_UID'];
  2616. unset($parent);
  2617. return self::LOOP_FINISH;
  2618. }
  2619. unset($parent);
  2620. });
  2621. return$validParentId;
  2622. }
  2623. /**
  2624. * 获取有最小报单PV的用效父级 20220407修改成是否此期此用户有原报单团队奖金
  2625. * @param $userId
  2626. * @return string
  2627. */
  2628. public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2629. $validParentId = '';
  2630. $validDeep = 1;
  2631. $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2632. if ( $this->checkHasOriBonusQyBd($parent['PARENT_UID']) ) {
  2633. $validParentId = $parent['PARENT_UID'];
  2634. unset($parent);
  2635. return self::LOOP_FINISH;
  2636. }
  2637. // 只找5层
  2638. if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2639. unset($parent);
  2640. return self::LOOP_FINISH;
  2641. }
  2642. $validDeep += 1;
  2643. unset($parent);
  2644. });
  2645. return$validParentId;
  2646. }
  2647. // public function getMinBdPvNetworkParent($userId, $openFindLimit = 1, $findLimitTimes = 5) {
  2648. // $validParentId = '';
  2649. // $validDeep = 1;
  2650. // $this->loopNetworkParentDo($userId, function ($parent) use(&$validParentId,&$validDeep,$openFindLimit,$findLimitTimes) {
  2651. // if ( $this->checkSmallMarketPerf($parent['PARENT_UID']) ) {
  2652. // $validParentId = $parent['PARENT_UID'];
  2653. // unset($parent);
  2654. // return self::LOOP_FINISH;
  2655. // }
  2656. // // 只找5层
  2657. // if ( $openFindLimit && $validDeep >= $findLimitTimes ) {
  2658. // unset($parent);
  2659. // return self::LOOP_FINISH;
  2660. // }
  2661. // $validDeep += 1;
  2662. // unset($parent);
  2663. // });
  2664. // return$validParentId;
  2665. // }
  2666. /**
  2667. * 按级别的收入上限
  2668. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  2669. * @param $bonus
  2670. * @param $userId
  2671. * @param $declarationLevel
  2672. * @return float
  2673. */
  2674. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2675. $decLevelConfig = $this->_decLevelConfig;
  2676. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2677. unset($decLevelConfig);
  2678. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  2679. if( $bonus <= $maxGetBonus) {
  2680. return $bonus;
  2681. }else {
  2682. return $maxGetBonus;
  2683. }
  2684. }
  2685. // public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  2686. // $decLevelConfig = $this->_decLevelConfig;
  2687. // $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  2688. // unset($decLevelConfig);
  2689. // //本月往期奖金 这里执行速度可能慢一些,但需求如此没有办法
  2690. // $lastData = CalcCache::lastPeriodMonthCalcBonus($userId, $this->_periodNum, $this->_calcMonth);
  2691. // $lastTotal = $lastData['ORI_BONUS_QY_SUM'] + $lastData['ORI_BONUS_VIP_SUM'];
  2692. // // 从缓存中获取用户的奖金
  2693. // $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2694. // $thisTotal = $bonusData['ORI_BONUS_QY'] + $bonusData['ORI_BONUS_VIP'];
  2695. // $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'] * $this->_calcMonthPeriodNumCount - $lastTotal - $thisTotal;
  2696. // unset($lastData, $lastTotal, $bonusData, $thisTotal);
  2697. // if( $maxGetBonus <= 0 ) return 0.00;
  2698. // if( $bonus <= $maxGetBonus) {
  2699. // return $bonus;
  2700. // }else {
  2701. // return $maxGetBonus;
  2702. // }
  2703. // }
  2704. /**
  2705. * 总奖金限制
  2706. * @param $bonus
  2707. * @param $userId
  2708. * @param $recNum
  2709. * @param $decAmount
  2710. * @return float
  2711. */
  2712. public function bonusTotalLimit($bonus, $userId, $recNum, $decAmount) {
  2713. //@todo 临时不做总奖金限制
  2714. return $bonus;
  2715. if( $recNum >= 3 ) return $bonus;
  2716. $limitAmount = 0;
  2717. switch ($recNum) {
  2718. case 0:
  2719. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalZeroLimit']['VALUE'];
  2720. break;
  2721. case 1:
  2722. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalOneLimit']['VALUE'];
  2723. break;
  2724. case 2:
  2725. $limitAmount = $decAmount * $this->_sysConfig['bonusTotalTwoLimit']['VALUE'];
  2726. break;
  2727. default:
  2728. break;
  2729. }
  2730. // 从缓存中获取用户的奖金
  2731. $lastUserBonus = CalcCache::LastUserBonus($userId, $this->_periodNum);//往期的总金额
  2732. $bonusData = CalcCache::bonus($userId, $this->_periodNum);
  2733. $bonusTotal = $bonusData['BONUS_TOTAL'] + $lastUserBonus['BONUS_TOTAL'];
  2734. unset($lastUserBonus, $userId, $recNum, $decAmount, $bonusData);
  2735. if( $bonusTotal >= $limitAmount ) return 0.00;
  2736. if( $bonusTotal + $bonus <= $limitAmount) {
  2737. return $bonus;
  2738. }else {
  2739. return $limitAmount - $bonusTotal;
  2740. }
  2741. }
  2742. /**
  2743. * 扣除复消积分和管理费
  2744. * @param $userId
  2745. * @param $bonus
  2746. * @return array
  2747. * @throws \yii\db\Exception
  2748. */
  2749. public function deductNoReconsumePoints($userId, $bonus): array
  2750. {
  2751. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2752. $surplus = $bonus - $manageTax;
  2753. return [
  2754. 'reConsumePoints' => 0.00,
  2755. 'manageTax' => Tool::formatPrice($manageTax),
  2756. 'surplus' => Tool::formatPrice($surplus),
  2757. ];
  2758. }
  2759. /**
  2760. * 扣除复消积分和管理费
  2761. * @param $userId
  2762. * @param $bonus
  2763. * @return array
  2764. * @throws \yii\db\Exception
  2765. */
  2766. public function deduct($userId, $bonus, $isGx = false) {
  2767. // 如果是共享奖金,则只有最后一期,才扣除管理费?其他三期不扣除管理费.
  2768. //判断是否达到了本月扣除复消的上限
  2769. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  2770. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  2771. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $cacheData['RECONSUME_POINTS_SUM'];
  2772. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  2773. unset($cacheData, $bonusCache);
  2774. $reConsumePoints = 0;
  2775. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  2776. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  2777. // 2022/06/07上限改成10000000 奖金乘以比例 上限1000 // 当月总复消积分 前三期加当前的
  2778. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  2779. }
  2780. unset($reConsumePointsTotal, $reConsumePointsCap);
  2781. $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  2782. $surplus = $bonus - $reConsumePoints - $manageTax;
  2783. return [
  2784. 'reConsumePoints' => Tool::formatPrice($reConsumePoints),
  2785. 'manageTax' => Tool::formatPrice($manageTax),
  2786. 'surplus' => Tool::formatPrice($surplus),
  2787. ];
  2788. }
  2789. /**
  2790. * 更新百分比并发送
  2791. * @param $percent
  2792. */
  2793. private function _updatePercent($percent) {
  2794. // 把数据写入数据库中
  2795. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  2796. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  2797. }
  2798. /**
  2799. * 把往期有月奖金的会员加到本期有奖会员缓存列表中
  2800. * @param int $offset
  2801. * @return bool
  2802. */
  2803. public function loopMonthBonusUserFromDbToCache($offset = 0) {
  2804. if (!$this->_isCalcMonth) {
  2805. return true;
  2806. }
  2807. $allData = CalcMonthBonusUser::findUseDbCalc()->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])->offset($offset)->limit($this->_limit)->asArray()->all();
  2808. if ( $allData ) {
  2809. foreach ($allData as $everyData) {
  2810. CalcCache::addHasBonusUsers($everyData['USER_ID'], $this->_periodNum);
  2811. unset($everyData);
  2812. }
  2813. unset($allData);
  2814. $this->loopMonthBonusUserFromDbToCache($offset + $this->_limit);
  2815. }
  2816. unset($allData);
  2817. return true;
  2818. }
  2819. /**
  2820. * 把缓存中的月奖用户信息存到数据库
  2821. * @param int $offset
  2822. * @return bool
  2823. * @throws \yii\db\Exception
  2824. */
  2825. public function loopMonthBonusUserToDb($offset = 0) {
  2826. echo sprintf("时间:[%s]月奖会员入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2827. $allData = CalcCache::getHasMonthBonusUsers($this->_periodNum, $offset, $this->_limit);
  2828. if($allData) {
  2829. $insertDataBonusUser = [];
  2830. foreach($allData as $userId){
  2831. $insertDataBonusUser[] = [
  2832. 'ID' => SnowFake::instance()->generateId(),
  2833. 'USER_ID' => $userId,
  2834. 'CALC_MONTH' => $this->_calcYearMonth,
  2835. 'PERIOD_NUM' => $this->_periodNum,
  2836. 'CREATED_AT' => Date::nowTime()
  2837. ];
  2838. unset($userId);
  2839. }
  2840. if( $insertDataBonusUser ) CalcMonthBonusUser::batchInsert($insertDataBonusUser);
  2841. unset($insertDataBonus, $allData);
  2842. return $this->loopMonthBonusUserToDb($offset + $this->_limit);
  2843. }
  2844. unset($allData);
  2845. return true;
  2846. }
  2847. //奖金入库
  2848. /**
  2849. * 循环奖服务奖金会员,并入库
  2850. * @param int $offset
  2851. * @return bool
  2852. * @throws \yii\db\Exception
  2853. */
  2854. public function loopBonusUsers($offset = 0) {
  2855. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  2856. // 从缓存列表里面从底层往上倒序获取会员
  2857. // 这里有问题,因为蓝星奖,是存储过程计算的,并没有加入缓存,这里需要修改.
  2858. // 给用户发送兑换积分,也需要进行调整查看。
  2859. // CalcCache::addHasBonusUsers($everyData['USER_ID'], $this->_periodNum);
  2860. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  2861. if($allData){
  2862. $insertDataBonus = [];
  2863. foreach($allData as $userId){
  2864. $tempBonusData = $this->bonusData($userId);
  2865. if(!empty($tempBonusData['result'])){
  2866. $insertDataBonus[] = $tempBonusData['result'];
  2867. }
  2868. unset($userId, $tempBonusData);
  2869. }
  2870. // echo json_encode($insertDataBonus);
  2871. // exit();
  2872. CalcBonus::batchInsert($insertDataBonus);
  2873. unset($insertDataBonus, $allData);
  2874. return $this->loopBonusUsers($offset + $this->_limit);
  2875. }
  2876. return true;
  2877. }
  2878. // 奖金入库完成,将各个奖金计算流水会员聘级,更新成蓝星奖当时计算的聘级
  2879. public function loopCalcBlueEmpLv($offset = 0) {
  2880. if( !$this->_isCalcMonth ) {
  2881. // 不是结算月,则不需要进行聘级调整
  2882. return false;
  2883. }
  2884. // 从缓存获取分页有收入的会员信息
  2885. $allData = CalcBonusBS::findUseDbCalc()
  2886. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  2887. ->groupBy('USER_ID')
  2888. ->offset($offset)
  2889. ->limit($this->_limit)
  2890. ->asArray()
  2891. ->all();
  2892. if ($allData) {
  2893. foreach ($allData as $data) {
  2894. $nowBsEmpLv = $data['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
  2895. // 修改AR_CALC_BONUS的LAST_EMP_LV聘级,修改奖金流水的聘级AR_FLOW_BONUS表的LAST_EMP_LV
  2896. // 期结算结果
  2897. CalcBonus::updateAll(['LAST_EMP_LV' => $nowBsEmpLv], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2898. [
  2899. ':USER_ID' => $data['USER_ID'],
  2900. ':PERIOD_NUM' => $this->_periodNum
  2901. ]
  2902. );
  2903. // 奖金流水
  2904. FlowBonus::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. CalcBonusGX::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. CalcBonusTG::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. CalcBonusQY::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. CalcBonusBD::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. // 更新form的聘级
  2939. CalcBonusGX::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2940. [
  2941. ':FROM_USER_ID' => $data['USER_ID'],
  2942. ':PERIOD_NUM' => $this->_periodNum
  2943. ]
  2944. );
  2945. CalcBonusTG::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2946. [
  2947. ':FROM_USER_ID' => $data['USER_ID'],
  2948. ':PERIOD_NUM' => $this->_periodNum
  2949. ]
  2950. );
  2951. CalcBonusBD::updateAll(['LAST_FROM_EMP_LV' => $nowBsEmpLv], 'FROM_USER_ID=:FROM_USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  2952. [
  2953. ':FROM_USER_ID' => $data['USER_ID'],
  2954. ':PERIOD_NUM' => $this->_periodNum
  2955. ]
  2956. );
  2957. }
  2958. unset($allData);
  2959. return $this->loopCalcBlueEmpLv($offset + $this->_limit);
  2960. }
  2961. unset($allData);
  2962. return true;
  2963. }
  2964. /**
  2965. * 奖金
  2966. * @param $userId
  2967. * @return array
  2968. * @throws \yii\db\Exception
  2969. */
  2970. public function bonusData($userId) {
  2971. // 从缓存中获取用户的奖金
  2972. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  2973. $standardBonus = CalcCache::standardBonus($userId, $this->_periodNum);
  2974. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  2975. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  2976. $empLv = $baseInfo['EMP_LV'];
  2977. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  2978. //没有共享和管理奖
  2979. $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;
  2980. $realBonusGx = 0;
  2981. $realBonusGl = 0;
  2982. $realBonusBs = 0; // 蓝星管理奖. BlueStar
  2983. $blueStartAmount = 0;
  2984. $buleStartOriBonus = 0;
  2985. $buleStartManageTax = 0;
  2986. $exchangePoints = 0; // 蓝星奖管理奖. 产生的兑换积分
  2987. $blueBonusGL = 0; //拆分蓝星管理奖,原奖金
  2988. $blueBonusYJ = 0; //拆分蓝星业绩奖,原奖金
  2989. $blueBonusGLSF = 0;//拆分蓝星管理奖,实发
  2990. $blueBonusYJSF = 0;//拆分蓝星业绩奖,实发
  2991. // rm1820 共享奖周算且不计入总原奖金中,所以需要在总原奖金中去掉
  2992. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $bonus['ORI_BONUS_GX'];
  2993. $oriGxMonthBonus = 0; // 月共享奖金,结算月之和原总金额
  2994. $monthGxReal = $monthGxManageTax = $monthGxpoint = 0;
  2995. $bonus['BONUS_GX'] = 0; // 以前的周共享奖实发金额,新版本每周不发,不扣除复消积分管理费,所以这里给0
  2996. if( $this->_isCalcMonth ) {
  2997. $fxPvStatus = $this->_isMonthPerfLimit($userId);
  2998. // BONUS_REAL 字段是发到用户的真实奖金
  2999. if ( $fxPvStatus ) {
  3000. // 月节点,获取蓝星管理奖数据
  3001. $userBS = CalcBonusBS::find()
  3002. ->where(
  3003. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  3004. [
  3005. ':PERIOD_NUM' => $this->_periodNum,
  3006. ':USER_ID' => $userId
  3007. ]
  3008. )
  3009. ->select('AMOUNT,ORI_BONUS,MANAGE_TAX,LEVEL_ID,PRODUCT_POINT,ORI_BONUS_ABBR,AMOUNT_ABBR,ORI_BONUS_MNT,AMOUNT_MNT')
  3010. ->limit(1)
  3011. ->orderBy('CREATED_AT DESC')
  3012. ->asArray()
  3013. ->all();
  3014. $userBS = is_array($userBS) ? reset($userBS) : [];
  3015. $blueStartAmount = isset($userBS['AMOUNT']) && !empty($userBS['AMOUNT']) ? $userBS['AMOUNT'] : 0; // 奖金
  3016. $buleStartOriBonus = isset($userBS['ORI_BONUS']) && !empty($userBS['ORI_BONUS']) ? $userBS['ORI_BONUS'] : 0; // 原奖金
  3017. $blueBonusGL = isset($userBS['ORI_BONUS_MNT']) && !empty($userBS['ORI_BONUS_MNT']) ? $userBS['ORI_BONUS_MNT'] : 0; // 拆分管理奖原奖金
  3018. $blueBonusYJ = isset($userBS['ORI_BONUS_ABBR']) && !empty($userBS['ORI_BONUS_ABBR']) ? $userBS['ORI_BONUS_ABBR'] : 0; // 拆分业绩奖原奖金
  3019. $blueBonusGLSF = isset($userBS['AMOUNT_MNT']) && !empty($userBS['AMOUNT_MNT']) ? $userBS['AMOUNT_MNT'] : 0; // 拆分管理奖 实发
  3020. $blueBonusYJSF = isset($userBS['AMOUNT_ABBR']) && !empty($userBS['AMOUNT_ABBR']) ? $userBS['AMOUNT_ABBR'] : 0; // 拆分业绩奖 实发
  3021. //$blueStartAmount = $blueBonusGLSF + $blueBonusYJSF; // 总实发,管理奖实发+业绩奖实发
  3022. #rm1820 共享奖金月节点合起来原奖金一起扣除管理费和复消积分
  3023. $monthSumData = CalcBonus::findUseSlaves()
  3024. ->select('SUM(ORI_BONUS_GX) AS BONUS_GX_SUM, SUM(BONUS_GL) AS BONUS_GL_SUM')
  3025. ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  3026. [
  3027. 'USER_ID'=>$userId,
  3028. 'CALC_MONTH'=>$this->_calcYearMonth
  3029. ]
  3030. )
  3031. ->asArray()
  3032. ->one();
  3033. $bonusGxSum = $monthSumData['BONUS_GX_SUM'] ?? 0;
  3034. $bonusGxSum = $bonus['ORI_BONUS_GX'] + $bonusGxSum;
  3035. $oriGxMonthBonus = $bonusGxSum;
  3036. if ($bonusGxSum > 0) {
  3037. // 扣除管理费和复消积分
  3038. //扣除相应的复消积分和管理费
  3039. $deductData = $this->deduct($userId, $bonusGxSum);
  3040. $monthGxReal = $deductData['surplus']; // 扣除管理费和复消积分后的实发金额
  3041. $monthGxManageTax = $deductData['manageTax']; // 管理费
  3042. $monthGxpoint = $deductData['reConsumePoints'];// 复消积分
  3043. }
  3044. $realBonusGx = $monthGxReal;
  3045. $bonusGlSum = $monthSumData['BONUS_GL_SUM'] ?? 0;
  3046. $bonus['RECONSUME_POINTS'] = $bonus['RECONSUME_POINTS'] + $monthGxpoint;
  3047. $bonus['MANAGE_TAX'] = $bonus['MANAGE_TAX'] + $monthGxManageTax;
  3048. $realBonusGl += $bonusGlSum + $bonus['BONUS_GL'];
  3049. $realBonusBs = $blueStartAmount;
  3050. $bonusReal += $realBonusGx + $realBonusBs + $realBonusGl;
  3051. $exchangePoints = isset($userBS['PRODUCT_POINT']) && !empty($userBS['PRODUCT_POINT']) ? $userBS['PRODUCT_POINT'] : 0; // 兑换积分
  3052. unset($monthSumData, $bonusGxSum, $bonusGlSum);
  3053. $oriBonusBs = isset($bonus['ORI_BONUS_BS']) && $bonus['ORI_BONUS_BS'] > 0 ? $bonus['ORI_BONUS_BS'] : 0;
  3054. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $blueBonusGL - $blueBonusYJ;
  3055. // 共享奖最后一起算,所以总原奖金中,需要加上整个月的数据
  3056. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] + $oriGxMonthBonus;
  3057. } else {
  3058. // 蓝星奖原奖金在扣管理费的时候被加入到了BONUS_TOTAL,需要再减去原奖金
  3059. $oriBonusBs = isset($bonus['ORI_BONUS_BS']) && $bonus['ORI_BONUS_BS'] > 0 ? $bonus['ORI_BONUS_BS'] : 0;
  3060. // 如果未满足,则复消积分,应该不能发放
  3061. $oriBonusBsYJCF = isset($bonus['ORI_BONUS_ABBR']) && $bonus['ORI_BONUS_ABBR'] > 0 ? $bonus['ORI_BONUS_ABBR'] : 0;
  3062. $oriBonusBsGLCF = isset($bonus['ORI_BONUS_MNT']) && $bonus['ORI_BONUS_MNT'] > 0 ? $bonus['ORI_BONUS_MNT'] : 0;
  3063. $bonus['BONUS_TOTAL'] = $bonus['BONUS_TOTAL'] - $oriBonusBs - $oriBonusBsYJCF - $oriBonusBsGLCF;
  3064. }
  3065. }
  3066. $result = [
  3067. 'USER_ID' => $userId,
  3068. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  3069. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  3070. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  3071. 'LAST_EMP_LV' => $empLv,
  3072. 'LAST_STATUS' => $baseInfo['STATUS'],
  3073. 'LAST_MOBILE' => $baseInfo['MOBILE'],
  3074. 'LAST_PERIOD_AT' => $baseInfo['PERIOD_NUM'],
  3075. 'LAST_CREATED_AT' => $baseInfo['CREATED_AT'],
  3076. 'LAST_SUB_COM_ID' => $baseInfo['SUB_COM_ID'],
  3077. 'LAST_PROVINCE' => $baseInfo['PROVINCE'],
  3078. 'LAST_CITY' => $baseInfo['CITY'],
  3079. 'LAST_COUNTY' => $baseInfo['COUNTY'],
  3080. 'LAST_SYSTEM_ID' => $baseInfo['SYSTEM_ID'] ? $baseInfo['SYSTEM_ID'] : '',
  3081. 'LAST_IS_DIRECT_SELLER' => $baseInfo['IS_DIRECT_SELLER'],
  3082. 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'],
  3083. 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'],
  3084. 'LAST_CON_USER_NAME' => $baseInfo['CON_USER_NAME'],
  3085. 'LAST_CON_REAL_NAME' => $baseInfo['CON_REAL_NAME'],
  3086. 'EXCHANGE_POINTS' => $exchangePoints, // 兑换积分
  3087. // 'LAST_LOCATION' => $baseInfo['LOCATION'] ? $baseInfo['LOCATION'] : 1,
  3088. //@todo
  3089. 'LAST_LOCATION' => 1,
  3090. 'BONUS_BD' => $bonus['BONUS_BD'],
  3091. 'BONUS_TG' => $bonus['BONUS_TG'],
  3092. 'BONUS_XF' => $bonus['BONUS_XF'],
  3093. 'BONUS_YJ' => $bonus['BONUS_YJ'],
  3094. 'BONUS_GX' => $bonus['BONUS_GX'],
  3095. 'BONUS_GL' => $bonus['BONUS_GL'],
  3096. 'BONUS_QY' => $bonus['BONUS_QY'],
  3097. 'BONUS_YC' => $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'],
  3098. 'BONUS_VIP' => $bonus['BONUS_VIP'],
  3099. 'RECONSUME_POINTS' => $bonus['RECONSUME_POINTS'],
  3100. 'MANAGE_TAX' => $bonus['MANAGE_TAX'],
  3101. 'BONUS_INCOME'=>$bonus['INCOME_TOTAL'],
  3102. 'BONUS_REAL'=> $bonusReal,
  3103. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  3104. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  3105. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  3106. 'ORI_BONUS_XF' => $bonus['ORI_BONUS_XF'],
  3107. 'ORI_BONUS_YJ' => $bonus['ORI_BONUS_YJ'],
  3108. 'ORI_BONUS_YJ_BD' => $bonus['ORI_BONUS_YJ_BD'],
  3109. 'ORI_BONUS_YJ_FX' => $bonus['ORI_BONUS_YJ_FX'],
  3110. 'ORI_BONUS_GX' => $bonus['ORI_BONUS_GX'],
  3111. 'ORI_MONTH_GX_BONUS' => $oriGxMonthBonus,
  3112. 'GX_MANAGE_TAX' => $monthGxManageTax, //月共享奖管理费
  3113. 'GX_RECONSUME_POINTS' => $monthGxpoint, //月共享奖复消积分
  3114. 'REAL_BONUS_GX' => $realBonusGx,
  3115. 'ORI_BONUS_GL' => $bonus['ORI_BONUS_GL'],
  3116. 'REAL_BONUS_GL' => $realBonusGl,
  3117. 'BONUS_BS' => $realBonusBs, // 新的管理奖金,即蓝星管理奖
  3118. 'ORI_BONUS_BS' => $buleStartOriBonus, // 蓝星管理奖金原奖金,即包含管理费
  3119. 'REAL_BONUS_BS' => $realBonusBs, // 实发蓝星管理奖金
  3120. 'ORI_BONUS_GL_BD' => $bonus['ORI_BONUS_GL_BD'],
  3121. 'ORI_BONUS_GL_FX' => $bonus['ORI_BONUS_GL_FX'],
  3122. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  3123. 'ORI_BONUS_QY_BD' => $bonus['ORI_BONUS_QY_BD'],
  3124. 'ORI_BONUS_QY_FX' => $bonus['ORI_BONUS_QY_FX'],
  3125. 'ORI_BONUS_YC' => $bonus['ORI_BONUS_YC'] + $bonus['ORI_BONUS_YC_EXTRA'],
  3126. 'ORI_BONUS_VIP' => $bonus['ORI_BONUS_VIP'],
  3127. 'ORI_BONUS_STANDARD' => $standardBonus,
  3128. 'BONUS_MNT' => $blueBonusGLSF,
  3129. 'ORI_BONUS_MNT' => $blueBonusGL,
  3130. 'ORI_BONUS_ABBR' => $blueBonusYJ,
  3131. 'BONUS_ABBR' => $blueBonusYJSF,
  3132. //以下没有用
  3133. 'BONUS_FW' => 0,
  3134. 'BONUS_FX' => $bonus['BONUS_FX'],
  3135. 'BONUS_LS' => $bonus['BONUS_LS'],
  3136. 'BONUS_BT' => $bonus['BONUS_BT'],
  3137. 'BONUS_BT_PROD' => $bonus['BONUS_BT_PROD'],
  3138. 'BONUS_BT_TOOL' => $bonus['BONUS_BT_TOOL'],
  3139. 'DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3140. 'BONUS_FL' => $bonus['BONUS_FL'],
  3141. 'BONUS_CF' => $bonus['BONUS_CF'],
  3142. 'BONUS_LX' => $bonus['BONUS_LX'],
  3143. 'SHOULD_QY' => $bonus['BONUS_QY'],
  3144. 'SHOULD_DEDUCT_ZR' => $bonus['DEDUCT_ZR'],
  3145. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  3146. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  3147. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  3148. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  3149. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  3150. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  3151. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  3152. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  3153. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  3154. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  3155. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  3156. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  3157. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  3158. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  3159. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  3160. 'PV_PCS' => $perfData['PV_PCS'],
  3161. 'PV_LS_TOUCH' => 0.00,
  3162. 'SURPLUS_LS' => 0.00,
  3163. 'QY_LS' => 0.00,
  3164. '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']),
  3165. 'PERIOD_NUM' => $this->_periodNum,
  3166. 'CALC_YEAR' => $this->_calcYear,
  3167. 'CALC_MONTH' => $this->_calcYearMonth,
  3168. 'CALCULATED_AT' => Date::nowTime(),
  3169. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  3170. 'CREATED_AT' => Date::nowTime(),
  3171. ];
  3172. $resend = [];
  3173. unset($bonus, $realBonusGx, $realBonusGl, $bonusReal);
  3174. return ['result'=>$result,'resend'=>$resend];
  3175. }
  3176. /**
  3177. * 根据本有收入计算基础积分
  3178. * @param int $offset
  3179. * @return bool
  3180. */
  3181. public function calcBaseScore(int $offset=0) {
  3182. if (!$this->_isCalcMonth) {
  3183. return true;
  3184. }
  3185. echo sprintf("时间:[%s]基础积分,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3186. //查询本月的,每人的累计总收入
  3187. $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();
  3188. if ($allData) {
  3189. foreach ($allData as $everyData) {
  3190. $validTotalSum = $everyData['TOTAL_SUM'] - $everyData['BD_TOTAL_SUM'];
  3191. if( $validTotalSum <=0 ) continue;
  3192. //达不到 见习主任级别,不产生基础积分
  3193. $userBaseInfo = CalcCache::getUserInfo($everyData['USER_ID'], $this->_periodNum);
  3194. $userEmpLevel = $this->_empLevelConfig[$userBaseInfo['EMP_LV']];
  3195. $userEmpLevelSort = $userEmpLevel['SORT'] ?? EmployLevel::EMP_LEVEL_SORT['NO_LEVEL'];
  3196. unset($userBaseInfo, $userEmpLevel);
  3197. if( $userEmpLevelSort < EmployLevel::EMP_LEVEL_SORT['JX_ZR_LEVEL'] ) {
  3198. unset($userEmpLevelSort);
  3199. continue;
  3200. }
  3201. unset($userEmpLevelSort);
  3202. //根据总收入获取积分
  3203. $baseScore = $this->_getBaseScoreByTotalBonus($validTotalSum);
  3204. //根据用户的级别判断 能否得到级别积分
  3205. if( $baseScore > 0 ) {
  3206. CalcCache::nowMonthScore($everyData['USER_ID'], $this->_periodNum, [
  3207. 'BASE_SCORE' => $baseScore,
  3208. ]);
  3209. CalcCache::addHasScoreUsers($everyData['USER_ID'], $this->_periodNum);
  3210. }
  3211. unset($everyData, $validTotalSum, $baseScore);
  3212. }
  3213. unset($allData);
  3214. return $this->calcBaseScore($offset + $this->_limit);
  3215. }
  3216. unset($allData);
  3217. return true;
  3218. }
  3219. /**
  3220. * 循环积分入库
  3221. * @param int $offset
  3222. * @return bool
  3223. * @throws \yii\db\Exception
  3224. */
  3225. public function loopWriteScore(int $offset = 0) {
  3226. if(!$this->_isCalcMonth){
  3227. return true;
  3228. }
  3229. echo sprintf("时间:[%s]积分入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  3230. // 从缓存列表里面从底层往上倒序获取会员
  3231. $allData = CalcCache::getHasScoreUsers($this->_periodNum, $offset, $this->_limit);
  3232. if($allData){
  3233. $insertDataMonthScore = [];
  3234. foreach($allData as $userId){
  3235. $monthScoreData = CalcCache::nowMonthScore($userId, $this->_periodNum);
  3236. $totalScore = $monthScoreData['BASE_SCORE'] + $monthScoreData['LEVEL_SCORE'] + $monthScoreData['UPGRADE_SCORE'];
  3237. if( $totalScore <= 0 ) continue;
  3238. $insertDataMonthScore[] = [
  3239. 'USER_ID' => $userId,
  3240. 'BASE_SCORE' => $monthScoreData['BASE_SCORE'],
  3241. 'LEVEL_SCORE' => $monthScoreData['LEVEL_SCORE'],
  3242. 'UPGRADE_SCORE' => $monthScoreData['UPGRADE_SCORE'],
  3243. 'TOTAL_SCORE' => $totalScore,
  3244. 'PERIOD_NUM' => $this->_periodNum,
  3245. 'CALC_MONTH' => $this->_calcYearMonth,
  3246. 'CREATED_AT' => Date::nowTime(),
  3247. ];
  3248. unset($userId, $monthScoreData, $totalScore);
  3249. }
  3250. ScoreMonth::batchInsert($insertDataMonthScore);
  3251. unset($insertDataMonthScore, $allData);
  3252. return $this->loopWriteScore($offset + $this->_limit);
  3253. }
  3254. unset($allData);
  3255. return true;
  3256. }
  3257. /**
  3258. * 根据总收入获取相应的基础积分
  3259. * @param $totalBonus
  3260. * @return int|mixed
  3261. */
  3262. private function _getBaseScoreByTotalBonus($totalBonus) {
  3263. $baseScoreConfig = Json::decode($this->_sysConfig['baseScore']['VALUE']);
  3264. $baseScore = 0;
  3265. foreach ($baseScoreConfig as $everyScoreData) {
  3266. if( $totalBonus < $everyScoreData['monthTotalPvMin'] ) continue;
  3267. if( $everyScoreData['monthTotalPvMax'] != 0 && $totalBonus >= $everyScoreData['monthTotalPvMax'] ) continue;
  3268. $baseScore = $everyScoreData['score'];
  3269. unset($everyScoreData);
  3270. break;
  3271. }
  3272. unset($baseScoreConfig, $totalBonus);
  3273. return $baseScore;
  3274. }
  3275. // 判断是否满足月最低消费
  3276. public function _isMonthPerfLimit($userId) {
  3277. $userMonthTotal = PerfMonth::find()->where(
  3278. 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  3279. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth]
  3280. )
  3281. ->asArray()
  3282. ->one();
  3283. $fxPvStatus = false;
  3284. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  3285. $fxPvStatus = true;
  3286. }
  3287. return $fxPvStatus;
  3288. }
  3289. }