CalcCache.php 66 KB

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