CalcCache.php 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744
  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\components\Redis;
  10. use common\helpers\Cache;
  11. use common\helpers\Date;
  12. use common\helpers\user\Info;
  13. use common\models\CalcBonus;
  14. use common\models\PerfMonth;
  15. use common\models\PerfPeriod;
  16. use common\models\Period;
  17. use common\models\DeclarationLevel;
  18. use common\models\DecRole;
  19. use common\models\EmployLevel;
  20. use common\models\StarCrownLevel;
  21. use common\models\User;
  22. use common\models\UserBonus;
  23. use common\models\UserNetwork;
  24. use common\models\UserPerf;
  25. use common\models\UserRelation;
  26. use common\models\RemainPv;
  27. use common\models\Order;
  28. use common\models\forms\OrderForm;
  29. use Yii;
  30. use common\models\UserInfo;
  31. use yii\helpers\Json;
  32. class CalcCache {
  33. const LIMIT = 1000;
  34. const REDIS_KEY_PREFIX_USER = 'calc:user_';
  35. const REDIS_KEY_PREFIX_USER_ACTIVE = 'calc:userActive_';
  36. const REDIS_KEY_PREFIX_USER_INFO = 'calc:userInfo_';
  37. const REDIS_KEY_PREFIX_USER_BONUS = 'calc:userBonus_';
  38. const REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS = 'calc:periodMonthCalcBonus_';
  39. const REDIS_KEY_PREFIX_USER_PERF = 'calc:userPerf_';
  40. const REDIS_KEY_PREFIX_SURPLUS_PERF = 'calc:spPerf_';
  41. const REDIS_KEY_PREFIX_NOW_PERIOD_PERF = 'calc:nowPeriodPerf_';
  42. const REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF = 'calc:nowStandardMonthPerf_';
  43. const REDIS_KEY_PREFIX_LAST_MONTH_PERF = 'calc:lastMonthPerf_';
  44. const REDIS_KEY_PREFIX_NOW_MONTH_PERF = 'calc:nowMonthPerf_';
  45. const REDIS_KEY_PREFIX_NOW_MONTH_SCORE = 'calc:nowMonthScore_';
  46. const REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS = 'calc:nowMonthLastPeriodReconsumePoints_';
  47. const REDIS_KEY_PREFIX_EMP_NUM_PERF = 'calc:empLevelNum_';
  48. const REDIS_KEY_PREFIX_BONUS = 'calc:bonus_';
  49. const REDIS_KEY_PREFIX_STANDARD_BONUS = 'calc:standard:bonus_';
  50. const REDIS_KEY_PREFIX_YC_BONUS = 'calc:yc:bonus_';
  51. const REDIS_KEY_PREFIX_FW_BONUS = 'calc:fw:bonus_';
  52. const REDIS_KEY_PREFIX_TOURISM_BONUS = 'calc:tourism:bonus_';
  53. const REDIS_KEY_PREFIX_GARAGE_BONUS = 'calc:garage:bonus_';
  54. const REDIS_KEY_PREFIX_HAS_PERF_USER = 'calc:hasPerfUser_';
  55. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER = 'calc:hasMonthPerfUser_';
  56. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER = 'calc:hasStandardMonthPerfUser_';
  57. const REDIS_KEY_PREFIX_HAS_BD_USER = 'calc:hasBDUser_';
  58. const REDIS_KEY_PREFIX_HAS_INCOME_USER = 'calc:hasIncomeUser_';
  59. const REDIS_KEY_PREFIX_HAS_BONUS_USER = 'calc:hasBonusUser_';
  60. const REDIS_KEY_PREFIX_HAS_YC_BONUS_USER = 'calc:hasYcBonusUser_';
  61. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER = 'calc:hasFwBonusUser_';
  62. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER = 'calc:hasMonthBonusUser_';
  63. const REDIS_KEY_PREFIX_HAS_PERF_USER_POOL = 'calc:hasPerfUserPool_';
  64. const REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL = 'calc:hasMonthPerfUserPool_';
  65. const REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL = 'calc:hasStandardMonthPerfUserPool_';
  66. const REDIS_KEY_PREFIX_HAS_BD_USER_POOL = 'calc:hasBDUserPool_';
  67. const REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL = 'calc:hasIncomeUserPool_';
  68. const REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL = 'calc:hasBonusUserPool_';
  69. const REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL = 'calc:hasYcBonusUserPool_';
  70. const REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL = 'calc:hasFwBonusUserPool_';
  71. const REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL = 'calc:hasMonthBonusUserPool_';
  72. const REDIS_KEY_PREFIX_HAS_SCORE_USER = 'calc:hasScoreUser_';
  73. const REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL = 'calc:hasScoreUserPool_';
  74. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER = 'calc:hasLSPCSUser_';
  75. const REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL = 'calc:hasLSPCSUserPool_';
  76. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER = 'calc:hasCFPercentPCSUser_';
  77. const REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL = 'calc:hasCFPercentPCSUserPool_';
  78. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER = 'calc:hasLXPercentPCSUser_';
  79. const REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL = 'calc:hasLXPercentPCSUserPool_';
  80. const REDIS_KEY_PREFIX_DEC_ROLE_CONFIG = 'calc:decRoleConfig_';
  81. const REDIS_KEY_PREFIX_CF_TOTAL_PERCENT = 'calc:cfTotalPercent_';
  82. const REDIS_KEY_PREFIX_LX_TOTAL_PERCENT = 'calc:lxTotalPercent_';
  83. const REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP = 'calc:userInfo:ChildOneDeep_';
  84. const REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA = 'calc:gxBonusDeepOneListData_';
  85. const REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA = 'calc:gxBonusDeepTwoListData_';
  86. const REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA = 'calc:ycBonusListData_';
  87. const REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA = 'calc:fwBonusListData_';
  88. const REDIS_KEY_PREFIX_BONUS_STAR_CROWN_LIST_DATA = 'calc:bonusStarCrownListData_';
  89. //@todo
  90. const REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF = 'calc:repairSurplusPerf_';
  91. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER = 'calc:hasRepairPerfUser_';
  92. const REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL = 'calc:hasRepairPerfPool_';
  93. //收入的奖金类型
  94. // const INCOME_TG_BONUS = 'BONUS_TG';
  95. // const INCOME_XF_BONUS = 'BONUS_XF';
  96. // const INCOME_YJ_BONUS = 'BONUS_YJ';
  97. // const INCOME_QY_BONUS = 'BONUS_QY';
  98. const INCOME_QY_BONUS_BD = 'BONUS_QY_BD';
  99. const INCOME_QY_BONUS_FX = 'BONUS_QY_FX';
  100. const INCOME_BONUS_TRAVEL = 'BONUS_TRAVEL';
  101. const INCOME_BONUS_CAR = 'BONUS_CAR';
  102. const INCOME_BONUS_HOUSE = 'BONUS_HOUSE';
  103. const CAPPED_BONUS_QY = 'CAPPED_BONUS_QY'; // 团队奖封顶前的金额
  104. const INCOME_BONUS_LIST = [
  105. // self::INCOME_TG_BONUS,
  106. // self::INCOME_XF_BONUS,
  107. // self::INCOME_YJ_BONUS,
  108. // self::INCOME_QY_BONUS,
  109. self::INCOME_QY_BONUS_BD,
  110. // self::INCOME_QY_BONUS_FX,
  111. ];
  112. const NOT_SEND_BONUS_LIST = [
  113. self::INCOME_QY_BONUS_BD,
  114. self::INCOME_QY_BONUS_FX,
  115. self::INCOME_BONUS_TRAVEL,
  116. self::INCOME_BONUS_CAR,
  117. self::INCOME_BONUS_HOUSE,
  118. self::CAPPED_BONUS_QY,
  119. ];
  120. const FROM_MEANS_BD = 'BD';
  121. const FROM_MEANS_FX = 'FX';
  122. /**
  123. * 结算奖金时要清空的缓存
  124. * @param $periodNum
  125. */
  126. public static function clearCalcBonusCache($periodNum) {
  127. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
  128. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
  129. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
  130. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum);
  131. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum);
  132. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum);
  133. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum);
  134. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum);
  135. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum);
  136. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum);
  137. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
  138. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
  139. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
  140. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
  141. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
  142. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
  143. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS_STAR_CROWN_LIST_DATA . $periodNum);
  144. }
  145. /**
  146. * 清网络缓存
  147. */
  148. public static function clearNetCache(){
  149. \Yii::$app->redis->del(Cache::USER_NETWORK_PARENTS);
  150. \Yii::$app->redis->del(Cache::USER_RELATION_PARENTS);
  151. }
  152. /**
  153. * 清空所有临时计算用到的缓存
  154. * @param $periodNum
  155. */
  156. public static function clearAll($periodNum) {
  157. //Yii::$app->redis->del(Yii::$app->redis->keys('calc*'));
  158. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER . $periodNum);
  159. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum);
  160. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO . $periodNum);
  161. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum);
  162. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum);
  163. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum);
  164. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_PERF . $periodNum);
  165. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum);
  166. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF . $periodNum);
  167. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum);
  168. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum);
  169. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_SCORE . $periodNum);
  170. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum);
  171. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum);
  172. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS . $periodNum);
  173. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum);
  174. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum);
  175. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum);
  176. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum);
  177. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum);
  178. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum);
  179. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum);
  180. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum);
  181. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum);
  182. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum);
  183. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum);
  184. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum);
  185. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum);
  186. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum);
  187. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum);
  188. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum);
  189. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum);
  190. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum);
  191. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum);
  192. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum);
  193. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum);
  194. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum);
  195. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum);
  196. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum);
  197. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum);
  198. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum);
  199. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum);
  200. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum);
  201. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum);
  202. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum);
  203. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum);
  204. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum);
  205. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum);
  206. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum);
  207. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum);
  208. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_BONUS_STAR_CROWN_LIST_DATA . $periodNum);
  209. }
  210. /**
  211. * 会员信息加入缓存
  212. * @param $periodNum
  213. * @param int $offset
  214. * @param int $limit
  215. * @return bool
  216. */
  217. public static function addUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  218. // echo sprintf("时间:[%s]缓存用户,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  219. $userHaveRemain = RemainPv::findAllAsArray('REMAIN_PV >0');
  220. // print_r($userHaveRemain);exit;
  221. $currentPeriod = Period::getInfoByPeriodNum($periodNum);
  222. // print_r($currentPeriod);exit;
  223. if($currentPeriod['IS_MONTH']){
  224. $periods = Period::getCurrentMonthPeriodByPeriodNum($periodNum);
  225. //先清除本期的假订单
  226. echo('首先,清除上次尝试生成业绩单时所创建的虚假订单'. date('Y-m-d H:i:s', time()) . PHP_EOL);
  227. $delFOrder = Order::deleteAll(['ORDER_TYPE'=>'FO','PERIOD_NUM'=>$periodNum]);
  228. // print($delFOrder);exit;
  229. // print_r($periods);exit;
  230. echo('检查有结余PV的用户,如果他当月PV不足30,则为其创建假订单'. PHP_EOL);
  231. foreach($userHaveRemain as $uR){
  232. $myPv = Order::find()->where(['PERIOD_NUM'=>$periods, 'USER_ID'=>$uR['USER_ID']])->SUM('PV');
  233. if($myPv<30){
  234. //制造虚拟订单
  235. echo('不足30了,生成假订单' . PHP_EOL);
  236. $newOrderForm = new OrderForm();
  237. $newOrderForm->addFakeOrder($uR['USER_ID'],$periodNum);
  238. }
  239. }
  240. exit;
  241. // $userOrderArray = Order::findAll()
  242. }
  243. // 修复USER_INFO表里面已经没有PERIOD_NUM了
  244. $allData = UserInfo::findUseDbCalc()->from(UserInfo::tableName(). 'AS UI')->select('UI.USER_ID,U.STATUS')->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')->where('1=1')->orderBy('UI.RELATION_DEEP DESC,UI.ID DESC')->offset($offset)->limit($limit)->asArray()->all();
  245. //$allData = User::findUseDbShopCalc()->select('ID AS USER_ID')->where('PERIOD_AT<=:PERIOD_AT AND IS_UNION=0 AND DELETED=0', [':PERIOD_AT' => $periodNum])->offset($offset)->limit($limit)->asArray()->all();
  246. // print_r($allData);
  247. if ($allData) {
  248. foreach ($allData as $data) {
  249. // if(User::find()->where('ID=:ID',[':ID'=>$data['USER_ID']])->exists()) continue;
  250. // 会员ID加入缓存
  251. $cacheKey = self::REDIS_KEY_PREFIX_USER . $periodNum;
  252. $value = $data['USER_ID'];
  253. Yii::$app->redis->rpush($cacheKey, $value);
  254. if( $data['STATUS'] == 1 ) {
  255. $activeCacheKey = self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum;
  256. Yii::$app->redis->rpush($activeCacheKey, $value);
  257. unset($activeCacheKey);
  258. }
  259. unset($data, $cacheKey, $value);
  260. }
  261. unset($allData);
  262. return self::addUsers($periodNum, $offset + $limit, $limit);
  263. }
  264. unset($allData);
  265. return true;
  266. }
  267. /**
  268. * 获取会员从缓存中
  269. * @param $periodNum
  270. * @param int $offset
  271. * @param int $limit
  272. * @return mixed
  273. */
  274. public static function getUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  275. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER . $periodNum, $offset, ($offset + $limit - 1));
  276. }
  277. /**
  278. * 获取激活会员从缓存中
  279. * @param $periodNum
  280. * @param int $offset
  281. * @param int $limit
  282. * @return mixed
  283. */
  284. public static function getActiveUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  285. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_USER_ACTIVE . $periodNum, $offset, ($offset + $limit - 1));
  286. }
  287. /**
  288. * 加入有业绩的会员
  289. * @param $userId
  290. * @param $periodNum
  291. */
  292. public static function addHasPerfUsers($userId, $periodNum) {
  293. // 先从已存在的会员池里面获取
  294. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId);
  295. if (!$isset) {
  296. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_PERF_USER_POOL . $periodNum, $userId, 1);
  297. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $userId);
  298. }
  299. unset($userId, $periodNum, $isset);
  300. }
  301. /**
  302. * 获取有业绩的会员
  303. * @param $periodNum
  304. * @param int $offset
  305. * @param int $limit
  306. * @return mixed
  307. */
  308. public static function getHasPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  309. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  310. }
  311. /**
  312. * 加入有月业绩的会员
  313. * @param $userId
  314. * @param $periodNum
  315. */
  316. public static function addHasMonthPerfUsers($userId, $periodNum) {
  317. // 先从已存在的会员池里面获取
  318. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId);
  319. if (!$isset) {
  320. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER_POOL . $periodNum, $userId, 1);
  321. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $userId);
  322. }
  323. unset($userId, $periodNum, $isset);
  324. }
  325. /**
  326. * 获取有业绩的会员
  327. * @param $periodNum
  328. * @param int $offset
  329. * @param int $limit
  330. * @return mixed
  331. */
  332. public static function getHasMonthPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  333. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  334. }
  335. /**
  336. * 加入有达标业绩的会员
  337. * @param $userId
  338. * @param $periodNum
  339. */
  340. public static function addHasStandardMonthPerfUsers($userId, $periodNum) {
  341. // 先从已存在的会员池里面获取
  342. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum, $userId);
  343. if (!$isset) {
  344. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER_POOL . $periodNum, $userId, 1);
  345. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum, $userId);
  346. }
  347. unset($userId, $periodNum, $isset);
  348. }
  349. /**
  350. * 获取有达标业绩的会员
  351. * @param $periodNum
  352. * @param int $offset
  353. * @param int $limit
  354. * @return mixed
  355. */
  356. public static function getHasStandardMonthPerfUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  357. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_STANDARD_MONTH_PERF_USER . $periodNum, $offset, ($offset + $limit - 1));
  358. }
  359. /**
  360. * 加入被报单的会员
  361. * @param $userId
  362. * @param $periodNum
  363. * @param $saveData
  364. */
  365. public static function addHasBDUsers($userId, $periodNum, $saveData) {
  366. // 先从已存在的会员池里面获取
  367. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
  368. if (!$data) {
  369. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId, Json::encode($saveData));
  370. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $userId);
  371. }
  372. unset($userId, $periodNum, $saveData, $isset);
  373. }
  374. /**
  375. * 获取被报单的会员信息
  376. * @param $userId
  377. * @param $periodNum
  378. * @return array
  379. */
  380. public static function getBDUsersInfo($userId, $periodNum) {
  381. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BD_USER_POOL . $periodNum, $userId);
  382. if( !$data ) return [];
  383. return Json::decode($data, true);
  384. }
  385. /**
  386. * 获取被报单的会员
  387. * @param $periodNum
  388. * @param int $offset
  389. * @param int $limit
  390. * @return mixed
  391. */
  392. public static function getHasBDUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  393. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BD_USER . $periodNum, $offset, ($offset + $limit - 1));
  394. }
  395. /**
  396. * 加入有收入的会员
  397. * @param $userId
  398. * @param $periodNum
  399. */
  400. public static function addHasIncomeUsers($userId, $periodNum) {
  401. // 先从已存在的会员池里面获取
  402. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId);
  403. if (!$isset) {
  404. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_INCOME_USER_POOL . $periodNum, $userId, 1);
  405. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $userId);
  406. }
  407. unset($userId, $periodNum, $isset);
  408. }
  409. /**
  410. * 获取有收入的会员
  411. * @param $periodNum
  412. * @param int $offset
  413. * @param int $limit
  414. * @return mixed
  415. */
  416. public static function getHasIncomeUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  417. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_INCOME_USER . $periodNum, $offset, ($offset + $limit - 1));
  418. }
  419. /**
  420. * 加入有奖金的会员
  421. * @param $userId
  422. * @param $periodNum
  423. */
  424. public static function addHasBonusUsers($userId, $periodNum) {
  425. // 先从已存在的会员池里面获取
  426. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId);
  427. if (!$isset) {
  428. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_BONUS_USER_POOL . $periodNum, $userId, 1);
  429. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $userId);
  430. }
  431. unset($userId, $periodNum, $isset);
  432. }
  433. /**
  434. * 获取有奖金的会员
  435. * @param $periodNum
  436. * @param int $offset
  437. * @param int $limit
  438. * @return mixed
  439. */
  440. public static function getHasBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  441. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  442. }
  443. /**
  444. * 加入荣衔奖的会员
  445. * @param $userId
  446. * @param $periodNum
  447. */
  448. public static function addHasYcBonusUsers($userId, $periodNum) {
  449. // 先从已存在的会员池里面获取
  450. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum, $userId);
  451. if (!$isset) {
  452. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER_POOL . $periodNum, $userId, 1);
  453. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum, $userId);
  454. }
  455. unset($userId, $periodNum, $isset);
  456. }
  457. /**
  458. * 保存荣衔奖奖金
  459. * @param $userId
  460. * @param $periodNum
  461. * @param $empBonus
  462. * @param array $fromData
  463. */
  464. public static function saveYCBonusList($userId, $periodNum, $empBonus, $fromData=[]) {
  465. $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId);
  466. if( $userYcBonusData ) {
  467. $ycBonusData = Json::decode($userYcBonusData, true);
  468. $ycBonusData['empBonus'] += $empBonus;
  469. // $ycBonusData['fromData'][] = $fromData;
  470. }else {
  471. $ycBonusData = [
  472. 'empBonus' => $empBonus,
  473. // 'fromData' => [
  474. // $fromData
  475. // ]
  476. ];
  477. }
  478. unset($userYcBonusData);
  479. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId, Json::encode($ycBonusData));
  480. unset($ycBonusData, $userId, $periodNum, $empBonus, $fromData);
  481. }
  482. /**
  483. * 返回荣衔奖信息
  484. * @param $userId
  485. * @param $periodNum
  486. * @return array
  487. */
  488. public static function getYCBonusList($userId, $periodNum) {
  489. $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_YC_BONUS_LIST_DATA . $periodNum, $userId);
  490. return $userYcBonusData ? Json::decode($userYcBonusData, true) : [];
  491. }
  492. /**
  493. * 获取有荣衔奖的会员
  494. * @param $periodNum
  495. * @param int $offset
  496. * @param int $limit
  497. * @return mixed
  498. */
  499. public static function getHasYcBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  500. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_YC_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  501. }
  502. /**
  503. * 获取有服务奖的会员
  504. * @param $periodNum
  505. * @param int $offset
  506. * @param int $limit
  507. * @return mixed
  508. */
  509. public static function getHasFwBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  510. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  511. }
  512. /**
  513. * 加入服务奖的会员
  514. * @param $userId
  515. * @param $periodNum
  516. */
  517. public static function addHasFwBonusUsers($userId, $periodNum) {
  518. // 先从已存在的会员池里面获取
  519. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId);
  520. if (!$isset) {
  521. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER_POOL . $periodNum, $userId, 1);
  522. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_FW_BONUS_USER . $periodNum, $userId);
  523. }
  524. unset($userId, $periodNum, $isset);
  525. }
  526. /**
  527. * 获取有月奖的会员
  528. * @param $periodNum
  529. * @param int $offset
  530. * @param int $limit
  531. * @return mixed
  532. */
  533. public static function getHasMonthBonusUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  534. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum, $offset, ($offset + $limit - 1));
  535. }
  536. /**
  537. * 加入月奖的会员
  538. * @param $userId
  539. * @param $periodNum
  540. */
  541. public static function addHasMonthBonusUsers($userId, $periodNum) {
  542. // 先从已存在的会员池里面获取
  543. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum, $userId);
  544. if (!$isset) {
  545. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER_POOL . $periodNum, $userId, 1);
  546. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_MONTH_BONUS_USER . $periodNum, $userId);
  547. }
  548. unset($userId, $periodNum, $isset);
  549. }
  550. /**
  551. * 保存荣衔奖奖金
  552. * @param $userId
  553. * @param $periodNum
  554. * @param $fwBonus
  555. * @param array $fromData
  556. */
  557. public static function saveFwBonusList($userId, $periodNum, $fwBonus, $fromData=[]) {
  558. $userFwBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);
  559. if( $userFwBonusData ) {
  560. $fwBonusData = Json::decode($userFwBonusData, true);
  561. $fwBonusData['fwBonus'] += $fwBonus;
  562. // $ycBonusData['fromData'][] = $fromData;
  563. }else {
  564. $fwBonusData = [
  565. 'fwBonus' => $fwBonus,
  566. // 'fromData' => [
  567. // $fromData
  568. // ]
  569. ];
  570. }
  571. unset($userFwBonusData);
  572. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId, Json::encode($fwBonusData));
  573. unset($fwBonusData, $userId, $periodNum, $empBonus, $fromData);
  574. }
  575. /**
  576. * 返回服务奖信息
  577. * @param $userId
  578. * @param $periodNum
  579. * @return array
  580. */
  581. public static function getFwBonusList($userId, $periodNum) {
  582. $userYcBonusData = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_FW_BONUS_LIST_DATA . $periodNum, $userId);
  583. return $userYcBonusData ? Json::decode($userYcBonusData, true) : [];
  584. }
  585. /**
  586. * 会员星级加入缓存
  587. * @param $userId
  588. * @param $periodNum
  589. * @param $starCrown
  590. */
  591. public static function addUserStartCrown($userId, $periodNum, $starCrown) {
  592. // 先从已存在的会员池里面获取
  593. $data = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_BONUS_STAR_CROWN_LIST_DATA . $periodNum, $userId);
  594. if (!$data) {
  595. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_BONUS_STAR_CROWN_LIST_DATA . $periodNum, $userId, $starCrown);
  596. }
  597. unset($userId, $periodNum, $saveData, $isset);
  598. }
  599. /**
  600. * 获取会员星级信息
  601. * @param $userId
  602. * @param $periodNum
  603. * @return array
  604. */
  605. public static function getUserStartCrown($userId, $periodNum) {
  606. return Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_BONUS_STAR_CROWN_LIST_DATA . $periodNum, $userId);
  607. }
  608. /**
  609. * 加入有复销业绩的会员列表
  610. * @param $userId
  611. * @param $periodNum
  612. */
  613. public static function addHasScoreUsers($userId, $periodNum) {
  614. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum, $userId);
  615. if (!$isset) {
  616. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_SCORE_USER_POOL . $periodNum, $userId, 1);
  617. $key = self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum;
  618. Yii::$app->redis->rpush($key, $userId);
  619. }
  620. }
  621. /**
  622. * 获取有复销业绩的会员列表
  623. * @param $periodNum
  624. * @param int $offset
  625. * @param int $limit
  626. * @return mixed
  627. */
  628. public static function getHasScoreUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  629. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_SCORE_USER . $periodNum, $offset, ($offset + $limit - 1));
  630. }
  631. /**
  632. * 加入有零售个人消费的会员列表
  633. * @param $userId
  634. * @param $periodNum
  635. */
  636. public static function addHasLSPCSUsers($userId, $periodNum) {
  637. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum, $userId);
  638. if (!$isset) {
  639. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER_POOL . $periodNum, $userId, 1);
  640. $key = self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum;
  641. $value = $userId;
  642. Yii::$app->redis->rpush($key, $value);
  643. }
  644. }
  645. /**
  646. * 加入有零售消费的会员列表
  647. * @param $periodNum
  648. * @param int $offset
  649. * @param int $limit
  650. * @return mixed
  651. */
  652. public static function getHasLSPCSUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  653. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_LS_PCS_USER . $periodNum, $offset, ($offset + $limit - 1));
  654. }
  655. /**
  656. * 有车房补贴比例的人
  657. * @param $userId
  658. * @param $periodNum
  659. */
  660. public static function addHasCFPercentUsers($userId, $periodNum) {
  661. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId);
  662. if (!$isset) {
  663. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER_POOL . $periodNum, $userId, 1);
  664. $key = self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum;
  665. $value = $userId;
  666. Yii::$app->redis->rpush($key, $value);
  667. }
  668. }
  669. /**
  670. * 获取有车房补贴的人
  671. * @param $periodNum
  672. * @param int $offset
  673. * @param int $limit
  674. * @return mixed
  675. */
  676. public static function getHasCFPercentUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  677. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_CF_PERCENT_USER . $periodNum, $offset, ($offset + $limit - 1));
  678. }
  679. /**
  680. * 有领袖分红的人
  681. * @param $userId
  682. * @param $periodNum
  683. */
  684. public static function addHasLXPercentUsers($userId, $periodNum) {
  685. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum, $userId);
  686. if (!$isset) {
  687. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER_POOL . $periodNum, $userId, 1);
  688. $key = self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum;
  689. $value = $userId;
  690. Yii::$app->redis->rpush($key, $value);
  691. }
  692. }
  693. /**
  694. * 获取有领袖分红的人
  695. * @param $periodNum
  696. * @param int $offset
  697. * @param int $limit
  698. * @return mixed
  699. */
  700. public static function getHasLXPercentUsers($periodNum, $offset = 0, $limit = self::LIMIT) {
  701. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_LX_PERCENT_USER . $periodNum, $offset, ($offset + $limit - 1));
  702. }
  703. /**
  704. * 通过创建时间获取指定长度的用户列表
  705. * @param $userId
  706. * @param $limit
  707. * @return array|\yii\db\ActiveRecord[]
  708. */
  709. public static function getUserListByCreatedAtFromDb($userId, $limit)
  710. {
  711. //查找这个人的CREATED_AT
  712. $user = User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID=:ID', ['ID'=>$userId])->asArray()->one();
  713. if( !$user ) return [];
  714. return User::findUseDbCalc()->select('ID,CREATED_AT')->where('ID<:ID AND CREATED_AT<=:CREATED_AT', [
  715. 'ID'=>$userId,
  716. 'CREATED_AT' => $user['CREATED_AT']
  717. ])->orderBy('CREATED_AT DESC,ID DESC')->limit($limit)->asArray()->all();
  718. }
  719. /**
  720. * 通过创建时间获取指定长度的用户列表
  721. * @param $userId
  722. * @param $limit
  723. * @return array|\yii\db\ActiveRecord[]
  724. */
  725. public static function getAfterUserListByCreatedAtFromDb($userId, $limit)
  726. {
  727. $cacheKey = Cache::USER_CREATED_AT_LIST . $limit;
  728. $value = Yii::$app->redis->hGet($cacheKey, $userId);
  729. if( $value ) {
  730. unset($cacheKey);
  731. return json_decode($value, true);
  732. }
  733. unset($value);
  734. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('USER_ID>:USER_ID', [
  735. 'USER_ID'=>$userId,
  736. ])->orderBy('USER_ID ASC')->limit($limit)->asArray()->all();
  737. if ( $limit == count($list) ) {
  738. Yii::$app->redis->hSet($cacheKey, $userId, json_encode($list));
  739. }
  740. unset($cacheKey);
  741. return $list;
  742. }
  743. /**
  744. * 获取会员信息从缓存
  745. * @param $userId
  746. * @param $periodNum
  747. * @return array
  748. * @throws \Exception
  749. */
  750. public static function getUserInfo($userId, $periodNum) {
  751. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  752. $data = Yii::$app->redis->hget($key, $userId);
  753. if (!$data) {
  754. $userInfo = UserInfo::findUseDbCalc()->select('USER_ID,USER_NAME,ZC_AMOUNT,ZC_PV,CON_UID,REC_UID,CON_NUM,REC_NUM,NETWORK_DEEP,RELATION_DEEP,STORE_TYPE,UPDATED_AT,SYSTEM_ID')->where('USER_ID=:USER_ID', [':USER_ID' => $userId])->asArray()->one();
  755. if( !$userInfo ) {
  756. throw new \Exception('用户信息表数据不存在,userId:'.$userId);
  757. }
  758. // 在获取商城库的会员的级别信息和是否报单中心的会员
  759. $userShopInfo = User::find()->select('REAL_NAME,LAST_DEC_LV,DEC_LV,EMP_LV,STAR_LV,IS_DEC,DEC_ID,DEC_ROLE_ID,STATUS,PERIOD_AT,MOBILE,CREATED_AT,SUB_COM_ID,PROVINCE,CITY,COUNTY,DEC_PROVINCE,DEC_CITY,DEC_COUNTY,IS_DIRECT_SELLER,SUB_COM_LEADER')->where('ID=:ID', [':ID' => $userId])->asArray()->one();
  760. if ( !$userShopInfo ) {
  761. throw new \Exception('用户表数据不存在,userId:'.$userId);
  762. }
  763. $userInfo['REAL_NAME'] = $userShopInfo['REAL_NAME'];
  764. // $userInfo['DEC_LV'] = $userShopInfo['DEC_LV'];
  765. //先用last_dec_lv
  766. $userInfo['DEC_LV'] = $userShopInfo['LAST_DEC_LV'];
  767. $userInfo['EMP_LV'] = $userShopInfo['EMP_LV'];
  768. $userInfo['STAR_LV'] = $userShopInfo['STAR_LV'];
  769. $userInfo['IS_DEC'] = $userShopInfo['IS_DEC'];
  770. $userInfo['DEC_ID'] = $userShopInfo['DEC_ID'];
  771. $userInfo['DEC_ROLE_ID'] = $userShopInfo['DEC_ROLE_ID'];
  772. $userInfo['DEC_PROVINCE'] = $userShopInfo['DEC_PROVINCE'];
  773. $userInfo['DEC_CITY'] = $userShopInfo['DEC_CITY'];
  774. $userInfo['DEC_COUNTY'] = $userShopInfo['DEC_COUNTY'];
  775. $userInfo['STATUS'] = $userShopInfo['STATUS'];
  776. $userInfo['PERIOD_NUM'] = $userShopInfo['PERIOD_AT'];
  777. $userInfo['MOBILE'] = $userShopInfo['MOBILE'];
  778. $userInfo['CREATED_AT'] = $userShopInfo['CREATED_AT'];
  779. $userInfo['SUB_COM_ID'] = $userShopInfo['SUB_COM_ID'];
  780. $userInfo['PROVINCE'] = $userShopInfo['PROVINCE'];
  781. $userInfo['CITY'] = $userShopInfo['CITY'];
  782. $userInfo['COUNTY'] = $userShopInfo['COUNTY'];
  783. $userInfo['IS_DIRECT_SELLER'] = $userShopInfo['IS_DIRECT_SELLER'];
  784. $userInfo['SUB_COM_LEADER'] = $userShopInfo['SUB_COM_LEADER'];
  785. $userInfo['REC_USER_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserNameByUserId($userInfo['REC_UID']) : '';
  786. $userInfo['REC_REAL_NAME'] = isset($userInfo['REC_UID']) && $userInfo['REC_UID'] ? Info::getUserRealNameByUserId($userInfo['REC_UID']) : '';
  787. $userInfo['CON_USER_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserNameByUserId($userInfo['CON_UID']) : '';
  788. $userInfo['CON_REAL_NAME'] = isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ? Info::getUserRealNameByUserId($userInfo['CON_UID']) : '';
  789. // $userInfo['REC_USER_NAME'] = '';
  790. // $userInfo['REC_REAL_NAME'] = '';
  791. // $userInfo['CON_USER_NAME'] = '';
  792. // $userInfo['CON_REAL_NAME'] = '';
  793. // $userInfo['LOCATION'] = 1;
  794. unset($userShopInfo);
  795. // if( isset($userInfo['CON_UID']) && $userInfo['CON_UID'] ) {
  796. // if ($networkParent = UserNetwork::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $userId])) {
  797. // $userInfo['LOCATION'] = $networkParent['RELATIVE_LOCATION'];
  798. // }
  799. // }
  800. if (!$userInfo['DEC_LV']) {
  801. $userInfo['DEC_LV'] = DeclarationLevel::getDefaultLevelId();
  802. }
  803. if (!$userInfo['EMP_LV']) {
  804. $userInfo['EMP_LV'] = EmployLevel::getDefaultLevelId();
  805. }
  806. if (!$userInfo['STAR_LV']) {
  807. $userInfo['STAR_LV'] = StarCrownLevel::getDefaultLevelId();
  808. }
  809. $data = Json::encode($userInfo);
  810. Yii::$app->redis->hset($key, $userId, $data);
  811. unset($userInfo, $key, $userId, $periodNum);
  812. }
  813. return $data ? Json::decode($data, true) : [];
  814. }
  815. public static function setUserInfo($userId, $periodNum, $userInfo) {
  816. $key = self::REDIS_KEY_PREFIX_USER_INFO . $periodNum;
  817. $data = Json::encode($userInfo);
  818. Yii::$app->redis->hset($key, $userId, $data);
  819. unset($userId, $key, $data, $userInfo, $periodNum);
  820. return true;
  821. }
  822. /**
  823. * 用户奖金信息
  824. * @param $userId
  825. * @param $periodNum
  826. * @return array|mixed|null|\yii\db\ActiveRecord
  827. */
  828. public static function LastUserBonus($userId, $periodNum) {
  829. $cacheKey = self::REDIS_KEY_PREFIX_USER_BONUS . $periodNum;
  830. $field = $userId;
  831. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  832. if ($cacheValue) {
  833. $value = Json::decode($cacheValue, true);
  834. } else {
  835. $value = UserBonus::findUseDbCalc()->select('USER_ID,BONUS_TOTAL')->where('USER_ID=:USER_ID', [
  836. 'USER_ID'=>$userId
  837. ])->asArray()->one();
  838. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  839. }
  840. return $value;
  841. }
  842. /**
  843. * 用户奖金信息
  844. * @param $userId
  845. * @param $periodNum
  846. * @param $yearMonth
  847. * @return array|mixed|null|\yii\db\ActiveRecord
  848. */
  849. public static function lastPeriodMonthCalcBonus($userId, $periodNum, $yearMonth) {
  850. $cacheKey = self::REDIS_KEY_PREFIX_PERIOD_MONTH_CALC_BONUS . $periodNum;
  851. $field = $userId;
  852. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  853. if ($cacheValue) {
  854. $value = Json::decode($cacheValue, true);
  855. } else {
  856. $value = CalcBonus::findUseDbCalc()->select('SUM(ORI_BONUS_QY) AS ORI_BONUS_QY_SUM,SUM(ORI_BONUS_VIP) AS ORI_BONUS_VIP_SUM')->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', [
  857. 'USER_ID'=>$userId,
  858. 'CALC_MONTH'=>$yearMonth
  859. ])->asArray()->one();
  860. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  861. }
  862. return $value;
  863. }
  864. /**
  865. * 本月往期的复消积分的数据
  866. * @param $userId
  867. * @param $periodNum
  868. * @param $calcYearMonth
  869. * @return array|mixed|null|\yii\db\ActiveRecord
  870. * @throws \yii\db\Exception
  871. */
  872. public static function monthLastPeriodReconsumePoints($userId, $periodNum, $calcYearMonth) {
  873. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_LAST_PERIOD_RECONSUME_POINTS . $periodNum;
  874. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  875. if ($cacheValue) {
  876. $value = Json::decode($cacheValue, true);
  877. } else {
  878. $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();
  879. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  880. }
  881. return $value;
  882. }
  883. /**
  884. * 获取结算时这一期的报单中心级别的配置
  885. * @param $periodNum
  886. * @return array|\yii\db\ActiveRecord[]
  887. */
  888. public static function getDecRoleConfig($periodNum) {
  889. $key = self::REDIS_KEY_PREFIX_DEC_ROLE_CONFIG . $periodNum;
  890. Yii::$app->redis->del($key);
  891. $data = Yii::$app->redis->get($key);
  892. if (!$data) {
  893. $data = DecRole::find()->where('1=1')->indexBy('ID')->asArray()->all();
  894. $data = Json::encode($data);
  895. Yii::$app->redis->set($key, $data);
  896. }
  897. return $data ? Json::decode($data) : [];
  898. }
  899. /**
  900. * 上一期结余业绩
  901. * @param $userId
  902. * @param $periodNum
  903. * @return array|mixed|\yii\db\ActiveRecord|null
  904. * @throws \yii\db\Exception
  905. */
  906. public static function surplusPerf($userId, $periodNum) {
  907. // $cacheKey = self::REDIS_KEY_PREFIX_SURPLUS_PERF . $periodNum;
  908. // $field = $userId;
  909. // $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  910. // if ($cacheValue) {
  911. // $value = Json::decode($cacheValue);
  912. // } else {
  913. // $value = UserPerf::getPeriodSurplusPerf($userId);
  914. // \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  915. // }
  916. // return $value;
  917. $userPerf = self::userPerf($userId, $periodNum);
  918. return [
  919. 'SURPLUS_1L' => $userPerf['SURPLUS_1L'],
  920. 'SURPLUS_2L' => $userPerf['SURPLUS_2L'],
  921. 'SURPLUS_3L' => $userPerf['SURPLUS_3L'],
  922. 'SURPLUS_4L' => $userPerf['SURPLUS_4L'],
  923. 'SURPLUS_5L' => $userPerf['SURPLUS_5L'],
  924. 'SURPLUS_1L_ZC' => $userPerf['SURPLUS_1L_ZC'],
  925. 'SURPLUS_2L_ZC' => $userPerf['SURPLUS_2L_ZC'],
  926. 'SURPLUS_3L_ZC' => $userPerf['SURPLUS_3L_ZC'],
  927. 'SURPLUS_4L_ZC' => $userPerf['SURPLUS_4L_ZC'],
  928. 'SURPLUS_5L_ZC' => $userPerf['SURPLUS_5L_ZC'],
  929. 'SURPLUS_1L_FX' => $userPerf['SURPLUS_1L_FX'],
  930. 'SURPLUS_2L_FX' => $userPerf['SURPLUS_2L_FX'],
  931. 'SURPLUS_3L_FX' => $userPerf['SURPLUS_3L_FX'],
  932. 'SURPLUS_4L_FX' => $userPerf['SURPLUS_4L_FX'],
  933. 'SURPLUS_5L_FX' => $userPerf['SURPLUS_5L_FX'],
  934. 'VIP_SURPLUS_1L_ZC' => $userPerf['VIP_SURPLUS_1L_ZC'],
  935. 'VIP_SURPLUS_2L_ZC' => $userPerf['VIP_SURPLUS_2L_ZC'],
  936. 'VIP_SURPLUS_3L_ZC' => $userPerf['VIP_SURPLUS_3L_ZC'],
  937. 'VIP_SURPLUS_4L_ZC' => $userPerf['VIP_SURPLUS_4L_ZC'],
  938. 'VIP_SURPLUS_5L_ZC' => $userPerf['VIP_SURPLUS_5L_ZC'],
  939. 'SURPLUS_LS' => 0,
  940. ];
  941. }
  942. /**
  943. * 获取repairSurplusPerf
  944. * @param $userId
  945. * @return int[]|mixed|null
  946. */
  947. public static function getRepairSurplusPerf($userId) {
  948. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  949. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  950. if ($cacheValue) {
  951. $value = Json::decode($cacheValue);
  952. }else {
  953. $value = [
  954. 'SURPLUS_1L' => 0,
  955. 'SURPLUS_2L' => 0,
  956. 'SURPLUS_3L' => 0,
  957. 'SURPLUS_4L' => 0,
  958. 'SURPLUS_5L' => 0,
  959. ];
  960. }
  961. return $value;
  962. }
  963. /**
  964. * 累加repairSurplusPerf
  965. * @param $userId
  966. * @param $surplusPerf
  967. * @return int[]|mixed|null
  968. */
  969. public static function setRepairSurplusPerf($userId, $surplusPerf) {
  970. $surplusPerfList = self::getRepairSurplusPerf($userId);
  971. foreach ($surplusPerfList as $key => $perf) {
  972. $thisPerf = $surplusPerf[$key] ?? 0;
  973. $surplusPerfList[$key] = $perf + $thisPerf;
  974. unset($key, $perf, $thisPerf);
  975. }
  976. $cacheKey = self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF;
  977. \Yii::$app->redis->hset($cacheKey, $userId, Json::encode($surplusPerfList));
  978. self::addHasRepairPerfUsers($userId);
  979. unset($userId, $cacheKey, $surplusPerf);
  980. return $surplusPerfList;
  981. }
  982. /**
  983. * 加入有往期结余业绩的会员
  984. * @param $userId
  985. */
  986. public static function addHasRepairPerfUsers($userId) {
  987. // 先从已存在的会员池里面获取
  988. $isset = Yii::$app->redis->hget(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId);
  989. if (!$isset) {
  990. Yii::$app->redis->hset(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL, $userId, 1);
  991. Yii::$app->redis->rpush(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $userId);
  992. }
  993. unset($userId, $isset);
  994. }
  995. /**
  996. * 获取有往期结余业绩的会员
  997. * @param int $offset
  998. * @param int $limit
  999. * @return mixed
  1000. */
  1001. public static function getHasRepairPerfUsers($offset = 0, $limit = self::LIMIT) {
  1002. return Yii::$app->redis->lrange(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER, $offset, ($offset + $limit - 1));
  1003. }
  1004. /**
  1005. * 修正业绩缓存清空
  1006. */
  1007. public static function clearRepairAllCache() {
  1008. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_REPAIR_SURPLUS_PERF);
  1009. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER);
  1010. Yii::$app->redis->del(self::REDIS_KEY_PREFIX_HAS_REPAIR_PERF_USER_POOL);
  1011. }
  1012. /**
  1013. * 上一期结余业绩
  1014. * @param $userId
  1015. * @param $periodNum
  1016. * @return array|mixed|\yii\db\ActiveRecord|null
  1017. * @throws \yii\db\Exception
  1018. */
  1019. public static function userPerf($userId, $periodNum) {
  1020. $cacheKey = self::REDIS_KEY_PREFIX_USER_PERF . $periodNum;
  1021. $field = $userId;
  1022. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1023. if ($cacheValue) {
  1024. $value = Json::decode($cacheValue);
  1025. } else {
  1026. $value = UserPerf::getOneUserPerf($userId);
  1027. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1028. }
  1029. return $value;
  1030. }
  1031. /**
  1032. * 当前期数的的业绩
  1033. * @param $userId
  1034. * @param $periodNum
  1035. * @param null $perf
  1036. * @return array|mixed
  1037. */
  1038. public static function nowPeriodPerf($userId, $periodNum, $perf = null) {
  1039. $cacheKey = self::REDIS_KEY_PREFIX_NOW_PERIOD_PERF . $periodNum;
  1040. $field = $userId;
  1041. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1042. if ($cacheValue) {
  1043. $value = Json::decode($cacheValue);
  1044. } else {
  1045. $value = [
  1046. 'FX_AMOUNT_CASH' => 0,
  1047. 'PV_PCS' => 0,
  1048. 'PV_PSS' => 0,//本期的团队业绩
  1049. 'PV_PCS_ZC' => 0,//注册
  1050. 'PV_PCS_YH' => 0,
  1051. 'PV_PCS_ZG' => 0,//增购
  1052. 'PV_PCS_LS' => 0,
  1053. 'PV_PCS_FX' => 0,
  1054. 'PV_PCS_FX_CASH' => 0,//现金复消
  1055. 'PV_PCS_FX_POINT' => 0,//积分复消
  1056. 'PV_1L' => 0,
  1057. 'PV_1L_TOUCH' => 0,
  1058. 'PV_1L_ZC' => 0,
  1059. 'PV_1L_YH' => 0,
  1060. 'PV_1L_ZG' => 0,
  1061. 'PV_1L_LS' => 0,
  1062. 'PV_1L_FX' => 0,
  1063. 'PV_2L' => 0,
  1064. 'PV_2L_TOUCH' => 0,
  1065. 'PV_2L_ZC' => 0,
  1066. 'PV_2L_YH' => 0,
  1067. 'PV_2L_ZG' => 0,
  1068. 'PV_2L_LS' => 0,
  1069. 'PV_2L_FX' => 0,
  1070. 'PV_3L' => 0,
  1071. 'PV_3L_TOUCH' => 0,
  1072. 'PV_3L_ZC' => 0,
  1073. 'PV_3L_YH' => 0,
  1074. 'PV_3L_ZG' => 0,
  1075. 'PV_3L_LS' => 0,
  1076. 'PV_3L_FX' => 0,
  1077. 'PV_4L' => 0,
  1078. 'PV_4L_TOUCH' => 0,
  1079. 'PV_4L_ZC' => 0,
  1080. 'PV_4L_YH' => 0,
  1081. 'PV_4L_ZG' => 0,
  1082. 'PV_4L_LS' => 0,
  1083. 'PV_4L_FX' => 0,
  1084. 'PV_5L' => 0,
  1085. 'PV_5L_TOUCH' => 0,
  1086. 'PV_5L_ZC' => 0,
  1087. 'PV_5L_YH' => 0,
  1088. 'PV_5L_ZG' => 0,
  1089. 'PV_5L_LS' => 0,
  1090. 'PV_5L_FX' => 0,
  1091. 'PV_LS_TOUCH' => 0,
  1092. 'SURPLUS_1L' => 0,
  1093. 'SURPLUS_2L' => 0,
  1094. 'SURPLUS_3L' => 0,
  1095. 'SURPLUS_4L' => 0,
  1096. 'SURPLUS_5L' => 0,
  1097. 'SURPLUS_1L_ZC' => 0,
  1098. 'SURPLUS_2L_ZC' => 0,
  1099. 'SURPLUS_3L_ZC' => 0,
  1100. 'SURPLUS_4L_ZC' => 0,
  1101. 'SURPLUS_5L_ZC' => 0,
  1102. 'SURPLUS_1L_FX' => 0,
  1103. 'SURPLUS_2L_FX' => 0,
  1104. 'SURPLUS_3L_FX' => 0,
  1105. 'SURPLUS_4L_FX' => 0,
  1106. 'SURPLUS_5L_FX' => 0,
  1107. 'SURPLUS_LS' => 0,
  1108. ];
  1109. }
  1110. if ($perf !== null) {
  1111. foreach ($perf as $key => $pv) {
  1112. if (strpos($key, 'SURPLUS') !== false) {
  1113. $value[$key] = $pv;
  1114. } else {
  1115. $value[$key] = $pv + $value[$key];
  1116. }
  1117. }
  1118. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1119. }
  1120. return $value;
  1121. }
  1122. /**
  1123. * 上个月的业绩
  1124. * @param $userId
  1125. * @param $periodNum
  1126. * @return array|mixed|null|\yii\db\ActiveRecord
  1127. */
  1128. public static function lastMonthPerf($userId, $periodNum) {
  1129. $period = Period::instance();
  1130. $last = $period->getLastMonth($periodNum);
  1131. $lastYearMonth = $last['yearMonth'];
  1132. $cacheKey = self::REDIS_KEY_PREFIX_LAST_MONTH_PERF . $periodNum;
  1133. $field = $userId;
  1134. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1135. if ($cacheValue) {
  1136. $value = Json::decode($cacheValue);
  1137. } else {
  1138. $value = PerfMonth::getMonthPerf($lastYearMonth, $userId);
  1139. \Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1140. }
  1141. return $value;
  1142. }
  1143. /**
  1144. * 当前月的业绩
  1145. * @param $userId
  1146. * @param $periodNum
  1147. * @param $perf
  1148. * @return array|mixed
  1149. */
  1150. public static function nowMonthPerf($userId, $periodNum, $perf = null) {
  1151. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_PERF . $periodNum;
  1152. $field = $userId;
  1153. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1154. if ($cacheValue) {
  1155. $value = Json::decode($cacheValue);
  1156. } else {
  1157. $baseInfo = self::getUserInfo($userId, $periodNum);
  1158. $value = [
  1159. 'USER_ID' => $userId,
  1160. 'FX_AMOUNT_CASH' => 0,
  1161. 'PV_PCS' => 0,
  1162. 'PV_PCS_FX' => 0,
  1163. 'PV_PSS' => 0,
  1164. 'PV_1L' => 0,
  1165. 'PV_2L' => 0,
  1166. 'PV_3L' => 0,
  1167. 'PV_4L' => 0,
  1168. 'PV_5L' => 0,
  1169. 'VIP_PV_1L_ZC' => 0,
  1170. 'VIP_PV_2L_ZC' => 0,
  1171. 'VIP_PV_3L_ZC' => 0,
  1172. 'VIP_PV_4L_ZC' => 0,
  1173. 'VIP_PV_5L_ZC' => 0,
  1174. 'PV_1L_TOTAL' => 0,
  1175. 'PV_2L_TOTAL' => 0,
  1176. 'PV_3L_TOTAL' => 0,
  1177. 'PV_4L_TOTAL' => 0,
  1178. 'PV_5L_TOTAL' => 0,
  1179. 'PV_PSS_TOTAL' => 0,
  1180. 'DEC_LEVEL' => $baseInfo['DEC_LV'],
  1181. 'EMP_LEVEL' => EmployLevel::getDefaultLevelId(),
  1182. 'CF_PERCENT' => 0,
  1183. 'LX_PERCENT' => 0,
  1184. 'FX_STATUS' => PerfMonth::NEXT_MONTH_FX_FALSE,
  1185. ];
  1186. }
  1187. if ($perf !== null) {
  1188. foreach ($perf as $key => $item) {
  1189. $value[$key] = $item;
  1190. }
  1191. }
  1192. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1193. return $value;
  1194. }
  1195. /**
  1196. * 当前期数的的业绩
  1197. * @param $userId
  1198. * @param $periodNum
  1199. * @param null $perf
  1200. * @return array|mixed
  1201. */
  1202. public static function nowStandardMonthPerf($userId, $periodNum, $perf = null) {
  1203. $cacheKey = self::REDIS_KEY_PREFIX_NOW_STANDARD_MONTH_PERF . $periodNum;
  1204. $field = $userId;
  1205. $cacheValue = \Yii::$app->redis->hget($cacheKey, $field);
  1206. if ($cacheValue) {
  1207. $value = Json::decode($cacheValue);
  1208. } else {
  1209. $value = [
  1210. 'AMOUNT_PCS' => 0,
  1211. 'AMOUNT_PSS' => 0
  1212. ];
  1213. }
  1214. if ($perf !== null) {
  1215. foreach ($perf as $key => $pv) {
  1216. $value[$key] = $pv + $value[$key];
  1217. }
  1218. Yii::$app->redis->hset($cacheKey, $field, Json::encode($value));
  1219. }
  1220. return $value;
  1221. }
  1222. /**
  1223. * 当前月的积分
  1224. * @param $userId
  1225. * @param $periodNum
  1226. * @param $perf
  1227. * @return array|mixed
  1228. */
  1229. public static function nowMonthScore($userId, $periodNum, $perf = null) {
  1230. $cacheKey = self::REDIS_KEY_PREFIX_NOW_MONTH_SCORE . $periodNum;
  1231. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1232. if ($cacheValue) {
  1233. $value = Json::decode($cacheValue);
  1234. } else {
  1235. $value = [
  1236. 'USER_ID' => $userId,
  1237. 'BASE_SCORE' => 0,
  1238. 'LEVEL_SCORE' => 0,
  1239. 'UPGRADE_SCORE' => 0,
  1240. // 'TOTAL_SCORE' => 0,
  1241. ];
  1242. }
  1243. unset($cacheValue);
  1244. if ($perf !== null) {
  1245. foreach ($perf as $key => $item) {
  1246. $value[$key] = $item;
  1247. // $value['TOTAL_SCORE'] += $item;
  1248. unset($key, $item);
  1249. }
  1250. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  1251. }
  1252. unset($userId, $periodNum, $perf, $cacheKey);
  1253. return $value;
  1254. }
  1255. /**
  1256. * 获取直推的所有子会员
  1257. * @param $userId
  1258. * @param $periodNum
  1259. * @return array
  1260. */
  1261. public static function getChildrenOneDeepFromRedis($userId, $periodNum){
  1262. $key = self::REDIS_KEY_PREFIX_USER_INFO_CHILD_ONE_DEEP . $periodNum;
  1263. $data = Yii::$app->redis->hget($key, $userId);
  1264. if(!$data){
  1265. $list = UserInfo::findUseDbCalc()->select('USER_ID')->where('REC_UID=:REC_UID', [
  1266. 'REC_UID' => $userId
  1267. ])->asArray()->all();
  1268. $data = Json::encode($list);
  1269. unset($list);
  1270. Yii::$app->redis->hset($key, $userId, $data);
  1271. }
  1272. unset($key, $userId, $periodNum);
  1273. return $data ? Json::decode($data, true) : [];
  1274. }
  1275. /**
  1276. * 每个区有多少个什么级别的数量
  1277. * @param $userId
  1278. * @param $periodNum
  1279. * @param null $levelNumArr
  1280. * @return array|mixed
  1281. */
  1282. public static function hasEmpLevelNum($userId, $periodNum, $levelNumArr = null) {
  1283. $key = self::REDIS_KEY_PREFIX_EMP_NUM_PERF . $periodNum;
  1284. $field = $userId;
  1285. $cacheValue = Yii::$app->redis->hget($key, $field);
  1286. if ($cacheValue) {
  1287. $value = Json::decode($cacheValue);
  1288. } else {
  1289. $allEmpLevel = EmployLevel::getFromCache();
  1290. $value = [];
  1291. $relationList = self::getChildrenOneDeepFromRedis($userId, $periodNum);
  1292. foreach ($relationList as $relationData) {
  1293. foreach ($allEmpLevel as $level) {
  1294. // 从月度业绩表中找到所有下级的各个部门的数量
  1295. $value[$relationData['USER_ID']][$level['ID']] = 0;
  1296. }
  1297. }
  1298. }
  1299. if ($levelNumArr != null) {
  1300. foreach ($levelNumArr as $departUserId => $item) {
  1301. foreach ($item as $levelID => $num) {
  1302. if( isset($value[$departUserId][$levelID]) ) {
  1303. $value[$departUserId][$levelID] += $num;
  1304. }else {
  1305. $value[$departUserId][$levelID] = $num;
  1306. }
  1307. }
  1308. }
  1309. }
  1310. Yii::$app->redis->hset($key, $field, Json::encode($value));
  1311. if ($levelNumArr == null) return $value;
  1312. }
  1313. /**
  1314. * 会员是否本月注册
  1315. * @param $userId
  1316. * @param $periodNum
  1317. * @return bool
  1318. * @throws \yii\db\Exception
  1319. */
  1320. public static function isMonthJoinFromRedis($userId, $periodNum) {
  1321. $period = Period::instance();
  1322. $calcYearMonth = $period->getYearMonth($periodNum);
  1323. $userInfo = self::getUserInfo($userId, $periodNum);
  1324. $addYearMonth = $period->getYearMonth($userInfo['PERIOD_NUM']);
  1325. //当月注册
  1326. if ($addYearMonth == $calcYearMonth) return true;
  1327. //上月注册
  1328. $calcLastYearMonth = $period->getLastMonth($periodNum);
  1329. if ($addYearMonth == $calcLastYearMonth['yearMonth']) return true;
  1330. // 未来注册
  1331. if ($addYearMonth > $calcYearMonth) return true;
  1332. return false;
  1333. }
  1334. /**
  1335. * 荣衔奖最大比例
  1336. * @param $userId
  1337. * @param $periodNum
  1338. * @param int $percent
  1339. * @return float|int
  1340. */
  1341. public static function ycMaxBonusPercent($userId, $periodNum, $percent=0) {
  1342. $cacheKey = self::REDIS_KEY_PREFIX_YC_BONUS . $periodNum;
  1343. $value = Yii::$app->redis->hget($cacheKey, $userId);
  1344. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  1345. if ( $percent > 0 && $percent > $maxPercent ) {
  1346. $maxPercent = $percent;
  1347. unset($periodNum, $percent);
  1348. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  1349. return $maxPercent;
  1350. }
  1351. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  1352. return $maxPercent;
  1353. }
  1354. /**
  1355. * 服务奖最大比例
  1356. * @param $userId
  1357. * @param $periodNum
  1358. * @param int $percent
  1359. * @return float|int
  1360. */
  1361. public static function fwMaxBonusPercent($userId, $periodNum, $percent=0) {
  1362. $cacheKey = self::REDIS_KEY_PREFIX_FW_BONUS . $periodNum;
  1363. $value = Yii::$app->redis->hget($cacheKey, $userId);
  1364. $maxPercent = floatval($value)>0 ? floatval($value) : 0;
  1365. if ( $percent > 0 && $percent > $maxPercent ) {
  1366. $maxPercent = $percent;
  1367. unset($periodNum, $percent);
  1368. Yii::$app->redis->hset($cacheKey, $userId, $maxPercent);
  1369. return $maxPercent;
  1370. }
  1371. unset($userId, $periodNum, $oriBonus, $cacheKey, $value);
  1372. return $maxPercent;
  1373. }
  1374. /**
  1375. * 奖金缓存
  1376. * @param $userId
  1377. * @param $periodNum
  1378. * @param string $bonusType
  1379. * @param float $oriBonus
  1380. * @param array $deductData
  1381. * @return array|mixed
  1382. */
  1383. public static function bonus($userId, $periodNum, $bonusType = null, $oriBonus = 0.00, $deductData=[], $fromMeans='') {
  1384. $cacheKey = self::REDIS_KEY_PREFIX_BONUS . $periodNum;
  1385. $value = [
  1386. 'BONUS_BD' => 0,
  1387. 'BONUS_TG' => 0,
  1388. 'BONUS_XF' => 0,
  1389. 'BONUS_YJ' => 0,
  1390. 'BONUS_GX' => 0,
  1391. 'BONUS_GL' => 0,
  1392. 'BONUS_QY' => 0,
  1393. 'BONUS_YC' => 0,
  1394. 'BONUS_VIP' => 0,
  1395. 'BONUS_BS' => 0,
  1396. 'BONUS_YC_EXTRA' => 0,
  1397. 'ORI_BONUS_BD' => 0,
  1398. 'ORI_BONUS_BS' => 0,
  1399. 'BONUS_BS_MNT' => 0,
  1400. 'BONUS_BS_ABBR' => 0,
  1401. 'ORI_BONUS_BS_MNT' => 0,
  1402. 'ORI_BONUS_BS_ABBR' => 0,
  1403. 'ORI_BONUS_TG' => 0,
  1404. 'ORI_BONUS_XF' => 0,
  1405. 'ORI_BONUS_YJ' => 0,
  1406. 'ORI_BONUS_YJ_BD' => 0,
  1407. 'ORI_BONUS_YJ_FX' => 0,
  1408. 'ORI_BONUS_GX' => 0,
  1409. 'ORI_BONUS_GL' => 0,
  1410. 'ORI_BONUS_GL_BD' => 0,
  1411. 'ORI_BONUS_GL_FX' => 0,
  1412. 'ORI_BONUS_QY' => 0,
  1413. 'ORI_BONUS_QY_BD' => 0,
  1414. 'ORI_BONUS_QY_FX' => 0,
  1415. 'ORI_BONUS_YC' => 0,
  1416. 'ORI_BONUS_VIP' => 0,
  1417. 'ORI_BONUS_YC_EXTRA' => 0,
  1418. // 'INCOME_TG_TOTAL' => 0,
  1419. // 'INCOME_YJ_TOTAL' => 0,
  1420. // 'INCOME_QY_TOTAL' => 0,
  1421. 'INCOME_TOTAL' => 0,
  1422. 'BONUS_TOTAL' => 0,
  1423. 'RECONSUME_POINTS' => 0,
  1424. 'MANAGE_TAX' => 0,
  1425. 'ORI_CAPPED_BONUS_QY' => 0,// 团队奖,封顶前金额
  1426. //没有用到的
  1427. 'BONUS_FX' => 0,
  1428. 'BONUS_LS' => 0,
  1429. 'BONUS_CF' => 0,
  1430. 'BONUS_LX' => 0,
  1431. 'BONUS_HB' => 0,
  1432. 'BONUS_BT' => 0,
  1433. 'BONUS_BT_PROD' => 0,
  1434. 'BONUS_BT_TOOL' => 0,
  1435. 'DEDUCT_ZR' => 0,
  1436. 'BONUS_FL' => 0,
  1437. ];
  1438. // 从 redis 中获取当前的结果
  1439. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1440. if ($cacheValue) {
  1441. $cacheValue = Json::decode($cacheValue);
  1442. $value = $cacheValue;
  1443. }
  1444. unset($cacheValue);
  1445. if ($oriBonus > 0) {
  1446. $oriBonusType = sprintf('ORI_%s', $bonusType);
  1447. $value[$oriBonusType] += $oriBonus;
  1448. if( $fromMeans !== '' ) {
  1449. $oriBonusMeansType = sprintf('ORI_%s_%s', $bonusType, $fromMeans);
  1450. $value[$oriBonusMeansType] += $oriBonus;
  1451. }
  1452. if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  1453. if( $deductData ) {
  1454. $value[$bonusType] += $deductData['surplus'];
  1455. $value['RECONSUME_POINTS'] += $deductData['reConsumePoints'];
  1456. $value['MANAGE_TAX'] += $deductData['manageTax'];
  1457. }else {
  1458. $value[$bonusType] += $oriBonus;
  1459. }
  1460. $value['BONUS_TOTAL'] += $oriBonus;
  1461. }
  1462. if( in_array($bonusType, self::INCOME_BONUS_LIST) ) {
  1463. self::addHasIncomeUsers($userId, $periodNum);
  1464. // if( !in_array($bonusType, self::NOT_SEND_BONUS_LIST) ) {
  1465. // $value['INCOME_TOTAL'] += $oriBonus;
  1466. // }
  1467. }
  1468. Yii::$app->redis->hset($cacheKey, $userId, Json::encode($value));
  1469. unset($oriBonusType);
  1470. self::addHasBonusUsers($userId, $periodNum);
  1471. }
  1472. unset($userId, $periodNum, $bonusType, $oriBonus, $deductData, $cacheKey);
  1473. return $value;
  1474. }
  1475. public static function standardBonus($userId, $periodNum, $bonus=0.00) {
  1476. $cacheKey = self::REDIS_KEY_PREFIX_STANDARD_BONUS . $periodNum;
  1477. $value = 0.00;
  1478. if( $bonus > 0 ) {
  1479. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1480. $value = $bonus;
  1481. //加入有奖金的会员中
  1482. self::addHasBonusUsers($userId, $periodNum);
  1483. }else {
  1484. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1485. if ($cacheValue) {
  1486. $value = $cacheValue;
  1487. }
  1488. unset($cacheValue);
  1489. }
  1490. unset($cacheKey, $userId, $periodNum, $bonus);
  1491. return $value;
  1492. }
  1493. public static function tourismBonus($userId, $periodNum, $bonus = 0.00) {
  1494. $cacheKey = self::REDIS_KEY_PREFIX_TOURISM_BONUS . $periodNum;
  1495. $value = 0.00;
  1496. if( $bonus > 0 ) {
  1497. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1498. $value = $bonus;
  1499. //加入有奖金的会员中
  1500. self::addHasBonusUsers($userId, $periodNum);
  1501. }else {
  1502. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1503. if ($cacheValue) {
  1504. $value = $cacheValue;
  1505. }
  1506. unset($cacheValue);
  1507. }
  1508. unset($cacheKey, $userId, $periodNum, $bonus);
  1509. return $value;
  1510. }
  1511. public static function garageBonus($userId, $periodNum, $bonus = 0.00) {
  1512. $cacheKey = self::REDIS_KEY_PREFIX_GARAGE_BONUS . $periodNum;
  1513. $value = 0.00;
  1514. if( $bonus > 0 ) {
  1515. Yii::$app->redis->hset($cacheKey, $userId, $bonus);
  1516. $value = $bonus;
  1517. //加入有奖金的会员中
  1518. self::addHasBonusUsers($userId, $periodNum);
  1519. }else {
  1520. $cacheValue = \Yii::$app->redis->hget($cacheKey, $userId);
  1521. if ($cacheValue) {
  1522. $value = $cacheValue;
  1523. }
  1524. unset($cacheValue);
  1525. }
  1526. unset($cacheKey, $userId, $periodNum, $bonus);
  1527. return $value;
  1528. }
  1529. /**
  1530. * 设置车房总系数
  1531. * @param $periodNum
  1532. * @param $value
  1533. */
  1534. public static function setCFTotalPercent($periodNum, $value) {
  1535. Yii::$app->redis->set(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum, $value);
  1536. }
  1537. /**
  1538. * 获得车房总系数
  1539. * @param $periodNum
  1540. * @return mixed
  1541. */
  1542. public static function getCFTotalPercent($periodNum) {
  1543. return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_CF_TOTAL_PERCENT . $periodNum);
  1544. }
  1545. /**
  1546. * 设置领袖总系数
  1547. * @param $periodNum
  1548. * @param $value
  1549. */
  1550. public static function setLXTotalPercent($periodNum, $value) {
  1551. Yii::$app->redis->set(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum, $value);
  1552. }
  1553. /**
  1554. * 获得领袖总系数
  1555. * @param $periodNum
  1556. * @return mixed
  1557. */
  1558. public static function getLXTotalPercent($periodNum) {
  1559. return Yii::$app->redis->get(self::REDIS_KEY_PREFIX_LX_TOTAL_PERCENT . $periodNum);
  1560. }
  1561. /**
  1562. * 添加上2代共享奖关系
  1563. * @param $bonusUserId
  1564. * @param $fromUserId
  1565. * @param $periodNum
  1566. * @param $validDeep
  1567. * @return boolean
  1568. */
  1569. public static function addShareBonusOneRelation($bonusUserId, $fromUserId, $periodNum, $validDeep) {
  1570. if ($validDeep == 1) {
  1571. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
  1572. }else {
  1573. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
  1574. }
  1575. $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);
  1576. $fromUserData = [];
  1577. if( $fromUserDataJson ) {
  1578. $fromUserData = Json::decode($fromUserDataJson, true);
  1579. }
  1580. unset($fromUserDataJson);
  1581. $fromUserData[] = $bonusUserId;
  1582. Yii::$app->redis->hset($cacheKey, $fromUserId, Json::encode($fromUserData));
  1583. unset($fromUserData, $bonusUserId, $fromUserId, $periodNum);
  1584. return true;
  1585. }
  1586. /**
  1587. * 获取上2代共享奖关系
  1588. * @param $fromUserId
  1589. * @param $periodNum
  1590. * @param $validDeep
  1591. * @return array
  1592. */
  1593. public static function getShareBonusOneRelation($fromUserId, $periodNum, $validDeep) {
  1594. if ($validDeep == 1) {
  1595. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_ONE_LIST_DATA . $periodNum;
  1596. }else {
  1597. $cacheKey = self::REDIS_KEY_PREFIX_GX_BONUS_DEEP_TWO_LIST_DATA . $periodNum;
  1598. }
  1599. $fromUserDataJson = Yii::$app->redis->hget($cacheKey, $fromUserId);
  1600. $bonusUserData = [];
  1601. if( $fromUserDataJson ) {
  1602. $bonusUserData = Json::decode($fromUserDataJson, true);
  1603. }
  1604. unset($fromUserDataJson);
  1605. return $bonusUserData;
  1606. }
  1607. }