CalcCache.php 65 KB

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