CalcCache.php 67 KB

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