CalcServeBonusCalc.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/8/2
  6. * Time: 上午10:32
  7. */
  8. namespace common\helpers\bonus;
  9. use common\helpers\Cache;
  10. use common\helpers\Date;
  11. use common\helpers\snowflake\SnowFake;
  12. use common\helpers\Tool;
  13. use common\models\CalcBonus;
  14. use common\models\CalcBonusBD;
  15. use common\models\CalcBonusBS;
  16. use common\models\CalcBonusQuarter;
  17. use common\models\CalcBonusGarage;
  18. use common\models\CalcBonusQY;
  19. use common\models\CalcBonusTG;
  20. use common\models\CalcBonusTourism;
  21. use common\models\CalcBonusVilla;
  22. use common\models\Config;
  23. use common\models\EmployLevel;
  24. use common\models\PerfMonth;
  25. use common\models\PerfPeriod;
  26. use common\models\Period;
  27. use common\models\ServeProcess;
  28. use common\models\StarCrownLevel;
  29. use Exception;
  30. use yii\base\BaseObject;
  31. use yii\base\StaticInstanceTrait;
  32. use yii\db\Query;
  33. class CalcServeBonusCalc extends BaseObject {
  34. use StaticInstanceTrait;
  35. private $_limit = 3000;
  36. private $_handleUserId;
  37. private $_sysConfig = [];
  38. private $_decLevelConfig = [];
  39. private $_empLevelConfig = [];
  40. private $_starCrownLevelConfig = [];
  41. private $_decRoleConfig = [];
  42. private $_errors = [];
  43. private $_periodNum = 0;
  44. private $_periodId;
  45. private $_isCalcMonth = 0;
  46. private $_calcYear;
  47. private $_calcMonth;
  48. private $_calcYearMonth;
  49. private $_calcMonthPeriodNumCount = 0;
  50. private $_isPerpare;
  51. //pv
  52. private $_pvRatio;
  53. private $_calcZone = ['openTravel', 'openCar', 'openHouse'];
  54. const LOOP_FINISH = 1;
  55. const LOOP_CONTINUE = 2;
  56. const ORDER_TYPE_TO_FW_COEFFICIENT = [
  57. 'ZC' => 'fwCoefficientFromZc',
  58. 'FX_CASH' => 'fwCoefficientFromFxCash',
  59. 'FX_POINT' => 'fwCoefficientFromFxPoint',
  60. ];
  61. //最小报单pv
  62. const MIN_BD_PV = 980;
  63. public function init() {
  64. parent::init();
  65. }
  66. /**
  67. * 设置期数
  68. * @param int $periodNum
  69. * @return int
  70. */
  71. public function setPeriodNum(int $periodNum) {
  72. return $this->_periodNum = $periodNum;
  73. }
  74. /**
  75. * 获取期数
  76. * @return int
  77. */
  78. public function getPeriodNum() {
  79. return $this->_periodNum;
  80. }
  81. /**
  82. * 加入错误错误
  83. * @param $attr
  84. * @param $error
  85. */
  86. public function addError($attr, $error) {
  87. $this->_errors[$attr][] = $error;
  88. }
  89. /**
  90. * 获取错误信息
  91. * @return array
  92. */
  93. public function getErrors() {
  94. return $this->_errors;
  95. }
  96. /**
  97. * 开始执行结算步骤
  98. * @param $periodNum
  99. * @param null $handleUserId
  100. * @return bool
  101. */
  102. public function calcStep() {
  103. try {
  104. $this->_errors = [];
  105. $this->setPeriodNum(0);
  106. $this->_handleUserId = '';
  107. $t1 = microtime(true);
  108. // 初始化结算任务
  109. $this->initCalcTask();
  110. // 判断是否能开始进行计算奖金
  111. $checkStart = $this->checkStart();
  112. if (!$checkStart) {
  113. return false;
  114. } else {
  115. // 更新状态为,奖金计算中
  116. Period::updateCalcProcess(4, $this->_periodNum);
  117. }
  118. $t2 = microtime(true);
  119. ServeProcess::recordProcess($t1, $t2, $this->_periodNum, '奖金计算初始化配置', 'bonus');
  120. // 设置结算状态
  121. $this->setCalcStatus('start');
  122. // 清空所有本期结算用到的缓存
  123. CalcCache::clearCalcBonusCache($this->_periodNum);
  124. $t3 = microtime(true);
  125. ServeProcess::recordProcess($t2, $t3, $this->_periodNum, '设置结算状态,清空缓存', 'bonus');
  126. // 清空相关表数据
  127. $this->clearCalcTableData();
  128. $t4 = microtime(true);
  129. ServeProcess::recordProcess($t3, $t4, $this->_periodNum, '清空相关表数据', 'bonus');
  130. echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t4 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  131. $this->_updatePercent(10);
  132. // 蓝星奖放到最前面 奖金计算开始
  133. if($this->_sysConfig['openGL']['VALUE']) {
  134. echo('计算蓝星奖开始,' . date('Y-m-d H:i:s', $t4) . PHP_EOL);
  135. // 调用存储过程,计算蓝星管理奖金
  136. $this->calcBsProcedure();
  137. // 将有蓝星管理奖金的用户加入到有奖金缓存用户中
  138. $this->calcBonusBsGL();
  139. if ($this->_isCalcMonth) {
  140. ServeProcess::recordProcess($t4, time(), $this->_periodNum, '计算蓝星奖', 'bonus');
  141. }
  142. }
  143. $t5 = microtime(true);
  144. echo('计算蓝星奖'.($this->_sysConfig['openGL']['VALUE']?'完成':'关闭').',耗时:' . round($t5 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  145. if($this->_sysConfig['openFW']['VALUE']) {
  146. $this->calcBonusBDStepOne();
  147. $this->calcBonusBDStepTwo();
  148. ServeProcess::recordProcess($t5, time(), $this->_periodNum, '计算服务奖', 'bonus');
  149. }
  150. $t6 = microtime(true);
  151. echo('计算服务奖'.($this->_sysConfig['openFW']['VALUE']?'完成':'关闭').',耗时:' . round($t6 - $t5, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  152. $this->_updatePercent(15);
  153. // 销售奖/推广奖
  154. if($this->_sysConfig['openTG']['VALUE']) {
  155. $this->calcBonusTG();
  156. ServeProcess::recordProcess($t6, time(), $this->_periodNum, '计算推广奖', 'bonus');
  157. }
  158. $t7 = microtime(true);
  159. echo('计算推广奖'.($this->_sysConfig['openTG']['VALUE']?'完成':'关闭').',耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  160. $this->_updatePercent(20);
  161. // 绩效奖/团队奖
  162. if($this->_sysConfig['openQY']['VALUE']) {
  163. $this->calcBonusQY();
  164. ServeProcess::recordProcess($t7, time(), $this->_periodNum, '计算团队奖', 'bonus');
  165. }
  166. $t8 = microtime(true);
  167. echo('计算团队奖'.($this->_sysConfig['openQY']['VALUE']?'完成':'关闭').',耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  168. $this->_updatePercent(35);
  169. // $this->calcBonusTourism($this->_sysConfig['openTourism']);
  170. // $t21 = microtime(true);
  171. // echo('计算旅游奖' . ($this->_sysConfig['openTourism']['VALUE'] ? '完成' : '关闭') . ',耗时:' . round($t21 - $t20, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  172. // $this->_updatePercent(68);
  173. if($this->_sysConfig['openVilla']['VALUE']) {
  174. $this->calcBonusVilla();
  175. ServeProcess::recordProcess($t8, time(), $this->_periodNum, '计算房奖', 'bonus');
  176. }
  177. $t22 = microtime(true);
  178. echo('计算房奖' . ($this->_sysConfig['openVilla']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t22 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  179. $this->_updatePercent(45);
  180. if($this->_sysConfig['openGarage']['VALUE']) {
  181. $this->calcBonusGarage();
  182. ServeProcess::recordProcess($t22, time(), $this->_periodNum, '计算车奖', 'bonus');
  183. }
  184. $t23 = microtime(true);
  185. echo('计算车奖' . ($this->_sysConfig['openGarage']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t23 - $t22, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  186. $this->_updatePercent(55);
  187. // 计算季度奖
  188. if($this->_sysConfig['openQuarter']['VALUE']) {
  189. $this->calcQuarter();
  190. ServeProcess::recordProcess($t23, time(), $this->_periodNum, '计算季度奖-调用存储过程', 'bonus');
  191. }
  192. $t24 = microtime(true);
  193. echo('计算季度奖' . ($this->_sysConfig['openQuarter']['VALUE'] ? '开启调用存储过程' : '关闭').',耗时:' . round($t24 - $t23, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  194. // 将用户写入缓存
  195. if($this->_sysConfig['openQuarter']['VALUE']) {
  196. $this->calcQuarterUser();
  197. ServeProcess::recordProcess($t24, time(), $this->_periodNum, '计算季度奖-存入奖金会员', 'bonus');
  198. }
  199. $this->_updatePercent(65);
  200. $t25 = microtime(true);
  201. echo('计算季度奖' . ($this->_sysConfig['openQuarter']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t25 - $t24, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  202. // 奖金写库
  203. $this->loopBonusUsers();
  204. $this->_updatePercent(75);
  205. $t30 = microtime(true);
  206. ServeProcess::recordProcess($t25, $t30, $this->_periodNum, '奖金写库', 'bonus');
  207. echo('奖金写库操作完成,耗时:' . round($t30 - $t25, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  208. ServeProcess::recordProcess($t30, time(), $this->_periodNum, '标记为计算完成', 'bonus');
  209. $this->endCalcTask();
  210. $this->_updatePercent(100);
  211. $t35 = microtime(true);
  212. echo('结算全部完成,共耗时:' . round($t35 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL);
  213. } catch (\Exception $e) {
  214. $this->errorCalcTask();
  215. $this->addError('calc', sprintf('File【%s】, Line【%s】, Msg【%s】', $e->getFile(), $e->getLine(), $e->getMessage()));
  216. return false;
  217. }
  218. return true;
  219. }
  220. /**
  221. * 结算完成
  222. */
  223. public function endCalcTask() {
  224. $this->setCalcStatus('end');// 更新结算状态
  225. }
  226. /**
  227. * 结算错误
  228. */
  229. public function errorCalcTask() {
  230. CalcCache::clearCalcBonusCache($this->_periodNum); // 清空所有本期结算用到的缓存
  231. $this->setCalcStatus('fail'); // 更新结算状态
  232. }
  233. /**
  234. * 初始化结算任务
  235. * @throws \yii\db\Exception
  236. */
  237. public function initCalcTask() {
  238. $periodObj = Period::instance();
  239. $periodDataArr = $periodObj->setPeriodNum($this->_periodNum);
  240. if (empty($this->_periodNum)) {
  241. $this->_periodNum = $periodDataArr['PERIOD_NUM'];
  242. }
  243. $this->_sysConfig = Cache::getSystemConfig();
  244. $this->_decLevelConfig = Cache::getDecLevelConfig();
  245. $this->_empLevelConfig = Cache::getEmpLevelConfig();
  246. $this->_starCrownLevelConfig = Cache::getStarCrownLevelConfig();
  247. $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum);
  248. $this->_periodId = $periodDataArr['ID'];
  249. $this->_isPerpare = $periodDataArr['IS_PREPARE'];
  250. $this->_isCalcMonth = $periodObj->isCalcMonth($this->_periodNum);
  251. $this->_calcYear = $periodObj->getYear($this->_periodNum);
  252. $this->_calcMonth = $periodObj->getMonth($this->_periodNum);
  253. $this->_calcYearMonth = $periodObj->getYearMonth($this->_periodNum);
  254. }
  255. // 校验是否能开始进行计算
  256. public function checkStart() {
  257. if ($this->_isPerpare == 3) {
  258. return true;
  259. }
  260. return false;
  261. }
  262. /**
  263. * 设置结算状态
  264. * @param $type
  265. * start|end|fail
  266. */
  267. public function setCalcStatus($type) {
  268. if ($type == 'start') {
  269. Period::updateAll(['IS_CALCING' => 1, 'IS_CALCULATED' => Period::CALCULATE_NONE, 'CALCULATE_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  270. } elseif ($type == 'end') {
  271. Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FINISH, 'CALCULATED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  272. } elseif ($type == 'fail') {
  273. Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FAIL, 'CALCULATED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  274. }
  275. }
  276. /**
  277. * 清空相关表数据
  278. */
  279. public function clearCalcTableData() {
  280. // 奖金表
  281. CalcBonus::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  282. CalcBonusQY::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  283. CalcBonusBD::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); // 实际上是服务奖流水表
  284. CalcBonusTG::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  285. // 月结时要清空的数据
  286. if ($this->_isCalcMonth) {
  287. CalcBonusTourism::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  288. CalcBonusGarage::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  289. CalcBonusVilla::pageDeleteAll('PERIOD_NUM='.$this->_periodNum);
  290. }
  291. }
  292. /**
  293. * 推广奖
  294. * @param int $offset
  295. * @return bool
  296. * @throws \yii\db\Exception
  297. */
  298. public function calcBonusTG(int $offset = 0) {
  299. $periodNum = $this->_periodNum;
  300. // 从缓存获取分页有业绩的会员信息
  301. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  302. if ($allData) {
  303. $insertBonusData = [];
  304. foreach ($allData as $userId) {
  305. // 从缓存中获取会员的业绩信息
  306. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  307. if( !$perfData ) continue;
  308. //个人业绩都算推荐奖,包括报单和复消、二次购物
  309. $perfPv = $perfData['PV_PCS_ZC'] ?? 0;
  310. if( $perfPv <= 0 ) continue;
  311. //推广奖使用个人PCS
  312. $recBonus = Tool::formatPrice($perfPv * $this->_sysConfig['recPercent']['VALUE'] / 100);
  313. if ($recBonus <= 0) continue;
  314. // 把对碰后的奖金存入缓存中
  315. $perfUserInfo = CalcCache::getUserInfo($userId, $periodNum);
  316. $bonusUserId = $perfUserInfo['REC_UID'] ?? '';
  317. if( !$bonusUserId ) continue;
  318. // 获取会员的报单级别
  319. $userBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  320. CalcCache::bonus($bonusUserId, $this->_periodNum, 'BONUS_TG', $recBonus);
  321. //来源会员信息
  322. $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  323. //推广奖流水
  324. $insertBonusData[] = [
  325. 'ID' => SnowFake::instance()->generateId(),
  326. 'USER_ID' => $bonusUserId,
  327. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  328. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  329. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  330. 'FROM_USER_ID' => $userId,
  331. 'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'],
  332. 'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'],
  333. 'LAST_FROM_STATUS' => $fromUserInfo['STATUS'],
  334. 'ORI_BONUS' => $recBonus,
  335. 'PERIOD_NUM' => $this->_periodNum,
  336. 'CALC_YEAR' => $this->_calcYear,
  337. 'CALC_MONTH' => $this->_calcYearMonth,
  338. 'CREATED_AT' => Date::nowTime(),
  339. 'LOGS' => json_encode([
  340. 'perfPv' => $perfPv,
  341. 'recPercentConfig' => $this->_sysConfig['recPercent']['VALUE'],
  342. 'decLevel' => $userBaseInfo['DEC_LV'],
  343. ]),
  344. ];
  345. unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo);
  346. }
  347. CalcBonusTG::batchInsert($insertBonusData);
  348. unset($allData, $insertBonusData);
  349. return $this->calcBonusTG($offset + $this->_limit);
  350. }
  351. unset($allData);
  352. return true;
  353. }
  354. /**
  355. * 服务奖第一步
  356. * @param int $offset
  357. * @return bool
  358. * @throws \yii\db\Exception
  359. */
  360. public function calcBonusBDStepOne(int $offset = 0) {
  361. echo sprintf("时间:[%s]服务奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  362. $periodNum = $this->_periodNum;
  363. // 从缓存获取分页有业绩的会员信息
  364. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  365. if ($allData) {
  366. $insertBonusData = [];
  367. foreach ($allData as $userId) {
  368. // 从缓存中获取会员的业绩信息
  369. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  370. if( !$perfData ) continue;
  371. $decRoleBonusFrom = explode(',', $this->_sysConfig['decRoleBonusFrom']['VALUE']);
  372. $validPvPcs = 0;
  373. foreach ($decRoleBonusFrom as $orderType) {
  374. $orderTypeName = sprintf('PV_PCS_%s', $orderType);
  375. $orderTypeValue = $perfData[$orderTypeName] ?? 0;
  376. $coefficientName = self::ORDER_TYPE_TO_FW_COEFFICIENT[$orderType];
  377. $coefficient = $this->_sysConfig[$coefficientName]['VALUE'] ?? 1;
  378. $validPvPcs += $orderTypeValue * $coefficient;
  379. unset($orderType, $orderTypeName, $orderTypeValue, $coefficientName, $coefficient);
  380. }
  381. unset($perfData, $decRoleBonusFrom);
  382. if ( $validPvPcs <= 0 ) continue;
  383. $this->loopRelationParentDo($userId, function ($parent) use($userId, $validPvPcs){
  384. try {
  385. //判断parent的报单中心级别 和 服务奖比例
  386. $bonusUserId = $parent['PARENT_UID'];
  387. //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化
  388. $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum);
  389. $isDec = $bonusUserInfo['IS_DEC'];
  390. if($isDec == 0) return self::LOOP_CONTINUE;
  391. $decRoleId = $bonusUserInfo['DEC_ROLE_ID'];
  392. if( !$decRoleId ) return self::LOOP_CONTINUE;
  393. if( !isset($this->_decRoleConfig[$decRoleId]) ) return self::LOOP_CONTINUE;
  394. $parentDecRoleLevel = $this->_decRoleConfig[$decRoleId];
  395. $parentFwBonusPercent = $parentDecRoleLevel['FW_BONUS_PERCENT'] ?? 0;
  396. $cacheMaxPercent = CalcCache::fwMaxBonusPercent($userId, $this->_periodNum);
  397. $diffPercent = $parentFwBonusPercent - $cacheMaxPercent;
  398. if( $diffPercent <= 0 ) return self::LOOP_CONTINUE;
  399. $fwBonus = $validPvPcs * $diffPercent / 100;
  400. if( $fwBonus <= 0 ) return self::LOOP_CONTINUE;
  401. //给本人添加服务奖比例
  402. CalcCache::fwMaxBonusPercent($userId, $this->_periodNum, $parentFwBonusPercent);
  403. //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加
  404. CalcCache::saveFwBonusList($bonusUserId, $this->_periodNum, $fwBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$validPvPcs]);
  405. CalcCache::addHasFwBonusUsers($bonusUserId, $this->_periodNum);
  406. } catch(Exception $e) {
  407. var_dump($e->getMessage(), '------------');
  408. }
  409. unset($bonusUserId, $bonusUserInfo, $isDec, $decRoleId, $parentDecRoleLevel, $parentFwBonusPercent, $cacheMaxPercent, $diffPercent, $fwBonus);
  410. });
  411. unset($userId, $validPvPcs);
  412. }
  413. unset($allData, $insertBonusData);
  414. return $this->calcBonusBDStepOne($offset + $this->_limit);
  415. }
  416. unset($allData);
  417. return true;
  418. }
  419. /**
  420. * 服务奖第二步
  421. * @param int $offset
  422. * @return bool
  423. * @throws \yii\db\Exception
  424. */
  425. public function calcBonusBDStepTwo(int $offset = 0) {
  426. echo sprintf("时间:[%s]服务奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  427. $allData = CalcCache::getHasFwBonusUsers($this->_periodNum, $offset, $this->_limit);
  428. if ($allData) {
  429. $insertBonusData = [];
  430. foreach ($allData as $userId) {
  431. $fwBonusData = CalcCache::getFwBonusList($userId, $this->_periodNum);
  432. if( !$fwBonusData ) continue;
  433. $fwBonus = $fwBonusData['fwBonus'] ?? 0;
  434. if( $fwBonus <=0 ) continue;
  435. //总金额限制
  436. try {
  437. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  438. CalcCache::bonus($userId, $this->_periodNum, 'BONUS_BD', $fwBonus);
  439. $decRoleId = $userBaseInfo['DEC_ROLE_ID'];
  440. $insertBonusData[] = [
  441. 'ID' => SnowFake::instance()->generateId(),
  442. 'USER_ID' => $userId,
  443. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  444. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  445. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  446. 'FROM_USER_ID' => $userId,
  447. 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'],
  448. 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'],
  449. 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'],
  450. 'AMOUNT' => $fwBonus,
  451. 'ORI_BONUS' => $fwBonus,
  452. 'RECONSUME_POINTS' => 0,
  453. 'MANAGE_TAX' => 0,
  454. 'PERIOD_NUM' => $this->_periodNum,
  455. 'CALC_YEAR' => $this->_calcYear,
  456. 'CALC_MONTH' => $this->_calcYearMonth,
  457. 'CREATED_AT' => Date::nowTime(),
  458. 'LOGS' => json_encode([
  459. 'decRoleId' => $decRoleId,
  460. ])
  461. ];
  462. } catch(Exception $e) {
  463. var_dump('---->>>', $e->getMessage());exit;
  464. }
  465. unset($userId, $fwBonusData, $userBaseInfo, $decRoleId, $fwBonus);
  466. }
  467. CalcBonusBD::batchInsert($insertBonusData);
  468. unset($insertBonusData, $allData);
  469. $this->calcBonusBDStepTwo($offset + $this->_limit);
  470. }
  471. unset($allData);
  472. return true;
  473. }
  474. /**
  475. * 团队奖
  476. * @param int $offset
  477. * @return bool
  478. * @throws \yii\db\Exception
  479. */
  480. public function calcBonusQY(int $offset = 0) {
  481. echo sprintf("时间:[%s]团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  482. $periodNum = $this->_periodNum;
  483. // 从缓存获取分页有业绩的会员信息
  484. $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit);
  485. if ($allData) {
  486. $insertBonusData = [];
  487. foreach ($allData as $userId) {
  488. // 从缓存中获取会员的业绩信息
  489. $perfData = CalcCache::nowPeriodPerf($userId, $periodNum);
  490. // 从缓存中获取会员的上期结余业绩信息
  491. $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum);
  492. // 本期 + 上期结余
  493. $perfArr = [
  494. 'SURPLUS_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  495. 'SURPLUS_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  496. 'SURPLUS_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  497. 'SURPLUS_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  498. 'SURPLUS_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  499. ];
  500. $oriPerfArr = [
  501. 'perfArr' => $perfArr,
  502. 'touchBonus' => 0,
  503. ];
  504. // 获取会员的报单级别
  505. $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  506. $decLevelConfig = $this->_decLevelConfig;
  507. $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']];
  508. // 对碰
  509. $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100);
  510. $touchPerfArr = [];
  511. foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) {
  512. $touchPerfArr[$keyR] = $perfR;
  513. }
  514. // 对碰完成后把结余的业绩存入本期业绩缓存中
  515. CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr);
  516. //更新数据库
  517. PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [
  518. 'USER_ID' => $userId,
  519. 'PERIOD_NUM' => $periodNum,
  520. ]);
  521. if ($touchBonusArr['touchBonus'] <= 0) continue;
  522. $teamBonus = $touchBonusArr['touchBonus'];
  523. $capBonusQy = $teamBonus; // 封顶前的奖金
  524. //判断级别上限,个人奖金封顶限制
  525. $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']);
  526. if( $teamBonus <= 0 ) continue;
  527. // 将封顶前的金额加入用户奖金缓存中,此金额不能发放(总奖金,总实际奖金)
  528. CalcCache::bonus($userId, $periodNum, 'CAPPED_BONUS_QY', $capBonusQy);
  529. // TODO:取小腿值
  530. $payLeg = min([$perfArr['SURPLUS_1L'], $perfArr['SURPLUS_2L']]);
  531. // 计算荣衔星级
  532. $starCrown = StarCrownLevel::getStarCrown($payLeg);
  533. // 是否活跃
  534. $isActive = $this->_isPerfActive($userId);
  535. $oriBonus = $isActive ? $teamBonus : 0;
  536. $lastCrownLv = $isActive ? $starCrown['ID'] : StarCrownLevel::getDefaultLevelId();
  537. //团队奖流水
  538. $insertBonusData[] = [
  539. 'ID' => SnowFake::instance()->generateId(),
  540. 'USER_ID' => $userId,
  541. 'ORI_CAPPED_BONUS_QY' => $capBonusQy,
  542. 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'],
  543. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  544. 'LAST_CROWN_LV' => $lastCrownLv,
  545. 'LAST_STATUS' => $userBaseInfo['STATUS'],
  546. 'AMOUNT' => $oriBonus,
  547. 'ORI_BONUS' => $oriBonus,
  548. 'PERIOD_NUM' => $this->_periodNum,
  549. 'CALC_YEAR' => $this->_calcYear,
  550. 'CALC_MONTH' => $this->_calcYearMonth,
  551. 'CREATED_AT' => Date::nowTime(),
  552. 'LOGS' => json_encode([
  553. 'perfArr' => $perfArr,
  554. 'touchPerfArrOri' => $touchBonusArr['perfArr'],
  555. 'touchPerfArr' => $touchPerfArr,
  556. 'nowDecLevelConfig' => $nowDecLevelConfig,
  557. 'decLevel' => $userBaseInfo['DEC_LV'],
  558. ]),
  559. 'IS_ACTIVE' => (int)$isActive,
  560. 'HOPE_CROWN_LV' => $starCrown['ID'],
  561. 'HOPE_BONUS' => $teamBonus,
  562. ];
  563. // 星级放入缓存
  564. CalcCache::addUserStarCrown($userId, $periodNum, $lastCrownLv);
  565. if ($oriBonus > 0) {
  566. // 把对碰后的奖金存入缓存中
  567. // CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $oriBonus, $deductData);
  568. CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus);
  569. }
  570. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  571. }
  572. CalcBonusQY::batchInsert($insertBonusData);
  573. unset($allData, $insertBonusData);
  574. return $this->calcBonusQY($offset + $this->_limit);
  575. }
  576. unset($allData);
  577. return true;
  578. }
  579. /**
  580. * 季度奖计算
  581. */
  582. public function calcQuarter() {
  583. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  584. // echo('不是季结点,进这里,不计算季度奖'. PHP_EOL);
  585. return false;
  586. }
  587. $result = \Yii::$app->db->createCommand("CALL QtrCalc(:periodNum)")
  588. ->bindValue(':periodNum' , $this->_periodNum )
  589. ->execute();
  590. return $result;
  591. }
  592. // 执行蓝星管理奖金的存储过程
  593. public function calcBsProcedure() {
  594. if( !$this->_isCalcMonth ) {
  595. // 不是结算月,则不进行计算
  596. return false;
  597. }
  598. $result = \Yii::$app->db->createCommand("CALL CalcBlue(:periodNum)")
  599. ->bindValue(':periodNum' , $this->_periodNum )
  600. ->execute();
  601. return $result;
  602. }
  603. // 执行旅游奖的计算
  604. public function calcBonusTourism() {
  605. // 月结,如果不是月结点,则直接退出
  606. if (!$this->_isCalcMonth) {
  607. return true;
  608. }
  609. $bonusConfig = $this->_sysConfig['openTourism'];
  610. // 达标条件:聘级、级别、奖项比例
  611. $config = json_decode($bonusConfig['OPTIONS'], true);
  612. // 奖金总比例
  613. $mate = $bonusConfig['VALUE'] / 100;
  614. // 会员级别
  615. $minDecLevel = $config['OPTIONS']['declarationLevel'] ?? [];
  616. // 月度公司总PV
  617. $monthTotalPV = PerfMonth::find()
  618. ->yearMonth($this->_calcYearMonth)
  619. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  620. ->sum('PV_PCS');
  621. // 用于分发的奖金总数
  622. $transferAmount = $monthTotalPV * $mate;
  623. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  624. $userStarDirector = CalcBonusBS::find()
  625. ->yearMonth($this->_calcYearMonth)
  626. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  627. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_EMP_LV,LAST_STATUS')
  628. ->groupBy('USER_ID')
  629. ->asArray()
  630. ->all();
  631. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  632. // 基于团队奖/绩效奖结果计算会员的StarCrown
  633. $userStarCrown = CalcBonusQY::find()
  634. ->yearMonth($this->_calcYearMonth)
  635. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  636. ->select('USER_ID,LAST_CROWN_LV')
  637. ->groupBy('USER_ID')
  638. ->asArray()
  639. ->all();
  640. $userStarCrownObj = array_column($userStarCrown, NULL, 'USER_ID');
  641. // 合并用户ID,去重
  642. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  643. // 奖金点数综合
  644. $bonusPointComplex = 0;
  645. $insertBonusData = [];
  646. foreach($bonusUsers as $userId) {
  647. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  648. $starDirectorPoint = $this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['TOURISM_PERCENT'] ?? 0;
  649. $starCrownPoint = $this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['TOURISM_PERCENT'] ?? 0;
  650. // 奖金比例:
  651. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  652. if ($bonusPoint <= 0) {
  653. continue;
  654. }
  655. $insertBonusData[] = [
  656. 'ID' => SnowFake::instance()->generateId(),
  657. 'USER_ID' => $userId,
  658. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'],
  659. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LEVEL_ID'],
  660. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'],
  661. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'],
  662. 'AMOUNT_STANDARD' => 0,
  663. 'POINT' => $bonusPoint,
  664. 'PERIOD_NUM' => $this->_periodNum,
  665. 'CALC_YEAR' => $this->_calcYear,
  666. 'CALC_MONTH' => $this->_calcYearMonth,
  667. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  668. 'CREATED_AT' => Date::nowTime(),
  669. 'PERF' => $monthTotalPV,
  670. 'TRANSFER_RATE' => $mate,
  671. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  672. 'CAP_AMOUNT' => 0,
  673. 'POINT_COMPLEX' => 0,
  674. ];
  675. $bonusPointComplex += $bonusPoint;
  676. }
  677. // 数据写入总表
  678. if ($insertBonusData) {
  679. foreach ($insertBonusData as &$bonusData) {
  680. // 计算奖金
  681. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  682. if ($amount <= 0) {
  683. continue;
  684. }
  685. // 会员级别达到要求才会发放奖金
  686. if ($bonusData['LAST_DEC_LV'] == $minDecLevel) {
  687. // 放入缓存
  688. CalcCache::tourismBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  689. }
  690. $bonusData['AMOUNT'] = $amount;
  691. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  692. }
  693. CalcBonusTourism::batchInsert($insertBonusData);
  694. }
  695. return true;
  696. }
  697. // 执行房奖的计算
  698. public function calcBonusVilla() {
  699. if (!$this->_isCalcMonth) {
  700. return true;
  701. }
  702. $bonusConfig = $this->_sysConfig['openVilla'];
  703. // 达标条件:聘级、级别、奖项比例
  704. $config = json_decode($bonusConfig['OPTIONS'], true);
  705. // 奖金总比例
  706. $mate = $bonusConfig['VALUE'] / 100;
  707. // 个人奖金封顶
  708. $capBonus = intval($this->_sysConfig['openVillaCap']['VALUE'] ?? 0);
  709. // 会员级别
  710. $minDecLevel = $config['declarationLevel'] ?? [];
  711. // 月度公司总PV
  712. $monthTotalPV = PerfMonth::find()
  713. ->yearMonth($this->_calcYearMonth)
  714. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  715. ->sum('PV_PCS');
  716. // 用于分发的奖金总数
  717. $transferAmount = $monthTotalPV * $mate;
  718. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  719. $subQuery = CalcBonusQY::find()
  720. ->yearMonth($this->_calcYearMonth)
  721. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  722. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  723. ->joinWith(['starCrown' => function($query) {
  724. $query->select(['LEVEL_NAME', 'SORT']);
  725. }])
  726. ->having(1)
  727. ->orderBy('USER_ID ASC, SORT DESC');
  728. $userStarCrownObj = (new Query())->from(['u' => $subQuery])->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')->groupBy('USER_ID')->indexBy('USER_ID')->all();
  729. // 奖金点数综合
  730. $bonusPointComplex = 0;
  731. $insertBonusData = [];
  732. foreach($userStarCrownObj as $item) {
  733. // 奖金比例
  734. $bonusPoint = $this->_starCrownLevelConfig[$item['LAST_CROWN_LV']]['VILLA_PERCENT'] ?? 0;
  735. if (!$bonusPoint) {
  736. continue;
  737. }
  738. // 会员级别达到要求才会发放奖金
  739. if ($item['LAST_DEC_LV'] != $minDecLevel) {
  740. continue;
  741. }
  742. $userBaseInfo = CalcCache::getUserInfo($item['USER_ID'], $this->_periodNum);
  743. $insertBonusData[] = [
  744. 'ID' => SnowFake::instance()->generateId(),
  745. 'USER_ID' => $item['USER_ID'],
  746. 'LAST_DEC_LV' => $item['LAST_DEC_LV'] ?? '',
  747. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  748. 'LAST_STATUS' => $item['LAST_STATUS'] ?? 0,
  749. 'LAST_CROWN_LV' => $item['LAST_CROWN_LV'] ?? '',
  750. 'AMOUNT' => 0,
  751. 'POINT' => $bonusPoint,
  752. 'PERIOD_NUM' => $this->_periodNum,
  753. 'CALC_YEAR' => $this->_calcYear,
  754. 'CALC_MONTH' => $this->_calcYearMonth,
  755. 'CREATED_AT' => Date::nowTime(),
  756. 'PERF' => $monthTotalPV,
  757. 'TRANSFER_RATE' => $mate,
  758. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  759. 'CAP_AMOUNT' => 0,
  760. 'POINT_COMPLEX' => 0,
  761. ];
  762. $bonusPointComplex += $bonusPoint;
  763. }
  764. // 数据写入总表
  765. if ($insertBonusData) {
  766. // 计算个人奖金
  767. foreach ($insertBonusData as &$bonusData) {
  768. // 计算奖金
  769. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  770. if ($amount <= 0) {
  771. continue;
  772. }
  773. // 封顶前奖金数
  774. $capAmount = $amount;
  775. // 奖金数不能大于封顶值
  776. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  777. $bonusData['AMOUNT'] = $amount;
  778. $bonusData['CAP_AMOUNT'] = $capAmount;
  779. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  780. // 放入缓存
  781. CalcCache::villaBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  782. }
  783. CalcBonusVilla::batchInsert($insertBonusData);
  784. }
  785. return true;
  786. }
  787. // 执行车奖的计算
  788. public function calcBonusGarage() {
  789. // 月结,如果不是月结点,则直接退出
  790. if (!$this->_isCalcMonth) {
  791. return true;
  792. }
  793. $bonusConfig = $this->_sysConfig['openGarage'];
  794. // 达标条件:聘级、级别、奖项比例
  795. $config = json_decode($bonusConfig['OPTIONS'], true);
  796. // 奖金总比例
  797. $mate = $bonusConfig['VALUE'] / 100;
  798. // 会员级别
  799. $minDecLevel = $config['declarationLevel'] ?? [];
  800. // 个人奖金封顶
  801. $capBonus = intval($this->_sysConfig['openGarageCap']['VALUE'] ?? 0);
  802. // 月度公司总PV
  803. $monthTotalPV = PerfMonth::find()
  804. ->yearMonth($this->_calcYearMonth)
  805. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  806. ->sum('PV_PCS');
  807. // 用于分发的奖金总数
  808. $transferAmount = $monthTotalPV * $mate;
  809. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  810. $userStarDirector = CalcBonusBS::find()
  811. ->yearMonth($this->_calcYearMonth)
  812. ->where('CALC_MONTH = :CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  813. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_STATUS')
  814. ->groupBy('USER_ID')
  815. ->asArray()
  816. ->all();
  817. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  818. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  819. $subQuery = CalcBonusQY::find()
  820. ->yearMonth($this->_calcYearMonth)
  821. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  822. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  823. ->joinWith(['starCrown' => function($query) {
  824. $query->select(['LEVEL_NAME', 'SORT']);
  825. }])
  826. ->having(1)
  827. ->orderBy('USER_ID ASC, SORT DESC');
  828. $userStarCrownObj = (new Query())->from(['u' => $subQuery])->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')->groupBy('USER_ID')->indexBy('USER_ID')->all();
  829. // 合并用户ID,去重
  830. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  831. sort($bonusUsers);
  832. // 奖金点数综合
  833. $bonusPointComplex = 0;
  834. $insertBonusData = [];
  835. foreach($bonusUsers as $userId) {
  836. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  837. $starDirectorPoint = !isset($userStarDirectorObj[$userId]['LEVEL_ID']) ? 0 : ($this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['GARAGE_PERCENT'] ?? 0);
  838. $starCrownPoint = !isset($userStarCrownObj[$userId]['LAST_CROWN_LV']) ? 0: ($this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['GARAGE_PERCENT'] ?? 0);
  839. // 奖金比例:
  840. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  841. if ($bonusPoint <= 0) {
  842. continue;
  843. }
  844. // 会员级别达到要求才会发放奖金
  845. $lastDecLv = $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? '');
  846. if ($lastDecLv != $minDecLevel) {
  847. continue;
  848. }
  849. $insertBonusData[] = [
  850. 'ID' => SnowFake::instance()->generateId(),
  851. 'USER_ID' => $userId,
  852. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? ''),
  853. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LEVEL_ID'] ?? '',
  854. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'] ?? ($userStarCrownObj[$userId]['LAST_STATUS'] ?? 1),
  855. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'] ?? '',
  856. 'AMOUNT' => 0,
  857. 'POINT' => $bonusPoint,
  858. 'PERIOD_NUM' => $this->_periodNum,
  859. 'CALC_YEAR' => $this->_calcYear,
  860. 'CALC_MONTH' => $this->_calcYearMonth,
  861. 'CREATED_AT' => Date::nowTime(),
  862. 'PERF' => $monthTotalPV,
  863. 'TRANSFER_RATE' => $mate,
  864. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  865. 'CAP_AMOUNT' => 0,
  866. 'POINT_COMPLEX' => 0,
  867. ];
  868. $bonusPointComplex += $bonusPoint;
  869. }
  870. // 数据写入总表
  871. if ($insertBonusData) {
  872. foreach ($insertBonusData as &$bonusData) {
  873. // 计算奖金
  874. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  875. if ($amount <= 0) {
  876. continue;
  877. }
  878. // 封顶前奖金数
  879. $capAmount = $amount;
  880. // 奖金数不能大于封顶值
  881. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  882. $bonusData['AMOUNT'] = $amount;
  883. $bonusData['CAP_AMOUNT'] = $capAmount;
  884. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  885. // 放入缓存
  886. CalcCache::garageBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  887. }
  888. CalcBonusGarage::batchInsert($insertBonusData);
  889. }
  890. return true;
  891. }
  892. /**
  893. * 季度奖写用户缓存
  894. */
  895. public function calcQuarterUser(int $offset = 0) {
  896. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  897. // 不是结算月,则不进行计算
  898. return false;
  899. }
  900. try{
  901. $allData = CalcBonusQuarter::finduseDbCalc()
  902. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  903. ->groupBy('USER_ID')
  904. ->offset($offset)
  905. ->limit($this->_limit)
  906. ->asArray()
  907. ->all();
  908. if ($allData){
  909. // 达标条件:会员级别:钻卡
  910. $config = json_decode($this->_sysConfig['openQuarter']['OPTIONS'], true);
  911. $minDecLevel = $config['declarationLevel'] ?? [];
  912. foreach ($allData as $user) {
  913. // 管理奖钻卡发放
  914. if ($user['LAST_DEC_LV'] == $minDecLevel) {
  915. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_QUARTER', $user['ORI_BONUS']);
  916. }
  917. }
  918. return $this->calcQuarterUser($offset + $this->_limit);
  919. }
  920. } catch(Exception $e) {
  921. var_dump($e->getMessage(),'--------------------');
  922. }
  923. unset($allData);
  924. return true;
  925. }
  926. /**
  927. * 蓝星管理奖金未拆分
  928. */
  929. public function calcBonusBsGL(int $offset = 0) {
  930. if( !$this->_isCalcMonth ) {
  931. // 不是结算月,则不进行计算
  932. return false;
  933. }
  934. // 从缓存获取分页有收入的会员信息
  935. $allData = CalcBonusBS::findUseDbCalc()
  936. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  937. ->groupBy('USER_ID')
  938. ->offset($offset)
  939. ->limit($this->_limit)
  940. ->asArray()
  941. ->all();
  942. if ($allData) {
  943. foreach ($allData as $user) {
  944. // 是否活跃会员
  945. $isActive = $this->_isPerfActive($user['USER_ID']);
  946. $oriBonus = $isActive ? $user['ORI_BONUS'] : 0;
  947. $lastEmpLv = $isActive ? $user['LEVEL_ID'] : EmployLevel::getDefaultLevelId();
  948. if ($oriBonus > 0) {
  949. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  950. }
  951. // 如果不活跃,则不发放奖金,积分,级别
  952. // 更新蓝星奖金存储过程的实发金额数据
  953. CalcBonusBS::updateAll(
  954. [
  955. 'IS_ACTIVE' => (int)$isActive,
  956. 'HOPE_EMP_LV' => $user['LEVEL_ID'],
  957. 'LEVEL_ID' => $lastEmpLv,
  958. ],
  959. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  960. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  961. // 修改用户缓存中记录的用户的最新级别
  962. $userInfo = CalcCache::getUserInfo($user['USER_ID'], $this->_periodNum);
  963. $userInfo['LAST_EMP_LV'] = $lastEmpLv;
  964. CalcCache::setUserInfo($user['USER_ID'], $this->_periodNum, $userInfo);
  965. }
  966. return $this->calcBonusBsGL($offset + $this->_limit);
  967. }
  968. unset($allData);
  969. return true;
  970. }
  971. /**
  972. * 对碰
  973. */
  974. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  975. $resultArr = $oriPerfArr;
  976. foreach ($perfArr as $keyT => $perfT) {
  977. if ($perfT <= 0) {
  978. unset($perfArr[$keyT]);
  979. }
  980. }
  981. if (count($perfArr) >= 2 && $loopTimes < 6) {
  982. arsort($perfArr, SORT_NUMERIC);
  983. $onePerf = null;
  984. $oneKey = null;
  985. // $touchSurplusAmount = 0;
  986. $touchAmount = 0;
  987. // 前两个进行对碰
  988. foreach ($perfArr as $key => $perf) {
  989. if ($onePerf === null) {
  990. $oneKey = $key;
  991. $onePerf = $perf;
  992. } else {
  993. $touchSurplusAmount = $perf - $onePerf;
  994. if ($touchSurplusAmount > 0) {
  995. unset($perfArr[$oneKey]);
  996. $resultArr['perfArr'][$oneKey] = 0;
  997. $perfArr[$key] = $touchSurplusAmount;
  998. $touchAmount = $onePerf;
  999. } elseif ($touchSurplusAmount < 0) {
  1000. unset($perfArr[$key]);
  1001. $resultArr['perfArr'][$key] = 0;
  1002. $perfArr[$oneKey] = abs($touchSurplusAmount);
  1003. $touchAmount = $perf;
  1004. } else {
  1005. unset($perfArr[$oneKey], $perfArr[$key]);
  1006. $resultArr['perfArr'][$oneKey] = 0;
  1007. $resultArr['perfArr'][$key] = 0;
  1008. $touchAmount = $perf;
  1009. }
  1010. break;
  1011. }
  1012. }
  1013. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  1014. $resultArr['touchBonus'] += $touchBonus;
  1015. foreach ($perfArr as $keyR => $perfR) {
  1016. $resultArr['perfArr'][$keyR] = $perfR;
  1017. }
  1018. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  1019. }
  1020. return $resultArr;
  1021. }
  1022. /**
  1023. * 循环父级并执行回调函数
  1024. * @param $userId
  1025. * @param callable $callbackFunc
  1026. * @param int $offset
  1027. * @return bool
  1028. */
  1029. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1030. $allParents = Cache::getAllNetworkParents($userId);
  1031. $allData = array_slice($allParents, $offset, $this->_limit);
  1032. unset($allParents);
  1033. if ($allData) {
  1034. foreach ($allData as $data) {
  1035. $funcResult = $callbackFunc($data);
  1036. if ($funcResult === self::LOOP_FINISH) {
  1037. return true;
  1038. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1039. continue;
  1040. }
  1041. unset($data, $funcResult);
  1042. }
  1043. unset($allData);
  1044. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1045. }
  1046. return true;
  1047. }
  1048. /**
  1049. * 循环推荐网络的父级
  1050. * @param $userId
  1051. * @param callable $callbackFunc
  1052. * @param int $offset
  1053. * @return bool
  1054. */
  1055. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1056. $allParents = Cache::getAllRelationParents($userId);
  1057. $allData = array_slice($allParents, $offset, $this->_limit);
  1058. unset($allParents);
  1059. if ($allData) {
  1060. foreach ($allData as $data) {
  1061. $funcResult = $callbackFunc($data);
  1062. if ($funcResult === self::LOOP_FINISH) {
  1063. return true;
  1064. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1065. continue;
  1066. }
  1067. unset($data, $funcResult);
  1068. }
  1069. unset($allData);
  1070. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1071. }
  1072. return true;
  1073. }
  1074. /**
  1075. * 按级别的收入上限
  1076. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  1077. * @param $bonus
  1078. * @param $userId
  1079. * @param $declarationLevel
  1080. * @return float
  1081. */
  1082. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  1083. $decLevelConfig = $this->_decLevelConfig;
  1084. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  1085. unset($decLevelConfig);
  1086. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  1087. if( $bonus <= $maxGetBonus) {
  1088. return $bonus;
  1089. }else {
  1090. return $maxGetBonus;
  1091. }
  1092. }
  1093. /**
  1094. * 扣除管理费 返回管理费
  1095. */
  1096. public function deductManageTax($bonus) {
  1097. return $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  1098. }
  1099. // 扣除复消积分 返回应扣除的复消费积分
  1100. public function deductReconsumePonits($userId, $bonus) {
  1101. //判断是否达到了本月扣除复消的上限
  1102. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  1103. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  1104. $lastPeriodPoints = '0.00';
  1105. if (!empty($cacheData)) {
  1106. $lastPeriodPoints = $cacheData['RECONSUME_POINTS_SUM'];
  1107. }
  1108. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $lastPeriodPoints;
  1109. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  1110. unset($cacheData, $bonusCache, $lastPeriodPoints);
  1111. $reConsumePoints = 0;
  1112. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  1113. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  1114. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  1115. }
  1116. unset($reConsumePointsTotal, $reConsumePointsCap);
  1117. return $reConsumePoints;
  1118. }
  1119. /**
  1120. * 更新百分比并发送
  1121. * @param $percent
  1122. */
  1123. private function _updatePercent($percent) {
  1124. // 把数据写入数据库中
  1125. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  1126. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  1127. }
  1128. /**
  1129. * 循环奖服务奖金会员,并入库
  1130. */
  1131. public function loopBonusUsers($offset = 0) {
  1132. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1133. // 从缓存列表里面从底层往上倒序获取会员
  1134. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  1135. if($allData){
  1136. $insertDataBonus = [];
  1137. foreach($allData as $userId){
  1138. try {
  1139. $tempBonusData = $this->bonusData($userId);
  1140. if(!empty($tempBonusData['result'])){
  1141. $insertDataBonus[] = $tempBonusData['result'];
  1142. }
  1143. }catch(Exception $e) {
  1144. var_dump($e->getMessage(),'ssssssssssssssss',$userId);
  1145. }
  1146. unset($userId, $tempBonusData);
  1147. }
  1148. CalcBonus::batchInsert($insertDataBonus);
  1149. unset($insertDataBonus, $allData);
  1150. return $this->loopBonusUsers($offset + $this->_limit);
  1151. }
  1152. return true;
  1153. }
  1154. /**
  1155. * 奖金入库
  1156. */
  1157. public function bonusData($userId) {
  1158. // 车奖和房奖是积分,不参加到奖金累计中.季度奖季度发放,无最低消费限制
  1159. try{
  1160. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  1161. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1162. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  1163. $tourismBonus = CalcCache::tourismBonus($userId, $this->_periodNum);
  1164. $garageBonus = CalcCache::garageBonus($userId, $this->_periodNum);
  1165. $villaBonus = CalcCache::villaBonus($userId, $this->_periodNum);
  1166. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  1167. $starCrownLv = CalcCache::getUserStarCrown($userId, $this->_periodNum);// 星级
  1168. $bonusReal = '0.000'; // 总实发
  1169. $deductManageTaxItems = Config::DEDUCT_MANAGE_TAX; // 扣除管理费的列表
  1170. $deductReconsumePointsItems = Config::DEDUCT_RECONSUME_POINTS;// 扣除复消积分的列表
  1171. $totalReconsumePointSum = $totalManageSum = '0.000'; // 需要扣除的奖金项,扣除完之后的实发金额
  1172. $deductManageTax = '0.000'; // 扣除的管理费
  1173. $deductReconsumePoints = '0.000'; // 扣除的复消积分
  1174. foreach($deductManageTaxItems as $v) {
  1175. $totalManageSum+= $bonus[$v]; // 需要扣除管理费的累计之和
  1176. }
  1177. foreach($deductReconsumePointsItems as $v) {
  1178. $totalReconsumePointSum+= $bonus[$v]; // 需要扣除复消积分的累计之和
  1179. }
  1180. // 扣除管理费
  1181. if ($totalManageSum > 0) {
  1182. $deductManageTax = $this->deductManageTax($totalManageSum);
  1183. }
  1184. // 扣除复消积分
  1185. if ($totalReconsumePointSum > 0) {
  1186. $deductReconsumePoints = $this->deductReconsumePonits($userId, $totalReconsumePointSum);
  1187. }
  1188. // 总实发=总原奖金-扣除的总管理费-总复消积分
  1189. $bonusReal = $bonus['BONUS_TOTAL'] - $deductManageTax - $deductReconsumePoints;
  1190. // NG系统,不发放兑换积分
  1191. // 因为是最后统一发奖金,每个奖金得实发是和原奖金一样得.只不过要扣除管理费和复消积分.
  1192. // 当然,ng现在不扣除管理费和复消积分.所以都是0
  1193. $result = [
  1194. 'USER_ID' => $userId,
  1195. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  1196. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  1197. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1198. 'LAST_EMP_LV' => $baseInfo['LAST_EMP_LV'],
  1199. 'LAST_CROWN_LV' => $starCrownLv ?? StarCrownLevel::getDefaultLevelId(),
  1200. 'RECONSUME_POINTS' => $deductReconsumePoints,
  1201. 'MANAGE_TAX' => $deductManageTax, // 管理费
  1202. 'BONUS_REAL'=> $bonusReal,
  1203. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  1204. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  1205. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  1206. 'ORI_BONUS_BS' => $bonus['ORI_BONUS_BS'], // 蓝星管理奖金原奖金
  1207. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  1208. 'ORI_CAPPED_BONUS_QY' => $bonus['ORI_CAPPED_BONUS_QY'], // 团队奖封顶前的奖金
  1209. 'ORI_BONUS_QUARTER' => $bonus['ORI_BONUS_QUARTER'],
  1210. 'ORI_BONUS_TOURISM' => $tourismBonus, // 旅游奖
  1211. 'ORI_BONUS_VILLA' => $villaBonus, // 房奖
  1212. 'ORI_BONUS_GARAGE' => $garageBonus, // 车奖
  1213. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  1214. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  1215. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  1216. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  1217. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  1218. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  1219. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  1220. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  1221. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  1222. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  1223. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  1224. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  1225. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  1226. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  1227. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  1228. 'PV_PCS' => $perfData['PV_PCS'],
  1229. 'PV_TOUCH' => Tool::formatPrice($perfData['PV_1L_TOUCH'] + $perfData['PV_2L_TOUCH'] + $perfData['PV_3L_TOUCH'] + $perfData['PV_4L_TOUCH'] + $perfData['PV_5L_TOUCH']),
  1230. 'PERIOD_NUM' => $this->_periodNum,
  1231. 'CALC_YEAR' => $this->_calcYear,
  1232. 'CALC_MONTH' => $this->_calcYearMonth,
  1233. 'CREATED_AT' => Date::nowTime(),
  1234. ];
  1235. } catch(Exception $e) {
  1236. var_dump($e->getMessage(),'-----------------------------------------------',$userId);
  1237. }
  1238. $resend = [];
  1239. unset($bonus, $bonusReal);
  1240. return ['result'=>$result,'resend'=>$resend];
  1241. }
  1242. // 判断是否满足月最低消费
  1243. public function _isMonthPerfLimit($userId) {
  1244. $userMonthTotal = PerfMonth::find()->where(
  1245. 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  1246. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth]
  1247. )
  1248. ->asArray()
  1249. ->one();
  1250. $fxPvStatus = false;
  1251. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  1252. $fxPvStatus = true;
  1253. }
  1254. return $fxPvStatus;
  1255. }
  1256. // 判断会员是否活跃
  1257. public function _isPerfActive($userId): bool
  1258. {
  1259. $pv = PerfPeriod::find()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH AND PERIOD_NUM<=:PERIOD_NUM',
  1260. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth, 'PERIOD_NUM'=>$this->_periodNum])
  1261. ->SUM('PV_PCS');
  1262. return $pv >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  1263. }
  1264. }