CalcCache.php 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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. // if (empty($data)) {
  514. // $userInfo = UserInfo::findUseDbCalc()
  515. // ->select('USER_ID,USER_NAME,CON_UID,REC_UID')
  516. // ->where('USER_ID=:USER_ID', [':USER_ID' => $userId])
  517. // ->asArray()
  518. // ->one();
  519. // if( !$userInfo ) {
  520. // throw new \Exception('用户信息表数据不存在,userId:'.$userId);
  521. // }
  522. //
  523. // $userShopInfo = User::findUseDbCalc()
  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. //
  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. //
  544. // unset($userShopInfo);
  545. //
  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. //
  556. // $data = Json::encode($userInfo);
  557. // Yii::$app->redis->hset($key, $userId, $data);
  558. // unset($key, $userId, $periodNum);
  559. // return $userInfo;
  560. // } else {
  561. return Json::decode($data);
  562. // }
  563. }
  564. public static function setUserInfo($userId, $periodNum, $userInfo) {
  565. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  566. $data = Json::encode($userInfo);
  567. Yii::$app->redis->hset($key, $userId, $data);
  568. unset($userId, $key, $data, $userInfo, $periodNum);
  569. return true;
  570. }
  571. /**
  572. * 本月往期的复消积分的数据
  573. * @param $userId
  574. * @param $periodNum
  575. * @param $calcYearMonth
  576. * @return array|mixed|null|\yii\db\ActiveRecord
  577. * @throws \yii\db\Exception
  578. */
  579. public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
  580. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
  581. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  582. if ($cacheValue) {
  583. $value = Json::decode($cacheValue, true);
  584. } else {
  585. $value = DeductReconsume::findUseDbCalc()
  586. ->select(["RECONSUME_POINTS_SUM"])
  587. ->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [':USER_ID' => $userId, ':CALC_MONTH'=>$calcYearMonth])
  588. ->asArray()
  589. ->one();
  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. $value = UserPerf::getOneUserPerf($userId);
  717. return $value;
  718. $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
  719. $field = $userId;
  720. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  721. if ($cacheValue) {
  722. $value = Json::decode($cacheValue);
  723. } else {
  724. $value = UserPerf::getOneUserPerf($userId);
  725. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  726. }
  727. return $value;
  728. }
  729. /**
  730. * 当前期数的的业绩
  731. * @param $userId
  732. * @param $periodNum
  733. * @param null $perf
  734. * @return array|mixed
  735. */
  736. public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
  737. $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
  738. $field = $userId;
  739. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  740. if ($cacheValue) {
  741. $value = Json::decode($cacheValue);
  742. } else {
  743. $value = [
  744. 'FX_AMOUNT_CASH' => 0,
  745. 'PV_PCS' => 0,
  746. 'PV_PSS' => 0,//本期的团队业绩
  747. 'PV_PCS_ZC' => 0,//注册
  748. 'PV_PCS_FX' => 0,
  749. 'PV_PCS_FX_CASH' => 0,//现金复消
  750. 'PV_PCS_FX_POINT' => 0,//积分复消
  751. 'PV_1L' => 0,
  752. 'PV_1L_TOUCH' => 0,
  753. 'PV_1L_ZC' => 0,
  754. 'PV_1L_FX' => 0,
  755. 'PV_2L' => 0,
  756. 'PV_2L_TOUCH' => 0,
  757. 'PV_2L_ZC' => 0,
  758. 'PV_2L_FX' => 0,
  759. 'PV_3L' => 0,
  760. 'PV_3L_TOUCH' => 0,
  761. 'PV_3L_ZC' => 0,
  762. 'PV_3L_FX' => 0,
  763. 'PV_4L' => 0,
  764. 'PV_4L_TOUCH' => 0,
  765. 'PV_4L_ZC' => 0,
  766. 'PV_4L_FX' => 0,
  767. 'PV_5L' => 0,
  768. 'PV_5L_TOUCH' => 0,
  769. 'PV_5L_ZC' => 0,
  770. 'PV_5L_FX' => 0,
  771. 'SURPLUS_1L' => 0,
  772. 'SURPLUS_2L' => 0,
  773. 'SURPLUS_3L' => 0,
  774. 'SURPLUS_4L' => 0,
  775. 'SURPLUS_5L' => 0,
  776. 'SURPLUS_1L_ZC' => 0,
  777. 'SURPLUS_2L_ZC' => 0,
  778. 'SURPLUS_3L_ZC' => 0,
  779. 'SURPLUS_4L_ZC' => 0,
  780. 'SURPLUS_5L_ZC' => 0,
  781. 'SURPLUS_1L_FX' => 0,
  782. 'SURPLUS_2L_FX' => 0,
  783. 'SURPLUS_3L_FX' => 0,
  784. 'SURPLUS_4L_FX' => 0,
  785. 'SURPLUS_5L_FX' => 0,
  786. ];
  787. }
  788. if ($perf !== null) {
  789. foreach ($perf as $key => $pv) {
  790. if (strpos($key, 'SURPLUS') !== false) {
  791. $value[$key] = $pv;
  792. } else {
  793. $value[$key] = $pv + $value[$key];
  794. }
  795. }
  796. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  797. }
  798. return $value;
  799. }
  800. /**
  801. * 上个月的业绩
  802. * @param $userId
  803. * @param $periodNum
  804. * @return array|mixed|null|\yii\db\ActiveRecord
  805. */
  806. public static function lastMonthPerf($userId, $periodNum) {
  807. $period = Period::instance();
  808. $last = $period->getLastMonth($periodNum);
  809. $lastYearMonth = $last['yearMonth'];
  810. $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
  811. $field = $userId;
  812. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  813. if ($cacheValue) {
  814. $value = Json::decode($cacheValue);
  815. } else {
  816. $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
  817. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  818. }
  819. return $value;
  820. }
  821. /**
  822. * 当前月的业绩
  823. * @param $userId
  824. * @param $periodNum
  825. * @param $perf
  826. * @return array|mixed
  827. */
  828. public static function nowMonthPerf($userId, $periodNum, $perf = null) {
  829. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
  830. $field = $userId;
  831. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  832. if ($cacheValue) {
  833. $value = Json::decode($cacheValue);
  834. } else {
  835. $baseInfo = self::getUserInfo($userId, $periodNum);
  836. $value = [
  837. 'USER_ID' => $userId,
  838. 'FX_AMOUNT_CASH' => 0,
  839. 'PV_PCS' => 0,
  840. 'PV_PCS_FX' => 0,
  841. 'PV_PSS' => 0,
  842. 'PV_1L' => 0,
  843. 'PV_2L' => 0,
  844. 'PV_3L' => 0,
  845. 'PV_4L' => 0,
  846. 'PV_5L' => 0,
  847. 'VIP_PV_1L_ZC' => 0,
  848. 'VIP_PV_2L_ZC' => 0,
  849. 'VIP_PV_3L_ZC' => 0,
  850. 'VIP_PV_4L_ZC' => 0,
  851. 'VIP_PV_5L_ZC' => 0,
  852. 'PV_1L_TOTAL' => 0,
  853. 'PV_2L_TOTAL' => 0,
  854. 'PV_3L_TOTAL' => 0,
  855. 'PV_4L_TOTAL' => 0,
  856. 'PV_5L_TOTAL' => 0,
  857. 'PV_PSS_TOTAL' => 0,
  858. 'DEC_LEVEL' => $baseInfo['DEC_LV'],
  859. 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
  860. ];
  861. }
  862. if ($perf !== null) {
  863. foreach ($perf as $key => $item) {
  864. $value[$key] = $item;
  865. }
  866. }
  867. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  868. return $value;
  869. }
  870. /**
  871. * 获取直推的所有子会员
  872. * @param $userId
  873. * @param $periodNum
  874. * @return array
  875. */
  876. public static function getChildrenOneDeepFromRedis($userId, $periodNum){
  877. $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
  878. $data = Yii::$app->redis->hget($key, $userId);
  879. if(!$data){
  880. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
  881. 'REC_UID' => $userId
  882. ])->asArray()->all();
  883. $data = Json::encode($list);
  884. unset($list);
  885. Yii::$app->redis->hset($key, $userId, $data);
  886. }
  887. unset($key, $userId, $periodNum);
  888. return $data ? Json::decode($data, true) : [];
  889. }
  890. /**
  891. * 服务奖最大比例
  892. * @param $userId
  893. * @param $periodNum
  894. * @param int $percent
  895. * @return float|int
  896. */
  897. public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
  898. $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
  899. $value = Yii::$app->redis->hget($cacheKey, $userId);
  900. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  901. if ( $percent > 0 && $percent > $maxPercent ) {
  902. $maxPercent = $percent;
  903. unset($periodNum, $percent);
  904. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  905. return $maxPercent;
  906. }
  907. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  908. return $maxPercent;
  909. }
  910. /**
  911. * 奖金缓存
  912. * @param $userId
  913. * @param $periodNum
  914. * @param string $bonusType
  915. * @param float $oriBonus
  916. * @param array $deductData
  917. * @return array|mixed
  918. */
  919. public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='') {
  920. $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
  921. $value = [
  922. 'BONUS_TG' => 0,
  923. 'BONUS_QY' => 0,
  924. 'BONUS_BS' => 0,
  925. 'BONUS_QUARTER' => 0,
  926. 'BONUS_BD' => 0,
  927. 'ORI_BONUS_BS' => 0,
  928. 'ORI_BONUS_BD' => 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. // 从 redis 中获取当前的结果
  939. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  940. if ($cacheValue) {
  941. $cacheValue = Json::decode($cacheValue);
  942. $value = $cacheValue;
  943. }
  944. unset($cacheValue);
  945. if ($oriBonus > 0) {
  946. $oriBonusType = sprintf('ORI_%s', $bonusType);
  947. $value[$oriBonusType] += $oriBonus;
  948. if( $fromMeans !== '' ) {
  949. $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
  950. $value[$oriBonusMeansType] += $oriBonus;
  951. }
  952. if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  953. if( $deductData ) {
  954. $value[$bonusType] += $deductData['surplus'];
  955. $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  956. $value['MANAGE_TAX'] += $deductData['manageTax'];
  957. }else {
  958. $value[$bonusType] += $oriBonus;
  959. }
  960. $value['BONUS_TOTAL'] += $oriBonus;
  961. }
  962. if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
  963. self::addHasIncomeUsers($userId, $periodNum);
  964. // if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  965. // $value['INCOME_TOTAL'] += $oriBonus;
  966. // }
  967. }
  968. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  969. unset($oriBonusType);
  970. self::addHasBonusUsers($userId, $periodNum);
  971. }
  972. unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
  973. return $value;
  974. }
  975. public static function tourismBonus($userId, $periodNum, $bonus = 0.00) {
  976. $cacheKey = self::REDIS_KEY_PREFIX_TOURISM_BONUS . $periodNum;
  977. $value = 0.00;
  978. if( $bonus > 0 ) {
  979. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  980. $value = $bonus;
  981. //加入有奖金的会员中
  982. self::addHasBonusUsers($userId, $periodNum);
  983. }else {
  984. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  985. if ($cacheValue) {
  986. $value = $cacheValue;
  987. }
  988. unset($cacheValue);
  989. }
  990. unset($cacheKey, $userId, $periodNum, $bonus);
  991. return $value;
  992. }
  993. public static function villaBonus($userId, $periodNum, $bonus = 0.00) {
  994. $cacheKey = self::REDIS_KEY_PREFIX_VILLA_BONUS . $periodNum;
  995. $value = 0.00;
  996. if( $bonus > 0 ) {
  997. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  998. $value = $bonus;
  999. //加入有奖金的会员中
  1000. self::addHasBonusUsers($userId, $periodNum);
  1001. }else {
  1002. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1003. if ($cacheValue) {
  1004. $value = $cacheValue;
  1005. }
  1006. unset($cacheValue);
  1007. }
  1008. unset($cacheKey, $userId, $periodNum, $bonus);
  1009. return $value;
  1010. }
  1011. public static function garageBonus($userId, $periodNum, $bonus = 0.00) {
  1012. $cacheKey = self::REDIS_KEY_PREFIX_GARAGE_BONUS . $periodNum;
  1013. $value = 0.00;
  1014. if( $bonus > 0 ) {
  1015. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1016. $value = $bonus;
  1017. //加入有奖金的会员中
  1018. self::addHasBonusUsers($userId, $periodNum);
  1019. }else {
  1020. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1021. if ($cacheValue) {
  1022. $value = $cacheValue;
  1023. }
  1024. unset($cacheValue);
  1025. }
  1026. unset($cacheKey, $userId, $periodNum, $bonus);
  1027. return $value;
  1028. }
  1029. }