BonusCalc.php 172 KB

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