CalcCache.php 42 KB

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