CalcCache.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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. // 先判断此期是否是月节点
  536. $nowPeriod = Period::find()
  537. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
  538. ->asArray()
  539. ->one();
  540. if ($nowPeriod['IS_MONTH'] == 1) {
  541. $bsMonthPeriod = $nowPeriod['PERIOD_NUM'];
  542. } else {
  543. $forwardMonthPeriod = Period::find()
  544. ->where('IS_MONTH=1 AND PERIOD_NUM<:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
  545. ->orderBy('PERIOD_NUM DESC')
  546. ->asArray()
  547. ->one();
  548. $bsMonthPeriod = $forwardMonthPeriod['PERIOD_NUM'];
  549. }
  550. $blueBsInfo = CalcBonusBS::findOneAsArray(
  551. 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID',
  552. [':PERIOD_NUM'=>$bsMonthPeriod, ':USER_ID'=>$userId]
  553. );
  554. if (!empty($blueBsInfo)) {
  555. $bsEmpLv = $blueBsInfo['LEVEL_ID']; // 当前蓝星奖计算(即管理奖) 的等级
  556. } else {
  557. $bsEmpLv = EmployLevel::NO_LEVEL_ID;
  558. }
  559. // 获取用户的最新级别数据,根据蓝星最近一期月结数据进行查询
  560. $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
  561. $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
  562. $userInfo['EMP_LV'] = $userShopInfo['EMP_LV'];
  563. $userInfo['LAST_EMP_LV'] = $bsEmpLv; // 最新的聘级
  564. $userInfo['CROWN_LV'] = $userShopInfo['CROWN_LV'];
  565. $userInfo['IS_DEC'] = $userShopInfo['IS_DEC'];
  566. $userInfo['STATUS'] = $userShopInfo['STATUS'];
  567. $userInfo['DEC_ID'] = $userShopInfo['DEC_ID'];
  568. unset($userShopInfo);
  569. if (!$userInfo['DEC_LV']) {
  570. $userInfo['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  571. }
  572. if (!$userInfo['EMP_LV']) {
  573. $userInfo['EMP_LV'] = EmployLevel::getDefaultLevelId();
  574. }
  575. if (!$userInfo['CROWN_LV']) {
  576. $userInfo['CROWN_LV'] = StarCrownLevel::getDefaultLevelId();
  577. }
  578. $data = Json::encode($userInfo);
  579. Yii::$app->redis->hset($key, $userId, $data);
  580. unset($userInfo, $key, $userId, $periodNum);
  581. }
  582. return $data ? Json::decode($data, true) : [];
  583. }
  584. public static function setUserInfo($userId, $periodNum, $userInfo) {
  585. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  586. $data = Json::encode($userInfo);
  587. Yii::$app->redis->hset($key, $userId, $data);
  588. unset($userId, $key, $data, $userInfo, $periodNum);
  589. return true;
  590. }
  591. /**
  592. * 本月往期的复消积分的数据
  593. * @param $userId
  594. * @param $periodNum
  595. * @param $calcYearMonth
  596. * @return array|mixed|null|\yii\db\ActiveRecord
  597. * @throws \yii\db\Exception
  598. */
  599. public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
  600. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
  601. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  602. if ($cacheValue) {
  603. $value = Json::decode($cacheValue, true);
  604. } else {
  605. $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();
  606. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  607. }
  608. return $value;
  609. }
  610. /**
  611. * 获取结算时这一期的报单中心级别的配置
  612. * @param $periodNum
  613. * @return array|\yii\db\ActiveRecord[]
  614. */
  615. public static function getDecRoleConfig($periodNum) {
  616. $key = self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum;
  617. Yii::$app->redis->del($key);
  618. $data = Yii::$app->redis->get($key);
  619. if (!$data) {
  620. $data = DecRole::find()->where('1=1')->indexBy('ID')->asArray()->all();
  621. $data = Json::encode($data);
  622. Yii::$app->redis->set($key, $data);
  623. }
  624. return $data ? Json::decode($data) : [];
  625. }
  626. /**
  627. * 上一期结余业绩
  628. * @param $userId
  629. * @param $periodNum
  630. * @return array|mixed|\yii\db\ActiveRecord|null
  631. * @throws \yii\db\Exception
  632. */
  633. public static function surplusPerf($userId, $periodNum) {
  634. $userPerf = self::userPerf($userId, $periodNum);
  635. return [
  636. 'SURPLUS_1L' => $userPerf['SURPLUS_1L'],
  637. 'SURPLUS_2L' => $userPerf['SURPLUS_2L'],
  638. 'SURPLUS_3L' => $userPerf['SURPLUS_3L'],
  639. 'SURPLUS_4L' => $userPerf['SURPLUS_4L'],
  640. 'SURPLUS_5L' => $userPerf['SURPLUS_5L'],
  641. 'SURPLUS_1L_ZC' => $userPerf['SURPLUS_1L_ZC'],
  642. 'SURPLUS_2L_ZC' => $userPerf['SURPLUS_2L_ZC'],
  643. 'SURPLUS_3L_ZC' => $userPerf['SURPLUS_3L_ZC'],
  644. 'SURPLUS_4L_ZC' => $userPerf['SURPLUS_4L_ZC'],
  645. 'SURPLUS_5L_ZC' => $userPerf['SURPLUS_5L_ZC'],
  646. 'SURPLUS_1L_FX' => $userPerf['SURPLUS_1L_FX'],
  647. 'SURPLUS_2L_FX' => $userPerf['SURPLUS_2L_FX'],
  648. 'SURPLUS_3L_FX' => $userPerf['SURPLUS_3L_FX'],
  649. 'SURPLUS_4L_FX' => $userPerf['SURPLUS_4L_FX'],
  650. 'SURPLUS_5L_FX' => $userPerf['SURPLUS_5L_FX'],
  651. ];
  652. }
  653. /**
  654. * 获取repairSurplusPerf
  655. * @param $userId
  656. * @return int[]|mixed|null
  657. */
  658. public static function getRepairSurplusPerf($userId) {
  659. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  660. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  661. if ($cacheValue) {
  662. $value = Json::decode($cacheValue);
  663. }else {
  664. $value = [
  665. 'SURPLUS_1L' => 0,
  666. 'SURPLUS_2L' => 0,
  667. 'SURPLUS_3L' => 0,
  668. 'SURPLUS_4L' => 0,
  669. 'SURPLUS_5L' => 0,
  670. ];
  671. }
  672. return $value;
  673. }
  674. /**
  675. * 累加repairSurplusPerf
  676. * @param $userId
  677. * @param $surplusPerf
  678. * @return int[]|mixed|null
  679. */
  680. public static function setRepairSurplusPerf($userId, $surplusPerf) {
  681. $surplusPerfList = self::getRepairSurplusPerf($userId);
  682. foreach ($surplusPerfList as $key => $perf) {
  683. $thisPerf = $surplusPerf[$key] ?? 0;
  684. $surplusPerfList[$key] = $perf + $thisPerf;
  685. unset($key, $perf, $thisPerf);
  686. }
  687. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  688. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($surplusPerfList));
  689. self::addHasRepairPerfUsers($userId);
  690. unset($userId, $cacheKey, $surplusPerf);
  691. return $surplusPerfList;
  692. }
  693. /**
  694. * 加入有往期结余业绩的会员
  695. * @param $userId
  696. */
  697. public static function addHasRepairPerfUsers($userId) {
  698. // 先从已存在的会员池里面获取
  699. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId);
  700. if (!$isset) {
  701. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId, 1);
  702. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $userId);
  703. }
  704. unset($userId, $isset);
  705. }
  706. /**
  707. * 获取有往期结余业绩的会员
  708. * @param int $offset
  709. * @param int $limit
  710. * @return mixed
  711. */
  712. public static function getHasRepairPerfUsers($offset = 0, $limit = self::LIMIT) {
  713. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $offset, ($offset + $limit - 1));
  714. }
  715. /**
  716. * 修正业绩缓存清空
  717. */
  718. public static function clearRepairAllCache() {
  719. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF);
  720. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER);
  721. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL);
  722. }
  723. /**
  724. * 上一期结余业绩
  725. * @param $userId
  726. * @param $periodNum
  727. * @return array|mixed|\yii\db\ActiveRecord|null
  728. * @throws \yii\db\Exception
  729. */
  730. public static function userPerf($userId, $periodNum) {
  731. $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
  732. $field = $userId;
  733. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  734. if ($cacheValue) {
  735. $value = Json::decode($cacheValue);
  736. } else {
  737. $value = UserPerf::getOneUserPerf($userId);
  738. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  739. }
  740. return $value;
  741. }
  742. /**
  743. * 当前期数的的业绩
  744. * @param $userId
  745. * @param $periodNum
  746. * @param null $perf
  747. * @return array|mixed
  748. */
  749. public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
  750. $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
  751. $field = $userId;
  752. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  753. if ($cacheValue) {
  754. $value = Json::decode($cacheValue);
  755. } else {
  756. $value = [
  757. 'FX_AMOUNT_CASH' => 0,
  758. 'PV_PCS' => 0,
  759. 'PV_PSS' => 0,//本期的团队业绩
  760. 'PV_PCS_ZC' => 0,//注册
  761. 'PV_PCS_FX' => 0,
  762. 'PV_PCS_FX_CASH' => 0,//现金复消
  763. 'PV_PCS_FX_POINT' => 0,//积分复消
  764. 'PV_1L' => 0,
  765. 'PV_1L_TOUCH' => 0,
  766. 'PV_1L_ZC' => 0,
  767. 'PV_1L_FX' => 0,
  768. 'PV_2L' => 0,
  769. 'PV_2L_TOUCH' => 0,
  770. 'PV_2L_ZC' => 0,
  771. 'PV_2L_FX' => 0,
  772. 'PV_3L' => 0,
  773. 'PV_3L_TOUCH' => 0,
  774. 'PV_3L_ZC' => 0,
  775. 'PV_3L_FX' => 0,
  776. 'PV_4L' => 0,
  777. 'PV_4L_TOUCH' => 0,
  778. 'PV_4L_ZC' => 0,
  779. 'PV_4L_FX' => 0,
  780. 'PV_5L' => 0,
  781. 'PV_5L_TOUCH' => 0,
  782. 'PV_5L_ZC' => 0,
  783. 'PV_5L_FX' => 0,
  784. 'SURPLUS_1L' => 0,
  785. 'SURPLUS_2L' => 0,
  786. 'SURPLUS_3L' => 0,
  787. 'SURPLUS_4L' => 0,
  788. 'SURPLUS_5L' => 0,
  789. 'SURPLUS_1L_ZC' => 0,
  790. 'SURPLUS_2L_ZC' => 0,
  791. 'SURPLUS_3L_ZC' => 0,
  792. 'SURPLUS_4L_ZC' => 0,
  793. 'SURPLUS_5L_ZC' => 0,
  794. 'SURPLUS_1L_FX' => 0,
  795. 'SURPLUS_2L_FX' => 0,
  796. 'SURPLUS_3L_FX' => 0,
  797. 'SURPLUS_4L_FX' => 0,
  798. 'SURPLUS_5L_FX' => 0,
  799. ];
  800. }
  801. if ($perf !== null) {
  802. foreach ($perf as $key => $pv) {
  803. if (strpos($key, 'SURPLUS') !== false) {
  804. $value[$key] = $pv;
  805. } else {
  806. $value[$key] = $pv + $value[$key];
  807. }
  808. }
  809. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  810. }
  811. return $value;
  812. }
  813. /**
  814. * 上个月的业绩
  815. * @param $userId
  816. * @param $periodNum
  817. * @return array|mixed|null|\yii\db\ActiveRecord
  818. */
  819. public static function lastMonthPerf($userId, $periodNum) {
  820. $period = Period::instance();
  821. $last = $period->getLastMonth($periodNum);
  822. $lastYearMonth = $last['yearMonth'];
  823. $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
  824. $field = $userId;
  825. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  826. if ($cacheValue) {
  827. $value = Json::decode($cacheValue);
  828. } else {
  829. $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
  830. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  831. }
  832. return $value;
  833. }
  834. /**
  835. * 当前月的业绩
  836. * @param $userId
  837. * @param $periodNum
  838. * @param $perf
  839. * @return array|mixed
  840. */
  841. public static function nowMonthPerf($userId, $periodNum, $perf = null) {
  842. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
  843. $field = $userId;
  844. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  845. if ($cacheValue) {
  846. $value = Json::decode($cacheValue);
  847. } else {
  848. $baseInfo = self::getUserInfo($userId, $periodNum);
  849. $value = [
  850. 'USER_ID' => $userId,
  851. 'FX_AMOUNT_CASH' => 0,
  852. 'PV_PCS' => 0,
  853. 'PV_PCS_FX' => 0,
  854. 'PV_PSS' => 0,
  855. 'PV_1L' => 0,
  856. 'PV_2L' => 0,
  857. 'PV_3L' => 0,
  858. 'PV_4L' => 0,
  859. 'PV_5L' => 0,
  860. 'VIP_PV_1L_ZC' => 0,
  861. 'VIP_PV_2L_ZC' => 0,
  862. 'VIP_PV_3L_ZC' => 0,
  863. 'VIP_PV_4L_ZC' => 0,
  864. 'VIP_PV_5L_ZC' => 0,
  865. 'PV_1L_TOTAL' => 0,
  866. 'PV_2L_TOTAL' => 0,
  867. 'PV_3L_TOTAL' => 0,
  868. 'PV_4L_TOTAL' => 0,
  869. 'PV_5L_TOTAL' => 0,
  870. 'PV_PSS_TOTAL' => 0,
  871. 'DEC_LEVEL' => $baseInfo['DEC_LV'],
  872. 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
  873. ];
  874. }
  875. if ($perf !== null) {
  876. foreach ($perf as $key => $item) {
  877. $value[$key] = $item;
  878. }
  879. }
  880. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  881. return $value;
  882. }
  883. /**
  884. * 获取直推的所有子会员
  885. * @param $userId
  886. * @param $periodNum
  887. * @return array
  888. */
  889. public static function getChildrenOneDeepFromRedis($userId, $periodNum){
  890. $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
  891. $data = Yii::$app->redis->hget($key, $userId);
  892. if(!$data){
  893. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
  894. 'REC_UID' => $userId
  895. ])->asArray()->all();
  896. $data = Json::encode($list);
  897. unset($list);
  898. Yii::$app->redis->hset($key, $userId, $data);
  899. }
  900. unset($key, $userId, $periodNum);
  901. return $data ? Json::decode($data, true) : [];
  902. }
  903. /**
  904. * 服务奖最大比例
  905. * @param $userId
  906. * @param $periodNum
  907. * @param int $percent
  908. * @return float|int
  909. */
  910. public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
  911. $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
  912. $value = Yii::$app->redis->hget($cacheKey, $userId);
  913. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  914. if ( $percent > 0 && $percent > $maxPercent ) {
  915. $maxPercent = $percent;
  916. unset($periodNum, $percent);
  917. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  918. return $maxPercent;
  919. }
  920. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  921. return $maxPercent;
  922. }
  923. /**
  924. * 奖金缓存
  925. * @param $userId
  926. * @param $periodNum
  927. * @param string $bonusType
  928. * @param float $oriBonus
  929. * @param array $deductData
  930. * @return array|mixed
  931. */
  932. public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='') {
  933. $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
  934. $value = [
  935. 'BONUS_TG' => 0,
  936. 'BONUS_QY' => 0,
  937. 'BONUS_BS' => 0,
  938. 'BONUS_QUARTER' => 0,
  939. 'ORI_BONUS_BS' => 0,
  940. 'BONUS_BS_MNT' => 0,
  941. 'BONUS_BS_ABBR' => 0,
  942. 'ORI_BONUS_BS_MNT' => 0,
  943. 'ORI_BONUS_BS_ABBR' => 0,
  944. 'ORI_BONUS_QUARTER' => 0,
  945. 'ORI_BONUS_TG' => 0,
  946. 'ORI_BONUS_QY' => 0,
  947. 'INCOME_TOTAL' => 0,
  948. 'BONUS_TOTAL' => 0,
  949. 'RECONSUME_POINTS' => 0,
  950. 'MANAGE_TAX' => 0,
  951. 'ORI_CAPPED_BONUS_QY' => 0,// 团队奖,封顶前金额
  952. //没有用到的
  953. 'BONUS_FX' => 0,
  954. 'BONUS_HB' => 0,
  955. 'BONUS_BT' => 0,
  956. 'BONUS_BT_PROD' => 0,
  957. 'BONUS_BT_TOOL' => 0,
  958. 'DEDUCT_ZR' => 0,
  959. 'BONUS_FL' => 0,
  960. ];
  961. // 从 redis 中获取当前的结果
  962. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  963. if ($cacheValue) {
  964. $cacheValue = Json::decode($cacheValue);
  965. $value = $cacheValue;
  966. }
  967. unset($cacheValue);
  968. if ($oriBonus > 0) {
  969. $oriBonusType = sprintf('ORI_%s', $bonusType);
  970. $value[$oriBonusType] += $oriBonus;
  971. if( $fromMeans !== '' ) {
  972. $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
  973. $value[$oriBonusMeansType] += $oriBonus;
  974. }
  975. if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  976. if( $deductData ) {
  977. $value[$bonusType] += $deductData['surplus'];
  978. $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  979. $value['MANAGE_TAX'] += $deductData['manageTax'];
  980. }else {
  981. $value[$bonusType] += $oriBonus;
  982. }
  983. $value['BONUS_TOTAL'] += $oriBonus;
  984. }
  985. if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
  986. self::addHasIncomeUsers($userId, $periodNum);
  987. // if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  988. // $value['INCOME_TOTAL'] += $oriBonus;
  989. // }
  990. }
  991. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  992. unset($oriBonusType);
  993. self::addHasBonusUsers($userId, $periodNum);
  994. }
  995. unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
  996. return $value;
  997. }
  998. public static function tourismBonus($userId, $periodNum, $bonus = 0.00) {
  999. $cacheKey = self::REDIS_KEY_PREFIX_TOURISM_BONUS . $periodNum;
  1000. $value = 0.00;
  1001. if( $bonus > 0 ) {
  1002. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1003. $value = $bonus;
  1004. //加入有奖金的会员中
  1005. self::addHasBonusUsers($userId, $periodNum);
  1006. }else {
  1007. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1008. if ($cacheValue) {
  1009. $value = $cacheValue;
  1010. }
  1011. unset($cacheValue);
  1012. }
  1013. unset($cacheKey, $userId, $periodNum, $bonus);
  1014. return $value;
  1015. }
  1016. public static function villaBonus($userId, $periodNum, $bonus = 0.00) {
  1017. $cacheKey = self::REDIS_KEY_PREFIX_VILLA_BONUS . $periodNum;
  1018. $value = 0.00;
  1019. if( $bonus > 0 ) {
  1020. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1021. $value = $bonus;
  1022. //加入有奖金的会员中
  1023. self::addHasBonusUsers($userId, $periodNum);
  1024. }else {
  1025. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1026. if ($cacheValue) {
  1027. $value = $cacheValue;
  1028. }
  1029. unset($cacheValue);
  1030. }
  1031. unset($cacheKey, $userId, $periodNum, $bonus);
  1032. return $value;
  1033. }
  1034. public static function garageBonus($userId, $periodNum, $bonus = 0.00) {
  1035. $cacheKey = self::REDIS_KEY_PREFIX_GARAGE_BONUS . $periodNum;
  1036. $value = 0.00;
  1037. if( $bonus > 0 ) {
  1038. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1039. $value = $bonus;
  1040. //加入有奖金的会员中
  1041. self::addHasBonusUsers($userId, $periodNum);
  1042. }else {
  1043. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1044. if ($cacheValue) {
  1045. $value = $cacheValue;
  1046. }
  1047. unset($cacheValue);
  1048. }
  1049. unset($cacheKey, $userId, $periodNum, $bonus);
  1050. return $value;
  1051. }
  1052. }