CalcCache.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/8/2
  6. * Time: 上午10:38
  7. */
  8. namespace common\helpers\bonus;
  9. use common\helpers\Cache;
  10. use common\models\CalcBonus;
  11. use common\models\CalcBonusBS;
  12. use common\models\PerfMonth;
  13. use common\models\Period;
  14. use common\models\DeclarationLevel;
  15. use common\models\DecRole;
  16. use common\models\EmployLevel;
  17. use common\models\StarCrownLevel;
  18. use common\models\User;
  19. use common\models\UserBonus;
  20. use common\models\UserPerf;
  21. use Yii;
  22. use common\models\UserInfo;
  23. use yii\helpers\Json;
  24. class CalcCache {
  25. const LIMIT = 10000;
  26. const REDIS_KEY_PREFIX_USER = 'calc:user_';
  27. const REDIS_KEY_PREFIX_USER_ACTIVE = 'calc:userActive_';
  28. const REDIS_KEY_PREFIX_USER_INFO = 'calc:userInfo_';
  29. const REDIS_KEY_PREFIX_USER_BONUS = 'calc:userBonus_';
  30. const REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS = 'calc:periodMonthCalcBonus_';
  31. const REDIS_KEY_PREFIX_USER_PERF = 'calc:userPerf_';
  32. const REDIS_KEY_PREFIX_SURPLUS_PERF = 'calc:spPerf_';
  33. const REDIS_KEY_PREFIX_NOW_PERIOD_PERF = 'calc:nowPeriodPerf_';
  34. const REDIS_KEY_PREFIX_LAST_MONTH_PERF = 'calc:lastMonthPerf_';
  35. const REDIS_KEY_PREFIX_NOW_MONTH_PERF = 'calc:nowMonthPerf_';
  36. const REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS = 'calc:nowMonthLastPeriodReconsumePoints_';
  37. const REDIS_KEY_PREFIX_EMP_NUM_PERF = 'calc:empLevelNum_';
  38. const REDIS_KEY_PREFIX_BONUS = 'calc:bonus_';
  39. const REDIS_KEY_PREFIX_FW_BONUS = 'calc:fw:bonus_';
  40. const REDIS_KEY_PREFIX_TOURISM_BONUS = 'calc:tourism:bonus_';
  41. const REDIS_KEY_PREFIX_VILLA_BONUS = 'calc:villa:bonus_';
  42. const REDIS_KEY_PREFIX_GARAGE_BONUS = 'calc:garage:bonus_';
  43. const REDIS_KEY_PREFIX_HAS_PERF_USER = 'calc:hasPerfUser_';
  44. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER = 'calc:hasMonthPerfUser_';
  45. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER = 'calc:hasStandardMonthPerfUser_';
  46. const REDIS_KEY_PREFIX_HAS_BD_USER = 'calc:hasBDUser_';
  47. const REDIS_KEY_PREFIX_HAS_INCOME_USER = 'calc:hasIncomeUser_';
  48. const REDIS_KEY_PREFIX_HAS_BONUS_USER = 'calc:hasBonusUser_';
  49. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER = 'calc:hasFwBonusUser_';
  50. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER = 'calc:hasMonthBonusUser_';
  51. const REDIS_KEY_PREFIX_HAS_PERF_USER_POOL = 'calc:hasPerfUserPool_';
  52. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL = 'calc:hasMonthPerfUserPool_';
  53. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL = 'calc:hasStandardMonthPerfUserPool_';
  54. const REDIS_KEY_PREFIX_HAS_BD_USER_POOL = 'calc:hasBDUserPool_';
  55. const REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL = 'calc:hasIncomeUserPool_';
  56. const REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL = 'calc:hasBonusUserPool_';
  57. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL = 'calc:hasFwBonusUserPool_';
  58. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL = 'calc:hasMonthBonusUserPool_';
  59. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER = 'calc:hasLSPCSUser_';
  60. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL = 'calc:hasLSPCSUserPool_';
  61. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER = 'calc:hasCFPercentPCSUser_';
  62. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL = 'calc:hasCFPercentPCSUserPool_';
  63. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER = 'calc:hasLXPercentPCSUser_';
  64. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL = 'calc:hasLXPercentPCSUserPool_';
  65. const REDIS_KEY_PREFIX_DEC_ROLE_CONFIG = 'calc:decRoleConfig_';
  66. const REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP = 'calc:userInfo:ChildOneDeep_';
  67. const REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA = 'calc:fwBonusListData_';
  68. const REDIS_KEY_PREFIX_BONUS_CROWN_CROWN_LIST_DATA = 'calc:bonusStarCrownListData_';
  69. //@todo
  70. const REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF = 'calc:repairSurplusPerf_';
  71. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER = 'calc:hasRepairPerfUser_';
  72. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL = 'calc:hasRepairPerfPool_';
  73. const INCOME_QY_BONUS_BD = 'BONUS_QY_BD';
  74. const INCOME_QY_BONUS_FX = 'BONUS_QY_FX';
  75. const INCOME_BONUS_TRAVEL = 'BONUS_TRAVEL';
  76. const INCOME_BONUS_CAR = 'BONUS_CAR';
  77. const INCOME_BONUS_HOUSE = 'BONUS_HOUSE';
  78. const CAPPED_BONUS_QY = 'CAPPED_BONUS_QY'; // 团队奖封顶前的金额
  79. const INCOME_BONUS_LIST = [
  80. self::INCOME_QY_BONUS_BD,
  81. ];
  82. const NOT_SEND_BONUS_LIST = [
  83. self::INCOME_QY_BONUS_BD,
  84. self::INCOME_QY_BONUS_FX,
  85. self::INCOME_BONUS_TRAVEL,
  86. self::INCOME_BONUS_CAR,
  87. self::INCOME_BONUS_HOUSE,
  88. self::CAPPED_BONUS_QY,
  89. ];
  90. const FROM_MEANS_BD = 'BD';
  91. /**
  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. $bsEmpLv = Period::userLastEmpLv($userId, $periodNum);
  535. // 获取用户的最新级别数据,根据蓝星最近一期月结数据进行查询
  536. $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
  537. $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
  538. $userInfo['EMP_LV'] = $userShopInfo['EMP_LV'];
  539. $userInfo['LAST_EMP_LV'] = $bsEmpLv; // 最新的聘级
  540. $userInfo['CROWN_LV'] = $userShopInfo['CROWN_LV'];
  541. $userInfo['IS_DEC'] = $userShopInfo['IS_DEC'];
  542. $userInfo['STATUS'] = $userShopInfo['STATUS'];
  543. $userInfo['DEC_ID'] = $userShopInfo['DEC_ID'];
  544. unset($userShopInfo);
  545. if (!$userInfo['DEC_LV']) {
  546. $userInfo['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  547. }
  548. if (!$userInfo['EMP_LV']) {
  549. $userInfo['EMP_LV'] = EmployLevel::getDefaultLevelId();
  550. }
  551. if (!$userInfo['CROWN_LV']) {
  552. $userInfo['CROWN_LV'] = StarCrownLevel::getDefaultLevelId();
  553. }
  554. $data = Json::encode($userInfo);
  555. Yii::$app->redis->hset($key, $userId, $data);
  556. unset($userInfo, $key, $userId, $periodNum);
  557. } else {
  558. if ($data) {
  559. $ret = Json::decode($data, true);
  560. $bsEmpLv = Period::userLastEmpLv($userId, $periodNum);
  561. $ret['LAST_EMP_LV'] = $bsEmpLv; // 最新的聘级
  562. return $ret;
  563. } else {
  564. return [];
  565. }
  566. }
  567. }
  568. public static function setUserInfo($userId, $periodNum, $userInfo) {
  569. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  570. $data = Json::encode($userInfo);
  571. Yii::$app->redis->hset($key, $userId, $data);
  572. unset($userId, $key, $data, $userInfo, $periodNum);
  573. return true;
  574. }
  575. /**
  576. * 本月往期的复消积分的数据
  577. * @param $userId
  578. * @param $periodNum
  579. * @param $calcYearMonth
  580. * @return array|mixed|null|\yii\db\ActiveRecord
  581. * @throws \yii\db\Exception
  582. */
  583. public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
  584. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
  585. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  586. if ($cacheValue) {
  587. $value = Json::decode($cacheValue, true);
  588. } else {
  589. $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();
  590. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  591. }
  592. return $value;
  593. }
  594. /**
  595. * 获取结算时这一期的报单中心级别的配置
  596. * @param $periodNum
  597. * @return array|\yii\db\ActiveRecord[]
  598. */
  599. public static function getDecRoleConfig($periodNum) {
  600. $key = self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum;
  601. Yii::$app->redis->del($key);
  602. $data = Yii::$app->redis->get($key);
  603. if (!$data) {
  604. $data = DecRole::find()->where('1=1')->indexBy('ID')->asArray()->all();
  605. $data = Json::encode($data);
  606. Yii::$app->redis->set($key, $data);
  607. }
  608. return $data ? Json::decode($data) : [];
  609. }
  610. /**
  611. * 上一期结余业绩
  612. * @param $userId
  613. * @param $periodNum
  614. * @return array|mixed|\yii\db\ActiveRecord|null
  615. * @throws \yii\db\Exception
  616. */
  617. public static function surplusPerf($userId, $periodNum) {
  618. $userPerf = self::userPerf($userId, $periodNum);
  619. return [
  620. 'SURPLUS_1L' => $userPerf['SURPLUS_1L'],
  621. 'SURPLUS_2L' => $userPerf['SURPLUS_2L'],
  622. 'SURPLUS_3L' => $userPerf['SURPLUS_3L'],
  623. 'SURPLUS_4L' => $userPerf['SURPLUS_4L'],
  624. 'SURPLUS_5L' => $userPerf['SURPLUS_5L'],
  625. 'SURPLUS_1L_ZC' => $userPerf['SURPLUS_1L_ZC'],
  626. 'SURPLUS_2L_ZC' => $userPerf['SURPLUS_2L_ZC'],
  627. 'SURPLUS_3L_ZC' => $userPerf['SURPLUS_3L_ZC'],
  628. 'SURPLUS_4L_ZC' => $userPerf['SURPLUS_4L_ZC'],
  629. 'SURPLUS_5L_ZC' => $userPerf['SURPLUS_5L_ZC'],
  630. 'SURPLUS_1L_FX' => $userPerf['SURPLUS_1L_FX'],
  631. 'SURPLUS_2L_FX' => $userPerf['SURPLUS_2L_FX'],
  632. 'SURPLUS_3L_FX' => $userPerf['SURPLUS_3L_FX'],
  633. 'SURPLUS_4L_FX' => $userPerf['SURPLUS_4L_FX'],
  634. 'SURPLUS_5L_FX' => $userPerf['SURPLUS_5L_FX'],
  635. ];
  636. }
  637. /**
  638. * 获取repairSurplusPerf
  639. * @param $userId
  640. * @return int[]|mixed|null
  641. */
  642. public static function getRepairSurplusPerf($userId) {
  643. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  644. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  645. if ($cacheValue) {
  646. $value = Json::decode($cacheValue);
  647. }else {
  648. $value = [
  649. 'SURPLUS_1L' => 0,
  650. 'SURPLUS_2L' => 0,
  651. 'SURPLUS_3L' => 0,
  652. 'SURPLUS_4L' => 0,
  653. 'SURPLUS_5L' => 0,
  654. ];
  655. }
  656. return $value;
  657. }
  658. /**
  659. * 累加repairSurplusPerf
  660. * @param $userId
  661. * @param $surplusPerf
  662. * @return int[]|mixed|null
  663. */
  664. public static function setRepairSurplusPerf($userId, $surplusPerf) {
  665. $surplusPerfList = self::getRepairSurplusPerf($userId);
  666. foreach ($surplusPerfList as $key => $perf) {
  667. $thisPerf = $surplusPerf[$key] ?? 0;
  668. $surplusPerfList[$key] = $perf + $thisPerf;
  669. unset($key, $perf, $thisPerf);
  670. }
  671. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  672. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($surplusPerfList));
  673. self::addHasRepairPerfUsers($userId);
  674. unset($userId, $cacheKey, $surplusPerf);
  675. return $surplusPerfList;
  676. }
  677. /**
  678. * 加入有往期结余业绩的会员
  679. * @param $userId
  680. */
  681. public static function addHasRepairPerfUsers($userId) {
  682. // 先从已存在的会员池里面获取
  683. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId);
  684. if (!$isset) {
  685. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId, 1);
  686. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $userId);
  687. }
  688. unset($userId, $isset);
  689. }
  690. /**
  691. * 获取有往期结余业绩的会员
  692. * @param int $offset
  693. * @param int $limit
  694. * @return mixed
  695. */
  696. public static function getHasRepairPerfUsers($offset = 0, $limit = self::LIMIT) {
  697. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $offset, ($offset + $limit - 1));
  698. }
  699. /**
  700. * 修正业绩缓存清空
  701. */
  702. public static function clearRepairAllCache() {
  703. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF);
  704. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER);
  705. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL);
  706. }
  707. /**
  708. * 上一期结余业绩
  709. * @param $userId
  710. * @param $periodNum
  711. * @return array|mixed|\yii\db\ActiveRecord|null
  712. * @throws \yii\db\Exception
  713. */
  714. public static function userPerf($userId, $periodNum) {
  715. $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
  716. $field = $userId;
  717. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  718. if ($cacheValue) {
  719. $value = Json::decode($cacheValue);
  720. } else {
  721. $value = UserPerf::getOneUserPerf($userId);
  722. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  723. }
  724. return $value;
  725. }
  726. /**
  727. * 当前期数的的业绩
  728. * @param $userId
  729. * @param $periodNum
  730. * @param null $perf
  731. * @return array|mixed
  732. */
  733. public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
  734. $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
  735. $field = $userId;
  736. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  737. if ($cacheValue) {
  738. $value = Json::decode($cacheValue);
  739. } else {
  740. $value = [
  741. 'FX_AMOUNT_CASH' => 0,
  742. 'PV_PCS' => 0,
  743. 'PV_PSS' => 0,//本期的团队业绩
  744. 'PV_PCS_ZC' => 0,//注册
  745. 'PV_PCS_FX' => 0,
  746. 'PV_PCS_FX_CASH' => 0,//现金复消
  747. 'PV_PCS_FX_POINT' => 0,//积分复消
  748. 'PV_1L' => 0,
  749. 'PV_1L_TOUCH' => 0,
  750. 'PV_1L_ZC' => 0,
  751. 'PV_1L_FX' => 0,
  752. 'PV_2L' => 0,
  753. 'PV_2L_TOUCH' => 0,
  754. 'PV_2L_ZC' => 0,
  755. 'PV_2L_FX' => 0,
  756. 'PV_3L' => 0,
  757. 'PV_3L_TOUCH' => 0,
  758. 'PV_3L_ZC' => 0,
  759. 'PV_3L_FX' => 0,
  760. 'PV_4L' => 0,
  761. 'PV_4L_TOUCH' => 0,
  762. 'PV_4L_ZC' => 0,
  763. 'PV_4L_FX' => 0,
  764. 'PV_5L' => 0,
  765. 'PV_5L_TOUCH' => 0,
  766. 'PV_5L_ZC' => 0,
  767. 'PV_5L_FX' => 0,
  768. 'SURPLUS_1L' => 0,
  769. 'SURPLUS_2L' => 0,
  770. 'SURPLUS_3L' => 0,
  771. 'SURPLUS_4L' => 0,
  772. 'SURPLUS_5L' => 0,
  773. 'SURPLUS_1L_ZC' => 0,
  774. 'SURPLUS_2L_ZC' => 0,
  775. 'SURPLUS_3L_ZC' => 0,
  776. 'SURPLUS_4L_ZC' => 0,
  777. 'SURPLUS_5L_ZC' => 0,
  778. 'SURPLUS_1L_FX' => 0,
  779. 'SURPLUS_2L_FX' => 0,
  780. 'SURPLUS_3L_FX' => 0,
  781. 'SURPLUS_4L_FX' => 0,
  782. 'SURPLUS_5L_FX' => 0,
  783. ];
  784. }
  785. if ($perf !== null) {
  786. foreach ($perf as $key => $pv) {
  787. if (strpos($key, 'SURPLUS') !== false) {
  788. $value[$key] = $pv;
  789. } else {
  790. $value[$key] = $pv + $value[$key];
  791. }
  792. }
  793. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  794. }
  795. return $value;
  796. }
  797. /**
  798. * 上个月的业绩
  799. * @param $userId
  800. * @param $periodNum
  801. * @return array|mixed|null|\yii\db\ActiveRecord
  802. */
  803. public static function lastMonthPerf($userId, $periodNum) {
  804. $period = Period::instance();
  805. $last = $period->getLastMonth($periodNum);
  806. $lastYearMonth = $last['yearMonth'];
  807. $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
  808. $field = $userId;
  809. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  810. if ($cacheValue) {
  811. $value = Json::decode($cacheValue);
  812. } else {
  813. $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
  814. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  815. }
  816. return $value;
  817. }
  818. /**
  819. * 当前月的业绩
  820. * @param $userId
  821. * @param $periodNum
  822. * @param $perf
  823. * @return array|mixed
  824. */
  825. public static function nowMonthPerf($userId, $periodNum, $perf = null) {
  826. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
  827. $field = $userId;
  828. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  829. if ($cacheValue) {
  830. $value = Json::decode($cacheValue);
  831. } else {
  832. $baseInfo = self::getUserInfo($userId, $periodNum);
  833. $value = [
  834. 'USER_ID' => $userId,
  835. 'FX_AMOUNT_CASH' => 0,
  836. 'PV_PCS' => 0,
  837. 'PV_PCS_FX' => 0,
  838. 'PV_PSS' => 0,
  839. 'PV_1L' => 0,
  840. 'PV_2L' => 0,
  841. 'PV_3L' => 0,
  842. 'PV_4L' => 0,
  843. 'PV_5L' => 0,
  844. 'VIP_PV_1L_ZC' => 0,
  845. 'VIP_PV_2L_ZC' => 0,
  846. 'VIP_PV_3L_ZC' => 0,
  847. 'VIP_PV_4L_ZC' => 0,
  848. 'VIP_PV_5L_ZC' => 0,
  849. 'PV_1L_TOTAL' => 0,
  850. 'PV_2L_TOTAL' => 0,
  851. 'PV_3L_TOTAL' => 0,
  852. 'PV_4L_TOTAL' => 0,
  853. 'PV_5L_TOTAL' => 0,
  854. 'PV_PSS_TOTAL' => 0,
  855. 'DEC_LEVEL' => $baseInfo['DEC_LV'],
  856. 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
  857. ];
  858. }
  859. if ($perf !== null) {
  860. foreach ($perf as $key => $item) {
  861. $value[$key] = $item;
  862. }
  863. }
  864. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  865. return $value;
  866. }
  867. /**
  868. * 获取直推的所有子会员
  869. * @param $userId
  870. * @param $periodNum
  871. * @return array
  872. */
  873. public static function getChildrenOneDeepFromRedis($userId, $periodNum){
  874. $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
  875. $data = Yii::$app->redis->hget($key, $userId);
  876. if(!$data){
  877. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
  878. 'REC_UID' => $userId
  879. ])->asArray()->all();
  880. $data = Json::encode($list);
  881. unset($list);
  882. Yii::$app->redis->hset($key, $userId, $data);
  883. }
  884. unset($key, $userId, $periodNum);
  885. return $data ? Json::decode($data, true) : [];
  886. }
  887. /**
  888. * 服务奖最大比例
  889. * @param $userId
  890. * @param $periodNum
  891. * @param int $percent
  892. * @return float|int
  893. */
  894. public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
  895. $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
  896. $value = Yii::$app->redis->hget($cacheKey, $userId);
  897. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  898. if ( $percent > 0 && $percent > $maxPercent ) {
  899. $maxPercent = $percent;
  900. unset($periodNum, $percent);
  901. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  902. return $maxPercent;
  903. }
  904. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  905. return $maxPercent;
  906. }
  907. /**
  908. * 奖金缓存
  909. * @param $userId
  910. * @param $periodNum
  911. * @param string $bonusType
  912. * @param float $oriBonus
  913. * @param array $deductData
  914. * @return array|mixed
  915. */
  916. public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='') {
  917. $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
  918. $value = [
  919. 'BONUS_TG' => 0,
  920. 'BONUS_QY' => 0,
  921. 'BONUS_BS' => 0,
  922. 'BONUS_QUARTER' => 0,
  923. 'ORI_BONUS_BS' => 0,
  924. 'BONUS_BS_MNT' => 0,
  925. 'BONUS_BS_ABBR' => 0,
  926. 'ORI_BONUS_BS_MNT' => 0,
  927. 'ORI_BONUS_BS_ABBR' => 0,
  928. 'ORI_BONUS_QUARTER' => 0,
  929. 'ORI_BONUS_TG' => 0,
  930. 'ORI_BONUS_QY' => 0,
  931. 'INCOME_TOTAL' => 0,
  932. 'BONUS_TOTAL' => 0,
  933. 'RECONSUME_POINTS' => 0,
  934. 'MANAGE_TAX' => 0,
  935. 'ORI_CAPPED_BONUS_QY' => 0,// 团队奖,封顶前金额
  936. //没有用到的
  937. 'BONUS_FX' => 0,
  938. 'BONUS_HB' => 0,
  939. 'BONUS_BT' => 0,
  940. 'BONUS_BT_PROD' => 0,
  941. 'BONUS_BT_TOOL' => 0,
  942. 'DEDUCT_ZR' => 0,
  943. 'BONUS_FL' => 0,
  944. ];
  945. // 从 redis 中获取当前的结果
  946. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  947. if ($cacheValue) {
  948. $cacheValue = Json::decode($cacheValue);
  949. $value = $cacheValue;
  950. }
  951. unset($cacheValue);
  952. if ($oriBonus > 0) {
  953. $oriBonusType = sprintf('ORI_%s', $bonusType);
  954. $value[$oriBonusType] += $oriBonus;
  955. if( $fromMeans !== '' ) {
  956. $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
  957. $value[$oriBonusMeansType] += $oriBonus;
  958. }
  959. if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  960. if( $deductData ) {
  961. $value[$bonusType] += $deductData['surplus'];
  962. $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  963. $value['MANAGE_TAX'] += $deductData['manageTax'];
  964. }else {
  965. $value[$bonusType] += $oriBonus;
  966. }
  967. $value['BONUS_TOTAL'] += $oriBonus;
  968. }
  969. if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
  970. self::addHasIncomeUsers($userId, $periodNum);
  971. // if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  972. // $value['INCOME_TOTAL'] += $oriBonus;
  973. // }
  974. }
  975. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  976. unset($oriBonusType);
  977. self::addHasBonusUsers($userId, $periodNum);
  978. }
  979. unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
  980. return $value;
  981. }
  982. public static function tourismBonus($userId, $periodNum, $bonus = 0.00) {
  983. $cacheKey = self::REDIS_KEY_PREFIX_TOURISM_BONUS . $periodNum;
  984. $value = 0.00;
  985. if( $bonus > 0 ) {
  986. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  987. $value = $bonus;
  988. //加入有奖金的会员中
  989. self::addHasBonusUsers($userId, $periodNum);
  990. }else {
  991. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  992. if ($cacheValue) {
  993. $value = $cacheValue;
  994. }
  995. unset($cacheValue);
  996. }
  997. unset($cacheKey, $userId, $periodNum, $bonus);
  998. return $value;
  999. }
  1000. public static function villaBonus($userId, $periodNum, $bonus = 0.00) {
  1001. $cacheKey = self::REDIS_KEY_PREFIX_VILLA_BONUS . $periodNum;
  1002. $value = 0.00;
  1003. if( $bonus > 0 ) {
  1004. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1005. $value = $bonus;
  1006. //加入有奖金的会员中
  1007. self::addHasBonusUsers($userId, $periodNum);
  1008. }else {
  1009. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1010. if ($cacheValue) {
  1011. $value = $cacheValue;
  1012. }
  1013. unset($cacheValue);
  1014. }
  1015. unset($cacheKey, $userId, $periodNum, $bonus);
  1016. return $value;
  1017. }
  1018. public static function garageBonus($userId, $periodNum, $bonus = 0.00) {
  1019. $cacheKey = self::REDIS_KEY_PREFIX_GARAGE_BONUS . $periodNum;
  1020. $value = 0.00;
  1021. if( $bonus > 0 ) {
  1022. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1023. $value = $bonus;
  1024. //加入有奖金的会员中
  1025. self::addHasBonusUsers($userId, $periodNum);
  1026. }else {
  1027. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1028. if ($cacheValue) {
  1029. $value = $cacheValue;
  1030. }
  1031. unset($cacheValue);
  1032. }
  1033. unset($cacheKey, $userId, $periodNum, $bonus);
  1034. return $value;
  1035. }
  1036. }