BonusCalc.php 147 KB

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