CalcCache.php 67 KB

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