CalcCache.php 66 KB

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