CalcCache.php 42 KB

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