CalcCache.php 65 KB

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