CalcServeBonusCalc.php 61 KB

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