CalcCache.php 67 KB

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