CalcCache.php 67 KB

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