CalcCache.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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\helpers\Cache;
  10. use common\models\Config;
  11. use common\models\PerfMonth;
  12. use common\models\Period;
  13. use common\models\DeclarationLevel;
  14. use common\models\DecRole;
  15. use common\models\DeductReconsume;
  16. use common\models\EmployLevel;
  17. use common\models\StarCrownLevel;
  18. use common\models\User;
  19. use common\models\UserPerf;
  20. use Yii;
  21. use common\models\UserInfo;
  22. use yii\helpers\Json;
  23. class CalcCache {
  24. const LIMIT = 10000;
  25. const REDIS_KEY_PREFIX_USER = 'calc:user_';
  26. const REDIS_KEY_PREFIX_USER_ACTIVE = 'calc:userActive_';
  27. const REDIS_KEY_PREFIX_USER_INFO = 'calc:userInfo_';
  28. const REDIS_KEY_PREFIX_USER_BONUS = 'calc:userBonus_';
  29. const REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS = 'calc:periodMonthCalcBonus_';
  30. const REDIS_KEY_PREFIX_USER_PERF = 'calc:userPerf_';
  31. const REDIS_KEY_PREFIX_SURPLUS_PERF = 'calc:spPerf_';
  32. const REDIS_KEY_PREFIX_NOW_PERIOD_PERF = 'calc:nowPeriodPerf_';
  33. const REDIS_KEY_PREFIX_LAST_MONTH_PERF = 'calc:lastMonthPerf_';
  34. const REDIS_KEY_PREFIX_NOW_MONTH_PERF = 'calc:nowMonthPerf_';
  35. const REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS = 'calc:nowMonthLastPeriodReconsumePoints_';
  36. const REDIS_KEY_PREFIX_EMP_NUM_PERF = 'calc:empLevelNum_';
  37. const REDIS_KEY_PREFIX_BONUS = 'calc:bonus_';
  38. const REDIS_KEY_PREFIX_FW_BONUS = 'calc:fw:bonus_';
  39. const REDIS_KEY_PREFIX_TOURISM_BONUS = 'calc:tourism:bonus_';
  40. const REDIS_KEY_PREFIX_VILLA_BONUS = 'calc:villa:bonus_';
  41. const REDIS_KEY_PREFIX_GARAGE_BONUS = 'calc:garage:bonus_';
  42. const REDIS_KEY_PREFIX_HAS_PERF_USER = 'calc:hasPerfUser_';
  43. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER = 'calc:hasMonthPerfUser_';
  44. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER = 'calc:hasStandardMonthPerfUser_';
  45. const REDIS_KEY_PREFIX_HAS_BD_USER = 'calc:hasBDUser_';
  46. const REDIS_KEY_PREFIX_HAS_INCOME_USER = 'calc:hasIncomeUser_';
  47. const REDIS_KEY_PREFIX_HAS_BONUS_USER = 'calc:hasBonusUser_';
  48. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER = 'calc:hasFwBonusUser_';
  49. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER = 'calc:hasMonthBonusUser_';
  50. const REDIS_KEY_PREFIX_HAS_PERF_USER_POOL = 'calc:hasPerfUserPool_';
  51. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL = 'calc:hasMonthPerfUserPool_';
  52. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL = 'calc:hasStandardMonthPerfUserPool_';
  53. const REDIS_KEY_PREFIX_HAS_BD_USER_POOL = 'calc:hasBDUserPool_';
  54. const REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL = 'calc:hasIncomeUserPool_';
  55. const REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL = 'calc:hasBonusUserPool_';
  56. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL = 'calc:hasFwBonusUserPool_';
  57. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL = 'calc:hasMonthBonusUserPool_';
  58. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER = 'calc:hasLSPCSUser_';
  59. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL = 'calc:hasLSPCSUserPool_';
  60. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER = 'calc:hasCFPercentPCSUser_';
  61. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL = 'calc:hasCFPercentPCSUserPool_';
  62. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER = 'calc:hasLXPercentPCSUser_';
  63. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL = 'calc:hasLXPercentPCSUserPool_';
  64. const REDIS_KEY_PREFIX_DEC_ROLE_CONFIG = 'calc:decRoleConfig_';
  65. const REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP = 'calc:userInfo:ChildOneDeep_';
  66. const REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA = 'calc:fwBonusListData_';
  67. const REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA = 'calc:bonusStarCrownListData_';
  68. //@todo
  69. const REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF = 'calc:repairSurplusPerf_';
  70. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER = 'calc:hasRepairPerfUser_';
  71. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL = 'calc:hasRepairPerfPool_';
  72. const INCOME_QY_BONUS_BD = 'BONUS_QY_BD';
  73. const INCOME_BONUS_TRAVEL = 'BONUS_TRAVEL';
  74. const INCOME_BONUS_CAR = 'BONUS_CAR';
  75. const INCOME_BONUS_HOUSE = 'BONUS_HOUSE';
  76. const CAPPED_BONUS_QY = 'CAPPED_BONUS_QY'; // 团队奖封顶前的金额
  77. const INCOME_BONUS_LIST = [
  78. self::INCOME_QY_BONUS_BD,
  79. ];
  80. const NOT_SEND_BONUS_LIST = [
  81. self::INCOME_QY_BONUS_BD,
  82. self::INCOME_BONUS_TRAVEL,
  83. self::INCOME_BONUS_CAR,
  84. self::INCOME_BONUS_HOUSE,
  85. self::CAPPED_BONUS_QY,
  86. ];
  87. const FROM_MEANS_BD = 'BD';
  88. /**
  89. * 结算奖金时要清空的缓存
  90. * @param $periodNum
  91. */
  92. public static function clearCalcBonusCache($periodNum) {
  93. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
  94. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
  95. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
  96. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
  97. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
  98. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
  99. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
  100. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
  101. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
  102. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum);
  103. DeclarationLevel::updateToCache();
  104. Config::updateToCache();
  105. DecRole::updateToCache();
  106. EmployLevel::updateToCache();
  107. StarCrownLevel::updateToCache();
  108. }
  109. /**
  110. * 清网络缓存
  111. */
  112. public static function clearNetCache(){
  113. \Yii::$app->redis->del(Cache::USER_NETWORK_PARENTS);
  114. \Yii::$app->redis->del(Cache::USER_RELATION_PARENTS);
  115. }
  116. /**
  117. * 清空所有临时计算用到的缓存
  118. * @param $periodNum
  119. */
  120. public static function clearAll($periodNum) {
  121. //Yii::$app->redis->del(Yii::$app->redis->keys('calc*'));
  122. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER . $periodNum);
  123. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum);
  124. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO . $periodNum);
  125. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum);
  126. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum);
  127. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum);
  128. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_PERF . $periodNum);
  129. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum);
  130. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum);
  131. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum);
  132. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum);
  133. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum);
  134. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
  135. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
  136. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum);
  137. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum);
  138. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum);
  139. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum);
  140. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum);
  141. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum);
  142. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum);
  143. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum);
  144. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
  145. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
  146. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
  147. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
  148. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
  149. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
  150. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum);
  151. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum);
  152. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
  153. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum);
  154. DeclarationLevel::updateToCache();
  155. Config::updateToCache();
  156. DecRole::updateToCache();
  157. EmployLevel::updateToCache();
  158. StarCrownLevel::updateToCache();
  159. }
  160. /**
  161. * 会员信息加入缓存
  162. * @param $periodNum
  163. * @param int $offset
  164. * @param int $limit
  165. * @return bool
  166. */
  167. public static function addUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  168. $allData = UserInfo::findUseDbCalc()
  169. ->from(UserInfo::tableName(). 'AS UI')
  170. ->select('UI.USER_ID,U.STATUS')
  171. ->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')
  172. ->where('1=1')
  173. ->orderBy('UI.ID DESC')
  174. ->offset($offset)
  175. ->limit($limit)
  176. ->asArray()
  177. ->all();
  178. if ($allData) {
  179. foreach ($allData as $data) {
  180. $cacheKey = self::REDIS_KEY_PREFIX_USER . $periodNum;
  181. $value = $data['USER_ID'];
  182. Yii::$app->redis->rpush($cacheKey, $value);
  183. if( $data['STATUS'] == 1 ) {
  184. $activeCacheKey = self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum;
  185. Yii::$app->redis->rpush($activeCacheKey, $value);
  186. unset($activeCacheKey);
  187. }
  188. unset($data, $cacheKey, $value);
  189. }
  190. unset($allData);
  191. return self::addUsers($periodNum, $offset + $limit, $limit);
  192. }
  193. unset($allData);
  194. return true;
  195. }
  196. /**
  197. * 获取会员从缓存中
  198. * @param $periodNum
  199. * @param int $offset
  200. * @param int $limit
  201. * @return mixed
  202. */
  203. public static function getUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  204. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER . $periodNum, $offset, ($offset + $limit - 1));
  205. }
  206. /**
  207. * 获取激活会员从缓存中
  208. * @param $periodNum
  209. * @param int $offset
  210. * @param int $limit
  211. * @return mixed
  212. */
  213. public static function getActiveUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  214. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum, $offset, ($offset + $limit - 1));
  215. }
  216. /**
  217. * 加入有业绩的会员
  218. * @param $userId
  219. * @param $periodNum
  220. */
  221. public static function addHasPerfUsers($userId, $periodNum) {
  222. // 先从已存在的会员池里面获取
  223. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId);
  224. if (!$isset) {
  225. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId, 1);
  226. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $userId);
  227. }
  228. unset($userId, $periodNum, $isset);
  229. }
  230. /**
  231. * 获取有业绩的会员
  232. * @param $periodNum
  233. * @param int $offset
  234. * @param int $limit
  235. * @return mixed
  236. */
  237. public static function getHasPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  238. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  239. }
  240. /**
  241. * 加入有月业绩的会员
  242. * @param $userId
  243. * @param $periodNum
  244. */
  245. public static function addHasMonthPerfUsers($userId, $periodNum) {
  246. // 先从已存在的会员池里面获取
  247. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId);
  248. if (!$isset) {
  249. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId, 1);
  250. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $userId);
  251. }
  252. unset($userId, $periodNum, $isset);
  253. }
  254. /**
  255. * 获取有业绩的会员
  256. * @param $periodNum
  257. * @param int $offset
  258. * @param int $limit
  259. * @return mixed
  260. */
  261. public static function getHasMonthPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  262. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  263. }
  264. /**
  265. * 加入被报单的会员
  266. * @param $userId
  267. * @param $periodNum
  268. * @param $saveData
  269. */
  270. public static function addHasBDUsers($userId, $periodNum, $saveData) {
  271. // 先从已存在的会员池里面获取
  272. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
  273. if (!$data) {
  274. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId, Json::encode($saveData));
  275. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $userId);
  276. }
  277. unset($userId, $periodNum, $saveData, $isset);
  278. }
  279. /**
  280. * 获取被报单的会员信息
  281. * @param $userId
  282. * @param $periodNum
  283. * @return array
  284. */
  285. public static function getBDUsersInfo($userId, $periodNum) {
  286. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
  287. if( !$data ) return [];
  288. return Json::decode($data, true);
  289. }
  290. /**
  291. * 获取被报单的会员
  292. * @param $periodNum
  293. * @param int $offset
  294. * @param int $limit
  295. * @return mixed
  296. */
  297. public static function getHasBDUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  298. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $offset, ($offset + $limit - 1));
  299. }
  300. /**
  301. * 加入有收入的会员
  302. * @param $userId
  303. * @param $periodNum
  304. */
  305. public static function addHasIncomeUsers($userId, $periodNum) {
  306. // 先从已存在的会员池里面获取
  307. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId);
  308. if (!$isset) {
  309. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId, 1);
  310. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $userId);
  311. }
  312. unset($userId, $periodNum, $isset);
  313. }
  314. /**
  315. * 获取有收入的会员
  316. * @param $periodNum
  317. * @param int $offset
  318. * @param int $limit
  319. * @return mixed
  320. */
  321. public static function getHasIncomeUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  322. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $offset, ($offset + $limit - 1));
  323. }
  324. /**
  325. * 加入有奖金的会员
  326. * @param $userId
  327. * @param $periodNum
  328. */
  329. public static function addHasBonusUsers($userId, $periodNum) {
  330. // 先从已存在的会员池里面获取
  331. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId);
  332. if (!$isset) {
  333. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId, 1);
  334. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $userId);
  335. }
  336. unset($userId, $periodNum, $isset);
  337. }
  338. /**
  339. * 获取有奖金的会员
  340. * @param $periodNum
  341. * @param int $offset
  342. * @param int $limit
  343. * @return mixed
  344. */
  345. public static function getHasBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  346. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  347. }
  348. /**
  349. * 获取有服务奖的会员
  350. * @param $periodNum
  351. * @param int $offset
  352. * @param int $limit
  353. * @return mixed
  354. */
  355. public static function getHasFwBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  356. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  357. }
  358. /**
  359. * 加入服务奖的会员
  360. * @param $userId
  361. * @param $periodNum
  362. */
  363. public static function addHasFwBonusUsers($userId, $periodNum) {
  364. // 先从已存在的会员池里面获取
  365. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId);
  366. if (!$isset) {
  367. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId, 1);
  368. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $userId);
  369. }
  370. unset($userId, $periodNum, $isset);
  371. }
  372. /**
  373. * 获取有月奖的会员
  374. * @param $periodNum
  375. * @param int $offset
  376. * @param int $limit
  377. * @return mixed
  378. */
  379. public static function getHasMonthBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  380. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  381. }
  382. /**
  383. * 保存服务奖奖金
  384. * @param $userId
  385. * @param $periodNum
  386. * @param $fwBonus
  387. * @param array $fromData
  388. */
  389. public static function saveFwBonusList($userId, $periodNum, $fwBonus, $fromData=[]) {
  390. $userFwBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);
  391. if( $userFwBonusData ) {
  392. $fwBonusData = Json::decode($userFwBonusData, true);
  393. $fwBonusData['fwBonus'] += $fwBonus;
  394. }else {
  395. $fwBonusData = [
  396. 'fwBonus' => $fwBonus,
  397. ];
  398. }
  399. unset($userFwBonusData);
  400. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId, Json::encode($fwBonusData));
  401. unset($fwBonusData, $userId, $periodNum, $empBonus, $fromData);
  402. }
  403. /**
  404. * 返回服务奖信息
  405. * @param $userId
  406. * @param $periodNum
  407. * @return array
  408. */
  409. public static function getFwBonusList($userId, $periodNum) {
  410. $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);
  411. return $userYcBonusData ? Json::decode($userYcBonusData, true) : [];
  412. }
  413. /**
  414. * 会员星级加入缓存
  415. * @param $userId
  416. * @param $periodNum
  417. * @param $crownCrown
  418. */
  419. public static function addUserStarCrown($userId, $periodNum, $starCrown) {
  420. // 先从已存在的会员池里面获取
  421. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum, $userId);
  422. if (!$data) {
  423. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum, $userId, $starCrown);
  424. }
  425. unset($userId, $periodNum, $saveData, $isset);
  426. }
  427. /**
  428. * 获取会员星级信息
  429. * @param $userId
  430. * @param $periodNum
  431. * @return array
  432. */
  433. public static function getUserStarCrown($userId, $periodNum) {
  434. return Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA . $periodNum, $userId);
  435. }
  436. /**
  437. * 有车房补贴比例的人
  438. * @param $userId
  439. * @param $periodNum
  440. */
  441. public static function addHasCFPercentUsers($userId, $periodNum) {
  442. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId);
  443. if (!$isset) {
  444. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId, 1);
  445. $key = self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum;
  446. $value = $userId;
  447. Yii::$app->redis->rpush($key, $value);
  448. }
  449. }
  450. /**
  451. * 获取有车房补贴的人
  452. * @param $periodNum
  453. * @param int $offset
  454. * @param int $limit
  455. * @return mixed
  456. */
  457. public static function getHasCFPercentUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  458. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum, $offset, ($offset + $limit - 1));
  459. }
  460. /**
  461. * 通过创建时间获取指定长度的用户列表
  462. * @param $userId
  463. * @param $limit
  464. * @return array|\yii\db\ActiveRecord[]
  465. */
  466. public static function getUserListByCreatedAtFromDb($userId, $limit)
  467. {
  468. //查找这个人的CREATED_AT
  469. $user = User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID=:ID', ['ID'=>$userId])->asArray()->one();
  470. if( !$user ) return [];
  471. return User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID<:ID AND CREATED_AT<=:CREATED_AT', [
  472. 'ID'=>$userId,
  473. 'CREATED_AT' => $user['CREATED_AT']
  474. ])->orderBy('CREATED_AT DESC,ID DESC')->limit($limit)->asArray()->all();
  475. }
  476. /**
  477. * 通过创建时间获取指定长度的用户列表
  478. * @param $userId
  479. * @param $limit
  480. * @return array|\yii\db\ActiveRecord[]
  481. */
  482. public static function getAfterUserListByCreatedAtFromDb($userId, $limit)
  483. {
  484. $cacheKey = Cache::USER_CREATED_AT_LIST . $limit;
  485. $value = Yii::$app->redis->hGet($cacheKey, $userId);
  486. if( $value ) {
  487. unset($cacheKey);
  488. return json_decode($value, true);
  489. }
  490. unset($value);
  491. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('USER_ID>:USER_ID', [
  492. 'USER_ID'=>$userId,
  493. ])->orderBy('USER_ID ASC')->limit($limit)->asArray()->all();
  494. if ( $limit == count($list) ) {
  495. Yii::$app->redis->hSet($cacheKey, $userId, json_encode($list));
  496. }
  497. unset($cacheKey);
  498. return $list;
  499. }
  500. // 从缓存获取哪些奖金需要扣除管理费和复消积分
  501. public static function deductBonus($periodNum) {
  502. }
  503. /**
  504. * 获取会员信息从缓存
  505. * @param $userId
  506. * @param $periodNum
  507. * @return array
  508. * @throws \Exception
  509. */
  510. public static function getUserInfo($userId, $periodNum) {
  511. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  512. $data = Yii::$app->redis->hget($key, $userId);
  513. $data = [];
  514. if (empty($data)) {
  515. $userInfo = UserInfo::findUseDbCalc()
  516. ->select('USER_ID,USER_NAME,CON_UID,REC_UID')
  517. ->where('USER_ID=:USER_ID', [':USER_ID' => $userId])
  518. ->asArray()
  519. ->one();
  520. if( !$userInfo ) {
  521. throw new \Exception('用户信息表数据不存在,userId:'.$userId);
  522. }
  523. $userShopInfo = User::find()
  524. ->select('LAST_DEC_LV,REAL_NAME,DEC_LV,EMP_LV,CROWN_LV,IS_DEC,STATUS,IS_STUDIO,DEC_ID,DEC_ROLE_ID,LAST_EMP_LV')
  525. ->where('ID=:ID', [':ID' => $userId])
  526. ->asArray()
  527. ->one();
  528. if ( !$userShopInfo ) {
  529. throw new \Exception('用户表数据不存在,userId:'.$userId);
  530. }
  531. //$bsEmpLv = Period::userLastEmpLv($userId, $periodNum);
  532. // 用户的最新蓝星级别,需要修改调整因为计算服务这里并没有以前的级别数据表数据,所以无法查询出以前的最近月节点数据
  533. // 获取用户的最新级别数据,根据蓝星最近一期月结数据进行查询
  534. $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
  535. $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
  536. $userInfo['EMP_LV'] = $userShopInfo['EMP_LV'];
  537. $userInfo['LAST_EMP_LV'] = empty($userShopInfo['LAST_EMP_LV']) ? EmployLevel::NO_LEVEL_ID : $userShopInfo['LAST_EMP_LV'];// 最新的聘级
  538. $userInfo['CROWN_LV'] = $userShopInfo['CROWN_LV'];
  539. $userInfo['IS_DEC'] = $userShopInfo['IS_DEC'];
  540. $userInfo['STATUS'] = $userShopInfo['STATUS'];
  541. $userInfo['DEC_ID'] = $userShopInfo['DEC_ID'];
  542. $userInfo['DEC_ROLE_ID'] = $userShopInfo['DEC_ROLE_ID'];
  543. unset($userShopInfo);
  544. if (!$userInfo['DEC_LV']) {
  545. $userInfo['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  546. }
  547. if (!$userInfo['EMP_LV']) {
  548. $userInfo['EMP_LV'] = EmployLevel::getDefaultLevelId();
  549. }
  550. if (!$userInfo['CROWN_LV']) {
  551. $userInfo['CROWN_LV'] = StarCrownLevel::getDefaultLevelId();
  552. }
  553. $data = Json::encode($userInfo);
  554. Yii::$app->redis->hset($key, $userId, $data);
  555. unset($key, $userId, $periodNum);
  556. return $userInfo;
  557. } else {
  558. if ($data) {
  559. $ret = Json::decode($data, true);
  560. //$bsEmpLv = Period::userLastEmpLv($userId, $periodNum);
  561. // $ret['LAST_EMP_LV'] = EmployLevel::NO_LEVEL_ID; // 最新的聘级
  562. return $ret;
  563. } else {
  564. return [];
  565. }
  566. }
  567. }
  568. public static function setUserInfo($userId, $periodNum, $userInfo) {
  569. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  570. $data = Json::encode($userInfo);
  571. Yii::$app->redis->hset($key, $userId, $data);
  572. unset($userId, $key, $data, $userInfo, $periodNum);
  573. return true;
  574. }
  575. /**
  576. * 本月往期的复消积分的数据
  577. * @param $userId
  578. * @param $periodNum
  579. * @param $calcYearMonth
  580. * @return array|mixed|null|\yii\db\ActiveRecord
  581. * @throws \yii\db\Exception
  582. */
  583. public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
  584. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
  585. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  586. if ($cacheValue) {
  587. $value = Json::decode($cacheValue, true);
  588. } else {
  589. $value = DeductReconsume::findUseDbCalc()
  590. ->select(["RECONSUME_POINTS_SUM"])
  591. ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $userId, ':CALC_MONTH'=>$calcYearMonth])
  592. ->asArray()
  593. ->one();
  594. //$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();
  595. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  596. }
  597. return $value;
  598. }
  599. /**
  600. * 获取结算时这一期的报单中心级别的配置
  601. * @param $periodNum
  602. * @return array|\yii\db\ActiveRecord[]
  603. */
  604. public static function getDecRoleConfig($periodNum) {
  605. $key = self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum;
  606. Yii::$app->redis->del($key);
  607. $data = Yii::$app->redis->get($key);
  608. if (!$data) {
  609. $data = DecRole::find()->where('1=1')->indexBy('ID')->asArray()->all();
  610. $data = Json::encode($data);
  611. Yii::$app->redis->set($key, $data);
  612. }
  613. return $data ? Json::decode($data) : [];
  614. }
  615. /**
  616. * 上一期结余业绩
  617. * @param $userId
  618. * @param $periodNum
  619. * @return array|mixed|\yii\db\ActiveRecord|null
  620. * @throws \yii\db\Exception
  621. */
  622. public static function surplusPerf($userId, $periodNum) {
  623. $userPerf = self::userPerf($userId, $periodNum);
  624. return [
  625. 'SURPLUS_1L' => $userPerf['SURPLUS_1L'],
  626. 'SURPLUS_2L' => $userPerf['SURPLUS_2L'],
  627. 'SURPLUS_3L' => $userPerf['SURPLUS_3L'],
  628. 'SURPLUS_4L' => $userPerf['SURPLUS_4L'],
  629. 'SURPLUS_5L' => $userPerf['SURPLUS_5L'],
  630. 'SURPLUS_1L_ZC' => $userPerf['SURPLUS_1L_ZC'],
  631. 'SURPLUS_2L_ZC' => $userPerf['SURPLUS_2L_ZC'],
  632. 'SURPLUS_3L_ZC' => $userPerf['SURPLUS_3L_ZC'],
  633. 'SURPLUS_4L_ZC' => $userPerf['SURPLUS_4L_ZC'],
  634. 'SURPLUS_5L_ZC' => $userPerf['SURPLUS_5L_ZC'],
  635. 'SURPLUS_1L_FX' => $userPerf['SURPLUS_1L_FX'],
  636. 'SURPLUS_2L_FX' => $userPerf['SURPLUS_2L_FX'],
  637. 'SURPLUS_3L_FX' => $userPerf['SURPLUS_3L_FX'],
  638. 'SURPLUS_4L_FX' => $userPerf['SURPLUS_4L_FX'],
  639. 'SURPLUS_5L_FX' => $userPerf['SURPLUS_5L_FX'],
  640. ];
  641. }
  642. /**
  643. * 获取repairSurplusPerf
  644. * @param $userId
  645. * @return int[]|mixed|null
  646. */
  647. public static function getRepairSurplusPerf($userId) {
  648. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  649. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  650. if ($cacheValue) {
  651. $value = Json::decode($cacheValue);
  652. }else {
  653. $value = [
  654. 'SURPLUS_1L' => 0,
  655. 'SURPLUS_2L' => 0,
  656. 'SURPLUS_3L' => 0,
  657. 'SURPLUS_4L' => 0,
  658. 'SURPLUS_5L' => 0,
  659. ];
  660. }
  661. return $value;
  662. }
  663. /**
  664. * 累加repairSurplusPerf
  665. * @param $userId
  666. * @param $surplusPerf
  667. * @return int[]|mixed|null
  668. */
  669. public static function setRepairSurplusPerf($userId, $surplusPerf) {
  670. $surplusPerfList = self::getRepairSurplusPerf($userId);
  671. foreach ($surplusPerfList as $key => $perf) {
  672. $thisPerf = $surplusPerf[$key] ?? 0;
  673. $surplusPerfList[$key] = $perf + $thisPerf;
  674. unset($key, $perf, $thisPerf);
  675. }
  676. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  677. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($surplusPerfList));
  678. self::addHasRepairPerfUsers($userId);
  679. unset($userId, $cacheKey, $surplusPerf);
  680. return $surplusPerfList;
  681. }
  682. /**
  683. * 加入有往期结余业绩的会员
  684. * @param $userId
  685. */
  686. public static function addHasRepairPerfUsers($userId) {
  687. // 先从已存在的会员池里面获取
  688. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId);
  689. if (!$isset) {
  690. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId, 1);
  691. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $userId);
  692. }
  693. unset($userId, $isset);
  694. }
  695. /**
  696. * 获取有往期结余业绩的会员
  697. * @param int $offset
  698. * @param int $limit
  699. * @return mixed
  700. */
  701. public static function getHasRepairPerfUsers($offset = 0, $limit = self::LIMIT) {
  702. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $offset, ($offset + $limit - 1));
  703. }
  704. /**
  705. * 修正业绩缓存清空
  706. */
  707. public static function clearRepairAllCache() {
  708. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF);
  709. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER);
  710. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL);
  711. }
  712. /**
  713. * 上一期结余业绩
  714. * @param $userId
  715. * @param $periodNum
  716. * @return array|mixed|\yii\db\ActiveRecord|null
  717. * @throws \yii\db\Exception
  718. */
  719. public static function userPerf($userId, $periodNum) {
  720. $value = UserPerf::getOneUserPerf($userId);
  721. return $value;
  722. $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
  723. $field = $userId;
  724. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  725. if ($cacheValue) {
  726. $value = Json::decode($cacheValue);
  727. } else {
  728. $value = UserPerf::getOneUserPerf($userId);
  729. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  730. }
  731. return $value;
  732. }
  733. /**
  734. * 当前期数的的业绩
  735. * @param $userId
  736. * @param $periodNum
  737. * @param null $perf
  738. * @return array|mixed
  739. */
  740. public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
  741. $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
  742. $field = $userId;
  743. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  744. if ($cacheValue) {
  745. $value = Json::decode($cacheValue);
  746. } else {
  747. $value = [
  748. 'FX_AMOUNT_CASH' => 0,
  749. 'PV_PCS' => 0,
  750. 'PV_PSS' => 0,//本期的团队业绩
  751. 'PV_PCS_ZC' => 0,//注册
  752. 'PV_PCS_FX' => 0,
  753. 'PV_PCS_FX_CASH' => 0,//现金复消
  754. 'PV_PCS_FX_POINT' => 0,//积分复消
  755. 'PV_1L' => 0,
  756. 'PV_1L_TOUCH' => 0,
  757. 'PV_1L_ZC' => 0,
  758. 'PV_1L_FX' => 0,
  759. 'PV_2L' => 0,
  760. 'PV_2L_TOUCH' => 0,
  761. 'PV_2L_ZC' => 0,
  762. 'PV_2L_FX' => 0,
  763. 'PV_3L' => 0,
  764. 'PV_3L_TOUCH' => 0,
  765. 'PV_3L_ZC' => 0,
  766. 'PV_3L_FX' => 0,
  767. 'PV_4L' => 0,
  768. 'PV_4L_TOUCH' => 0,
  769. 'PV_4L_ZC' => 0,
  770. 'PV_4L_FX' => 0,
  771. 'PV_5L' => 0,
  772. 'PV_5L_TOUCH' => 0,
  773. 'PV_5L_ZC' => 0,
  774. 'PV_5L_FX' => 0,
  775. 'SURPLUS_1L' => 0,
  776. 'SURPLUS_2L' => 0,
  777. 'SURPLUS_3L' => 0,
  778. 'SURPLUS_4L' => 0,
  779. 'SURPLUS_5L' => 0,
  780. 'SURPLUS_1L_ZC' => 0,
  781. 'SURPLUS_2L_ZC' => 0,
  782. 'SURPLUS_3L_ZC' => 0,
  783. 'SURPLUS_4L_ZC' => 0,
  784. 'SURPLUS_5L_ZC' => 0,
  785. 'SURPLUS_1L_FX' => 0,
  786. 'SURPLUS_2L_FX' => 0,
  787. 'SURPLUS_3L_FX' => 0,
  788. 'SURPLUS_4L_FX' => 0,
  789. 'SURPLUS_5L_FX' => 0,
  790. ];
  791. }
  792. if ($perf !== null) {
  793. foreach ($perf as $key => $pv) {
  794. if (strpos($key, 'SURPLUS') !== false) {
  795. $value[$key] = $pv;
  796. } else {
  797. $value[$key] = $pv + $value[$key];
  798. }
  799. }
  800. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  801. }
  802. return $value;
  803. }
  804. /**
  805. * 上个月的业绩
  806. * @param $userId
  807. * @param $periodNum
  808. * @return array|mixed|null|\yii\db\ActiveRecord
  809. */
  810. public static function lastMonthPerf($userId, $periodNum) {
  811. $period = Period::instance();
  812. $last = $period->getLastMonth($periodNum);
  813. $lastYearMonth = $last['yearMonth'];
  814. $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
  815. $field = $userId;
  816. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  817. if ($cacheValue) {
  818. $value = Json::decode($cacheValue);
  819. } else {
  820. $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
  821. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  822. }
  823. return $value;
  824. }
  825. /**
  826. * 当前月的业绩
  827. * @param $userId
  828. * @param $periodNum
  829. * @param $perf
  830. * @return array|mixed
  831. */
  832. public static function nowMonthPerf($userId, $periodNum, $perf = null) {
  833. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
  834. $field = $userId;
  835. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  836. if ($cacheValue) {
  837. $value = Json::decode($cacheValue);
  838. } else {
  839. $baseInfo = self::getUserInfo($userId, $periodNum);
  840. $value = [
  841. 'USER_ID' => $userId,
  842. 'FX_AMOUNT_CASH' => 0,
  843. 'PV_PCS' => 0,
  844. 'PV_PCS_FX' => 0,
  845. 'PV_PSS' => 0,
  846. 'PV_1L' => 0,
  847. 'PV_2L' => 0,
  848. 'PV_3L' => 0,
  849. 'PV_4L' => 0,
  850. 'PV_5L' => 0,
  851. 'VIP_PV_1L_ZC' => 0,
  852. 'VIP_PV_2L_ZC' => 0,
  853. 'VIP_PV_3L_ZC' => 0,
  854. 'VIP_PV_4L_ZC' => 0,
  855. 'VIP_PV_5L_ZC' => 0,
  856. 'PV_1L_TOTAL' => 0,
  857. 'PV_2L_TOTAL' => 0,
  858. 'PV_3L_TOTAL' => 0,
  859. 'PV_4L_TOTAL' => 0,
  860. 'PV_5L_TOTAL' => 0,
  861. 'PV_PSS_TOTAL' => 0,
  862. 'DEC_LEVEL' => $baseInfo['DEC_LV'],
  863. 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
  864. ];
  865. }
  866. if ($perf !== null) {
  867. foreach ($perf as $key => $item) {
  868. $value[$key] = $item;
  869. }
  870. }
  871. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  872. return $value;
  873. }
  874. /**
  875. * 获取直推的所有子会员
  876. * @param $userId
  877. * @param $periodNum
  878. * @return array
  879. */
  880. public static function getChildrenOneDeepFromRedis($userId, $periodNum){
  881. $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
  882. $data = Yii::$app->redis->hget($key, $userId);
  883. if(!$data){
  884. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
  885. 'REC_UID' => $userId
  886. ])->asArray()->all();
  887. $data = Json::encode($list);
  888. unset($list);
  889. Yii::$app->redis->hset($key, $userId, $data);
  890. }
  891. unset($key, $userId, $periodNum);
  892. return $data ? Json::decode($data, true) : [];
  893. }
  894. /**
  895. * 服务奖最大比例
  896. * @param $userId
  897. * @param $periodNum
  898. * @param int $percent
  899. * @return float|int
  900. */
  901. public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
  902. $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
  903. $value = Yii::$app->redis->hget($cacheKey, $userId);
  904. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  905. if ( $percent > 0 && $percent > $maxPercent ) {
  906. $maxPercent = $percent;
  907. unset($periodNum, $percent);
  908. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  909. return $maxPercent;
  910. }
  911. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  912. return $maxPercent;
  913. }
  914. /**
  915. * 奖金缓存
  916. * @param $userId
  917. * @param $periodNum
  918. * @param string $bonusType
  919. * @param float $oriBonus
  920. * @param array $deductData
  921. * @return array|mixed
  922. */
  923. public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='') {
  924. $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
  925. $value = [
  926. 'BONUS_TG' => 0,
  927. 'BONUS_QY' => 0,
  928. 'BONUS_BS' => 0,
  929. 'BONUS_QUARTER' => 0,
  930. 'BONUS_BD' => 0,
  931. 'ORI_BONUS_BS' => 0,
  932. 'ORI_BONUS_BD' => 0,
  933. 'ORI_BONUS_QUARTER' => 0,
  934. 'ORI_BONUS_TG' => 0,
  935. 'ORI_BONUS_QY' => 0,
  936. 'INCOME_TOTAL' => 0,
  937. 'BONUS_TOTAL' => 0,
  938. 'RECONSUME_POINTS' => 0,
  939. 'MANAGE_TAX' => 0,
  940. 'ORI_CAPPED_BONUS_QY' => 0,// 团队奖,封顶前金额
  941. ];
  942. // 从 redis 中获取当前的结果
  943. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  944. if ($cacheValue) {
  945. $cacheValue = Json::decode($cacheValue);
  946. $value = $cacheValue;
  947. }
  948. unset($cacheValue);
  949. if ($oriBonus > 0) {
  950. $oriBonusType = sprintf('ORI_%s', $bonusType);
  951. $value[$oriBonusType] += $oriBonus;
  952. if( $fromMeans !== '' ) {
  953. $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
  954. $value[$oriBonusMeansType] += $oriBonus;
  955. }
  956. if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  957. if( $deductData ) {
  958. $value[$bonusType] += $deductData['surplus'];
  959. $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  960. $value['MANAGE_TAX'] += $deductData['manageTax'];
  961. }else {
  962. $value[$bonusType] += $oriBonus;
  963. }
  964. $value['BONUS_TOTAL'] += $oriBonus;
  965. }
  966. if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
  967. self::addHasIncomeUsers($userId, $periodNum);
  968. // if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  969. // $value['INCOME_TOTAL'] += $oriBonus;
  970. // }
  971. }
  972. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  973. unset($oriBonusType);
  974. self::addHasBonusUsers($userId, $periodNum);
  975. }
  976. unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
  977. return $value;
  978. }
  979. public static function tourismBonus($userId, $periodNum, $bonus = 0.00) {
  980. $cacheKey = self::REDIS_KEY_PREFIX_TOURISM_BONUS . $periodNum;
  981. $value = 0.00;
  982. if( $bonus > 0 ) {
  983. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  984. $value = $bonus;
  985. //加入有奖金的会员中
  986. self::addHasBonusUsers($userId, $periodNum);
  987. }else {
  988. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  989. if ($cacheValue) {
  990. $value = $cacheValue;
  991. }
  992. unset($cacheValue);
  993. }
  994. unset($cacheKey, $userId, $periodNum, $bonus);
  995. return $value;
  996. }
  997. public static function villaBonus($userId, $periodNum, $bonus = 0.00) {
  998. $cacheKey = self::REDIS_KEY_PREFIX_VILLA_BONUS . $periodNum;
  999. $value = 0.00;
  1000. if( $bonus > 0 ) {
  1001. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1002. $value = $bonus;
  1003. //加入有奖金的会员中
  1004. self::addHasBonusUsers($userId, $periodNum);
  1005. }else {
  1006. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1007. if ($cacheValue) {
  1008. $value = $cacheValue;
  1009. }
  1010. unset($cacheValue);
  1011. }
  1012. unset($cacheKey, $userId, $periodNum, $bonus);
  1013. return $value;
  1014. }
  1015. public static function garageBonus($userId, $periodNum, $bonus = 0.00) {
  1016. $cacheKey = self::REDIS_KEY_PREFIX_GARAGE_BONUS . $periodNum;
  1017. $value = 0.00;
  1018. if( $bonus > 0 ) {
  1019. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1020. $value = $bonus;
  1021. //加入有奖金的会员中
  1022. self::addHasBonusUsers($userId, $periodNum);
  1023. }else {
  1024. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1025. if ($cacheValue) {
  1026. $value = $cacheValue;
  1027. }
  1028. unset($cacheValue);
  1029. }
  1030. unset($cacheKey, $userId, $periodNum, $bonus);
  1031. return $value;
  1032. }
  1033. }