BonusCalc.php 158 KB

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