CalcCache.php 42 KB

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