CalcCache.php 66 KB

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