CalcServeBonusCalc.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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['openBS']['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['openBS']['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. 'ORI_BONUS' => $oriBonus,
  547. 'PERIOD_NUM' => $this->_periodNum,
  548. 'CALC_YEAR' => $this->_calcYear,
  549. 'CALC_MONTH' => $this->_calcYearMonth,
  550. 'CREATED_AT' => Date::nowTime(),
  551. 'LOGS' => json_encode([
  552. 'perfArr' => $perfArr,
  553. 'touchPerfArrOri' => $touchBonusArr['perfArr'],
  554. 'touchPerfArr' => $touchPerfArr,
  555. 'nowDecLevelConfig' => $nowDecLevelConfig,
  556. 'decLevel' => $userBaseInfo['DEC_LV'],
  557. ]),
  558. 'IS_ACTIVE' => (int)$isActive,
  559. 'HOPE_CROWN_LV' => $starCrown['ID'],
  560. 'HOPE_BONUS' => $teamBonus,
  561. ];
  562. // 星级放入缓存
  563. CalcCache::addUserStarCrown($userId, $periodNum, $lastCrownLv);
  564. if ($oriBonus > 0) {
  565. // 把对碰后的奖金存入缓存中
  566. // CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $oriBonus, $deductData);
  567. CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus);
  568. }
  569. unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData);
  570. }
  571. CalcBonusQY::batchInsert($insertBonusData);
  572. unset($allData, $insertBonusData);
  573. return $this->calcBonusQY($offset + $this->_limit);
  574. }
  575. unset($allData);
  576. return true;
  577. }
  578. /**
  579. * 季度奖计算
  580. */
  581. public function calcQuarter() {
  582. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  583. // echo('不是季结点,进这里,不计算季度奖'. PHP_EOL);
  584. return false;
  585. }
  586. $result = \Yii::$app->db->createCommand("CALL QtrCalc(:periodNum)")
  587. ->bindValue(':periodNum' , $this->_periodNum )
  588. ->execute();
  589. return $result;
  590. }
  591. // 执行蓝星管理奖金的存储过程
  592. public function calcBsProcedure() {
  593. if( !$this->_isCalcMonth ) {
  594. // 不是结算月,则不进行计算
  595. return false;
  596. }
  597. $result = \Yii::$app->db->createCommand("CALL CalcBlue(:periodNum)")
  598. ->bindValue(':periodNum' , $this->_periodNum )
  599. ->execute();
  600. return $result;
  601. }
  602. // 执行旅游奖的计算
  603. public function calcBonusTourism() {
  604. // 月结,如果不是月结点,则直接退出
  605. if (!$this->_isCalcMonth) {
  606. return true;
  607. }
  608. $bonusConfig = $this->_sysConfig['openTourism'];
  609. // 达标条件:聘级、级别、奖项比例
  610. $config = json_decode($bonusConfig['OPTIONS'], true);
  611. // 奖金总比例
  612. $mate = $bonusConfig['VALUE'] / 100;
  613. // 会员级别
  614. $minDecLevel = $config['OPTIONS']['declarationLevel'] ?? [];
  615. // 月度公司总PV
  616. $monthTotalPV = PerfMonth::find()
  617. ->yearMonth($this->_calcYearMonth)
  618. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  619. ->sum('PV_PCS');
  620. // 用于分发的奖金总数
  621. $transferAmount = $monthTotalPV * $mate;
  622. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  623. $userStarDirector = CalcBonusBS::find()
  624. ->yearMonth($this->_calcYearMonth)
  625. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  626. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_EMP_LV,LAST_STATUS')
  627. ->groupBy('USER_ID')
  628. ->asArray()
  629. ->all();
  630. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  631. // 基于团队奖/绩效奖结果计算会员的StarCrown
  632. $userStarCrown = CalcBonusQY::find()
  633. ->yearMonth($this->_calcYearMonth)
  634. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  635. ->select('USER_ID,LAST_CROWN_LV')
  636. ->groupBy('USER_ID')
  637. ->asArray()
  638. ->all();
  639. $userStarCrownObj = array_column($userStarCrown, NULL, 'USER_ID');
  640. // 合并用户ID,去重
  641. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  642. // 奖金点数综合
  643. $bonusPointComplex = 0;
  644. $insertBonusData = [];
  645. foreach($bonusUsers as $userId) {
  646. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  647. $starDirectorPoint = $this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['TOURISM_PERCENT'] ?? 0;
  648. $starCrownPoint = $this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['TOURISM_PERCENT'] ?? 0;
  649. // 奖金比例:
  650. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  651. if ($bonusPoint <= 0) {
  652. continue;
  653. }
  654. $insertBonusData[] = [
  655. 'ID' => SnowFake::instance()->generateId(),
  656. 'USER_ID' => $userId,
  657. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'],
  658. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LAST_EMP_LV'],
  659. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'],
  660. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'],
  661. 'AMOUNT_STANDARD' => 0,
  662. 'POINT' => $bonusPoint,
  663. 'PERIOD_NUM' => $this->_periodNum,
  664. 'CALC_YEAR' => $this->_calcYear,
  665. 'CALC_MONTH' => $this->_calcYearMonth,
  666. 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH),
  667. 'CREATED_AT' => Date::nowTime(),
  668. 'PERF' => $monthTotalPV,
  669. 'TRANSFER_RATE' => $mate,
  670. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  671. 'CAP_AMOUNT' => 0,
  672. 'POINT_COMPLEX' => 0,
  673. ];
  674. $bonusPointComplex += $bonusPoint;
  675. }
  676. // 数据写入总表
  677. if ($insertBonusData) {
  678. foreach ($insertBonusData as &$bonusData) {
  679. // 计算奖金
  680. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  681. if ($amount <= 0) {
  682. continue;
  683. }
  684. // 会员级别达到要求才会发放奖金
  685. if ($bonusData['LAST_DEC_LV'] == $minDecLevel) {
  686. // 放入缓存
  687. CalcCache::tourismBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  688. }
  689. $bonusData['AMOUNT'] = $amount;
  690. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  691. }
  692. CalcBonusTourism::batchInsert($insertBonusData);
  693. }
  694. return true;
  695. }
  696. // 执行房奖的计算
  697. public function calcBonusVilla() {
  698. if (!$this->_isCalcMonth) {
  699. return true;
  700. }
  701. $bonusConfig = $this->_sysConfig['openVilla'];
  702. // 达标条件:聘级、级别、奖项比例
  703. $config = json_decode($bonusConfig['OPTIONS'], true);
  704. // 奖金总比例
  705. $mate = $bonusConfig['VALUE'] / 100;
  706. // 个人奖金封顶
  707. $capBonus = intval($this->_sysConfig['openVillaCap']['VALUE'] ?? 0);
  708. // 会员级别
  709. $minDecLevel = $config['declarationLevel'] ?? [];
  710. // 月度公司总PV
  711. $monthTotalPV = PerfMonth::find()
  712. ->yearMonth($this->_calcYearMonth)
  713. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  714. ->sum('PV_PCS');
  715. // 用于分发的奖金总数
  716. $transferAmount = $monthTotalPV * $mate;
  717. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  718. $subQuery = CalcBonusQY::find()
  719. ->yearMonth($this->_calcYearMonth)
  720. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  721. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  722. ->joinWith(['starCrown' => function($query) {
  723. $query->select(['LEVEL_NAME', 'SORT']);
  724. }])
  725. ->having(1)
  726. ->orderBy('USER_ID ASC, SORT DESC');
  727. $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();
  728. // 奖金点数综合
  729. $bonusPointComplex = 0;
  730. $insertBonusData = [];
  731. foreach($userStarCrownObj as $item) {
  732. // 奖金比例
  733. $bonusPoint = $this->_starCrownLevelConfig[$item['LAST_CROWN_LV']]['VILLA_PERCENT'] ?? 0;
  734. if (!$bonusPoint) {
  735. continue;
  736. }
  737. // 会员级别达到要求才会发放奖金
  738. if ($item['LAST_DEC_LV'] != $minDecLevel) {
  739. continue;
  740. }
  741. $userBaseInfo = CalcCache::getUserInfo($item['USER_ID'], $this->_periodNum);
  742. $insertBonusData[] = [
  743. 'ID' => SnowFake::instance()->generateId(),
  744. 'USER_ID' => $item['USER_ID'],
  745. 'LAST_DEC_LV' => $item['LAST_DEC_LV'] ?? '',
  746. 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],
  747. 'LAST_STATUS' => $item['LAST_STATUS'] ?? 0,
  748. 'LAST_CROWN_LV' => $item['LAST_CROWN_LV'] ?? '',
  749. 'AMOUNT' => 0,
  750. 'POINT' => $bonusPoint,
  751. 'PERIOD_NUM' => $this->_periodNum,
  752. 'CALC_YEAR' => $this->_calcYear,
  753. 'CALC_MONTH' => $this->_calcYearMonth,
  754. 'CREATED_AT' => Date::nowTime(),
  755. 'PERF' => $monthTotalPV,
  756. 'TRANSFER_RATE' => $mate,
  757. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  758. 'CAP_AMOUNT' => 0,
  759. 'POINT_COMPLEX' => 0,
  760. ];
  761. $bonusPointComplex += $bonusPoint;
  762. }
  763. // 数据写入总表
  764. if ($insertBonusData) {
  765. // 计算个人奖金
  766. foreach ($insertBonusData as &$bonusData) {
  767. // 计算奖金
  768. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  769. if ($amount <= 0) {
  770. continue;
  771. }
  772. // 封顶前奖金数
  773. $capAmount = $amount;
  774. // 奖金数不能大于封顶值
  775. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  776. $bonusData['AMOUNT'] = $amount;
  777. $bonusData['CAP_AMOUNT'] = $capAmount;
  778. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  779. // 放入缓存
  780. CalcCache::villaBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  781. }
  782. CalcBonusVilla::batchInsert($insertBonusData);
  783. }
  784. return true;
  785. }
  786. // 执行车奖的计算
  787. public function calcBonusGarage() {
  788. // 月结,如果不是月结点,则直接退出
  789. if (!$this->_isCalcMonth) {
  790. return true;
  791. }
  792. $bonusConfig = $this->_sysConfig['openGarage'];
  793. // 达标条件:聘级、级别、奖项比例
  794. $config = json_decode($bonusConfig['OPTIONS'], true);
  795. // 奖金总比例
  796. $mate = $bonusConfig['VALUE'] / 100;
  797. // 会员级别
  798. $minDecLevel = $config['declarationLevel'] ?? [];
  799. // 个人奖金封顶
  800. $capBonus = intval($this->_sysConfig['openGarageCap']['VALUE'] ?? 0);
  801. // 月度公司总PV
  802. $monthTotalPV = PerfMonth::find()
  803. ->yearMonth($this->_calcYearMonth)
  804. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  805. ->sum('PV_PCS');
  806. // 用于分发的奖金总数
  807. $transferAmount = $monthTotalPV * $mate;
  808. // 基于蓝星奖结果计算符合获奖条件的会员StarDirector
  809. $userStarDirector = CalcBonusBS::find()
  810. ->yearMonth($this->_calcYearMonth)
  811. ->where('CALC_MONTH = :CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth])
  812. ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_STATUS')
  813. ->groupBy('USER_ID')
  814. ->asArray()
  815. ->all();
  816. $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID');
  817. // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级
  818. $subQuery = CalcBonusQY::find()
  819. ->yearMonth($this->_calcYearMonth)
  820. ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID])
  821. ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT')
  822. ->joinWith(['starCrown' => function($query) {
  823. $query->select(['LEVEL_NAME', 'SORT']);
  824. }])
  825. ->having(1)
  826. ->orderBy('USER_ID ASC, SORT DESC');
  827. $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();
  828. // 合并用户ID,去重
  829. $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj)));
  830. sort($bonusUsers);
  831. // 奖金点数综合
  832. $bonusPointComplex = 0;
  833. $insertBonusData = [];
  834. foreach($bonusUsers as $userId) {
  835. // 计算奖金:取starDirectorPoint和starCrownPoint的大个值
  836. $starDirectorPoint = !isset($userStarDirectorObj[$userId]['LEVEL_ID']) ? 0 : ($this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['GARAGE_PERCENT'] ?? 0);
  837. $starCrownPoint = !isset($userStarCrownObj[$userId]['LAST_CROWN_LV']) ? 0: ($this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['GARAGE_PERCENT'] ?? 0);
  838. // 奖金比例:
  839. $bonusPoint = max($starDirectorPoint, $starCrownPoint);
  840. if ($bonusPoint <= 0) {
  841. continue;
  842. }
  843. // 会员级别达到要求才会发放奖金
  844. $lastDecLv = $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? '');
  845. if ($lastDecLv != $minDecLevel) {
  846. continue;
  847. }
  848. $insertBonusData[] = [
  849. 'ID' => SnowFake::instance()->generateId(),
  850. 'USER_ID' => $userId,
  851. 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? ''),
  852. 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LEVEL_ID'] ?? '',
  853. 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'] ?? ($userStarCrownObj[$userId]['LAST_STATUS'] ?? 1),
  854. 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'] ?? '',
  855. 'AMOUNT' => 0,
  856. 'POINT' => $bonusPoint,
  857. 'PERIOD_NUM' => $this->_periodNum,
  858. 'CALC_YEAR' => $this->_calcYear,
  859. 'CALC_MONTH' => $this->_calcYearMonth,
  860. 'CREATED_AT' => Date::nowTime(),
  861. 'PERF' => $monthTotalPV,
  862. 'TRANSFER_RATE' => $mate,
  863. 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount),
  864. 'CAP_AMOUNT' => 0,
  865. 'POINT_COMPLEX' => 0,
  866. ];
  867. $bonusPointComplex += $bonusPoint;
  868. }
  869. // 数据写入总表
  870. if ($insertBonusData) {
  871. foreach ($insertBonusData as &$bonusData) {
  872. // 计算奖金
  873. $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex));
  874. if ($amount <= 0) {
  875. continue;
  876. }
  877. // 封顶前奖金数
  878. $capAmount = $amount;
  879. // 奖金数不能大于封顶值
  880. $amount = ($amount > $capBonus) ? $capBonus : $amount;
  881. $bonusData['AMOUNT'] = $amount;
  882. $bonusData['CAP_AMOUNT'] = $capAmount;
  883. $bonusData['POINT_COMPLEX'] = $bonusPointComplex;
  884. // 放入缓存
  885. CalcCache::garageBonus($bonusData['USER_ID'], $this->_periodNum, $amount);
  886. }
  887. CalcBonusGarage::batchInsert($insertBonusData);
  888. }
  889. return true;
  890. }
  891. /**
  892. * 季度奖写用户缓存
  893. */
  894. public function calcQuarterUser(int $offset = 0) {
  895. if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) {
  896. // 不是结算月,则不进行计算
  897. return false;
  898. }
  899. try{
  900. $allData = CalcBonusQuarter::finduseDbCalc()
  901. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  902. ->groupBy('USER_ID')
  903. ->offset($offset)
  904. ->limit($this->_limit)
  905. ->asArray()
  906. ->all();
  907. if ($allData){
  908. // 达标条件:会员级别:钻卡
  909. $config = json_decode($this->_sysConfig['openQuarter']['OPTIONS'], true);
  910. $minDecLevel = $config['declarationLevel'] ?? [];
  911. foreach ($allData as $user) {
  912. // 管理奖钻卡发放
  913. if ($user['LAST_DEC_LV'] == $minDecLevel) {
  914. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_QUARTER', $user['ORI_BONUS']);
  915. }
  916. }
  917. return $this->calcQuarterUser($offset + $this->_limit);
  918. }
  919. } catch(Exception $e) {
  920. var_dump($e->getMessage(),'--------------------');
  921. }
  922. unset($allData);
  923. return true;
  924. }
  925. /**
  926. * 蓝星管理奖金未拆分
  927. */
  928. public function calcBonusBsGL(int $offset = 0) {
  929. if( !$this->_isCalcMonth ) {
  930. // 不是结算月,则不进行计算
  931. return false;
  932. }
  933. // 从缓存获取分页有收入的会员信息
  934. $allData = CalcBonusBS::findUseDbCalc()
  935. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum])
  936. ->groupBy('USER_ID')
  937. ->offset($offset)
  938. ->limit($this->_limit)
  939. ->asArray()
  940. ->all();
  941. if ($allData) {
  942. foreach ($allData as $user) {
  943. // 是否活跃会员
  944. $isActive = $this->_isPerfActive($user['USER_ID']);
  945. $oriBonus = $isActive ? $user['ORI_BONUS'] : 0;
  946. $lastEmpLv = $isActive ? $user['LEVEL_ID'] : EmployLevel::getDefaultLevelId();
  947. if ($oriBonus > 0) {
  948. CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']);
  949. }
  950. // 如果不活跃,则不发放奖金,积分,级别
  951. // 更新蓝星奖金存储过程的实发金额数据
  952. CalcBonusBS::updateAll(
  953. [
  954. 'IS_ACTIVE' => (int)$isActive,
  955. 'HOPE_EMP_LV' => $user['LEVEL_ID'],
  956. 'LEVEL_ID' => $lastEmpLv,
  957. ],
  958. 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM',
  959. [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]);
  960. }
  961. return $this->calcBonusBsGL($offset + $this->_limit);
  962. }
  963. unset($allData);
  964. return true;
  965. }
  966. /**
  967. * 对碰
  968. */
  969. public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) {
  970. $resultArr = $oriPerfArr;
  971. foreach ($perfArr as $keyT => $perfT) {
  972. if ($perfT <= 0) {
  973. unset($perfArr[$keyT]);
  974. }
  975. }
  976. if (count($perfArr) >= 2 && $loopTimes < 6) {
  977. arsort($perfArr, SORT_NUMERIC);
  978. $onePerf = null;
  979. $oneKey = null;
  980. // $touchSurplusAmount = 0;
  981. $touchAmount = 0;
  982. // 前两个进行对碰
  983. foreach ($perfArr as $key => $perf) {
  984. if ($onePerf === null) {
  985. $oneKey = $key;
  986. $onePerf = $perf;
  987. } else {
  988. $touchSurplusAmount = $perf - $onePerf;
  989. if ($touchSurplusAmount > 0) {
  990. unset($perfArr[$oneKey]);
  991. $resultArr['perfArr'][$oneKey] = 0;
  992. $perfArr[$key] = $touchSurplusAmount;
  993. $touchAmount = $onePerf;
  994. } elseif ($touchSurplusAmount < 0) {
  995. unset($perfArr[$key]);
  996. $resultArr['perfArr'][$key] = 0;
  997. $perfArr[$oneKey] = abs($touchSurplusAmount);
  998. $touchAmount = $perf;
  999. } else {
  1000. unset($perfArr[$oneKey], $perfArr[$key]);
  1001. $resultArr['perfArr'][$oneKey] = 0;
  1002. $resultArr['perfArr'][$key] = 0;
  1003. $touchAmount = $perf;
  1004. }
  1005. break;
  1006. }
  1007. }
  1008. $touchBonus = Tool::formatPrice($touchAmount * $percent);
  1009. $resultArr['touchBonus'] += $touchBonus;
  1010. foreach ($perfArr as $keyR => $perfR) {
  1011. $resultArr['perfArr'][$keyR] = $perfR;
  1012. }
  1013. return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1);
  1014. }
  1015. return $resultArr;
  1016. }
  1017. /**
  1018. * 循环父级并执行回调函数
  1019. * @param $userId
  1020. * @param callable $callbackFunc
  1021. * @param int $offset
  1022. * @return bool
  1023. */
  1024. public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1025. $allParents = Cache::getAllNetworkParents($userId);
  1026. $allData = array_slice($allParents, $offset, $this->_limit);
  1027. unset($allParents);
  1028. if ($allData) {
  1029. foreach ($allData as $data) {
  1030. $funcResult = $callbackFunc($data);
  1031. if ($funcResult === self::LOOP_FINISH) {
  1032. return true;
  1033. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1034. continue;
  1035. }
  1036. unset($data, $funcResult);
  1037. }
  1038. unset($allData);
  1039. return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1040. }
  1041. return true;
  1042. }
  1043. /**
  1044. * 循环推荐网络的父级
  1045. * @param $userId
  1046. * @param callable $callbackFunc
  1047. * @param int $offset
  1048. * @return bool
  1049. */
  1050. public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) {
  1051. $allParents = Cache::getAllRelationParents($userId);
  1052. $allData = array_slice($allParents, $offset, $this->_limit);
  1053. unset($allParents);
  1054. if ($allData) {
  1055. foreach ($allData as $data) {
  1056. $funcResult = $callbackFunc($data);
  1057. if ($funcResult === self::LOOP_FINISH) {
  1058. return true;
  1059. } elseif ($funcResult === self::LOOP_CONTINUE) {
  1060. continue;
  1061. }
  1062. unset($data, $funcResult);
  1063. }
  1064. unset($allData);
  1065. return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit);
  1066. }
  1067. return true;
  1068. }
  1069. /**
  1070. * 按级别的收入上限
  1071. * 新的需求调整: 改成不按月进行限制,并且去掉vip奖
  1072. * @param $bonus
  1073. * @param $userId
  1074. * @param $declarationLevel
  1075. * @return float
  1076. */
  1077. public function declarationLevelCap($bonus, $userId, $declarationLevel) {
  1078. $decLevelConfig = $this->_decLevelConfig;
  1079. $nowDecLevelConfig = $decLevelConfig[$declarationLevel];
  1080. unset($decLevelConfig);
  1081. $maxGetBonus = $nowDecLevelConfig['INCOME_CAP'];
  1082. if( $bonus <= $maxGetBonus) {
  1083. return $bonus;
  1084. }else {
  1085. return $maxGetBonus;
  1086. }
  1087. }
  1088. /**
  1089. * 扣除管理费 返回管理费
  1090. */
  1091. public function deductManageTax($bonus) {
  1092. return $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100;
  1093. }
  1094. // 扣除复消积分 返回应扣除的复消费积分
  1095. public function deductReconsumePonits($userId, $bonus) {
  1096. //判断是否达到了本月扣除复消的上限
  1097. $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth);
  1098. $bonusCache = CalcCache::bonus($userId, $this->_periodNum);
  1099. $lastPeriodPoints = '0.00';
  1100. if (!empty($cacheData)) {
  1101. $lastPeriodPoints = $cacheData['RECONSUME_POINTS_SUM'];
  1102. }
  1103. $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $lastPeriodPoints;
  1104. $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE'];
  1105. unset($cacheData, $bonusCache, $lastPeriodPoints);
  1106. $reConsumePoints = 0;
  1107. if( $reConsumePointsTotal < $reConsumePointsCap ) {
  1108. $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100;
  1109. $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal);
  1110. }
  1111. unset($reConsumePointsTotal, $reConsumePointsCap);
  1112. return $reConsumePoints;
  1113. }
  1114. /**
  1115. * 更新百分比并发送
  1116. * @param $percent
  1117. */
  1118. private function _updatePercent($percent) {
  1119. // 把数据写入数据库中
  1120. Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]);
  1121. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']);
  1122. }
  1123. /**
  1124. * 循环奖服务奖金会员,并入库
  1125. */
  1126. public function loopBonusUsers($offset = 0) {
  1127. echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset);
  1128. // 从缓存列表里面从底层往上倒序获取会员
  1129. $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit);
  1130. if($allData){
  1131. $insertDataBonus = [];
  1132. foreach($allData as $userId){
  1133. try {
  1134. $tempBonusData = $this->bonusData($userId);
  1135. if(!empty($tempBonusData['result'])){
  1136. $insertDataBonus[] = $tempBonusData['result'];
  1137. }
  1138. }catch(Exception $e) {
  1139. var_dump($e->getMessage(),'ssssssssssssssss',$userId);
  1140. }
  1141. unset($userId, $tempBonusData);
  1142. }
  1143. CalcBonus::batchInsert($insertDataBonus);
  1144. unset($insertDataBonus, $allData);
  1145. return $this->loopBonusUsers($offset + $this->_limit);
  1146. }
  1147. return true;
  1148. }
  1149. /**
  1150. * 奖金入库
  1151. */
  1152. public function bonusData($userId) {
  1153. // 车奖和房奖是积分,不参加到奖金累计中.季度奖季度发放,无最低消费限制
  1154. try{
  1155. $bonus = CalcCache::bonus($userId, $this->_periodNum);
  1156. $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum);
  1157. $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum);
  1158. $tourismBonus = CalcCache::tourismBonus($userId, $this->_periodNum);
  1159. $garageBonus = CalcCache::garageBonus($userId, $this->_periodNum);
  1160. $villaBonus = CalcCache::villaBonus($userId, $this->_periodNum);
  1161. $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum);
  1162. $starCrownLv = CalcCache::getUserStarCrown($userId, $this->_periodNum);// 星级
  1163. $bonusReal = '0.000'; // 总实发
  1164. $deductManageTaxItems = Config::DEDUCT_MANAGE_TAX; // 扣除管理费的列表
  1165. $deductReconsumePointsItems = Config::DEDUCT_RECONSUME_POINTS;// 扣除复消积分的列表
  1166. $totalReconsumePointSum = $totalManageSum = '0.000'; // 需要扣除的奖金项,扣除完之后的实发金额
  1167. $deductManageTax = '0.000'; // 扣除的管理费
  1168. $deductReconsumePoints = '0.000'; // 扣除的复消积分
  1169. foreach($deductManageTaxItems as $v) {
  1170. $totalManageSum+= $bonus[$v]; // 需要扣除管理费的累计之和
  1171. }
  1172. foreach($deductReconsumePointsItems as $v) {
  1173. $totalReconsumePointSum+= $bonus[$v]; // 需要扣除复消积分的累计之和
  1174. }
  1175. // 扣除管理费
  1176. if ($totalManageSum > 0) {
  1177. $deductManageTax = $this->deductManageTax($totalManageSum);
  1178. }
  1179. // 扣除复消积分
  1180. if ($totalReconsumePointSum > 0) {
  1181. $deductReconsumePoints = $this->deductReconsumePonits($userId, $totalReconsumePointSum);
  1182. }
  1183. // 总实发=总原奖金-扣除的总管理费-总复消积分
  1184. $bonusReal = $bonus['BONUS_TOTAL'] - $deductManageTax - $deductReconsumePoints;
  1185. // NG系统,不发放兑换积分
  1186. // 因为是最后统一发奖金,每个奖金得实发是和原奖金一样得.只不过要扣除管理费和复消积分.
  1187. // 当然,ng现在不扣除管理费和复消积分.所以都是0
  1188. $result = [
  1189. 'USER_ID' => $userId,
  1190. 'LAST_USER_NAME' => $baseInfo['USER_NAME'],
  1191. 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'],
  1192. 'LAST_DEC_LV' => $baseInfo['DEC_LV'],
  1193. 'LAST_EMP_LV' => $baseInfo['LAST_EMP_LV'],
  1194. 'LAST_CROWN_LV' => $starCrownLv ?? StarCrownLevel::getDefaultLevelId(),
  1195. 'RECONSUME_POINTS' => $deductReconsumePoints,
  1196. 'MANAGE_TAX' => $deductManageTax, // 管理费
  1197. 'BONUS_REAL'=> $bonusReal,
  1198. 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'],
  1199. 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'],
  1200. 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'],
  1201. 'ORI_BONUS_BS' => $bonus['ORI_BONUS_BS'], // 蓝星管理奖金原奖金
  1202. 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'],
  1203. 'ORI_CAPPED_BONUS_QY' => $bonus['ORI_CAPPED_BONUS_QY'], // 团队奖封顶前的奖金
  1204. 'ORI_BONUS_QUARTER' => $bonus['ORI_BONUS_QUARTER'],
  1205. 'ORI_BONUS_TOURISM' => $tourismBonus, // 旅游奖
  1206. 'ORI_BONUS_VILLA' => $villaBonus, // 房奖
  1207. 'ORI_BONUS_GARAGE' => $garageBonus, // 车奖
  1208. 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩
  1209. 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'],
  1210. 'SURPLUS_1L' => $perfData['SURPLUS_1L'],
  1211. 'PV_2L' => $perfData['PV_2L_TOUCH'],
  1212. 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'],
  1213. 'SURPLUS_2L' => $perfData['SURPLUS_2L'],
  1214. 'PV_3L' => $perfData['PV_3L_TOUCH'],
  1215. 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'],
  1216. 'SURPLUS_3L' => $perfData['SURPLUS_3L'],
  1217. 'PV_4L' => $perfData['PV_4L_TOUCH'],
  1218. 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'],
  1219. 'SURPLUS_4L' => $perfData['SURPLUS_4L'],
  1220. 'PV_5L' => $perfData['PV_5L_TOUCH'],
  1221. 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'],
  1222. 'SURPLUS_5L' => $perfData['SURPLUS_5L'],
  1223. 'PV_PCS' => $perfData['PV_PCS'],
  1224. 'PV_TOUCH' => Tool::formatPrice($perfData['PV_1L_TOUCH'] + $perfData['PV_2L_TOUCH'] + $perfData['PV_3L_TOUCH'] + $perfData['PV_4L_TOUCH'] + $perfData['PV_5L_TOUCH']),
  1225. 'PERIOD_NUM' => $this->_periodNum,
  1226. 'CALC_YEAR' => $this->_calcYear,
  1227. 'CALC_MONTH' => $this->_calcYearMonth,
  1228. 'CREATED_AT' => Date::nowTime(),
  1229. ];
  1230. } catch(Exception $e) {
  1231. var_dump($e->getMessage(),'-----------------------------------------------',$userId);
  1232. }
  1233. $resend = [];
  1234. unset($bonus, $bonusReal);
  1235. return ['result'=>$result,'resend'=>$resend];
  1236. }
  1237. // 判断是否满足月最低消费
  1238. public function _isMonthPerfLimit($userId) {
  1239. $userMonthTotal = PerfMonth::find()->where(
  1240. 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH',
  1241. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth]
  1242. )
  1243. ->asArray()
  1244. ->one();
  1245. $fxPvStatus = false;
  1246. if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) {
  1247. $fxPvStatus = true;
  1248. }
  1249. return $fxPvStatus;
  1250. }
  1251. // 判断会员是否活跃
  1252. public function _isPerfActive($userId): bool
  1253. {
  1254. $pv = PerfPeriod::find()->where('USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH AND PERIOD_NUM<=:PERIOD_NUM',
  1255. ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth, 'PERIOD_NUM'=>$this->_periodNum])
  1256. ->SUM('PV_PCS');
  1257. return $pv >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE'];
  1258. }
  1259. }