CalcCache.php 64 KB

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