CalcCache.php 63 KB

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