CalcCache.php 64 KB

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