CalcCache.php 64 KB

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