CalcCache.php 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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. \Yii::$app->redis->hdel($key, $userId);
  711. $data = Yii::$app->redis->hget($key, $userId);
  712. if (!$data) {
  713. $userInfo = UserInfo::findUseDbCalc()
  714. ->select('USER_ID,USER_NAME,ZC_AMOUNT,ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,STORE_TYPE,
  715. UPDATED_AT,SYSTEM_ID')
  716. ->where('USER_ID=:USER_ID', [':USER_ID' => $userId])
  717. ->asArray()
  718. ->one();
  719. if( !$userInfo ) {
  720. throw new \Exception('用户信息表数据不存在,userId:'.$userId);
  721. }
  722. // 在获取商城库的会员的级别信息和是否报单中心的会员
  723. $userShopInfo = User::find()
  724. ->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,
  725. PROVINCE,CITY,COUNTY,DEC_PROVINCE,DEC_CITY,DEC_COUNTY,IS_DIRECT_SELLER,SUB_COM_LEADER')
  726. ->where('ID=:ID', [':ID' => $userId])
  727. ->asArray()
  728. ->one();
  729. if ( !$userShopInfo ) {
  730. throw new \Exception('用户表数据不存在,userId:'.$userId);
  731. }
  732. $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
  733. $userInfo['IS_STUDIO'] = $userShopInfo['IS_STUDIO'];
  734. // $userInfo['DEC_LV'] = $userShopInfo['DEC_LV'];
  735. //先用last_dec_lv
  736. $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
  737. $userInfo['EMP_LV'] = $userShopInfo['EMP_LV'];
  738. $userInfo['IS_DEC'] = $userShopInfo['IS_DEC'];
  739. $userInfo['DEC_ID'] = $userShopInfo['DEC_ID'];
  740. $userInfo['DEC_ROLE_ID'] = $userShopInfo['DEC_ROLE_ID'];
  741. $userInfo['DEC_PROVINCE'] = $userShopInfo['DEC_PROVINCE'];
  742. $userInfo['DEC_CITY'] = $userShopInfo['DEC_CITY'];
  743. $userInfo['DEC_COUNTY'] = $userShopInfo['DEC_COUNTY'];
  744. $userInfo['STATUS'] = $userShopInfo['STATUS'];
  745. $userInfo['PERIOD_NUM'] = $userShopInfo['PERIOD_AT'];
  746. $userInfo['MOBILE'] = $userShopInfo['MOBILE'];
  747. $userInfo['CREATED_AT'] = $userShopInfo['CREATED_AT'];
  748. $userInfo['SUB_COM_ID'] = $userShopInfo['SUB_COM_ID'];
  749. $userInfo['PROVINCE'] = $userShopInfo['PROVINCE'];
  750. $userInfo['CITY'] = $userShopInfo['CITY'];
  751. $userInfo['COUNTY'] = $userShopInfo['COUNTY'];
  752. $userInfo['IS_DIRECT_SELLER'] = $userShopInfo['IS_DIRECT_SELLER'];
  753. $userInfo['SUB_COM_LEADER'] = $userShopInfo['SUB_COM_LEADER'];
  754. $userInfo['REC_USER_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserNameByUserId($userInfo['REC_UID']) : '';
  755. $userInfo['REC_REAL_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserRealNameByUserId($userInfo['REC_UID']) : '';
  756. $userInfo['CON_USER_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserNameByUserId($userInfo['CON_UID']) : '';
  757. $userInfo['CON_REAL_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserRealNameByUserId($userInfo['CON_UID']) : '';
  758. // $userInfo['REC_USER_NAME'] = '';
  759. // $userInfo['REC_REAL_NAME'] = '';
  760. // $userInfo['CON_USER_NAME'] = '';
  761. // $userInfo['CON_REAL_NAME'] = '';
  762. // $userInfo['LOCATION'] = 1;
  763. unset($userShopInfo);
  764. // if( isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ) {
  765. // if ($networkParent = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId])) {
  766. // $userInfo['LOCATION'] = $networkParent['RELATIVE_LOCATION'];
  767. // }
  768. // }
  769. if (!$userInfo['DEC_LV']) {
  770. $userInfo['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  771. }
  772. if (!$userInfo['EMP_LV']) {
  773. $userInfo['EMP_LV'] = EmployLevel::getDefaultLevelId();
  774. }
  775. $data = Json::encode($userInfo);
  776. Yii::$app->redis->hset($key, $userId, $data);
  777. unset($userInfo, $key, $userId, $periodNum);
  778. }
  779. return $data ? Json::decode($data, true) : [];
  780. }
  781. public static function setUserInfo($userId, $periodNum, $userInfo) {
  782. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  783. $data = Json::encode($userInfo);
  784. Yii::$app->redis->hset($key, $userId, $data);
  785. unset($userId, $key, $data, $userInfo, $periodNum);
  786. return true;
  787. }
  788. /**
  789. * 用户奖金信息
  790. * @param $userId
  791. * @param $periodNum
  792. * @return array|mixed|null|\yii\db\ActiveRecord
  793. */
  794. public static function LastUserBonus($userId, $periodNum) {
  795. $cacheKey = self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum;
  796. $field = $userId;
  797. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  798. if ($cacheValue) {
  799. $value = Json::decode($cacheValue, true);
  800. } else {
  801. $value = UserBonus::findUseDbCalc()->select('USER_ID,BONUS_TOTAL')->where('USER_ID=:USER_ID', [
  802. 'USER_ID'=>$userId
  803. ])->asArray()->one();
  804. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  805. }
  806. return $value;
  807. }
  808. /**
  809. * 用户奖金信息
  810. * @param $userId
  811. * @param $periodNum
  812. * @param $yearMonth
  813. * @return array|mixed|null|\yii\db\ActiveRecord
  814. */
  815. public static function lastPeriodMonthCalcBonus($userId, $periodNum, $yearMonth) {
  816. $cacheKey = self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum;
  817. $field = $userId;
  818. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  819. if ($cacheValue) {
  820. $value = Json::decode($cacheValue, true);
  821. } else {
  822. $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', [
  823. 'USER_ID'=>$userId,
  824. 'CALC_MONTH'=>$yearMonth
  825. ])->asArray()->one();
  826. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  827. }
  828. return $value;
  829. }
  830. /**
  831. * 本月往期的复消积分的数据
  832. * @param $userId
  833. * @param $periodNum
  834. * @param $calcYearMonth
  835. * @return array|mixed|null|\yii\db\ActiveRecord
  836. * @throws \yii\db\Exception
  837. */
  838. public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
  839. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
  840. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  841. if ($cacheValue) {
  842. $value = Json::decode($cacheValue, true);
  843. } else {
  844. $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();
  845. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  846. }
  847. return $value;
  848. }
  849. /**
  850. * 获取结算时这一期的报单中心级别的配置
  851. * @param $periodNum
  852. * @return array|\yii\db\ActiveRecord[]
  853. */
  854. public static function getDecRoleConfig($periodNum) {
  855. $key = self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum;
  856. Yii::$app->redis->del($key);
  857. $data = Yii::$app->redis->get($key);
  858. if (!$data) {
  859. $data = DecRole::find()->where('1=1')->indexBy('ID')->asArray()->all();
  860. $data = Json::encode($data);
  861. Yii::$app->redis->set($key, $data);
  862. }
  863. return $data ? Json::decode($data) : [];
  864. }
  865. /**
  866. * 上一期结余业绩
  867. * @param $userId
  868. * @param $periodNum
  869. * @return array|mixed|\yii\db\ActiveRecord|null
  870. * @throws \yii\db\Exception
  871. */
  872. public static function surplusPerf($userId, $periodNum) {
  873. // $cacheKey = self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum;
  874. // $field = $userId;
  875. // $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  876. // if ($cacheValue) {
  877. // $value = Json::decode($cacheValue);
  878. // } else {
  879. // $value = UserPerf::getPeriodSurplusPerf($userId);
  880. // \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  881. // }
  882. // return $value;
  883. $userPerf = self::userPerf($userId, $periodNum);
  884. return [
  885. 'SURPLUS_1L' => $userPerf['SURPLUS_1L'],
  886. 'SURPLUS_2L' => $userPerf['SURPLUS_2L'],
  887. 'SURPLUS_3L' => $userPerf['SURPLUS_3L'],
  888. 'SURPLUS_4L' => $userPerf['SURPLUS_4L'],
  889. 'SURPLUS_5L' => $userPerf['SURPLUS_5L'],
  890. 'SURPLUS_1L_ZC' => $userPerf['SURPLUS_1L_ZC'],
  891. 'SURPLUS_2L_ZC' => $userPerf['SURPLUS_2L_ZC'],
  892. 'SURPLUS_3L_ZC' => $userPerf['SURPLUS_3L_ZC'],
  893. 'SURPLUS_4L_ZC' => $userPerf['SURPLUS_4L_ZC'],
  894. 'SURPLUS_5L_ZC' => $userPerf['SURPLUS_5L_ZC'],
  895. 'SURPLUS_1L_FX' => $userPerf['SURPLUS_1L_FX'],
  896. 'SURPLUS_2L_FX' => $userPerf['SURPLUS_2L_FX'],
  897. 'SURPLUS_3L_FX' => $userPerf['SURPLUS_3L_FX'],
  898. 'SURPLUS_4L_FX' => $userPerf['SURPLUS_4L_FX'],
  899. 'SURPLUS_5L_FX' => $userPerf['SURPLUS_5L_FX'],
  900. 'VIP_SURPLUS_1L_ZC' => $userPerf['VIP_SURPLUS_1L_ZC'],
  901. 'VIP_SURPLUS_2L_ZC' => $userPerf['VIP_SURPLUS_2L_ZC'],
  902. 'VIP_SURPLUS_3L_ZC' => $userPerf['VIP_SURPLUS_3L_ZC'],
  903. 'VIP_SURPLUS_4L_ZC' => $userPerf['VIP_SURPLUS_4L_ZC'],
  904. 'VIP_SURPLUS_5L_ZC' => $userPerf['VIP_SURPLUS_5L_ZC'],
  905. 'SURPLUS_LS' => 0,
  906. ];
  907. }
  908. /**
  909. * 获取repairSurplusPerf
  910. * @param $userId
  911. * @return int[]|mixed|null
  912. */
  913. public static function getRepairSurplusPerf($userId) {
  914. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  915. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  916. if ($cacheValue) {
  917. $value = Json::decode($cacheValue);
  918. }else {
  919. $value = [
  920. 'SURPLUS_1L' => 0,
  921. 'SURPLUS_2L' => 0,
  922. 'SURPLUS_3L' => 0,
  923. 'SURPLUS_4L' => 0,
  924. 'SURPLUS_5L' => 0,
  925. ];
  926. }
  927. return $value;
  928. }
  929. /**
  930. * 累加repairSurplusPerf
  931. * @param $userId
  932. * @param $surplusPerf
  933. * @return int[]|mixed|null
  934. */
  935. public static function setRepairSurplusPerf($userId, $surplusPerf) {
  936. $surplusPerfList = self::getRepairSurplusPerf($userId);
  937. foreach ($surplusPerfList as $key => $perf) {
  938. $thisPerf = $surplusPerf[$key] ?? 0;
  939. $surplusPerfList[$key] = $perf + $thisPerf;
  940. unset($key, $perf, $thisPerf);
  941. }
  942. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  943. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($surplusPerfList));
  944. self::addHasRepairPerfUsers($userId);
  945. unset($userId, $cacheKey, $surplusPerf);
  946. return $surplusPerfList;
  947. }
  948. /**
  949. * 加入有往期结余业绩的会员
  950. * @param $userId
  951. */
  952. public static function addHasRepairPerfUsers($userId) {
  953. // 先从已存在的会员池里面获取
  954. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId);
  955. if (!$isset) {
  956. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId, 1);
  957. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $userId);
  958. }
  959. unset($userId, $isset);
  960. }
  961. /**
  962. * 获取有往期结余业绩的会员
  963. * @param int $offset
  964. * @param int $limit
  965. * @return mixed
  966. */
  967. public static function getHasRepairPerfUsers($offset = 0, $limit = self::LIMIT) {
  968. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $offset, ($offset + $limit - 1));
  969. }
  970. /**
  971. * 修正业绩缓存清空
  972. */
  973. public static function clearRepairAllCache() {
  974. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF);
  975. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER);
  976. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL);
  977. }
  978. /**
  979. * 上一期结余业绩
  980. * @param $userId
  981. * @param $periodNum
  982. * @return array|mixed|\yii\db\ActiveRecord|null
  983. * @throws \yii\db\Exception
  984. */
  985. public static function userPerf($userId, $periodNum) {
  986. $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
  987. $field = $userId;
  988. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  989. if ($cacheValue) {
  990. $value = Json::decode($cacheValue);
  991. } else {
  992. $value = UserPerf::getOneUserPerf($userId);
  993. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  994. }
  995. return $value;
  996. }
  997. /**
  998. * 当前期数的的业绩
  999. * @param $userId
  1000. * @param $periodNum
  1001. * @param null $perf
  1002. * @return array|mixed
  1003. */
  1004. public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
  1005. $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
  1006. $field = $userId;
  1007. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1008. if ($cacheValue) {
  1009. $value = Json::decode($cacheValue);
  1010. } else {
  1011. $value = [
  1012. 'STORE_PV_GRAND' => 0, // 用户的店铺业绩
  1013. 'FX_AMOUNT_CASH' => 0,
  1014. 'PV_PCS' => 0,
  1015. 'PV_PSS' => 0,//本期的团队业绩
  1016. 'PV_PCS_ZC' => 0,//注册
  1017. 'PV_PCS_YH' => 0,
  1018. 'PV_PCS_ZG' => 0,//增购
  1019. 'PV_PCS_LS' => 0,
  1020. 'PV_PCS_FX' => 0,
  1021. 'PV_PCS_FX_CASH' => 0,//现金复消
  1022. 'PV_PCS_FX_POINT' => 0,//积分复消
  1023. 'PV_1L' => 0,
  1024. 'PV_1L_TOUCH' => 0,
  1025. 'PV_1L_ZC' => 0,
  1026. 'PV_1L_YH' => 0,
  1027. 'PV_1L_ZG' => 0,
  1028. 'PV_1L_LS' => 0,
  1029. 'PV_1L_FX' => 0,
  1030. 'PV_2L' => 0,
  1031. 'PV_2L_TOUCH' => 0,
  1032. 'PV_2L_ZC' => 0,
  1033. 'PV_2L_YH' => 0,
  1034. 'PV_2L_ZG' => 0,
  1035. 'PV_2L_LS' => 0,
  1036. 'PV_2L_FX' => 0,
  1037. 'PV_3L' => 0,
  1038. 'PV_3L_TOUCH' => 0,
  1039. 'PV_3L_ZC' => 0,
  1040. 'PV_3L_YH' => 0,
  1041. 'PV_3L_ZG' => 0,
  1042. 'PV_3L_LS' => 0,
  1043. 'PV_3L_FX' => 0,
  1044. 'PV_4L' => 0,
  1045. 'PV_4L_TOUCH' => 0,
  1046. 'PV_4L_ZC' => 0,
  1047. 'PV_4L_YH' => 0,
  1048. 'PV_4L_ZG' => 0,
  1049. 'PV_4L_LS' => 0,
  1050. 'PV_4L_FX' => 0,
  1051. 'PV_5L' => 0,
  1052. 'PV_5L_TOUCH' => 0,
  1053. 'PV_5L_ZC' => 0,
  1054. 'PV_5L_YH' => 0,
  1055. 'PV_5L_ZG' => 0,
  1056. 'PV_5L_LS' => 0,
  1057. 'PV_5L_FX' => 0,
  1058. 'PV_LS_TOUCH' => 0,
  1059. 'SURPLUS_1L' => 0,
  1060. 'SURPLUS_2L' => 0,
  1061. 'SURPLUS_3L' => 0,
  1062. 'SURPLUS_4L' => 0,
  1063. 'SURPLUS_5L' => 0,
  1064. 'SURPLUS_1L_ZC' => 0,
  1065. 'SURPLUS_2L_ZC' => 0,
  1066. 'SURPLUS_3L_ZC' => 0,
  1067. 'SURPLUS_4L_ZC' => 0,
  1068. 'SURPLUS_5L_ZC' => 0,
  1069. 'SURPLUS_1L_FX' => 0,
  1070. 'SURPLUS_2L_FX' => 0,
  1071. 'SURPLUS_3L_FX' => 0,
  1072. 'SURPLUS_4L_FX' => 0,
  1073. 'SURPLUS_5L_FX' => 0,
  1074. 'SURPLUS_LS' => 0,
  1075. ];
  1076. }
  1077. if ($perf !== null) {
  1078. foreach ($perf as $key => $pv) {
  1079. if (strpos($key, 'SURPLUS') !== false) {
  1080. $value[$key] = $pv;
  1081. } else {
  1082. $value[$key] = $pv + $value[$key];
  1083. }
  1084. }
  1085. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1086. }
  1087. return $value;
  1088. }
  1089. /**
  1090. * 上个月的业绩
  1091. * @param $userId
  1092. * @param $periodNum
  1093. * @return array|mixed|null|\yii\db\ActiveRecord
  1094. */
  1095. public static function lastMonthPerf($userId, $periodNum) {
  1096. $period = Period::instance();
  1097. $last = $period->getLastMonth($periodNum);
  1098. $lastYearMonth = $last['yearMonth'];
  1099. $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
  1100. $field = $userId;
  1101. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1102. if ($cacheValue) {
  1103. $value = Json::decode($cacheValue);
  1104. } else {
  1105. $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
  1106. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1107. }
  1108. return $value;
  1109. }
  1110. /**
  1111. * 当前月的业绩
  1112. * @param $userId
  1113. * @param $periodNum
  1114. * @param $perf
  1115. * @return array|mixed
  1116. */
  1117. public static function nowMonthPerf($userId, $periodNum, $perf = null) {
  1118. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
  1119. $field = $userId;
  1120. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1121. if ($cacheValue) {
  1122. $value = Json::decode($cacheValue);
  1123. } else {
  1124. $baseInfo = self::getUserInfo($userId, $periodNum);
  1125. $value = [
  1126. 'USER_ID' => $userId,
  1127. 'FX_AMOUNT_CASH' => 0,
  1128. 'PV_PCS' => 0,
  1129. 'PV_PCS_FX' => 0,
  1130. 'PV_PSS' => 0,
  1131. 'PV_1L' => 0,
  1132. 'PV_2L' => 0,
  1133. 'PV_3L' => 0,
  1134. 'PV_4L' => 0,
  1135. 'PV_5L' => 0,
  1136. 'VIP_PV_1L_ZC' => 0,
  1137. 'VIP_PV_2L_ZC' => 0,
  1138. 'VIP_PV_3L_ZC' => 0,
  1139. 'VIP_PV_4L_ZC' => 0,
  1140. 'VIP_PV_5L_ZC' => 0,
  1141. 'PV_1L_TOTAL' => 0,
  1142. 'PV_2L_TOTAL' => 0,
  1143. 'PV_3L_TOTAL' => 0,
  1144. 'PV_4L_TOTAL' => 0,
  1145. 'PV_5L_TOTAL' => 0,
  1146. 'PV_PSS_TOTAL' => 0,
  1147. 'DEC_LEVEL' => $baseInfo['DEC_LV'],
  1148. 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
  1149. 'CF_PERCENT' => 0,
  1150. 'LX_PERCENT' => 0,
  1151. 'FX_STATUS' => PerfMonth::NEXT_MONTH_FX_FALSE
  1152. ];
  1153. }
  1154. if ($perf !== null) {
  1155. foreach ($perf as $key => $item) {
  1156. $value[$key] = $item;
  1157. }
  1158. }
  1159. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1160. return $value;
  1161. }
  1162. /**
  1163. * 当前期数的的业绩
  1164. * @param $userId
  1165. * @param $periodNum
  1166. * @param null $perf
  1167. * @return array|mixed
  1168. */
  1169. public static function nowStandardMonthPerf($userId, $periodNum, $perf = null) {
  1170. $cacheKey = self::REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF . $periodNum;
  1171. $field = $userId;
  1172. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1173. if ($cacheValue) {
  1174. $value = Json::decode($cacheValue);
  1175. } else {
  1176. $value = [
  1177. 'AMOUNT_PCS' => 0,
  1178. 'AMOUNT_PSS' => 0
  1179. ];
  1180. }
  1181. if ($perf !== null) {
  1182. foreach ($perf as $key => $pv) {
  1183. $value[$key] = $pv + $value[$key];
  1184. }
  1185. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1186. }
  1187. return $value;
  1188. }
  1189. /**
  1190. * 当前月的积分
  1191. * @param $userId
  1192. * @param $periodNum
  1193. * @param $perf
  1194. * @return array|mixed
  1195. */
  1196. public static function nowMonthScore($userId, $periodNum, $perf = null) {
  1197. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_SCORE . $periodNum;
  1198. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1199. if ($cacheValue) {
  1200. $value = Json::decode($cacheValue);
  1201. } else {
  1202. $value = [
  1203. 'USER_ID' => $userId,
  1204. 'BASE_SCORE' => 0,
  1205. 'LEVEL_SCORE' => 0,
  1206. 'UPGRADE_SCORE' => 0,
  1207. // 'TOTAL_SCORE' => 0,
  1208. ];
  1209. }
  1210. unset($cacheValue);
  1211. if ($perf !== null) {
  1212. foreach ($perf as $key => $item) {
  1213. $value[$key] = $item;
  1214. // $value['TOTAL_SCORE'] += $item;
  1215. unset($key, $item);
  1216. }
  1217. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  1218. }
  1219. unset($userId, $periodNum, $perf, $cacheKey);
  1220. return $value;
  1221. }
  1222. /**
  1223. * 获取直推的所有子会员
  1224. * @param $userId
  1225. * @param $periodNum
  1226. * @return array
  1227. */
  1228. public static function getChildrenOneDeepFromRedis($userId, $periodNum){
  1229. $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
  1230. $data = Yii::$app->redis->hget($key, $userId);
  1231. if(!$data){
  1232. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
  1233. 'REC_UID' => $userId
  1234. ])->asArray()->all();
  1235. $data = Json::encode($list);
  1236. unset($list);
  1237. Yii::$app->redis->hset($key, $userId, $data);
  1238. }
  1239. unset($key, $userId, $periodNum);
  1240. return $data ? Json::decode($data, true) : [];
  1241. }
  1242. /**
  1243. * 每个区有多少个什么级别的数量
  1244. * @param $userId
  1245. * @param $periodNum
  1246. * @param null $levelNumArr
  1247. * @return array|mixed
  1248. */
  1249. public static function hasEmpLevelNum($userId, $periodNum, $levelNumArr = null) {
  1250. $key = self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum;
  1251. $field = $userId;
  1252. $cacheValue = Yii::$app->redis->hget($key, $field);
  1253. if ($cacheValue) {
  1254. $value = Json::decode($cacheValue);
  1255. } else {
  1256. $allEmpLevel = EmployLevel::getFromCache();
  1257. $value = [];
  1258. $relationList = self::getChildrenOneDeepFromRedis($userId, $periodNum);
  1259. foreach ($relationList as $relationData) {
  1260. foreach ($allEmpLevel as $level) {
  1261. // 从月度业绩表中找到所有下级的各个部门的数量
  1262. $value[$relationData['USER_ID']][$level['ID']] = 0;
  1263. }
  1264. }
  1265. }
  1266. if ($levelNumArr != null) {
  1267. foreach ($levelNumArr as $departUserId => $item) {
  1268. foreach ($item as $levelID => $num) {
  1269. if( isset($value[$departUserId][$levelID]) ) {
  1270. $value[$departUserId][$levelID] += $num;
  1271. }else {
  1272. $value[$departUserId][$levelID] = $num;
  1273. }
  1274. }
  1275. }
  1276. }
  1277. Yii::$app->redis->hset($key, $field, Json::encode($value));
  1278. if ($levelNumArr == null) return $value;
  1279. }
  1280. /**
  1281. * 会员是否本月注册
  1282. * @param $userId
  1283. * @param $periodNum
  1284. * @return bool
  1285. * @throws \yii\db\Exception
  1286. */
  1287. public static function isMonthJoinFromRedis($userId, $periodNum) {
  1288. $period = Period::instance();
  1289. $calcYearMonth = $period->getYearMonth($periodNum);
  1290. $userInfo = self::getUserInfo($userId, $periodNum);
  1291. $addYearMonth = $period->getYearMonth($userInfo['PERIOD_NUM']);
  1292. //当月注册
  1293. if ($addYearMonth == $calcYearMonth) return true;
  1294. //上月注册
  1295. $calcLastYearMonth = $period->getLastMonth($periodNum);
  1296. if ($addYearMonth == $calcLastYearMonth['yearMonth']) return true;
  1297. // 未来注册
  1298. if ($addYearMonth > $calcYearMonth) return true;
  1299. return false;
  1300. }
  1301. /**
  1302. * 荣衔奖最大比例
  1303. * @param $userId
  1304. * @param $periodNum
  1305. * @param int $percent
  1306. * @return float|int
  1307. */
  1308. public static function ycMaxBonusPercent($userId, $periodNum, $percent=0) {
  1309. $cacheKey = self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum;
  1310. $value = Yii::$app->redis->hget($cacheKey, $userId);
  1311. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  1312. if ( $percent > 0 && $percent > $maxPercent ) {
  1313. $maxPercent = $percent;
  1314. unset($periodNum, $percent);
  1315. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  1316. return $maxPercent;
  1317. }
  1318. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  1319. return $maxPercent;
  1320. }
  1321. /**
  1322. * 服务奖最大比例
  1323. * @param $userId
  1324. * @param $periodNum
  1325. * @param int $percent
  1326. * @return float|int
  1327. */
  1328. public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
  1329. $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
  1330. $value = Yii::$app->redis->hget($cacheKey, $userId);
  1331. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  1332. if ( $percent > 0 && $percent > $maxPercent ) {
  1333. $maxPercent = $percent;
  1334. unset($periodNum, $percent);
  1335. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  1336. return $maxPercent;
  1337. }
  1338. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  1339. return $maxPercent;
  1340. }
  1341. /**
  1342. * 奖金缓存
  1343. * @param $userId
  1344. * @param $periodNum
  1345. * @param string $bonusType
  1346. * @param float $oriBonus
  1347. * @param array $deductData
  1348. * @return array|mixed
  1349. */
  1350. public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='',$isGx=false) {
  1351. // 如果是共享奖金,则不能往缓存中添加复消积分.共享奖金的复消积分最后再累加上去.
  1352. $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
  1353. $value = [
  1354. 'BONUS_BD' => 0,
  1355. 'BONUS_TG' => 0,
  1356. 'BONUS_XF' => 0,
  1357. 'BONUS_YJ' => 0,
  1358. 'BONUS_GX' => 0,
  1359. 'BONUS_GL' => 0,
  1360. 'BONUS_QY' => 0,
  1361. 'BONUS_YC' => 0,
  1362. 'BONUS_VIP' => 0,
  1363. 'BONUS_BS' => 0,
  1364. 'BONUS_STORE' => 0, // 店服务奖实发
  1365. 'BONUS_MNT'=>0, //蓝星管理奖实发
  1366. 'BONUS_ABBR'=>0, // 蓝星业绩奖实发
  1367. 'ORI_BONUS_MNT'=>0, //蓝星管理奖原奖金
  1368. 'ORI_BONUS_ABBR'=>0, //蓝星业绩奖原奖金
  1369. 'MANAGE_TAX_MNT'=>0, //蓝星管理奖,管理费
  1370. 'MANAGE_TAX_ABBR'=>0, //蓝星业绩奖,管理费
  1371. 'BONUS_YC_EXTRA' => 0,
  1372. 'ORI_BONUS_STORE' => 0, // 店服务奖原奖金
  1373. 'ORI_BONUS_BD' => 0,
  1374. 'ORI_BONUS_BS' => 0,
  1375. 'ORI_BONUS_TG' => 0,
  1376. 'ORI_BONUS_XF' => 0,
  1377. 'ORI_BONUS_YJ' => 0,
  1378. 'ORI_BONUS_YJ_BD' => 0,
  1379. 'ORI_BONUS_YJ_FX' => 0,
  1380. 'ORI_BONUS_GX' => 0,
  1381. 'ORI_BONUS_GL' => 0,
  1382. 'ORI_BONUS_GL_BD' => 0,
  1383. 'ORI_BONUS_GL_FX' => 0,
  1384. 'ORI_BONUS_QY' => 0,
  1385. 'ORI_BONUS_QY_BD' => 0,
  1386. 'ORI_BONUS_QY_FX' => 0,
  1387. 'ORI_BONUS_YC' => 0,
  1388. 'ORI_BONUS_VIP' => 0,
  1389. 'ORI_BONUS_YC_EXTRA' => 0,
  1390. // 'INCOME_TG_TOTAL' => 0,
  1391. // 'INCOME_YJ_TOTAL' => 0,
  1392. // 'INCOME_QY_TOTAL' => 0,
  1393. 'INCOME_TOTAL' => 0,
  1394. 'BONUS_TOTAL' => 0,
  1395. 'RECONSUME_POINTS' => 0,
  1396. 'GX_RECONSUME_POINTS'=>0, // 共享奖复消积分,非月节点,不能发放,记录每期复消积分数据,发放的时候减去
  1397. 'GX_MANAGE_TAX' =>0 ,
  1398. 'MANAGE_TAX' => 0,
  1399. //没有用到的
  1400. 'BONUS_FX' => 0,
  1401. 'BONUS_LS' => 0,
  1402. 'BONUS_CF' => 0,
  1403. 'BONUS_LX' => 0,
  1404. 'BONUS_HB' => 0,
  1405. 'BONUS_BT' => 0,
  1406. 'BONUS_BT_PROD' => 0,
  1407. 'BONUS_BT_TOOL' => 0,
  1408. 'DEDUCT_ZR' => 0,
  1409. 'BONUS_FL' => 0,
  1410. ];
  1411. // 从 redis 中获取当前的结果
  1412. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1413. if ($cacheValue) {
  1414. $cacheValue = Json::decode($cacheValue);
  1415. $value = $cacheValue;
  1416. }
  1417. unset($cacheValue);
  1418. if ($oriBonus > 0) {
  1419. $oriBonusType = sprintf('ORI_%s', $bonusType);
  1420. $value[$oriBonusType] += $oriBonus;
  1421. if( $fromMeans !== '' ) {
  1422. $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
  1423. $value[$oriBonusMeansType] += $oriBonus;
  1424. }
  1425. if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  1426. if( $deductData ) {
  1427. $value[$bonusType] += $deductData['surplus'];
  1428. $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  1429. $value['MANAGE_TAX'] += $deductData['manageTax'];
  1430. if ($isGx === true) {
  1431. $value['GX_RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  1432. $value['GX_MANAGE_TAX'] += $deductData['manageTax'];
  1433. }
  1434. }else {
  1435. $value[$bonusType] += $oriBonus;
  1436. }
  1437. $value['BONUS_TOTAL'] += $oriBonus;
  1438. }
  1439. if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
  1440. self::addHasIncomeUsers($userId, $periodNum);
  1441. // if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  1442. // $value['INCOME_TOTAL'] += $oriBonus;
  1443. // }
  1444. }
  1445. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  1446. unset($oriBonusType);
  1447. self::addHasBonusUsers($userId, $periodNum);
  1448. }
  1449. unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
  1450. // echo json_encode($value);
  1451. // exit();
  1452. return $value;
  1453. }
  1454. // 共享奖金,记录向上分奖金次数,如果超过五次,则不能继续分享奖金
  1455. public static function gxShareTimes($userId, $periodNum) {
  1456. $cacheKey = self::REDIS_KEY_PREFIX_GX_SHARE_TIMES_LIMIT . $periodNum;
  1457. // 从 redis 中获取当前的结果
  1458. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1459. $shareTimes = 0;
  1460. if ($cacheValue) {
  1461. $cacheValue = Json::decode($cacheValue);
  1462. $shareTimes = $cacheValue['share_times'];
  1463. }
  1464. $shareTimes += 1;
  1465. Yii::$app->redis->hset($cacheKey, $userId, Json::encode([
  1466. 'share_times' => $shareTimes
  1467. ]));
  1468. return $shareTimes;
  1469. }
  1470. public static function standardBonus($userId, $periodNum, $bonus=0.00) {
  1471. $cacheKey = self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum;
  1472. $value = 0.00;
  1473. if( $bonus > 0 ) {
  1474. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1475. $value = $bonus;
  1476. //加入有奖金的会员中
  1477. self::addHasBonusUsers($userId, $periodNum);
  1478. }else {
  1479. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1480. if ($cacheValue) {
  1481. $value = $cacheValue;
  1482. }
  1483. unset($cacheValue);
  1484. }
  1485. unset($cacheKey, $userId, $periodNum, $bonus);
  1486. return $value;
  1487. }
  1488. /**
  1489. * 设置车房总系数
  1490. * @param $periodNum
  1491. * @param $value
  1492. */
  1493. public static function setCFTotalPercent($periodNum, $value) {
  1494. Yii::$app->redis->set(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum, $value);
  1495. }
  1496. /**
  1497. * 获得车房总系数
  1498. * @param $periodNum
  1499. * @return mixed
  1500. */
  1501. public static function getCFTotalPercent($periodNum) {
  1502. return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum);
  1503. }
  1504. /**
  1505. * 设置领袖总系数
  1506. * @param $periodNum
  1507. * @param $value
  1508. */
  1509. public static function setLXTotalPercent($periodNum, $value) {
  1510. Yii::$app->redis->set(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum, $value);
  1511. }
  1512. /**
  1513. * 获得领袖总系数
  1514. * @param $periodNum
  1515. * @return mixed
  1516. */
  1517. public static function getLXTotalPercent($periodNum) {
  1518. return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum);
  1519. }
  1520. /**
  1521. * 添加上2代共享奖关系
  1522. * @param $bonusUserId
  1523. * @param $fromUserId
  1524. * @param $periodNum
  1525. * @param $validDeep
  1526. * @return boolean
  1527. */
  1528. public static function addShareBonusOneRelation($bonusUserId, $fromUserId, $periodNum, $validDeep) {
  1529. if ($validDeep == 1) {
  1530. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
  1531. }else {
  1532. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
  1533. }
  1534. $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);
  1535. $fromUserData = [];
  1536. if( $fromUserDataJson ) {
  1537. $fromUserData = Json::decode($fromUserDataJson, true);
  1538. }
  1539. unset($fromUserDataJson);
  1540. $fromUserData[] = $bonusUserId;
  1541. Yii::$app->redis->hset($cacheKey, $fromUserId, Json::encode($fromUserData));
  1542. unset($fromUserData, $bonusUserId, $fromUserId, $periodNum);
  1543. return true;
  1544. }
  1545. /**
  1546. * 获取上2代共享奖关系
  1547. * @param $fromUserId
  1548. * @param $periodNum
  1549. * @param $validDeep
  1550. * @return array
  1551. */
  1552. public static function getShareBonusOneRelation($fromUserId, $periodNum, $validDeep) {
  1553. if ($validDeep == 1) {
  1554. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
  1555. }else {
  1556. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
  1557. }
  1558. $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);
  1559. $bonusUserData = [];
  1560. if( $fromUserDataJson ) {
  1561. $bonusUserData = Json::decode($fromUserDataJson, true);
  1562. }
  1563. unset($fromUserDataJson);
  1564. return $bonusUserData;
  1565. }
  1566. }