'fwCoefficientFromZc', 'FX_CASH' => 'fwCoefficientFromFxCash', 'FX_POINT' => 'fwCoefficientFromFxPoint', ]; //最小报单pv const MIN_BD_PV = 980; public function init() { parent::init(); } /** * 设置期数 * @param int $periodNum * @return int */ public function setPeriodNum(int $periodNum) { return $this->_periodNum = $periodNum; } /** * 获取期数 * @return int */ public function getPeriodNum() { return $this->_periodNum; } /** * 加入错误错误 * @param $attr * @param $error */ public function addError($attr, $error) { $this->_errors[$attr][] = $error; } /** * 获取错误信息 * @return array */ public function getErrors() { return $this->_errors; } /** * 开始执行结算步骤 * @param $periodNum * @param null $handleUserId * @return bool */ public function calcStep($periodNum, $handleUserId = null) { try { $this->_errors = []; $this->setPeriodNum($periodNum); $this->_handleUserId = $handleUserId; $t1 = microtime(true); // 初始化结算任务 $this->initCalcTask(); $t2 = microtime(true); // 设置结算状态 $this->setCalcStatus('start'); // 清空所有本期结算用到的缓存 CalcCache::clearCalcBonusCache($this->_periodNum); $t3 = microtime(true); // 清空相关表数据 $this->clearCalcTableData(); $t4 = microtime(true); echo('初始化、清空缓存及相关数据表完成,耗时:' . round($t4 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL); $this->_updatePercent(10); // 蓝星奖放到最前面 奖金计算开始 if($this->_sysConfig['openGL']['VALUE']) { echo('计算蓝星奖开始,' . date('Y-m-d H:i:s', $t4) . PHP_EOL); // 调用存储过程,计算蓝星管理奖金 $this->calcBsProcedure(); // 将有蓝星管理奖金的用户加入到有奖金缓存用户中 $this->calcBonusBsGL(); // 将有【蓝星业绩奖金】的用户加入到有奖金缓存用户中 // $this->calcBonusBsYJ(); // // 将有【蓝星管理奖金】的用户加入到有奖金缓存用户中 // $this->calcBonusBsGL(); } $t5 = microtime(true); echo('计算蓝星奖'.($this->_sysConfig['openGL']['VALUE']?'完成':'关闭').',耗时:' . round($t5 - $t4, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL); if($this->_sysConfig['openFW']['VALUE']) { $this->calcBonusBDStepOne(); $this->calcBonusBDStepTwo(); } $t6 = microtime(true); echo('计算服务奖'.($this->_sysConfig['openFW']['VALUE']?'完成':'关闭').',耗时:' . round($t6 - $t5, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL); $this->_updatePercent(15); // 销售奖/推广奖 if($this->_sysConfig['openTG']['VALUE']) { $this->calcBonusTG(); } $t7 = microtime(true); echo('计算推广奖'.($this->_sysConfig['openTG']['VALUE']?'完成':'关闭').',耗时:' . round($t7 - $t6, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL); $this->_updatePercent(20); // 绩效奖/团队奖 if($this->_sysConfig['openQY']['VALUE']) { $this->calcBonusQY(); } $t8 = microtime(true); echo('计算团队奖'.($this->_sysConfig['openQY']['VALUE']?'完成':'关闭').',耗时:' . round($t8 - $t7, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL); $this->_updatePercent(35); // $this->calcBonusTourism($this->_sysConfig['openTourism']); // $t21 = microtime(true); // echo('计算旅游奖' . ($this->_sysConfig['openTourism']['VALUE'] ? '完成' : '关闭') . ',耗时:' . round($t21 - $t20, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL); // $this->_updatePercent(68); $this->calcBonusVilla(); $t22 = microtime(true); echo('计算房奖' . ($this->_sysConfig['openVilla']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t22 - $t8, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL); $this->_updatePercent(45); $this->calcBonusGarage(); $t23 = microtime(true); echo('计算车奖' . ($this->_sysConfig['openGarage']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t23 - $t22, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL); $this->_updatePercent(55); // 计算季度奖 $this->calcQuarter(); $t24 = microtime(true); echo('计算季度奖' . ($this->_sysConfig['openQuarter']['VALUE'] ? '开启调用存储过程' : '关闭').',耗时:' . round($t24 - $t23, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL); // 将用户写入缓存 $this->calcQuarterUser(); $this->_updatePercent(65); $t25 = microtime(true); echo('计算季度奖' . ($this->_sysConfig['openQuarter']['VALUE'] ? '完成' : '关闭').',耗时:' . round($t25 - $t24, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL); // 奖金写库 $this->loopBonusUsers(); $this->_updatePercent(75); $t30 = microtime(true); echo('奖金写库操作完成,耗时:' . round($t30 - $t25, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL); Period::updateCalcProcess(3, $this->_periodNum); $this->_updatePercent(100); $t35 = microtime(true); echo('结算全部完成,共耗时:' . round($t35 - $t1, 3) . ',内存使用:' . (round(memory_get_usage() / 1024 / 1024, 3)) . 'MB' . PHP_EOL . PHP_EOL); } catch (\Exception $e) { $this->errorCalcTask(); $this->addError('calc', sprintf('File【%s】, Line【%s】, Msg【%s】', $e->getFile(), $e->getLine(), $e->getMessage())); return false; } return true; } /** * 结算完成 */ public function endCalcTask() { // 更新结算状态 $this->setCalcStatus('end'); } /** * 结算错误 */ public function errorCalcTask() { // 清空所有本期结算用到的缓存 CalcCache::clearCalcBonusCache($this->_periodNum); // 更新结算状态 $this->setCalcStatus('fail'); } /** * 初始化结算任务 * @throws \yii\db\Exception */ public function initCalcTask() { $periodObj = Period::instance(); $periodDataArr = $periodObj->setPeriodNum($this->_periodNum); if (empty($this->_periodNum)) { $this->_periodNum = $periodDataArr['PERIOD_NUM']; } $this->_sysConfig = Cache::getSystemConfig(); $this->_decLevelConfig = Cache::getDecLevelConfig(); $this->_empLevelConfig = Cache::getEmpLevelConfig(); $this->_starCrownLevelConfig = Cache::getStarCrownLevelConfig(); $this->_decRoleConfig = CalcCache::getDecRoleConfig($this->_periodNum); $periodNum = $this->_periodNum; $this->_periodId = $periodDataArr['ID']; $this->_isCalcMonth = $periodObj->isCalcMonth($periodNum); $this->_calcYear = $periodObj->getYear($periodNum); $this->_calcMonth = $periodObj->getMonth($periodNum); $this->_calcYearMonth = $periodObj->getYearMonth($periodNum); } /** * 设置结算状态 * @param $type * start|end|fail */ public function setCalcStatus($type) { if ($type == 'start') { Period::updateAll(['IS_CALCING' => 1, 'IS_CALCULATED' => Period::CALCULATE_NONE, 'CALCULATE_STARTED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]); } elseif ($type == 'end') { Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FINISH, 'CALCULATED_AT' => Date::nowTime()], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]); } elseif ($type == 'fail') { Period::updateAll(['IS_CALCING' => 0, 'IS_CALCULATED' => Period::CALCULATE_FAIL, 'CALCULATED_AT' => 0], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]); } } /** * 清空相关表数据 */ public function clearCalcTableData() { // 奖金表 CalcBonus::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); CalcBonusQY::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); CalcBonusBD::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); // 实际上是服务奖流水表 CalcBonusTG::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); // 月结时要清空的数据 if ($this->_isCalcMonth) { CalcBonusTourism::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); CalcBonusGarage::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); CalcBonusVilla::pageDeleteAll('PERIOD_NUM='.$this->_periodNum); } } /** * 推广奖 * @param int $offset * @return bool * @throws \yii\db\Exception */ public function calcBonusTG(int $offset = 0) { $periodNum = $this->_periodNum; // 从缓存获取分页有业绩的会员信息 $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit); if ($allData) { $insertBonusData = []; foreach ($allData as $userId) { // 从缓存中获取会员的业绩信息 $perfData = CalcCache::nowPeriodPerf($userId, $periodNum); if( !$perfData ) continue; //个人业绩都算推荐奖,包括报单和复消、二次购物 $perfPv = $perfData['PV_PCS_ZC'] ?? 0; if( $perfPv <= 0 ) continue; //推广奖使用个人PCS $recBonus = Tool::formatPrice($perfPv * $this->_sysConfig['recPercent']['VALUE'] / 100); if ($recBonus <= 0) continue; // 把对碰后的奖金存入缓存中 $perfUserInfo = CalcCache::getUserInfo($userId, $periodNum); $bonusUserId = $perfUserInfo['REC_UID'] ?? ''; if( !$bonusUserId ) continue; // 获取会员的报单级别 $userBaseInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum); //扣除相应的复消积分和管理费 $deductData = $this->deduct($bonusUserId, $recBonus); CalcCache::bonus($bonusUserId, $periodNum, 'BONUS_TG', $recBonus, $deductData); //来源会员信息 $fromUserInfo = CalcCache::getUserInfo($userId, $this->_periodNum); //推广奖流水 $insertBonusData[] = [ 'ID' => SnowFake::instance()->generateId(), 'USER_ID' => $bonusUserId, 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'], 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'],, 'LAST_STATUS' => $userBaseInfo['STATUS'], 'FROM_USER_ID' => $userId, 'LAST_FROM_DEC_LV' => $fromUserInfo['DEC_LV'], 'LAST_FROM_EMP_LV' => $fromUserInfo['EMP_LV'], 'LAST_FROM_STATUS' => $fromUserInfo['STATUS'], 'AMOUNT' => $deductData['surplus'], 'ORI_BONUS' => $recBonus, 'RECONSUME_POINTS' => $deductData['reConsumePoints'], 'MANAGE_TAX' => $deductData['manageTax'], 'PERIOD_NUM' => $this->_periodNum, 'CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime(), 'LOGS' => json_encode([ 'perfPv' => $perfPv, 'recPercentConfig' => $this->_sysConfig['recPercent']['VALUE'], 'recNum' => $userBaseInfo['REC_NUM'], 'decAmount' => $userBaseInfo['ZC_AMOUNT'], 'decLevel' => $userBaseInfo['DEC_LV'], 'bonusTotalLimit' => [ $this->_sysConfig['bonusTotalZeroLimit']['VALUE'], $this->_sysConfig['bonusTotalOneLimit']['VALUE'], $this->_sysConfig['bonusTotalTwoLimit']['VALUE'], ], ]), ]; unset($perfData, $perfPv, $perfUserInfo, $recBonus, $bonusUserId, $userBaseInfo, $userId, $deductData, $fromUserInfo); } CalcBonusTG::batchInsert($insertBonusData); unset($allData, $insertBonusData); return $this->calcBonusTG($offset + $this->_limit); } unset($allData); return true; } /** * 服务奖第一步 * @param int $offset * @return bool * @throws \yii\db\Exception */ public function calcBonusBDStepOne(int $offset = 0) { echo sprintf("时间:[%s]服务奖第【1】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset); $periodNum = $this->_periodNum; // 从缓存获取分页有业绩的会员信息 $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit); if ($allData) { $insertBonusData = []; foreach ($allData as $userId) { // 从缓存中获取会员的业绩信息 $perfData = CalcCache::nowPeriodPerf($userId, $periodNum); if( !$perfData ) continue; $decRoleBonusFrom = explode(',', $this->_sysConfig['decRoleBonusFrom']['VALUE']); $validPvPcs = 0; foreach ($decRoleBonusFrom as $orderType) { $orderTypeName = sprintf('PV_PCS_%s', $orderType); $orderTypeValue = $perfData[$orderTypeName] ?? 0; $coefficientName = self::ORDER_TYPE_TO_FW_COEFFICIENT[$orderType]; $coefficient = $this->_sysConfig[$coefficientName]['VALUE'] ?? 1; $validPvPcs += $orderTypeValue * $coefficient; unset($orderType, $orderTypeName, $orderTypeValue, $coefficientName, $coefficient); } unset($perfData, $decRoleBonusFrom); if ( $validPvPcs <= 0 ) continue; $this->loopRelationParentDo($userId, function ($parent) use($userId, $validPvPcs){ //判断parent的报单中心级别 和 服务奖比例 $bonusUserId = $parent['PARENT_UID']; //计算级别之后更新过userInfo的缓存,缓存中级别发生了变化 $bonusUserInfo = CalcCache::getUserInfo($bonusUserId, $this->_periodNum); $isDec = $bonusUserInfo['IS_DEC']; if($isDec == 0) return self::LOOP_CONTINUE; $decRoleId = $bonusUserInfo['DEC_ROLE_ID']; if( !$decRoleId ) return self::LOOP_CONTINUE; if( !isset($this->_decRoleConfig[$decRoleId]) ) return self::LOOP_CONTINUE; $parentDecRoleLevel = $this->_decRoleConfig[$decRoleId]; $parentFwBonusPercent = $parentDecRoleLevel['FW_BONUS_PERCENT'] ?? 0; $cacheMaxPercent = CalcCache::fwMaxBonusPercent($userId, $this->_periodNum); $diffPercent = $parentFwBonusPercent - $cacheMaxPercent; if( $diffPercent <= 0 ) return self::LOOP_CONTINUE; $fwBonus = $validPvPcs * $diffPercent / 100; if( $fwBonus <= 0 ) return self::LOOP_CONTINUE; //给本人添加服务奖比例 CalcCache::fwMaxBonusPercent($userId, $this->_periodNum, $parentFwBonusPercent); //记录奖金和奖金来源到缓存 并实现在缓存中奖金累加 CalcCache::saveFwBonusList($bonusUserId, $this->_periodNum, $fwBonus, ['fromUid'=>$userId, 'fromPvPcs'=>$validPvPcs]); CalcCache::addHasFwBonusUsers($bonusUserId, $this->_periodNum); unset($bonusUserId, $bonusUserInfo, $isDec, $decRoleId, $parentDecRoleLevel, $parentFwBonusPercent, $cacheMaxPercent, $diffPercent, $fwBonus); }); unset($userId, $validPvPcs); } unset($allData, $insertBonusData); return $this->calcBonusBDStepOne($offset + $this->_limit); } unset($allData); return true; } /** * 服务奖第二步 * @param int $offset * @return bool * @throws \yii\db\Exception */ public function calcBonusBDStepTwo(int $offset = 0) { echo sprintf("时间:[%s]服务奖第【2】步,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset); $allData = CalcCache::getHasFwBonusUsers($this->_periodNum, $offset, $this->_limit); if ($allData) { $insertBonusData = []; foreach ($allData as $userId) { $fwBonusData = CalcCache::getFwBonusList($userId, $this->_periodNum); if( !$fwBonusData ) continue; $fwBonus = $fwBonusData['fwBonus'] ?? 0; if( $fwBonus <=0 ) continue; //总金额限制 $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum); CalcCache::bonus($userId, $this->_periodNum, 'BONUS_BD', $fwBonus); $decRoleId = $userBaseInfo['DEC_ROLE_ID']; $insertBonusData[] = [ 'ID' => SnowFake::instance()->generateId(), 'USER_ID' => $userId, 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'], 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'], 'LAST_STATUS' => $userBaseInfo['STATUS'], 'FROM_USER_ID' => $userId, 'LAST_FROM_DEC_LV' => $userBaseInfo['DEC_LV'], 'LAST_FROM_EMP_LV' => $userBaseInfo['EMP_LV'], 'LAST_FROM_STATUS' => $userBaseInfo['STATUS'], 'AMOUNT' => $fwBonus, 'ORI_BONUS' => $fwBonus, 'RECONSUME_POINTS' => 0, 'MANAGE_TAX' => 0, 'PERIOD_NUM' => $this->_periodNum, 'CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime(), 'LOGS' => json_encode([ 'decRoleId' => $decRoleId, ]) ]; unset($userId, $fwBonusData, $userBaseInfo, $decRoleId, $fwBonus); } CalcBonusBD::batchInsert($insertBonusData); unset($insertBonusData, $allData); $this->calcBonusBDStepTwo($offset + $this->_limit); } unset($allData); return true; } /** * 团队奖 * @param int $offset * @return bool * @throws \yii\db\Exception */ public function calcBonusQY(int $offset = 0) { echo sprintf("时间:[%s]团队奖,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset); $periodNum = $this->_periodNum; // 从缓存获取分页有业绩的会员信息 $allData = CalcCache::getHasPerfUsers($this->_periodNum, $offset, $this->_limit); if ($allData) { $insertBonusData = []; foreach ($allData as $userId) { // 从缓存中获取会员的业绩信息 $perfData = CalcCache::nowPeriodPerf($userId, $periodNum); // 从缓存中获取会员的上期结余业绩信息 $pervSurplusPerf = CalcCache::surplusPerf($userId, $periodNum); // 本期 + 上期结余 $perfArr = [ 'SURPLUS_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'], 'SURPLUS_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'], 'SURPLUS_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'], 'SURPLUS_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'], 'SURPLUS_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'], ]; $oriPerfArr = [ 'perfArr' => $perfArr, 'touchBonus' => 0, ]; // 获取会员的报单级别 $userBaseInfo = CalcCache::getUserInfo($userId, $this->_periodNum); $decLevelConfig = $this->_decLevelConfig; $nowDecLevelConfig = $decLevelConfig[$userBaseInfo['DEC_LV']]; // 对碰 $touchBonusArr = $this->touchPerf($oriPerfArr, $perfArr, $nowDecLevelConfig['QY_PERCENT']/100); $touchPerfArr = []; foreach ($touchBonusArr['perfArr'] as $keyR => $perfR) { $touchPerfArr[$keyR] = $perfR; } // 对碰完成后把结余的业绩存入本期业绩缓存中 CalcCache::nowPeriodPerf($userId, $periodNum, $touchPerfArr); //更新数据库 PerfPeriod::updateAll($touchPerfArr, 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [ 'USER_ID' => $userId, 'PERIOD_NUM' => $periodNum, ]); if ($touchBonusArr['touchBonus'] <= 0) continue; $teamBonus = $touchBonusArr['touchBonus']; $capBonusQy = $teamBonus; // 封顶前的奖金 //判断级别上限,个人奖金封顶限制 $teamBonus = $this->declarationLevelCap($teamBonus, $userId, $userBaseInfo['DEC_LV']); if( $teamBonus <= 0 ) continue; // 将封顶前的金额加入用户奖金缓存中,此金额不能发放(总奖金,总实际奖金) CalcCache::bonus($userId, $periodNum, 'CAPPED_BONUS_QY', $capBonusQy); //扣除相应的复消积分和管理费 $deductData = $this->deduct($userId, $teamBonus); // 把对碰后的奖金存入缓存中 CalcCache::bonus($userId, $periodNum, 'BONUS_QY', $teamBonus, $deductData); // TODO:取小腿值 $payLeg = min([$perfArr['SURPLUS_1L'], $perfArr['SURPLUS_2L']]); // 计算荣衔星级 $starCrown = StarCrownLevel::getStarCrown($payLeg); // 星级放入缓存 CalcCache::addUserStarCrown($userId, $periodNum, $starCrown['ID']); //团队奖流水 $insertBonusData[] = [ 'ID' => SnowFake::instance()->generateId(), 'USER_ID' => $userId, 'ORI_CAPPED_BONUS_QY' => $capBonusQy, 'LAST_DEC_LV' => $userBaseInfo['DEC_LV'], 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'], 'LAST_CROWN_LV' => $starCrown['ID'], 'LAST_STATUS' => $userBaseInfo['STATUS'], 'AMOUNT' => $deductData['surplus'], 'ORI_BONUS' => $teamBonus, 'RECONSUME_POINTS' => $deductData['reConsumePoints'], 'MANAGE_TAX' => $deductData['manageTax'], 'PERIOD_NUM' => $this->_periodNum, 'CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime(), 'LOGS' => json_encode([ 'perfArr' => $perfArr, 'touchPerfArrOri' => $touchBonusArr['perfArr'], 'touchPerfArr' => $touchPerfArr, 'nowDecLevelConfig' => $nowDecLevelConfig, 'decLevel' => $userBaseInfo['DEC_LV'], ]), ]; unset($perfData, $pervSurplusPerf, $perfArr, $oriPerfArr, $touchPerfArr, $userBaseInfo, $decLevelConfig, $touchBonusArr, $userId, $nowDecLevelConfig, $teamBonus, $deductData); } CalcBonusQY::batchInsert($insertBonusData); unset($allData, $insertBonusData); return $this->calcBonusQY($offset + $this->_limit); } unset($allData); return true; } /** * 季度奖计算 * */ public function calcQuarter() { if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) { // echo('不是季结点,进这里,不计算季度奖'. PHP_EOL); return false; } $result = \Yii::$app->db->createCommand("CALL QtrCalc(:periodNum)") ->bindValue(':periodNum' , $this->_periodNum ) ->execute(); return $result; } // 执行蓝星管理奖金的存储过程 public function calcBsProcedure() { if( !$this->_isCalcMonth ) { // 不是结算月,则不进行计算 return false; } $result = \Yii::$app->db->createCommand("CALL CalcBlue(:periodNum)") ->bindValue(':periodNum' , $this->_periodNum ) ->execute(); return $result; } // 执行旅游奖的计算 public function calcBonusTourism() { // 月结,如果不是月结点,则直接退出 if (!$this->_isCalcMonth) { return true; } $bonusConfig = $this->_sysConfig['openTourism']; // 达标条件:聘级、级别、奖项比例 $config = json_decode($bonusConfig['OPTIONS'], true); // 奖金总比例 $mate = $bonusConfig['VALUE'] / 100; // 会员级别 $minDecLevel = $config['OPTIONS']['declarationLevel'] ?? []; // 月度公司总PV $monthTotalPV = PerfMonth::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth]) ->sum('PV_PCS'); // 用于分发的奖金总数 $transferAmount = $monthTotalPV * $mate; // 基于蓝星奖结果计算符合获奖条件的会员StarDirector $userStarDirector = CalcBonusBS::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth]) ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_EMP_LV,LAST_STATUS') ->groupBy('USER_ID') ->asArray() ->all(); $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID'); // 基于团队奖/绩效奖结果计算会员的StarCrown $userStarCrown = CalcBonusQY::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth]) ->select('USER_ID,LAST_CROWN_LV') ->groupBy('USER_ID') ->asArray() ->all(); $userStarCrownObj = array_column($userStarCrown, NULL, 'USER_ID'); // 合并用户ID,去重 $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj))); // 奖金点数综合 $bonusPointComplex = 0; $insertBonusData = []; foreach($bonusUsers as $userId) { // 计算奖金:取starDirectorPoint和starCrownPoint的大个值 $starDirectorPoint = $this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['TOURISM_PERCENT'] ?? 0; $starCrownPoint = $this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['TOURISM_PERCENT'] ?? 0; // 奖金比例: $bonusPoint = max($starDirectorPoint, $starCrownPoint); if ($bonusPoint <= 0) { continue; } $insertBonusData[] = [ 'ID' => SnowFake::instance()->generateId(), 'USER_ID' => $userId, 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'], 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LAST_EMP_LV'], 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'], 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'], 'AMOUNT_STANDARD' => 0, 'POINT' => $bonusPoint, 'PERIOD_NUM' => $this->_periodNum, 'CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcYearMonth, 'P_CALC_MONTH' => Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH), 'CREATED_AT' => Date::nowTime(), 'PERF' => $monthTotalPV, 'TRANSFER_RATE' => $mate, 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount), 'CAP_AMOUNT' => 0, 'POINT_COMPLEX' => 0, ]; $bonusPointComplex += $bonusPoint; } // 数据写入总表 if ($insertBonusData) { foreach ($insertBonusData as &$bonusData) { // 计算奖金 $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex)); if ($amount <= 0) { continue; } // 会员级别达到要求才会发放奖金 if ($bonusData['LAST_DEC_LV'] == $minDecLevel) { // 放入缓存 CalcCache::tourismBonus($bonusData['USER_ID'], $this->_periodNum, $amount); // 加入月奖的会员 CalcCache::addHasMonthBonusUsers($bonusData['USER_ID'], $this->_periodNum); } $bonusData['AMOUNT'] = $amount; $bonusData['POINT_COMPLEX'] = $bonusPointComplex; } CalcBonusTourism::batchInsert($insertBonusData); } return true; } // 执行房奖的计算 public function calcBonusVilla() { // 月结,如果不是月结点,则直接退出 if (!$this->_isCalcMonth) { return true; } $bonusConfig = $this->_sysConfig['openVilla']; // 达标条件:聘级、级别、奖项比例 $config = json_decode($bonusConfig['OPTIONS'], true); // 奖金总比例 $mate = $bonusConfig['VALUE'] / 100; // 个人奖金封顶 $capBonus = intval($this->_sysConfig['openVillaCap']['VALUE'] ?? 0); // 会员级别 $minDecLevel = $config['declarationLevel'] ?? []; // 月度公司总PV $monthTotalPV = PerfMonth::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth]) ->sum('PV_PCS'); // 用于分发的奖金总数 $transferAmount = $monthTotalPV * $mate; // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级 $subQuery = CalcBonusQY::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID]) ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT') ->joinWith(['starCrown' => function($query) { $query->select(['LEVEL_NAME', 'SORT']); }]) ->having(1) ->orderBy('USER_ID ASC, SORT DESC'); $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(); // 奖金点数综合 $bonusPointComplex = 0; $insertBonusData = []; foreach($userStarCrownObj as $item) { // 奖金比例 $bonusPoint = $this->_starCrownLevelConfig[$item['LAST_CROWN_LV']]['VILLA_PERCENT'] ?? 0; if (!$bonusPoint) { continue; } // 会员级别达到要求才会发放奖金 if ($item['LAST_DEC_LV'] != $minDecLevel) { continue; } $userBaseInfo = CalcCache::getUserInfo($item['USER_ID'], $this->_periodNum); $insertBonusData[] = [ 'ID' => SnowFake::instance()->generateId(), 'USER_ID' => $item['USER_ID'], 'LAST_DEC_LV' => $item['LAST_DEC_LV'] ?? '', 'LAST_EMP_LV' => $userBaseInfo['LAST_EMP_LV'], 'LAST_STATUS' => $item['LAST_STATUS'] ?? 0, 'LAST_CROWN_LV' => $item['LAST_CROWN_LV'] ?? '', 'AMOUNT' => 0, 'POINT' => $bonusPoint, 'PERIOD_NUM' => $this->_periodNum, 'CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime(), 'PERF' => $monthTotalPV, 'TRANSFER_RATE' => $mate, 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount), 'CAP_AMOUNT' => 0, 'POINT_COMPLEX' => 0, ]; $bonusPointComplex += $bonusPoint; } // 数据写入总表 if ($insertBonusData) { // 计算个人奖金 foreach ($insertBonusData as &$bonusData) { // 计算奖金 $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex)); if ($amount <= 0) { continue; } // 封顶前奖金数 $capAmount = $amount; // 奖金数不能大于封顶值 $amount = ($amount > $capBonus) ? $capBonus : $amount; $bonusData['AMOUNT'] = $amount; $bonusData['CAP_AMOUNT'] = $capAmount; $bonusData['POINT_COMPLEX'] = $bonusPointComplex; // 放入缓存 CalcCache::villaBonus($bonusData['USER_ID'], $this->_periodNum, $amount); // 加入月奖的会员 CalcCache::addHasMonthBonusUsers($bonusData['USER_ID'], $this->_periodNum); } CalcBonusVilla::batchInsert($insertBonusData); } return true; } // 执行车奖的计算 public function calcBonusGarage() { // 月结,如果不是月结点,则直接退出 if (!$this->_isCalcMonth) { return true; } $bonusConfig = $this->_sysConfig['openGarage']; // 达标条件:聘级、级别、奖项比例 $config = json_decode($bonusConfig['OPTIONS'], true); // 奖金总比例 $mate = $bonusConfig['VALUE'] / 100; // 会员级别 $minDecLevel = $config['declarationLevel'] ?? []; // 个人奖金封顶 $capBonus = intval($this->_sysConfig['openGarageCap']['VALUE'] ?? 0); // 月度公司总PV $monthTotalPV = PerfMonth::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth]) ->sum('PV_PCS'); // 用于分发的奖金总数 $transferAmount = $monthTotalPV * $mate; // 基于蓝星奖结果计算符合获奖条件的会员StarDirector $userStarDirector = CalcBonusBS::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH = :CALC_MONTH', [':CALC_MONTH' => $this->_calcYearMonth]) ->select('USER_ID,LEVEL_ID,LAST_DEC_LV,LAST_STATUS') ->groupBy('USER_ID') ->asArray() ->all(); $userStarDirectorObj = array_column($userStarDirector, NULL, 'USER_ID'); // 基于团队奖/绩效奖结果计算会员的StarCrown.StarCrown基于周期计算,一个月会产生多次,取月周期中的最高星级 $subQuery = CalcBonusQY::find() ->yearMonth($this->_calcYearMonth) ->where('CALC_MONTH = :CALC_MONTH AND LAST_CROWN_LV <> :NO_CROWN_LV', [':CALC_MONTH' => $this->_calcYearMonth, ':NO_CROWN_LV' => StarCrownLevel::NO_LEVEL_ID]) ->select('USER_ID,LAST_DEC_LV,LAST_CROWN_LV,LAST_STATUS,LEVEL_NAME,SORT') ->joinWith(['starCrown' => function($query) { $query->select(['LEVEL_NAME', 'SORT']); }]) ->having(1) ->orderBy('USER_ID ASC, SORT DESC'); $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(); // 合并用户ID,去重 $bonusUsers = array_unique(array_merge(array_keys($userStarDirectorObj), array_keys($userStarCrownObj))); sort($bonusUsers); // 奖金点数综合 $bonusPointComplex = 0; $insertBonusData = []; foreach($bonusUsers as $userId) { // 计算奖金:取starDirectorPoint和starCrownPoint的大个值 $starDirectorPoint = !isset($userStarDirectorObj[$userId]['LEVEL_ID']) ? 0 : ($this->_empLevelConfig[$userStarDirectorObj[$userId]['LEVEL_ID']]['GARAGE_PERCENT'] ?? 0); $starCrownPoint = !isset($userStarCrownObj[$userId]['LAST_CROWN_LV']) ? 0: ($this->_starCrownLevelConfig[$userStarCrownObj[$userId]['LAST_CROWN_LV']]['GARAGE_PERCENT'] ?? 0); // 奖金比例: $bonusPoint = max($starDirectorPoint, $starCrownPoint); if ($bonusPoint <= 0) { continue; } // 会员级别达到要求才会发放奖金 $lastDecLv = $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? ''); if ($lastDecLv != $minDecLevel) { continue; } $insertBonusData[] = [ 'ID' => SnowFake::instance()->generateId(), 'USER_ID' => $userId, 'LAST_DEC_LV' => $userStarDirectorObj[$userId]['LAST_DEC_LV'] ?? ($userStarCrownObj[$userId]['LAST_DEC_LV'] ?? ''), 'LAST_EMP_LV' => $userStarDirectorObj[$userId]['LEVEL_ID'] ?? '', 'LAST_STATUS' => $userStarDirectorObj[$userId]['LAST_STATUS'] ?? ($userStarCrownObj[$userId]['LAST_STATUS'] ?? 1), 'LAST_CROWN_LV' => $userStarCrownObj[$userId]['LAST_CROWN_LV'] ?? '', 'AMOUNT' => 0, 'POINT' => $bonusPoint, 'PERIOD_NUM' => $this->_periodNum, 'CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime(), 'PERF' => $monthTotalPV, 'TRANSFER_RATE' => $mate, 'TRANSFER_AMOUNT' => Tool::formatPrice($transferAmount), 'CAP_AMOUNT' => 0, 'POINT_COMPLEX' => 0, ]; $bonusPointComplex += $bonusPoint; } // 数据写入总表 if ($insertBonusData) { foreach ($insertBonusData as &$bonusData) { // 计算奖金 $amount = Tool::formatPrice($transferAmount * ($bonusData['POINT'] / $bonusPointComplex)); if ($amount <= 0) { continue; } // 封顶前奖金数 $capAmount = $amount; // 奖金数不能大于封顶值 $amount = ($amount > $capBonus) ? $capBonus : $amount; $bonusData['AMOUNT'] = $amount; $bonusData['CAP_AMOUNT'] = $capAmount; $bonusData['POINT_COMPLEX'] = $bonusPointComplex; // 放入缓存 CalcCache::garageBonus($bonusData['USER_ID'], $this->_periodNum, $amount); // 加入月奖的会员 CalcCache::addHasMonthBonusUsers($bonusData['USER_ID'], $this->_periodNum); } CalcBonusGarage::batchInsert($insertBonusData); } return true; } /** * 季度奖写用户缓存 * */ public function calcQuarterUser(int $offset = 0) { if( !$this->_isCalcMonth || !in_array($this->_calcMonth, [3,6,9,12])) { // 不是结算月,则不进行计算 return false; } $allData = CalcBonusQuarter::finduseDbCalc() ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]) ->groupBy('USER_ID') ->offset($offset) ->limit($this->_limit) ->asArray() ->all(); if ($allData){ // 达标条件:会员级别:钻卡 $config = json_decode($this->_sysConfig['openQuarter']['OPTIONS'], true); $minDecLevel = $config['declarationLevel'] ?? []; foreach ($allData as $user) { // 扣除相应的复消积分和管理费 $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS']); $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金 $manageTax = $deductData['manageTax']; // 管理费 $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分 // 管理奖钻卡发放 if ($user['LAST_DEC_LV'] == $minDecLevel) { // 把对碰后的奖金存入缓存中 CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_QUARTER', $user['ORI_BONUS'], $deductData); // 加入月奖的会员 CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum); } // 更新奖金存储过程的实发金额数据 CalcBonusQuarter::updateAll([ 'RECONSUME_POINTS' => $point, 'AMOUNT' => $realBonusBs, 'MANAGE_TAX' => $manageTax], 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]); } return $this->calcQuarterUser($offset + $this->_limit); } unset($allData); return true; } /** * 蓝星管理奖金未拆分 * @param int $offset * @return bool * @throws \yii\db\Exception */ public function calcBonusBsGL(int $offset = 0) { if( !$this->_isCalcMonth ) { // 不是结算月,则不进行计算 return false; } // 从缓存获取分页有收入的会员信息 $allData = CalcBonusBS::findUseDbCalc() ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]) ->groupBy('USER_ID') ->offset($offset) ->limit($this->_limit) ->asArray() ->all(); if ($allData) { foreach ($allData as $user) { // 添加到有奖励信息的数据中 CalcCache::addHasBonusUsers($user['USER_ID'], $this->_periodNum); $fxStatus = $this->_isMonthPerfLimit($user['USER_ID']); if ($fxStatus) { // //扣除相应的复消积分和管理费 // $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS']); // // 把对碰后的奖金存入缓存中 // CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS'], $deductData); CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS', $user['ORI_BONUS']); // $realBonusBs = $user['ORI_BONUS']; // 扣除管理费和复消积分后的实发蓝星奖金 // $manageTax = $deductData['manageTax']; // 管理费 // $point = $deductData['reConsumePoints'];// 复消积分 // // 更新蓝星奖金存储过程的实发金额数据 // CalcBonusBS::updateAll(['AMOUNT' => $realBonusBs, 'MANAGE_TAX' => $manageTax, 'RECONSUME_POINTS' => $point], // 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', // [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]); } } return $this->calcBonusBsGL($offset + $this->_limit); } unset($allData); return true; } // /** // * 蓝星管理奖金 // * @param int $offset // * @return bool // * @throws \yii\db\Exception // */ // public function calcBonusBsGL(int $offset = 0) { // if( !$this->_isCalcMonth ) { // // 不是结算月,则不进行计算 // return false; // } // // 从缓存获取分页有收入的会员信息 // $allData = CalcBonusBS::findUseDbCalc() // ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]) // ->groupBy('USER_ID') // ->offset($offset) // ->limit($this->_limit) // ->asArray() // ->all(); // if ($allData) { // // 达标条件:会员级别:钻卡 // $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true); // $minDecLevel = $config['mntDec'] ?? []; // foreach ($allData as $user) { // //扣除相应的复消积分和管理费 // $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_MNT']); // $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金 // $manageTax = $deductData['manageTax']; // 管理费 // $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分 // // 管理奖钻卡发放 // // if (in_array($user['LAST_DEC_LV'], $minDecLevel)) { // // 把对碰后的奖金存入缓存中 // CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_MNT', $user['ORI_BONUS_MNT'], $deductData); // // 加入月奖的会员 // CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum); // // } // // 更新蓝星奖金存储过程的实发金额数据 // CalcBonusBS::updateAll([ // 'RECONSUME_POINTS' => $point, // 'AMOUNT_MNT' => $realBonusBs, // 'MANAGE_TAX_MNT' => $manageTax], // 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', // [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]); // } // return $this->calcBonusBsGL($offset + $this->_limit); // } // unset($allData); // return true; // } // /** // * 蓝星业绩奖金 // * @param int $offset // * @return bool // * @throws \yii\db\Exception // */ // public function calcBonusBsYJ(int $offset = 0) { // if( !$this->_isCalcMonth ) { // // 不是结算月,则不进行计算 // return false; // } // // 从缓存获取分页有收入的会员信息 // $allData = CalcBonusBS::findUseDbCalc() // ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]) // ->groupBy('USER_ID') // ->offset($offset) // ->limit($this->_limit) // ->asArray() // ->all(); // if ($allData) { // // 达标条件:会员级别:金卡、钻卡 // $config = json_decode($this->_sysConfig['openGL']['OPTIONS'], true); // $minDecLevel = $config['abbrDec'] ?? []; // foreach ($allData as $user) { // //扣除相应的复消积分和管理费 // $deductData = $this->deduct($user['USER_ID'], $user['ORI_BONUS_ABBR']); // $realBonusBs = $deductData['surplus']; // 扣除管理费和复消积分后的实发蓝星奖金 // $manageTax = $deductData['manageTax']; // 管理费 // $point = $deductData['reConsumePoints'] + $user['RECONSUME_POINTS'];// 复消积分 // // 业绩奖金卡、钻卡发放 // // if (in_array($user['LAST_DEC_LV'], $minDecLevel)) { // // 把对碰后的奖金存入缓存中 // CalcCache::bonus($user['USER_ID'], $this->_periodNum, 'BONUS_BS_ABBR', $user['ORI_BONUS_ABBR'], $deductData); // // 加入月奖的会员 // CalcCache::addHasMonthBonusUsers($user['USER_ID'], $this->_periodNum); // // } // // 更新蓝星业绩奖金存储过程的实发金额数据 // CalcBonusBS::updateAll([ // 'AMOUNT_ABBR' => $realBonusBs, // 'MANAGE_TAX_ABBR' => $manageTax, // 'RECONSUME_POINTS' => $point], // 'USER_ID=:USER_ID AND PERIOD_NUM=:PERIOD_NUM', // [':USER_ID' => $user['USER_ID'], ':PERIOD_NUM' => $this->_periodNum]); // } // return $this->calcBonusBsYJ($offset + $this->_limit); // } // unset($allData); // return true; // } /** * 对碰 * @param array $oriPerfArr * @param array $perfArr * @param $percent * @param $loopTimes * @return array */ public function touchPerf(array $oriPerfArr, array $perfArr, $percent, $loopTimes=1) { $resultArr = $oriPerfArr; foreach ($perfArr as $keyT => $perfT) { if ($perfT <= 0) { unset($perfArr[$keyT]); } } if (count($perfArr) >= 2 && $loopTimes < 6) { arsort($perfArr, SORT_NUMERIC); $onePerf = null; $oneKey = null; // $touchSurplusAmount = 0; $touchAmount = 0; // 前两个进行对碰 foreach ($perfArr as $key => $perf) { if ($onePerf === null) { $oneKey = $key; $onePerf = $perf; } else { $touchSurplusAmount = $perf - $onePerf; if ($touchSurplusAmount > 0) { unset($perfArr[$oneKey]); $resultArr['perfArr'][$oneKey] = 0; $perfArr[$key] = $touchSurplusAmount; $touchAmount = $onePerf; } elseif ($touchSurplusAmount < 0) { unset($perfArr[$key]); $resultArr['perfArr'][$key] = 0; $perfArr[$oneKey] = abs($touchSurplusAmount); $touchAmount = $perf; } else { unset($perfArr[$oneKey], $perfArr[$key]); $resultArr['perfArr'][$oneKey] = 0; $resultArr['perfArr'][$key] = 0; $touchAmount = $perf; } break; } } $touchBonus = Tool::formatPrice($touchAmount * $percent); $resultArr['touchBonus'] += $touchBonus; foreach ($perfArr as $keyR => $perfR) { $resultArr['perfArr'][$keyR] = $perfR; } return $this->touchPerf($resultArr, $perfArr, $percent, $loopTimes+1); } return $resultArr; } /** * 循环父级并执行回调函数 * @param $userId * @param callable $callbackFunc * @param int $offset * @return bool */ public function loopNetworkParentDo($userId, callable $callbackFunc, int $offset = 0) { $allParents = Cache::getAllNetworkParents($userId); $allData = array_slice($allParents, $offset, $this->_limit); unset($allParents); if ($allData) { foreach ($allData as $data) { $funcResult = $callbackFunc($data); if ($funcResult === self::LOOP_FINISH) { return true; } elseif ($funcResult === self::LOOP_CONTINUE) { continue; } unset($data, $funcResult); } unset($allData); return $this->loopNetworkParentDo($userId, $callbackFunc, $offset + $this->_limit); } return true; } /** * 循环推荐网络的父级 * @param $userId * @param callable $callbackFunc * @param int $offset * @return bool */ public function loopRelationParentDo($userId, callable $callbackFunc, int $offset = 0) { $allParents = Cache::getAllRelationParents($userId); $allData = array_slice($allParents, $offset, $this->_limit); unset($allParents); if ($allData) { foreach ($allData as $data) { $funcResult = $callbackFunc($data); if ($funcResult === self::LOOP_FINISH) { return true; } elseif ($funcResult === self::LOOP_CONTINUE) { continue; } unset($data, $funcResult); } unset($allData); return $this->loopRelationParentDo($userId, $callbackFunc, $offset + $this->_limit); } return true; } /** * 按级别的收入上限 * 新的需求调整: 改成不按月进行限制,并且去掉vip奖 * @param $bonus * @param $userId * @param $declarationLevel * @return float */ public function declarationLevelCap($bonus, $userId, $declarationLevel) { $decLevelConfig = $this->_decLevelConfig; $nowDecLevelConfig = $decLevelConfig[$declarationLevel]; unset($decLevelConfig); $maxGetBonus = $nowDecLevelConfig['INCOME_CAP']; if( $bonus <= $maxGetBonus) { return $bonus; }else { return $maxGetBonus; } } /** * 扣除复消积分和管理费 * @param $userId * @param $bonus * @return array * @throws \yii\db\Exception */ public function deduct($userId, $bonus) { //判断是否达到了本月扣除复消的上限 $cacheData = CalcCache::monthLastPeriodReconsumePoints($userId, $this->_periodNum, $this->_calcYearMonth); $bonusCache = CalcCache::bonus($userId, $this->_periodNum); $reConsumePointsTotal = $bonusCache['RECONSUME_POINTS'] + $cacheData['RECONSUME_POINTS_SUM']; $reConsumePointsCap = $this->_sysConfig['reConsumePointsMonthCap']['VALUE']; unset($cacheData, $bonusCache); $reConsumePoints = 0; if( $reConsumePointsTotal < $reConsumePointsCap ) { $reConsumePoints = $bonus * $this->_sysConfig['reConsumePointsPercent']['VALUE'] / 100; $reConsumePoints = min($reConsumePoints, $reConsumePointsCap-$reConsumePointsTotal); } unset($reConsumePointsTotal, $reConsumePointsCap); $manageTax = $bonus * $this->_sysConfig['manageTaxPercent']['VALUE'] / 100; $surplus = $bonus - $reConsumePoints - $manageTax; return [ 'reConsumePoints' => Tool::formatPrice($reConsumePoints),//复效积分 'manageTax' => Tool::formatPrice($manageTax),//管理费 'surplus' => Tool::formatPrice($surplus),//真实奖金 ]; } /** * 更新百分比并发送 * @param $percent */ private function _updatePercent($percent) { // 把数据写入数据库中 Period::updateAll(['CALC_PERCENT' => $percent], 'PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $this->_periodNum]); \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'PERIOD', 'ID' => $this->_periodId, 'FIELD' => 'CALC_PERCENT']); } /** * 循环奖服务奖金会员,并入库 * @param int $offset * @return bool * @throws \yii\db\Exception */ public function loopBonusUsers($offset = 0) { echo sprintf("时间:[%s]缓存奖金数据入库,当前offset为:【%s】" . PHP_EOL, date('Y-m-d H:i:s', time()) , $offset); // 从缓存列表里面从底层往上倒序获取会员 $allData = CalcCache::getHasBonusUsers($this->_periodNum, $offset, $this->_limit); if($allData){ $insertDataBonus = []; foreach($allData as $userId){ $tempBonusData = $this->bonusData($userId); if(!empty($tempBonusData['result'])){ $insertDataBonus[] = $tempBonusData['result']; } unset($userId, $tempBonusData); } CalcBonus::batchInsert($insertDataBonus); unset($insertDataBonus, $allData); return $this->loopBonusUsers($offset + $this->_limit); } return true; } /** * 奖金 * @param $userId * @return array * @throws \yii\db\Exception */ public function bonusData($userId) { // 从缓存中获取用户的奖金 $bonus = CalcCache::bonus($userId, $this->_periodNum); $baseInfo = CalcCache::getUserInfo($userId, $this->_periodNum); $perfData = CalcCache::nowPeriodPerf($userId, $this->_periodNum); $tourismBonus = CalcCache::tourismBonus($userId, $this->_periodNum); $garageBonus = CalcCache::garageBonus($userId, $this->_periodNum); $villaBonus = CalcCache::villaBonus($userId, $this->_periodNum); $pervSurplusPerf = CalcCache::surplusPerf($userId, $this->_periodNum); // 星级 $starCrownLv = CalcCache::getUserStarCrown($userId, $this->_periodNum); //没有共享和管理奖 $bonusReal = $bonus['BONUS_BD'] + $bonus['BONUS_TG'] + $bonus['BONUS_XF'] + $bonus['BONUS_YJ'] + $bonus['BONUS_QY'] + $bonus['BONUS_YC'] + $bonus['BONUS_YC_EXTRA'] + $bonus['BONUS_VIP'] + $bonus['BONUS_BS_MNT'] + $bonus['BONUS_BS_ABBR'] + $bonus['BONUS_QUARTER']; $realBonusGx = 0; $realBonusGl = 0; $realBonusBs = 0; // 蓝星管理奖. BlueStar $blueStartOriBonus = 0; $blueStartManageTax = 0; $exchangePoints = 0; // 蓝星奖管理奖. 产生的兑换积分 $realBonusBsMnt = 0; // 蓝星管理奖——实发奖金 $blueStartOriBonusMnt = 0; // 蓝星管理奖——原奖金 $blueStartManageTaxMnt = 0; // 蓝星管理奖——管理费 $realBonusBsAbbr = 0; // 蓝星业绩奖——实发奖金 $blueStartOriBonusAbbr = 0; // 蓝星业绩奖——原奖金 $blueStartManageTaxAbbr = 0; // 蓝星业绩奖——管理费 if( $this->_isCalcMonth ) { // 个人月消费PV大于配置值,才会计算发放蓝星奖 $fxPvStatus = $this->_isMonthPerfLimit($userId); // BONUS_REAL 字段是发到用户的真实奖金 if ( $fxPvStatus ) { $userBS = CalcBonusBS::find() ->where( 'PERIOD_NUM=:PERIOD_NUM AND USER_ID=:USER_ID', [ ':PERIOD_NUM' => $this->_periodNum, ':USER_ID' => $userId ] ) ->select('AMOUNT,ORI_BONUS,MANAGE_TAX,LEVEL_ID,PRODUCT_POINT,AMOUNT_MNT,ORI_BONUS_MNT,MANAGE_TAX_MNT,AMOUNT_ABBR,ORI_BONUS_ABBR,MANAGE_TAX_ABBR') ->limit(1) ->orderBy('CREATED_AT DESC') ->asArray() ->all(); $userBS = is_array($userBS) ? reset($userBS) : []; $blueStartAmount = isset($userBS['AMOUNT']) && !empty($userBS['AMOUNT']) ? $userBS['AMOUNT'] : 0; // 奖金 $blueStartOriBonus = isset($userBS['ORI_BONUS']) && !empty($userBS['ORI_BONUS']) ? $userBS['ORI_BONUS'] : 0; // 原奖金 $blueStartManageTax = isset($userBS['MANAGE_TAX']) && !empty($userBS['MANAGE_TAX']) ? $userBS['MANAGE_TAX'] : 0; // 管理费 $realBonusBsMnt = $userBS['AMOUNT_MNT'] ?? 0; // 蓝星管理奖. 实发奖金 $blueStartOriBonusMnt = $userBS['ORI_BONUS_MNT'] ?? 0; // 蓝星管理奖. 原奖金 $blueStartManageTaxMnt = $userBS['MANAGE_TAX_MNT'] ?? 0; // 蓝星管理奖. 管理费 $realBonusBsAbbr = $userBS['AMOUNT_ABBR'] ?? 0; // 蓝星业绩奖. 奖金 $blueStartOriBonusAbbr = $userBS['ORI_BONUS_ABBR'] ?? 0; // 蓝星业绩奖. 原奖金 $blueStartManageTaxAbbr = $userBS['MANAGE_TAX_ABBR'] ?? 0; // 蓝星业绩奖. 管理费 $blueStartManageTax += $blueStartManageTaxMnt + $blueStartManageTaxAbbr; // 管理费 $realBonusBs = $blueStartAmount; // 蓝星奖直接取数据库中算好的值PRODUCT_POINT $exchangePoints = isset($userBS['PRODUCT_POINT']) && !empty($userBS['PRODUCT_POINT']) ? $userBS['PRODUCT_POINT'] : 0; // 兑换积分 } } if( $this->_isCalcMonth ) { //季度奖 if(in_array($this->_calcMonth, [3,6,9,12])){ // 季度奖 } } $result = [ 'USER_ID' => $userId, 'LAST_USER_NAME' => $baseInfo['USER_NAME'], 'LAST_REAL_NAME' => $baseInfo['REAL_NAME'], 'LAST_DEC_LV' => $baseInfo['DEC_LV'], 'LAST_EMP_LV' => $baseInfo['LAST_EMP_LV'], 'LAST_CROWN_LV' => $starCrownLv ?? StarCrownLevel::getDefaultLevelId(), 'LAST_STATUS' => $baseInfo['STATUS'], 'LAST_REC_USER_NAME' => $baseInfo['REC_USER_NAME'], 'LAST_REC_REAL_NAME' => $baseInfo['REC_REAL_NAME'], 'LAST_CON_USER_NAME' => $baseInfo['CON_USER_NAME'], 'LAST_CON_REAL_NAME' => $baseInfo['CON_REAL_NAME'], 'EXCHANGE_POINTS' => $exchangePoints, // 兑换积分 // 'LAST_LOCATION' => $baseInfo['LOCATION'] ? $baseInfo['LOCATION'] : 1, //@todo 'LAST_LOCATION' => 1, 'BONUS_BD' => $bonus['BONUS_BD'], 'BONUS_TG' => $bonus['BONUS_TG'], 'BONUS_QY' => $bonus['BONUS_QY'], 'RECONSUME_POINTS' => $bonus['RECONSUME_POINTS'], 'MANAGE_TAX' => $blueStartManageTax, // 管理费 'BONUS_INCOME'=>$bonus['INCOME_TOTAL'], 'BONUS_REAL'=> $bonusReal, 'BONUS_TOTAL'=>$bonus['BONUS_TOTAL'], 'ORI_BONUS_BD' => $bonus['ORI_BONUS_BD'], 'ORI_BONUS_TG' => $bonus['ORI_BONUS_TG'], 'BONUS_BS' => $realBonusBs, // 新的管理奖金,即蓝星管理奖 'ORI_BONUS_BS' => $blueStartOriBonus, // 蓝星管理奖金原奖金,即包含管理费 'REAL_BONUS_BS' => $realBonusBs, // 实发蓝星管理奖金 'BONUS_BS_MNT' => $realBonusBsMnt, // 蓝星管理奖 'ORI_BONUS_BS_MNT' => $blueStartOriBonusMnt, // 蓝星管理奖金原奖金,即包含管理费 'REAL_BONUS_BS_MNT' => $realBonusBsMnt, // 实发蓝星管理奖金 'MANAGE_TAX_MNT' => $blueStartManageTaxMnt, // 实发蓝星管理——管理费 'BONUS_BS_ABBR' => $realBonusBsAbbr, // 蓝星业绩奖 'ORI_BONUS_BS_ABBR' => $blueStartOriBonusAbbr, // 蓝星业绩奖金原奖金,即包含管理费 'REAL_BONUS_BS_ABBR' => $realBonusBsAbbr, // 实发蓝星业绩奖金 'MANAGE_TAX_ABBR' => $blueStartManageTaxAbbr, // 实发蓝星业绩奖——管理费 'ORI_BONUS_QY' => $bonus['ORI_BONUS_QY'], 'ORI_CAPPED_BONUS_QY' => $bonus['ORI_CAPPED_BONUS_QY'], // 团队奖封顶前的奖金 'BONUS_QUARTER' => $bonus['BONUS_QUARTER'], 'ORI_BONUS_QUARTER' => $bonus['ORI_BONUS_QUARTER'], 'BONUS_TOURISM' => $tourismBonus, // 旅游奖 'BONUS_VILLA' => $villaBonus, // 房奖 'BONUS_GARAGE' => $garageBonus, // 车奖 //以下没有用 'PV_1L' => $perfData['PV_1L_TOUCH'],//TOUCH为碰业绩 'QY_1L' => $perfData['PV_1L_TOUCH'] + $pervSurplusPerf['SURPLUS_1L'], 'SURPLUS_1L' => $perfData['SURPLUS_1L'], 'PV_2L' => $perfData['PV_2L_TOUCH'], 'QY_2L' => $perfData['PV_2L_TOUCH'] + $pervSurplusPerf['SURPLUS_2L'], 'SURPLUS_2L' => $perfData['SURPLUS_2L'], 'PV_3L' => $perfData['PV_3L_TOUCH'], 'QY_3L' => $perfData['PV_3L_TOUCH'] + $pervSurplusPerf['SURPLUS_3L'], 'SURPLUS_3L' => $perfData['SURPLUS_3L'], 'PV_4L' => $perfData['PV_4L_TOUCH'], 'QY_4L' => $perfData['PV_4L_TOUCH'] + $pervSurplusPerf['SURPLUS_4L'], 'SURPLUS_4L' => $perfData['SURPLUS_4L'], 'PV_5L' => $perfData['PV_5L_TOUCH'], 'QY_5L' => $perfData['PV_5L_TOUCH'] + $pervSurplusPerf['SURPLUS_5L'], 'SURPLUS_5L' => $perfData['SURPLUS_5L'], 'PV_PCS' => $perfData['PV_PCS'], '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']), 'PERIOD_NUM' => $this->_periodNum, 'CALC_YEAR' => $this->_calcYear, 'CALC_MONTH' => $this->_calcYearMonth, 'CREATED_AT' => Date::nowTime(), ]; $resend = []; unset($bonus, $realBonusGx, $realBonusGl, $bonusReal); return ['result'=>$result,'resend'=>$resend]; } // 判断是否满足月最低消费 public function _isMonthPerfLimit($userId) { $userMonthTotal = PerfMonth::find()->where( 'USER_ID=:USER_ID AND CALC_MONTH=:CALC_MONTH', ['USER_ID'=>$userId, 'CALC_MONTH'=>$this->_calcYearMonth] ) ->asArray() ->one(); $fxPvStatus = false; if (isset($userMonthTotal['PV_PCS']) && $userMonthTotal['PV_PCS'] >= $this->_sysConfig['monthPcsPvFxCondition']['VALUE']) { $fxPvStatus = true; } return $fxPvStatus; } }