CalcCache.php 63 KB

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