CalcCache.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/8/2
  6. * Time: 上午10:38
  7. */
  8. namespace common\helpers\bonus;
  9. use common\components\Redis;
  10. use common\helpers\Cache;
  11. use common\helpers\Date;
  12. use common\helpers\user\Info;
  13. use common\models\CalcBonus;
  14. use common\models\PerfMonth;
  15. use common\models\PerfPeriod;
  16. use common\models\Period;
  17. use common\models\DeclarationLevel;
  18. use common\models\DecRole;
  19. use common\models\EmployLevel;
  20. use common\models\StarCrownLevel;
  21. use common\models\User;
  22. use common\models\UserBonus;
  23. use common\models\UserNetwork;
  24. use common\models\UserPerf;
  25. use common\models\UserRelation;
  26. use Yii;
  27. use common\models\UserInfo;
  28. use yii\helpers\Json;
  29. class CalcCache {
  30. const LIMIT = 1000;
  31. const REDIS_KEY_PREFIX_USER = 'calc:user_';
  32. const REDIS_KEY_PREFIX_USER_ACTIVE = 'calc:userActive_';
  33. const REDIS_KEY_PREFIX_USER_INFO = 'calc:userInfo_';
  34. const REDIS_KEY_PREFIX_USER_BONUS = 'calc:userBonus_';
  35. const REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS = 'calc:periodMonthCalcBonus_';
  36. const REDIS_KEY_PREFIX_USER_PERF = 'calc:userPerf_';
  37. const REDIS_KEY_PREFIX_SURPLUS_PERF = 'calc:spPerf_';
  38. const REDIS_KEY_PREFIX_NOW_PERIOD_PERF = 'calc:nowPeriodPerf_';
  39. const REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF = 'calc:nowStandardMonthPerf_';
  40. const REDIS_KEY_PREFIX_LAST_MONTH_PERF = 'calc:lastMonthPerf_';
  41. const REDIS_KEY_PREFIX_NOW_MONTH_PERF = 'calc:nowMonthPerf_';
  42. const REDIS_KEY_PREFIX_NOW_MONTH_SCORE = 'calc:nowMonthScore_';
  43. const REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS = 'calc:nowMonthLastPeriodReconsumePoints_';
  44. const REDIS_KEY_PREFIX_EMP_NUM_PERF = 'calc:empLevelNum_';
  45. const REDIS_KEY_PREFIX_BONUS = 'calc:bonus_';
  46. const REDIS_KEY_PREFIX_STANDARD_BONUS = 'calc:standard:bonus_';
  47. const REDIS_KEY_PREFIX_YC_BONUS = 'calc:yc:bonus_';
  48. const REDIS_KEY_PREFIX_FW_BONUS = 'calc:fw:bonus_';
  49. const REDIS_KEY_PREFIX_TOURISM_BONUS = 'calc:tourism:bonus_';
  50. const REDIS_KEY_PREFIX_VILLA_BONUS = 'calc:villa:bonus_';
  51. const REDIS_KEY_PREFIX_GARAGE_BONUS = 'calc:garage:bonus_';
  52. const REDIS_KEY_PREFIX_HAS_PERF_USER = 'calc:hasPerfUser_';
  53. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER = 'calc:hasMonthPerfUser_';
  54. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER = 'calc:hasStandardMonthPerfUser_';
  55. const REDIS_KEY_PREFIX_HAS_BD_USER = 'calc:hasBDUser_';
  56. const REDIS_KEY_PREFIX_HAS_INCOME_USER = 'calc:hasIncomeUser_';
  57. const REDIS_KEY_PREFIX_HAS_BONUS_USER = 'calc:hasBonusUser_';
  58. const REDIS_KEY_PREFIX_HAS_YC_BONUS_USER = 'calc:hasYcBonusUser_';
  59. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER = 'calc:hasFwBonusUser_';
  60. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER = 'calc:hasMonthBonusUser_';
  61. const REDIS_KEY_PREFIX_HAS_PERF_USER_POOL = 'calc:hasPerfUserPool_';
  62. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL = 'calc:hasMonthPerfUserPool_';
  63. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL = 'calc:hasStandardMonthPerfUserPool_';
  64. const REDIS_KEY_PREFIX_HAS_BD_USER_POOL = 'calc:hasBDUserPool_';
  65. const REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL = 'calc:hasIncomeUserPool_';
  66. const REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL = 'calc:hasBonusUserPool_';
  67. const REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL = 'calc:hasYcBonusUserPool_';
  68. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL = 'calc:hasFwBonusUserPool_';
  69. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL = 'calc:hasMonthBonusUserPool_';
  70. const REDIS_KEY_PREFIX_HAS_SCORE_USER = 'calc:hasScoreUser_';
  71. const REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL = 'calc:hasScoreUserPool_';
  72. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER = 'calc:hasLSPCSUser_';
  73. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL = 'calc:hasLSPCSUserPool_';
  74. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER = 'calc:hasCFPercentPCSUser_';
  75. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL = 'calc:hasCFPercentPCSUserPool_';
  76. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER = 'calc:hasLXPercentPCSUser_';
  77. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL = 'calc:hasLXPercentPCSUserPool_';
  78. const REDIS_KEY_PREFIX_DEC_ROLE_CONFIG = 'calc:decRoleConfig_';
  79. const REDIS_KEY_PREFIX_CF_TOTAL_PERCENT = 'calc:cfTotalPercent_';
  80. const REDIS_KEY_PREFIX_LX_TOTAL_PERCENT = 'calc:lxTotalPercent_';
  81. const REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP = 'calc:userInfo:ChildOneDeep_';
  82. const REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA = 'calc:gxBonusDeepOneListData_';
  83. const REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA = 'calc:gxBonusDeepTwoListData_';
  84. const REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA = 'calc:ycBonusListData_';
  85. const REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA = 'calc:fwBonusListData_';
  86. const REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA = 'calc:bonusStarCrownListData_';
  87. //@todo
  88. const REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF = 'calc:repairSurplusPerf_';
  89. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER = 'calc:hasRepairPerfUser_';
  90. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL = 'calc:hasRepairPerfPool_';
  91. //收入的奖金类型
  92. // const INCOME_TG_BONUS = 'BONUS_TG';
  93. // const INCOME_XF_BONUS = 'BONUS_XF';
  94. // const INCOME_YJ_BONUS = 'BONUS_YJ';
  95. // const INCOME_QY_BONUS = 'BONUS_QY';
  96. const INCOME_QY_BONUS_BD = 'BONUS_QY_BD';
  97. const INCOME_QY_BONUS_FX = 'BONUS_QY_FX';
  98. const INCOME_BONUS_TRAVEL = 'BONUS_TRAVEL';
  99. const INCOME_BONUS_CAR = 'BONUS_CAR';
  100. const INCOME_BONUS_HOUSE = 'BONUS_HOUSE';
  101. const CAPPED_BONUS_QY = 'CAPPED_BONUS_QY'; // 团队奖封顶前的金额
  102. const INCOME_BONUS_LIST = [
  103. // self::INCOME_TG_BONUS,
  104. // self::INCOME_XF_BONUS,
  105. // self::INCOME_YJ_BONUS,
  106. // self::INCOME_QY_BONUS,
  107. self::INCOME_QY_BONUS_BD,
  108. // self::INCOME_QY_BONUS_FX,
  109. ];
  110. const NOT_SEND_BONUS_LIST = [
  111. self::INCOME_QY_BONUS_BD,
  112. self::INCOME_QY_BONUS_FX,
  113. self::INCOME_BONUS_TRAVEL,
  114. self::INCOME_BONUS_CAR,
  115. self::INCOME_BONUS_HOUSE,
  116. self::CAPPED_BONUS_QY,
  117. ];
  118. const FROM_MEANS_BD = 'BD';
  119. const FROM_MEANS_FX = 'FX';
  120. /**
  121. * 结算奖金时要清空的缓存
  122. * @param $periodNum
  123. */
  124. public static function clearCalcBonusCache($periodNum) {
  125. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
  126. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
  127. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
  128. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum);
  129. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum);
  130. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum);
  131. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum);
  132. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum);
  133. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum);
  134. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum);
  135. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
  136. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
  137. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
  138. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
  139. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
  140. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
  141. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum);
  142. }
  143. /**
  144. * 清网络缓存
  145. */
  146. public static function clearNetCache(){
  147. \Yii::$app->redis->del(Cache::USER_NETWORK_PARENTS);
  148. \Yii::$app->redis->del(Cache::USER_RELATION_PARENTS);
  149. }
  150. /**
  151. * 清空所有临时计算用到的缓存
  152. * @param $periodNum
  153. */
  154. public static function clearAll($periodNum) {
  155. //Yii::$app->redis->del(Yii::$app->redis->keys('calc*'));
  156. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER . $periodNum);
  157. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum);
  158. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO . $periodNum);
  159. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum);
  160. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum);
  161. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum);
  162. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_PERF . $periodNum);
  163. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum);
  164. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF . $periodNum);
  165. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum);
  166. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum);
  167. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_SCORE . $periodNum);
  168. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum);
  169. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum);
  170. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
  171. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum);
  172. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum);
  173. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
  174. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum);
  175. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum);
  176. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum);
  177. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum);
  178. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum);
  179. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum);
  180. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum);
  181. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum);
  182. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum);
  183. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum);
  184. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
  185. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
  186. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum);
  187. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum);
  188. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
  189. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
  190. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
  191. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
  192. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum);
  193. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum);
  194. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum);
  195. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum);
  196. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum);
  197. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum);
  198. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum);
  199. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum);
  200. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum);
  201. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum);
  202. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum);
  203. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum);
  204. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum);
  205. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
  206. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum);
  207. }
  208. /**
  209. * 会员信息加入缓存
  210. * @param $periodNum
  211. * @param int $offset
  212. * @param int $limit
  213. * @return bool
  214. */
  215. public static function addUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  216. // echo sprintf("时间:[%s]缓存用户,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  217. // 修复USER_INFO表里面已经没有PERIOD_NUM了
  218. $allData = UserInfo::findUseDbCalc()->from(UserInfo::tableName(). 'AS UI')->select('UI.USER_ID,U.STATUS')->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')->where('1=1')->orderBy('UI.RELATION_DEEP DESC,UI.ID DESC')->offset($offset)->limit($limit)->asArray()->all();
  219. //$allData = User::findUseDbShopCalc()->select('ID AS USER_ID')->where('PERIOD_AT<=:PERIOD_AT AND IS_UNION=0 AND DELETED=0', [':PERIOD_AT' => $periodNum])->offset($offset)->limit($limit)->asArray()->all();
  220. if ($allData) {
  221. foreach ($allData as $data) {
  222. // if(User::find()->where('ID=:ID',[':ID'=>$data['USER_ID']])->exists()) continue;
  223. // 会员ID加入缓存
  224. $cacheKey = self::REDIS_KEY_PREFIX_USER . $periodNum;
  225. $value = $data['USER_ID'];
  226. Yii::$app->redis->rpush($cacheKey, $value);
  227. if( $data['STATUS'] == 1 ) {
  228. $activeCacheKey = self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum;
  229. Yii::$app->redis->rpush($activeCacheKey, $value);
  230. unset($activeCacheKey);
  231. }
  232. unset($data, $cacheKey, $value);
  233. }
  234. unset($allData);
  235. return self::addUsers($periodNum, $offset + $limit, $limit);
  236. }
  237. unset($allData);
  238. return true;
  239. }
  240. /**
  241. * 获取会员从缓存中
  242. * @param $periodNum
  243. * @param int $offset
  244. * @param int $limit
  245. * @return mixed
  246. */
  247. public static function getUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  248. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER . $periodNum, $offset, ($offset + $limit - 1));
  249. }
  250. /**
  251. * 获取激活会员从缓存中
  252. * @param $periodNum
  253. * @param int $offset
  254. * @param int $limit
  255. * @return mixed
  256. */
  257. public static function getActiveUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  258. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum, $offset, ($offset + $limit - 1));
  259. }
  260. /**
  261. * 加入有业绩的会员
  262. * @param $userId
  263. * @param $periodNum
  264. */
  265. public static function addHasPerfUsers($userId, $periodNum) {
  266. // 先从已存在的会员池里面获取
  267. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId);
  268. if (!$isset) {
  269. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId, 1);
  270. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $userId);
  271. }
  272. unset($userId, $periodNum, $isset);
  273. }
  274. /**
  275. * 获取有业绩的会员
  276. * @param $periodNum
  277. * @param int $offset
  278. * @param int $limit
  279. * @return mixed
  280. */
  281. public static function getHasPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  282. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  283. }
  284. /**
  285. * 加入有月业绩的会员
  286. * @param $userId
  287. * @param $periodNum
  288. */
  289. public static function addHasMonthPerfUsers($userId, $periodNum) {
  290. // 先从已存在的会员池里面获取
  291. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId);
  292. if (!$isset) {
  293. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId, 1);
  294. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $userId);
  295. }
  296. unset($userId, $periodNum, $isset);
  297. }
  298. /**
  299. * 获取有业绩的会员
  300. * @param $periodNum
  301. * @param int $offset
  302. * @param int $limit
  303. * @return mixed
  304. */
  305. public static function getHasMonthPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  306. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  307. }
  308. /**
  309. * 加入有达标业绩的会员
  310. * @param $userId
  311. * @param $periodNum
  312. */
  313. public static function addHasStandardMonthPerfUsers($userId, $periodNum) {
  314. // 先从已存在的会员池里面获取
  315. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum, $userId);
  316. if (!$isset) {
  317. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum, $userId, 1);
  318. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum, $userId);
  319. }
  320. unset($userId, $periodNum, $isset);
  321. }
  322. /**
  323. * 获取有达标业绩的会员
  324. * @param $periodNum
  325. * @param int $offset
  326. * @param int $limit
  327. * @return mixed
  328. */
  329. public static function getHasStandardMonthPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  330. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  331. }
  332. /**
  333. * 加入被报单的会员
  334. * @param $userId
  335. * @param $periodNum
  336. * @param $saveData
  337. */
  338. public static function addHasBDUsers($userId, $periodNum, $saveData) {
  339. // 先从已存在的会员池里面获取
  340. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
  341. if (!$data) {
  342. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId, Json::encode($saveData));
  343. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $userId);
  344. }
  345. unset($userId, $periodNum, $saveData, $isset);
  346. }
  347. /**
  348. * 获取被报单的会员信息
  349. * @param $userId
  350. * @param $periodNum
  351. * @return array
  352. */
  353. public static function getBDUsersInfo($userId, $periodNum) {
  354. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
  355. if( !$data ) return [];
  356. return Json::decode($data, true);
  357. }
  358. /**
  359. * 获取被报单的会员
  360. * @param $periodNum
  361. * @param int $offset
  362. * @param int $limit
  363. * @return mixed
  364. */
  365. public static function getHasBDUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  366. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $offset, ($offset + $limit - 1));
  367. }
  368. /**
  369. * 加入有收入的会员
  370. * @param $userId
  371. * @param $periodNum
  372. */
  373. public static function addHasIncomeUsers($userId, $periodNum) {
  374. // 先从已存在的会员池里面获取
  375. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId);
  376. if (!$isset) {
  377. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId, 1);
  378. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $userId);
  379. }
  380. unset($userId, $periodNum, $isset);
  381. }
  382. /**
  383. * 获取有收入的会员
  384. * @param $periodNum
  385. * @param int $offset
  386. * @param int $limit
  387. * @return mixed
  388. */
  389. public static function getHasIncomeUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  390. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $offset, ($offset + $limit - 1));
  391. }
  392. /**
  393. * 加入有奖金的会员
  394. * @param $userId
  395. * @param $periodNum
  396. */
  397. public static function addHasBonusUsers($userId, $periodNum) {
  398. // 先从已存在的会员池里面获取
  399. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId);
  400. if (!$isset) {
  401. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId, 1);
  402. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $userId);
  403. }
  404. unset($userId, $periodNum, $isset);
  405. }
  406. /**
  407. * 获取有奖金的会员
  408. * @param $periodNum
  409. * @param int $offset
  410. * @param int $limit
  411. * @return mixed
  412. */
  413. public static function getHasBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  414. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  415. }
  416. /**
  417. * 加入荣衔奖的会员
  418. * @param $userId
  419. * @param $periodNum
  420. */
  421. public static function addHasYcBonusUsers($userId, $periodNum) {
  422. // 先从已存在的会员池里面获取
  423. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum, $userId);
  424. if (!$isset) {
  425. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum, $userId, 1);
  426. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum, $userId);
  427. }
  428. unset($userId, $periodNum, $isset);
  429. }
  430. /**
  431. * 保存荣衔奖奖金
  432. * @param $userId
  433. * @param $periodNum
  434. * @param $empBonus
  435. * @param array $fromData
  436. */
  437. public static function saveYCBonusList($userId, $periodNum, $empBonus, $fromData=[]) {
  438. $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId);
  439. if( $userYcBonusData ) {
  440. $ycBonusData = Json::decode($userYcBonusData, true);
  441. $ycBonusData['empBonus'] += $empBonus;
  442. // $ycBonusData['fromData'][] = $fromData;
  443. }else {
  444. $ycBonusData = [
  445. 'empBonus' => $empBonus,
  446. // 'fromData' => [
  447. // $fromData
  448. // ]
  449. ];
  450. }
  451. unset($userYcBonusData);
  452. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId, Json::encode($ycBonusData));
  453. unset($ycBonusData, $userId, $periodNum, $empBonus, $fromData);
  454. }
  455. /**
  456. * 返回荣衔奖信息
  457. * @param $userId
  458. * @param $periodNum
  459. * @return array
  460. */
  461. public static function getYCBonusList($userId, $periodNum) {
  462. $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId);
  463. return $userYcBonusData ? Json::decode($userYcBonusData, true) : [];
  464. }
  465. /**
  466. * 获取有荣衔奖的会员
  467. * @param $periodNum
  468. * @param int $offset
  469. * @param int $limit
  470. * @return mixed
  471. */
  472. public static function getHasYcBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  473. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  474. }
  475. /**
  476. * 获取有服务奖的会员
  477. * @param $periodNum
  478. * @param int $offset
  479. * @param int $limit
  480. * @return mixed
  481. */
  482. public static function getHasFwBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  483. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  484. }
  485. /**
  486. * 加入服务奖的会员
  487. * @param $userId
  488. * @param $periodNum
  489. */
  490. public static function addHasFwBonusUsers($userId, $periodNum) {
  491. // 先从已存在的会员池里面获取
  492. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId);
  493. if (!$isset) {
  494. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId, 1);
  495. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $userId);
  496. }
  497. unset($userId, $periodNum, $isset);
  498. }
  499. /**
  500. * 获取有月奖的会员
  501. * @param $periodNum
  502. * @param int $offset
  503. * @param int $limit
  504. * @return mixed
  505. */
  506. public static function getHasMonthBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  507. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  508. }
  509. /**
  510. * 加入月奖的会员
  511. * @param $userId
  512. * @param $periodNum
  513. */
  514. public static function addHasMonthBonusUsers($userId, $periodNum) {
  515. // 先从已存在的会员池里面获取
  516. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum, $userId);
  517. if (!$isset) {
  518. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum, $userId, 1);
  519. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum, $userId);
  520. }
  521. unset($userId, $periodNum, $isset);
  522. }
  523. /**
  524. * 保存荣衔奖奖金
  525. * @param $userId
  526. * @param $periodNum
  527. * @param $fwBonus
  528. * @param array $fromData
  529. */
  530. public static function saveFwBonusList($userId, $periodNum, $fwBonus, $fromData=[]) {
  531. $userFwBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);
  532. if( $userFwBonusData ) {
  533. $fwBonusData = Json::decode($userFwBonusData, true);
  534. $fwBonusData['fwBonus'] += $fwBonus;
  535. // $ycBonusData['fromData'][] = $fromData;
  536. }else {
  537. $fwBonusData = [
  538. 'fwBonus' => $fwBonus,
  539. // 'fromData' => [
  540. // $fromData
  541. // ]
  542. ];
  543. }
  544. unset($userFwBonusData);
  545. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId, Json::encode($fwBonusData));
  546. unset($fwBonusData, $userId, $periodNum, $empBonus, $fromData);
  547. }
  548. /**
  549. * 返回服务奖信息
  550. * @param $userId
  551. * @param $periodNum
  552. * @return array
  553. */
  554. public static function getFwBonusList($userId, $periodNum) {
  555. $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);
  556. return $userYcBonusData ? Json::decode($userYcBonusData, true) : [];
  557. }
  558. /**
  559. * 会员星级加入缓存
  560. * @param $userId
  561. * @param $periodNum
  562. * @param $crownCrown
  563. */
  564. public static function addUserStarCrown($userId, $periodNum, $starCrown) {
  565. // 先从已存在的会员池里面获取
  566. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum, $userId);
  567. if (!$data) {
  568. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum, $userId, $starCrown);
  569. }
  570. unset($userId, $periodNum, $saveData, $isset);
  571. }
  572. /**
  573. * 获取会员星级信息
  574. * @param $userId
  575. * @param $periodNum
  576. * @return array
  577. */
  578. public static function getUserStarCrown($userId, $periodNum) {
  579. return Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum, $userId);
  580. }
  581. /**
  582. * 加入有复销业绩的会员列表
  583. * @param $userId
  584. * @param $periodNum
  585. */
  586. public static function addHasScoreUsers($userId, $periodNum) {
  587. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum, $userId);
  588. if (!$isset) {
  589. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum, $userId, 1);
  590. $key = self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum;
  591. Yii::$app->redis->rpush($key, $userId);
  592. }
  593. }
  594. /**
  595. * 获取有复销业绩的会员列表
  596. * @param $periodNum
  597. * @param int $offset
  598. * @param int $limit
  599. * @return mixed
  600. */
  601. public static function getHasScoreUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  602. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum, $offset, ($offset + $limit - 1));
  603. }
  604. /**
  605. * 加入有零售个人消费的会员列表
  606. * @param $userId
  607. * @param $periodNum
  608. */
  609. public static function addHasLSPCSUsers($userId, $periodNum) {
  610. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum, $userId);
  611. if (!$isset) {
  612. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum, $userId, 1);
  613. $key = self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum;
  614. $value = $userId;
  615. Yii::$app->redis->rpush($key, $value);
  616. }
  617. }
  618. /**
  619. * 加入有零售消费的会员列表
  620. * @param $periodNum
  621. * @param int $offset
  622. * @param int $limit
  623. * @return mixed
  624. */
  625. public static function getHasLSPCSUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  626. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum, $offset, ($offset + $limit - 1));
  627. }
  628. /**
  629. * 有车房补贴比例的人
  630. * @param $userId
  631. * @param $periodNum
  632. */
  633. public static function addHasCFPercentUsers($userId, $periodNum) {
  634. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId);
  635. if (!$isset) {
  636. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId, 1);
  637. $key = self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum;
  638. $value = $userId;
  639. Yii::$app->redis->rpush($key, $value);
  640. }
  641. }
  642. /**
  643. * 获取有车房补贴的人
  644. * @param $periodNum
  645. * @param int $offset
  646. * @param int $limit
  647. * @return mixed
  648. */
  649. public static function getHasCFPercentUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  650. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum, $offset, ($offset + $limit - 1));
  651. }
  652. /**
  653. * 有领袖分红的人
  654. * @param $userId
  655. * @param $periodNum
  656. */
  657. public static function addHasLXPercentUsers($userId, $periodNum) {
  658. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum, $userId);
  659. if (!$isset) {
  660. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum, $userId, 1);
  661. $key = self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum;
  662. $value = $userId;
  663. Yii::$app->redis->rpush($key, $value);
  664. }
  665. }
  666. /**
  667. * 获取有领袖分红的人
  668. * @param $periodNum
  669. * @param int $offset
  670. * @param int $limit
  671. * @return mixed
  672. */
  673. public static function getHasLXPercentUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  674. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum, $offset, ($offset + $limit - 1));
  675. }
  676. /**
  677. * 通过创建时间获取指定长度的用户列表
  678. * @param $userId
  679. * @param $limit
  680. * @return array|\yii\db\ActiveRecord[]
  681. */
  682. public static function getUserListByCreatedAtFromDb($userId, $limit)
  683. {
  684. //查找这个人的CREATED_AT
  685. $user = User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID=:ID', ['ID'=>$userId])->asArray()->one();
  686. if( !$user ) return [];
  687. return User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID<:ID AND CREATED_AT<=:CREATED_AT', [
  688. 'ID'=>$userId,
  689. 'CREATED_AT' => $user['CREATED_AT']
  690. ])->orderBy('CREATED_AT DESC,ID DESC')->limit($limit)->asArray()->all();
  691. }
  692. /**
  693. * 通过创建时间获取指定长度的用户列表
  694. * @param $userId
  695. * @param $limit
  696. * @return array|\yii\db\ActiveRecord[]
  697. */
  698. public static function getAfterUserListByCreatedAtFromDb($userId, $limit)
  699. {
  700. $cacheKey = Cache::USER_CREATED_AT_LIST . $limit;
  701. $value = Yii::$app->redis->hGet($cacheKey, $userId);
  702. if( $value ) {
  703. unset($cacheKey);
  704. return json_decode($value, true);
  705. }
  706. unset($value);
  707. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('USER_ID>:USER_ID', [
  708. 'USER_ID'=>$userId,
  709. ])->orderBy('USER_ID ASC')->limit($limit)->asArray()->all();
  710. if ( $limit == count($list) ) {
  711. Yii::$app->redis->hSet($cacheKey, $userId, json_encode($list));
  712. }
  713. unset($cacheKey);
  714. return $list;
  715. }
  716. /**
  717. * 获取会员信息从缓存
  718. * @param $userId
  719. * @param $periodNum
  720. * @return array
  721. * @throws \Exception
  722. */
  723. public static function getUserInfo($userId, $periodNum) {
  724. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  725. $data = Yii::$app->redis->hget($key, $userId);
  726. if (!$data) {
  727. $userInfo = UserInfo::findUseDbCalc()->select('USER_ID,USER_NAME,ZC_AMOUNT,ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,STORE_TYPE,UPDATED_AT,SYSTEM_ID')->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  728. if( !$userInfo ) {
  729. throw new \Exception('用户信息表数据不存在,userId:'.$userId);
  730. }
  731. // 在获取商城库的会员的级别信息和是否报单中心的会员
  732. $userShopInfo = User::find()->select('REAL_NAME,LAST_DEC_LV,DEC_LV,EMP_LV,CROWN_LV,IS_DEC,DEC_ID,DEC_ROLE_ID,STATUS,PERIOD_AT,MOBILE,CREATED_AT,SUB_COM_ID,PROVINCE,CITY,COUNTY,DEC_PROVINCE,DEC_CITY,DEC_COUNTY,IS_DIRECT_SELLER,SUB_COM_LEADER')->where('ID=:ID', [':ID' => $userId])->asArray()->one();
  733. if ( !$userShopInfo ) {
  734. throw new \Exception('用户表数据不存在,userId:'.$userId);
  735. }
  736. $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
  737. // $userInfo['DEC_LV'] = $userShopInfo['DEC_LV'];
  738. //先用last_dec_lv
  739. $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
  740. $userInfo['EMP_LV'] = $userShopInfo['EMP_LV'];
  741. $userInfo['CROWN_LV'] = $userShopInfo['CROWN_LV'];
  742. $userInfo['IS_DEC'] = $userShopInfo['IS_DEC'];
  743. $userInfo['DEC_ID'] = $userShopInfo['DEC_ID'];
  744. $userInfo['DEC_ROLE_ID'] = $userShopInfo['DEC_ROLE_ID'];
  745. $userInfo['DEC_PROVINCE'] = $userShopInfo['DEC_PROVINCE'];
  746. $userInfo['DEC_CITY'] = $userShopInfo['DEC_CITY'];
  747. $userInfo['DEC_COUNTY'] = $userShopInfo['DEC_COUNTY'];
  748. $userInfo['STATUS'] = $userShopInfo['STATUS'];
  749. $userInfo['PERIOD_NUM'] = $userShopInfo['PERIOD_AT'];
  750. $userInfo['MOBILE'] = $userShopInfo['MOBILE'];
  751. $userInfo['CREATED_AT'] = $userShopInfo['CREATED_AT'];
  752. $userInfo['SUB_COM_ID'] = $userShopInfo['SUB_COM_ID'];
  753. $userInfo['PROVINCE'] = $userShopInfo['PROVINCE'];
  754. $userInfo['CITY'] = $userShopInfo['CITY'];
  755. $userInfo['COUNTY'] = $userShopInfo['COUNTY'];
  756. $userInfo['IS_DIRECT_SELLER'] = $userShopInfo['IS_DIRECT_SELLER'];
  757. $userInfo['SUB_COM_LEADER'] = $userShopInfo['SUB_COM_LEADER'];
  758. $userInfo['REC_USER_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserNameByUserId($userInfo['REC_UID']) : '';
  759. $userInfo['REC_REAL_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserRealNameByUserId($userInfo['REC_UID']) : '';
  760. $userInfo['CON_USER_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserNameByUserId($userInfo['CON_UID']) : '';
  761. $userInfo['CON_REAL_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserRealNameByUserId($userInfo['CON_UID']) : '';
  762. // $userInfo['REC_USER_NAME'] = '';
  763. // $userInfo['REC_REAL_NAME'] = '';
  764. // $userInfo['CON_USER_NAME'] = '';
  765. // $userInfo['CON_REAL_NAME'] = '';
  766. // $userInfo['LOCATION'] = 1;
  767. unset($userShopInfo);
  768. // if( isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ) {
  769. // if ($networkParent = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId])) {
  770. // $userInfo['LOCATION'] = $networkParent['RELATIVE_LOCATION'];
  771. // }
  772. // }
  773. if (!$userInfo['DEC_LV']) {
  774. $userInfo['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  775. }
  776. if (!$userInfo['EMP_LV']) {
  777. $userInfo['EMP_LV'] = EmployLevel::getDefaultLevelId();
  778. }
  779. if (!$userInfo['CROWN_LV']) {
  780. $userInfo['CROWN_LV'] = StarCrownLevel::getDefaultLevelId();
  781. }
  782. $data = Json::encode($userInfo);
  783. Yii::$app->redis->hset($key, $userId, $data);
  784. unset($userInfo, $key, $userId, $periodNum);
  785. }
  786. return $data ? Json::decode($data, true) : [];
  787. }
  788. public static function setUserInfo($userId, $periodNum, $userInfo) {
  789. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  790. $data = Json::encode($userInfo);
  791. Yii::$app->redis->hset($key, $userId, $data);
  792. unset($userId, $key, $data, $userInfo, $periodNum);
  793. return true;
  794. }
  795. /**
  796. * 用户奖金信息
  797. * @param $userId
  798. * @param $periodNum
  799. * @return array|mixed|null|\yii\db\ActiveRecord
  800. */
  801. public static function LastUserBonus($userId, $periodNum) {
  802. $cacheKey = self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum;
  803. $field = $userId;
  804. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  805. if ($cacheValue) {
  806. $value = Json::decode($cacheValue, true);
  807. } else {
  808. $value = UserBonus::findUseDbCalc()->select('USER_ID,BONUS_TOTAL')->where('USER_ID=:USER_ID', [
  809. 'USER_ID'=>$userId
  810. ])->asArray()->one();
  811. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  812. }
  813. return $value;
  814. }
  815. /**
  816. * 用户奖金信息
  817. * @param $userId
  818. * @param $periodNum
  819. * @param $yearMonth
  820. * @return array|mixed|null|\yii\db\ActiveRecord
  821. */
  822. public static function lastPeriodMonthCalcBonus($userId, $periodNum, $yearMonth) {
  823. $cacheKey = self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum;
  824. $field = $userId;
  825. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  826. if ($cacheValue) {
  827. $value = Json::decode($cacheValue, true);
  828. } else {
  829. $value = CalcBonus::findUseDbCalc()->select('SUM(ORI_BONUS_QY) AS ORI_BONUS_QY_SUM,SUM(ORI_BONUS_VIP) AS ORI_BONUS_VIP_SUM')->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  830. 'USER_ID'=>$userId,
  831. 'CALC_MONTH'=>$yearMonth
  832. ])->asArray()->one();
  833. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  834. }
  835. return $value;
  836. }
  837. /**
  838. * 本月往期的复消积分的数据
  839. * @param $userId
  840. * @param $periodNum
  841. * @param $calcYearMonth
  842. * @return array|mixed|null|\yii\db\ActiveRecord
  843. * @throws \yii\db\Exception
  844. */
  845. public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
  846. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
  847. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  848. if ($cacheValue) {
  849. $value = Json::decode($cacheValue, true);
  850. } else {
  851. $value = CalcBonus::findUseDbCalc()->select(["SUM(RECONSUME_POINTS) as RECONSUME_POINTS_SUM"])->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $userId, ':CALC_MONTH'=>$calcYearMonth])->asArray()->one();
  852. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  853. }
  854. return $value;
  855. }
  856. /**
  857. * 获取结算时这一期的报单中心级别的配置
  858. * @param $periodNum
  859. * @return array|\yii\db\ActiveRecord[]
  860. */
  861. public static function getDecRoleConfig($periodNum) {
  862. $key = self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum;
  863. Yii::$app->redis->del($key);
  864. $data = Yii::$app->redis->get($key);
  865. if (!$data) {
  866. $data = DecRole::find()->where('1=1')->indexBy('ID')->asArray()->all();
  867. $data = Json::encode($data);
  868. Yii::$app->redis->set($key, $data);
  869. }
  870. return $data ? Json::decode($data) : [];
  871. }
  872. /**
  873. * 上一期结余业绩
  874. * @param $userId
  875. * @param $periodNum
  876. * @return array|mixed|\yii\db\ActiveRecord|null
  877. * @throws \yii\db\Exception
  878. */
  879. public static function surplusPerf($userId, $periodNum) {
  880. // $cacheKey = self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum;
  881. // $field = $userId;
  882. // $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  883. // if ($cacheValue) {
  884. // $value = Json::decode($cacheValue);
  885. // } else {
  886. // $value = UserPerf::getPeriodSurplusPerf($userId);
  887. // \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  888. // }
  889. // return $value;
  890. $userPerf = self::userPerf($userId, $periodNum);
  891. return [
  892. 'SURPLUS_1L' => $userPerf['SURPLUS_1L'],
  893. 'SURPLUS_2L' => $userPerf['SURPLUS_2L'],
  894. 'SURPLUS_3L' => $userPerf['SURPLUS_3L'],
  895. 'SURPLUS_4L' => $userPerf['SURPLUS_4L'],
  896. 'SURPLUS_5L' => $userPerf['SURPLUS_5L'],
  897. 'SURPLUS_1L_ZC' => $userPerf['SURPLUS_1L_ZC'],
  898. 'SURPLUS_2L_ZC' => $userPerf['SURPLUS_2L_ZC'],
  899. 'SURPLUS_3L_ZC' => $userPerf['SURPLUS_3L_ZC'],
  900. 'SURPLUS_4L_ZC' => $userPerf['SURPLUS_4L_ZC'],
  901. 'SURPLUS_5L_ZC' => $userPerf['SURPLUS_5L_ZC'],
  902. 'SURPLUS_1L_FX' => $userPerf['SURPLUS_1L_FX'],
  903. 'SURPLUS_2L_FX' => $userPerf['SURPLUS_2L_FX'],
  904. 'SURPLUS_3L_FX' => $userPerf['SURPLUS_3L_FX'],
  905. 'SURPLUS_4L_FX' => $userPerf['SURPLUS_4L_FX'],
  906. 'SURPLUS_5L_FX' => $userPerf['SURPLUS_5L_FX'],
  907. 'VIP_SURPLUS_1L_ZC' => $userPerf['VIP_SURPLUS_1L_ZC'],
  908. 'VIP_SURPLUS_2L_ZC' => $userPerf['VIP_SURPLUS_2L_ZC'],
  909. 'VIP_SURPLUS_3L_ZC' => $userPerf['VIP_SURPLUS_3L_ZC'],
  910. 'VIP_SURPLUS_4L_ZC' => $userPerf['VIP_SURPLUS_4L_ZC'],
  911. 'VIP_SURPLUS_5L_ZC' => $userPerf['VIP_SURPLUS_5L_ZC'],
  912. 'SURPLUS_LS' => 0,
  913. ];
  914. }
  915. /**
  916. * 获取repairSurplusPerf
  917. * @param $userId
  918. * @return int[]|mixed|null
  919. */
  920. public static function getRepairSurplusPerf($userId) {
  921. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  922. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  923. if ($cacheValue) {
  924. $value = Json::decode($cacheValue);
  925. }else {
  926. $value = [
  927. 'SURPLUS_1L' => 0,
  928. 'SURPLUS_2L' => 0,
  929. 'SURPLUS_3L' => 0,
  930. 'SURPLUS_4L' => 0,
  931. 'SURPLUS_5L' => 0,
  932. ];
  933. }
  934. return $value;
  935. }
  936. /**
  937. * 累加repairSurplusPerf
  938. * @param $userId
  939. * @param $surplusPerf
  940. * @return int[]|mixed|null
  941. */
  942. public static function setRepairSurplusPerf($userId, $surplusPerf) {
  943. $surplusPerfList = self::getRepairSurplusPerf($userId);
  944. foreach ($surplusPerfList as $key => $perf) {
  945. $thisPerf = $surplusPerf[$key] ?? 0;
  946. $surplusPerfList[$key] = $perf + $thisPerf;
  947. unset($key, $perf, $thisPerf);
  948. }
  949. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  950. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($surplusPerfList));
  951. self::addHasRepairPerfUsers($userId);
  952. unset($userId, $cacheKey, $surplusPerf);
  953. return $surplusPerfList;
  954. }
  955. /**
  956. * 加入有往期结余业绩的会员
  957. * @param $userId
  958. */
  959. public static function addHasRepairPerfUsers($userId) {
  960. // 先从已存在的会员池里面获取
  961. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId);
  962. if (!$isset) {
  963. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId, 1);
  964. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $userId);
  965. }
  966. unset($userId, $isset);
  967. }
  968. /**
  969. * 获取有往期结余业绩的会员
  970. * @param int $offset
  971. * @param int $limit
  972. * @return mixed
  973. */
  974. public static function getHasRepairPerfUsers($offset = 0, $limit = self::LIMIT) {
  975. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $offset, ($offset + $limit - 1));
  976. }
  977. /**
  978. * 修正业绩缓存清空
  979. */
  980. public static function clearRepairAllCache() {
  981. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF);
  982. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER);
  983. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL);
  984. }
  985. /**
  986. * 上一期结余业绩
  987. * @param $userId
  988. * @param $periodNum
  989. * @return array|mixed|\yii\db\ActiveRecord|null
  990. * @throws \yii\db\Exception
  991. */
  992. public static function userPerf($userId, $periodNum) {
  993. $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
  994. $field = $userId;
  995. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  996. if ($cacheValue) {
  997. $value = Json::decode($cacheValue);
  998. } else {
  999. $value = UserPerf::getOneUserPerf($userId);
  1000. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1001. }
  1002. return $value;
  1003. }
  1004. /**
  1005. * 当前期数的的业绩
  1006. * @param $userId
  1007. * @param $periodNum
  1008. * @param null $perf
  1009. * @return array|mixed
  1010. */
  1011. public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
  1012. $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
  1013. $field = $userId;
  1014. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1015. if ($cacheValue) {
  1016. $value = Json::decode($cacheValue);
  1017. } else {
  1018. $value = [
  1019. 'FX_AMOUNT_CASH' => 0,
  1020. 'PV_PCS' => 0,
  1021. 'PV_PSS' => 0,//本期的团队业绩
  1022. 'PV_PCS_ZC' => 0,//注册
  1023. 'PV_PCS_YH' => 0,
  1024. 'PV_PCS_ZG' => 0,//增购
  1025. 'PV_PCS_LS' => 0,
  1026. 'PV_PCS_FX' => 0,
  1027. 'PV_PCS_FX_CASH' => 0,//现金复消
  1028. 'PV_PCS_FX_POINT' => 0,//积分复消
  1029. 'PV_1L' => 0,
  1030. 'PV_1L_TOUCH' => 0,
  1031. 'PV_1L_ZC' => 0,
  1032. 'PV_1L_YH' => 0,
  1033. 'PV_1L_ZG' => 0,
  1034. 'PV_1L_LS' => 0,
  1035. 'PV_1L_FX' => 0,
  1036. 'PV_2L' => 0,
  1037. 'PV_2L_TOUCH' => 0,
  1038. 'PV_2L_ZC' => 0,
  1039. 'PV_2L_YH' => 0,
  1040. 'PV_2L_ZG' => 0,
  1041. 'PV_2L_LS' => 0,
  1042. 'PV_2L_FX' => 0,
  1043. 'PV_3L' => 0,
  1044. 'PV_3L_TOUCH' => 0,
  1045. 'PV_3L_ZC' => 0,
  1046. 'PV_3L_YH' => 0,
  1047. 'PV_3L_ZG' => 0,
  1048. 'PV_3L_LS' => 0,
  1049. 'PV_3L_FX' => 0,
  1050. 'PV_4L' => 0,
  1051. 'PV_4L_TOUCH' => 0,
  1052. 'PV_4L_ZC' => 0,
  1053. 'PV_4L_YH' => 0,
  1054. 'PV_4L_ZG' => 0,
  1055. 'PV_4L_LS' => 0,
  1056. 'PV_4L_FX' => 0,
  1057. 'PV_5L' => 0,
  1058. 'PV_5L_TOUCH' => 0,
  1059. 'PV_5L_ZC' => 0,
  1060. 'PV_5L_YH' => 0,
  1061. 'PV_5L_ZG' => 0,
  1062. 'PV_5L_LS' => 0,
  1063. 'PV_5L_FX' => 0,
  1064. 'PV_LS_TOUCH' => 0,
  1065. 'SURPLUS_1L' => 0,
  1066. 'SURPLUS_2L' => 0,
  1067. 'SURPLUS_3L' => 0,
  1068. 'SURPLUS_4L' => 0,
  1069. 'SURPLUS_5L' => 0,
  1070. 'SURPLUS_1L_ZC' => 0,
  1071. 'SURPLUS_2L_ZC' => 0,
  1072. 'SURPLUS_3L_ZC' => 0,
  1073. 'SURPLUS_4L_ZC' => 0,
  1074. 'SURPLUS_5L_ZC' => 0,
  1075. 'SURPLUS_1L_FX' => 0,
  1076. 'SURPLUS_2L_FX' => 0,
  1077. 'SURPLUS_3L_FX' => 0,
  1078. 'SURPLUS_4L_FX' => 0,
  1079. 'SURPLUS_5L_FX' => 0,
  1080. 'SURPLUS_LS' => 0,
  1081. ];
  1082. }
  1083. if ($perf !== null) {
  1084. foreach ($perf as $key => $pv) {
  1085. if (strpos($key, 'SURPLUS') !== false) {
  1086. $value[$key] = $pv;
  1087. } else {
  1088. $value[$key] = $pv + $value[$key];
  1089. }
  1090. }
  1091. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1092. }
  1093. return $value;
  1094. }
  1095. /**
  1096. * 上个月的业绩
  1097. * @param $userId
  1098. * @param $periodNum
  1099. * @return array|mixed|null|\yii\db\ActiveRecord
  1100. */
  1101. public static function lastMonthPerf($userId, $periodNum) {
  1102. $period = Period::instance();
  1103. $last = $period->getLastMonth($periodNum);
  1104. $lastYearMonth = $last['yearMonth'];
  1105. $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
  1106. $field = $userId;
  1107. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1108. if ($cacheValue) {
  1109. $value = Json::decode($cacheValue);
  1110. } else {
  1111. $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
  1112. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1113. }
  1114. return $value;
  1115. }
  1116. /**
  1117. * 当前月的业绩
  1118. * @param $userId
  1119. * @param $periodNum
  1120. * @param $perf
  1121. * @return array|mixed
  1122. */
  1123. public static function nowMonthPerf($userId, $periodNum, $perf = null) {
  1124. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
  1125. $field = $userId;
  1126. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1127. if ($cacheValue) {
  1128. $value = Json::decode($cacheValue);
  1129. } else {
  1130. $baseInfo = self::getUserInfo($userId, $periodNum);
  1131. $value = [
  1132. 'USER_ID' => $userId,
  1133. 'FX_AMOUNT_CASH' => 0,
  1134. 'PV_PCS' => 0,
  1135. 'PV_PCS_FX' => 0,
  1136. 'PV_PSS' => 0,
  1137. 'PV_1L' => 0,
  1138. 'PV_2L' => 0,
  1139. 'PV_3L' => 0,
  1140. 'PV_4L' => 0,
  1141. 'PV_5L' => 0,
  1142. 'VIP_PV_1L_ZC' => 0,
  1143. 'VIP_PV_2L_ZC' => 0,
  1144. 'VIP_PV_3L_ZC' => 0,
  1145. 'VIP_PV_4L_ZC' => 0,
  1146. 'VIP_PV_5L_ZC' => 0,
  1147. 'PV_1L_TOTAL' => 0,
  1148. 'PV_2L_TOTAL' => 0,
  1149. 'PV_3L_TOTAL' => 0,
  1150. 'PV_4L_TOTAL' => 0,
  1151. 'PV_5L_TOTAL' => 0,
  1152. 'PV_PSS_TOTAL' => 0,
  1153. 'DEC_LEVEL' => $baseInfo['DEC_LV'],
  1154. 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
  1155. 'CF_PERCENT' => 0,
  1156. 'LX_PERCENT' => 0,
  1157. 'FX_STATUS' => PerfMonth::NEXT_MONTH_FX_FALSE,
  1158. ];
  1159. }
  1160. if ($perf !== null) {
  1161. foreach ($perf as $key => $item) {
  1162. $value[$key] = $item;
  1163. }
  1164. }
  1165. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1166. return $value;
  1167. }
  1168. /**
  1169. * 当前期数的的业绩
  1170. * @param $userId
  1171. * @param $periodNum
  1172. * @param null $perf
  1173. * @return array|mixed
  1174. */
  1175. public static function nowStandardMonthPerf($userId, $periodNum, $perf = null) {
  1176. $cacheKey = self::REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF . $periodNum;
  1177. $field = $userId;
  1178. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1179. if ($cacheValue) {
  1180. $value = Json::decode($cacheValue);
  1181. } else {
  1182. $value = [
  1183. 'AMOUNT_PCS' => 0,
  1184. 'AMOUNT_PSS' => 0
  1185. ];
  1186. }
  1187. if ($perf !== null) {
  1188. foreach ($perf as $key => $pv) {
  1189. $value[$key] = $pv + $value[$key];
  1190. }
  1191. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1192. }
  1193. return $value;
  1194. }
  1195. /**
  1196. * 当前月的积分
  1197. * @param $userId
  1198. * @param $periodNum
  1199. * @param $perf
  1200. * @return array|mixed
  1201. */
  1202. public static function nowMonthScore($userId, $periodNum, $perf = null) {
  1203. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_SCORE . $periodNum;
  1204. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1205. if ($cacheValue) {
  1206. $value = Json::decode($cacheValue);
  1207. } else {
  1208. $value = [
  1209. 'USER_ID' => $userId,
  1210. 'BASE_SCORE' => 0,
  1211. 'LEVEL_SCORE' => 0,
  1212. 'UPGRADE_SCORE' => 0,
  1213. // 'TOTAL_SCORE' => 0,
  1214. ];
  1215. }
  1216. unset($cacheValue);
  1217. if ($perf !== null) {
  1218. foreach ($perf as $key => $item) {
  1219. $value[$key] = $item;
  1220. // $value['TOTAL_SCORE'] += $item;
  1221. unset($key, $item);
  1222. }
  1223. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  1224. }
  1225. unset($userId, $periodNum, $perf, $cacheKey);
  1226. return $value;
  1227. }
  1228. /**
  1229. * 获取直推的所有子会员
  1230. * @param $userId
  1231. * @param $periodNum
  1232. * @return array
  1233. */
  1234. public static function getChildrenOneDeepFromRedis($userId, $periodNum){
  1235. $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
  1236. $data = Yii::$app->redis->hget($key, $userId);
  1237. if(!$data){
  1238. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
  1239. 'REC_UID' => $userId
  1240. ])->asArray()->all();
  1241. $data = Json::encode($list);
  1242. unset($list);
  1243. Yii::$app->redis->hset($key, $userId, $data);
  1244. }
  1245. unset($key, $userId, $periodNum);
  1246. return $data ? Json::decode($data, true) : [];
  1247. }
  1248. /**
  1249. * 每个区有多少个什么级别的数量
  1250. * @param $userId
  1251. * @param $periodNum
  1252. * @param null $levelNumArr
  1253. * @return array|mixed
  1254. */
  1255. public static function hasEmpLevelNum($userId, $periodNum, $levelNumArr = null) {
  1256. $key = self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum;
  1257. $field = $userId;
  1258. $cacheValue = Yii::$app->redis->hget($key, $field);
  1259. if ($cacheValue) {
  1260. $value = Json::decode($cacheValue);
  1261. } else {
  1262. $allEmpLevel = EmployLevel::getFromCache();
  1263. $value = [];
  1264. $relationList = self::getChildrenOneDeepFromRedis($userId, $periodNum);
  1265. foreach ($relationList as $relationData) {
  1266. foreach ($allEmpLevel as $level) {
  1267. // 从月度业绩表中找到所有下级的各个部门的数量
  1268. $value[$relationData['USER_ID']][$level['ID']] = 0;
  1269. }
  1270. }
  1271. }
  1272. if ($levelNumArr != null) {
  1273. foreach ($levelNumArr as $departUserId => $item) {
  1274. foreach ($item as $levelID => $num) {
  1275. if( isset($value[$departUserId][$levelID]) ) {
  1276. $value[$departUserId][$levelID] += $num;
  1277. }else {
  1278. $value[$departUserId][$levelID] = $num;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. Yii::$app->redis->hset($key, $field, Json::encode($value));
  1284. if ($levelNumArr == null) return $value;
  1285. }
  1286. /**
  1287. * 会员是否本月注册
  1288. * @param $userId
  1289. * @param $periodNum
  1290. * @return bool
  1291. * @throws \yii\db\Exception
  1292. */
  1293. public static function isMonthJoinFromRedis($userId, $periodNum) {
  1294. $period = Period::instance();
  1295. $calcYearMonth = $period->getYearMonth($periodNum);
  1296. $userInfo = self::getUserInfo($userId, $periodNum);
  1297. $addYearMonth = $period->getYearMonth($userInfo['PERIOD_NUM']);
  1298. //当月注册
  1299. if ($addYearMonth == $calcYearMonth) return true;
  1300. //上月注册
  1301. $calcLastYearMonth = $period->getLastMonth($periodNum);
  1302. if ($addYearMonth == $calcLastYearMonth['yearMonth']) return true;
  1303. // 未来注册
  1304. if ($addYearMonth > $calcYearMonth) return true;
  1305. return false;
  1306. }
  1307. /**
  1308. * 荣衔奖最大比例
  1309. * @param $userId
  1310. * @param $periodNum
  1311. * @param int $percent
  1312. * @return float|int
  1313. */
  1314. public static function ycMaxBonusPercent($userId, $periodNum, $percent=0) {
  1315. $cacheKey = self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum;
  1316. $value = Yii::$app->redis->hget($cacheKey, $userId);
  1317. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  1318. if ( $percent > 0 && $percent > $maxPercent ) {
  1319. $maxPercent = $percent;
  1320. unset($periodNum, $percent);
  1321. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  1322. return $maxPercent;
  1323. }
  1324. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  1325. return $maxPercent;
  1326. }
  1327. /**
  1328. * 服务奖最大比例
  1329. * @param $userId
  1330. * @param $periodNum
  1331. * @param int $percent
  1332. * @return float|int
  1333. */
  1334. public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
  1335. $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
  1336. $value = Yii::$app->redis->hget($cacheKey, $userId);
  1337. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  1338. if ( $percent > 0 && $percent > $maxPercent ) {
  1339. $maxPercent = $percent;
  1340. unset($periodNum, $percent);
  1341. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  1342. return $maxPercent;
  1343. }
  1344. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  1345. return $maxPercent;
  1346. }
  1347. /**
  1348. * 奖金缓存
  1349. * @param $userId
  1350. * @param $periodNum
  1351. * @param string $bonusType
  1352. * @param float $oriBonus
  1353. * @param array $deductData
  1354. * @return array|mixed
  1355. */
  1356. public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='') {
  1357. $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
  1358. $value = [
  1359. 'BONUS_BD' => 0,
  1360. 'BONUS_TG' => 0,
  1361. 'BONUS_XF' => 0,
  1362. 'BONUS_YJ' => 0,
  1363. 'BONUS_GX' => 0,
  1364. 'BONUS_GL' => 0,
  1365. 'BONUS_QY' => 0,
  1366. 'BONUS_YC' => 0,
  1367. 'BONUS_VIP' => 0,
  1368. 'BONUS_BS' => 0,
  1369. 'BONUS_QUARTER' => 0,
  1370. 'BONUS_YC_EXTRA' => 0,
  1371. 'ORI_BONUS_BD' => 0,
  1372. 'ORI_BONUS_BS' => 0,
  1373. 'BONUS_BS_MNT' => 0,
  1374. 'BONUS_BS_ABBR' => 0,
  1375. 'ORI_BONUS_BS_MNT' => 0,
  1376. 'ORI_BONUS_BS_ABBR' => 0,
  1377. 'ORI_BONUS_QUARTER' => 0,
  1378. 'ORI_BONUS_TG' => 0,
  1379. 'ORI_BONUS_XF' => 0,
  1380. 'ORI_BONUS_YJ' => 0,
  1381. 'ORI_BONUS_YJ_BD' => 0,
  1382. 'ORI_BONUS_YJ_FX' => 0,
  1383. 'ORI_BONUS_GX' => 0,
  1384. 'ORI_BONUS_GL' => 0,
  1385. 'ORI_BONUS_GL_BD' => 0,
  1386. 'ORI_BONUS_GL_FX' => 0,
  1387. 'ORI_BONUS_QY' => 0,
  1388. 'ORI_BONUS_QY_BD' => 0,
  1389. 'ORI_BONUS_QY_FX' => 0,
  1390. 'ORI_BONUS_YC' => 0,
  1391. 'ORI_BONUS_VIP' => 0,
  1392. 'ORI_BONUS_YC_EXTRA' => 0,
  1393. // 'INCOME_TG_TOTAL' => 0,
  1394. // 'INCOME_YJ_TOTAL' => 0,
  1395. // 'INCOME_QY_TOTAL' => 0,
  1396. 'INCOME_TOTAL' => 0,
  1397. 'BONUS_TOTAL' => 0,
  1398. 'RECONSUME_POINTS' => 0,
  1399. 'MANAGE_TAX' => 0,
  1400. 'ORI_CAPPED_BONUS_QY' => 0,// 团队奖,封顶前金额
  1401. //没有用到的
  1402. 'BONUS_FX' => 0,
  1403. 'BONUS_LS' => 0,
  1404. 'BONUS_CF' => 0,
  1405. 'BONUS_LX' => 0,
  1406. 'BONUS_HB' => 0,
  1407. 'BONUS_BT' => 0,
  1408. 'BONUS_BT_PROD' => 0,
  1409. 'BONUS_BT_TOOL' => 0,
  1410. 'DEDUCT_ZR' => 0,
  1411. 'BONUS_FL' => 0,
  1412. ];
  1413. // 从 redis 中获取当前的结果
  1414. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1415. if ($cacheValue) {
  1416. $cacheValue = Json::decode($cacheValue);
  1417. $value = $cacheValue;
  1418. }
  1419. unset($cacheValue);
  1420. if ($oriBonus > 0) {
  1421. $oriBonusType = sprintf('ORI_%s', $bonusType);
  1422. $value[$oriBonusType] += $oriBonus;
  1423. if( $fromMeans !== '' ) {
  1424. $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
  1425. $value[$oriBonusMeansType] += $oriBonus;
  1426. }
  1427. if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  1428. if( $deductData ) {
  1429. $value[$bonusType] += $deductData['surplus'];
  1430. $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  1431. $value['MANAGE_TAX'] += $deductData['manageTax'];
  1432. }else {
  1433. $value[$bonusType] += $oriBonus;
  1434. }
  1435. $value['BONUS_TOTAL'] += $oriBonus;
  1436. }
  1437. if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
  1438. self::addHasIncomeUsers($userId, $periodNum);
  1439. // if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  1440. // $value['INCOME_TOTAL'] += $oriBonus;
  1441. // }
  1442. }
  1443. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  1444. unset($oriBonusType);
  1445. self::addHasBonusUsers($userId, $periodNum);
  1446. }
  1447. unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
  1448. return $value;
  1449. }
  1450. public static function standardBonus($userId, $periodNum, $bonus=0.00) {
  1451. $cacheKey = self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum;
  1452. $value = 0.00;
  1453. if( $bonus > 0 ) {
  1454. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1455. $value = $bonus;
  1456. //加入有奖金的会员中
  1457. self::addHasBonusUsers($userId, $periodNum);
  1458. }else {
  1459. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1460. if ($cacheValue) {
  1461. $value = $cacheValue;
  1462. }
  1463. unset($cacheValue);
  1464. }
  1465. unset($cacheKey, $userId, $periodNum, $bonus);
  1466. return $value;
  1467. }
  1468. public static function tourismBonus($userId, $periodNum, $bonus = 0.00) {
  1469. $cacheKey = self::REDIS_KEY_PREFIX_TOURISM_BONUS . $periodNum;
  1470. $value = 0.00;
  1471. if( $bonus > 0 ) {
  1472. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1473. $value = $bonus;
  1474. //加入有奖金的会员中
  1475. self::addHasBonusUsers($userId, $periodNum);
  1476. }else {
  1477. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1478. if ($cacheValue) {
  1479. $value = $cacheValue;
  1480. }
  1481. unset($cacheValue);
  1482. }
  1483. unset($cacheKey, $userId, $periodNum, $bonus);
  1484. return $value;
  1485. }
  1486. public static function villaBonus($userId, $periodNum, $bonus = 0.00) {
  1487. $cacheKey = self::REDIS_KEY_PREFIX_VILLA_BONUS . $periodNum;
  1488. $value = 0.00;
  1489. if( $bonus > 0 ) {
  1490. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1491. $value = $bonus;
  1492. //加入有奖金的会员中
  1493. self::addHasBonusUsers($userId, $periodNum);
  1494. }else {
  1495. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1496. if ($cacheValue) {
  1497. $value = $cacheValue;
  1498. }
  1499. unset($cacheValue);
  1500. }
  1501. unset($cacheKey, $userId, $periodNum, $bonus);
  1502. return $value;
  1503. }
  1504. public static function garageBonus($userId, $periodNum, $bonus = 0.00) {
  1505. $cacheKey = self::REDIS_KEY_PREFIX_GARAGE_BONUS . $periodNum;
  1506. $value = 0.00;
  1507. if( $bonus > 0 ) {
  1508. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1509. $value = $bonus;
  1510. //加入有奖金的会员中
  1511. self::addHasBonusUsers($userId, $periodNum);
  1512. }else {
  1513. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1514. if ($cacheValue) {
  1515. $value = $cacheValue;
  1516. }
  1517. unset($cacheValue);
  1518. }
  1519. unset($cacheKey, $userId, $periodNum, $bonus);
  1520. return $value;
  1521. }
  1522. /**
  1523. * 设置车房总系数
  1524. * @param $periodNum
  1525. * @param $value
  1526. */
  1527. public static function setCFTotalPercent($periodNum, $value) {
  1528. Yii::$app->redis->set(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum, $value);
  1529. }
  1530. /**
  1531. * 获得车房总系数
  1532. * @param $periodNum
  1533. * @return mixed
  1534. */
  1535. public static function getCFTotalPercent($periodNum) {
  1536. return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum);
  1537. }
  1538. /**
  1539. * 设置领袖总系数
  1540. * @param $periodNum
  1541. * @param $value
  1542. */
  1543. public static function setLXTotalPercent($periodNum, $value) {
  1544. Yii::$app->redis->set(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum, $value);
  1545. }
  1546. /**
  1547. * 获得领袖总系数
  1548. * @param $periodNum
  1549. * @return mixed
  1550. */
  1551. public static function getLXTotalPercent($periodNum) {
  1552. return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum);
  1553. }
  1554. /**
  1555. * 添加上2代共享奖关系
  1556. * @param $bonusUserId
  1557. * @param $fromUserId
  1558. * @param $periodNum
  1559. * @param $validDeep
  1560. * @return boolean
  1561. */
  1562. public static function addShareBonusOneRelation($bonusUserId, $fromUserId, $periodNum, $validDeep) {
  1563. if ($validDeep == 1) {
  1564. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
  1565. }else {
  1566. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
  1567. }
  1568. $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);
  1569. $fromUserData = [];
  1570. if( $fromUserDataJson ) {
  1571. $fromUserData = Json::decode($fromUserDataJson, true);
  1572. }
  1573. unset($fromUserDataJson);
  1574. $fromUserData[] = $bonusUserId;
  1575. Yii::$app->redis->hset($cacheKey, $fromUserId, Json::encode($fromUserData));
  1576. unset($fromUserData, $bonusUserId, $fromUserId, $periodNum);
  1577. return true;
  1578. }
  1579. /**
  1580. * 获取上2代共享奖关系
  1581. * @param $fromUserId
  1582. * @param $periodNum
  1583. * @param $validDeep
  1584. * @return array
  1585. */
  1586. public static function getShareBonusOneRelation($fromUserId, $periodNum, $validDeep) {
  1587. if ($validDeep == 1) {
  1588. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
  1589. }else {
  1590. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
  1591. }
  1592. $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);
  1593. $bonusUserData = [];
  1594. if( $fromUserDataJson ) {
  1595. $bonusUserData = Json::decode($fromUserDataJson, true);
  1596. }
  1597. unset($fromUserDataJson);
  1598. return $bonusUserData;
  1599. }
  1600. }