CalcCache.php 66 KB

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