CalcCache.php 66 KB

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