CalcCache.php 64 KB

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