CalcCache.php 65 KB

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