CalcCache.php 66 KB

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