CalcCache.php 39 KB

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