CalcCache.php 42 KB

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