CalcCache.php 42 KB

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