['type' => 'separately', 'table' => 'AR_CALC_BONUS'], 'calcBonusBs' => ['type' => 'same', 'table' => 'AR_CALC_BONUS_BS'], 'calcBonusQuarter' => ['type' => 'same', 'table' => 'AR_CALC_BONUS_QUARTER'], 'calcBonusTourism' => ['type' => 'same', 'table' => 'AR_CALC_BONUS_TOURISM'], 'calcBonusBd' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_BD'], 'calcBonusQy' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_QY'], 'calcBonusTg' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_TG'], 'calcBonusVilla' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_VILLA'], 'calcBonusGarage' => ['type' => 'calc_month', 'table' => 'AR_CALC_BONUS_GARAGE'], ]; public function __construct($periodNum) { parent::__construct($periodNum); } public function start(): array { $db = $this->_calc_db_name; try { //清除本期原有数据,可多次拉取 // \Yii::$app->$db->createCommand()->delete($table, "PERIOD_NUM = $this->_periodNum")->execute(); $this->clearCalcTableData(); foreach (self::BASE_INFO_METHODS as $method => $info) { $table = $info['table']; if ('same' == $info['type']) { $_offset = 0; sameBonus: $offset = $_offset * $this->_limit; $data = \Yii::$app->$db->createCommand("SELECT * from $table where PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll(); if (!empty($data)) { $fieldArray = array_keys($data[0]); $_offset += 1; \Yii::$app->db->createCommand()->batchInsert($table, $fieldArray, $data)->execute(); $data = null; goto sameBonus; } $data = null; continue; } if ('calc_month' == $info['type']) { $_offset = 0; calcMonth: $offset = $_offset * $this->_limit; $data = \Yii::$app->$db->createCommand("SELECT *,FROM_UNIXTIME(CREATED_AT,'%Y-%m-%d') as P_CALC_MONTH from $table where PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll(); if (!empty($data)) { $fieldArray = array_keys($data[0]); $_offset += 1; \Yii::$app->db->createCommand()->batchInsert($table, $fieldArray, $data)->execute(); $data = null; goto calcMonth; } $data = null; continue; } if ('separately' == $info['type'] && method_exists($this, $method)) { $this->$method(); continue;//必要,避免增加逻辑代码后忘记此处 } } } catch (\Exception $e) { return $this->fail('msg:' . $e->getMessage() . 'line:' . $e->getLine()); } //同步周期表的值到业务系统 self::pullPeriodForUpdate($this->_periodNum); return $this->success(); } public function calcBonus() { $repeatField = [ // 'ORI_CAPPED_BONUS_QY' => ['CAPPED_BONUS_QY'], 'ORI_BONUS_QY' => ['BONUS_QY'], 'ORI_BONUS_BS' => ['BONUS_BS', 'REAL_BONUS_BS'], 'ORI_BONUS_QUARTER' => ['BONUS_QUARTER'], 'ORI_BONUS_TOURISM' => ['BONUS_TOURISM'], 'ORI_BONUS_GARAGE' => ['BONUS_GARAGE'], 'ORI_BONUS_VILLA' => ['BONUS_VILLA'], 'ORI_BONUS_BD' => ['BONUS_BD'], 'ORI_BONUS_TG' => ['BONUS_TG'], ]; $db = $this->_calc_db_name; $table = self::BASE_INFO_METHODS['calcBonus']['table']; $pCalcMonth = Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH); $calcPeriod = $this->getCalcPeriod(); $calculatedAt = $calcPeriod['CALCULATED_AT']; $_offset = 0; $this->_limit = 2000; calcMonth: $offset = $_offset * $this->_limit; $data = \Yii::$app->$db->createCommand("SELECT b.*, 1 as LAST_LOCATION, $calculatedAt as CALCULATED_AT, '$pCalcMonth' as P_CALC_MONTH from $table as b where b.PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll(); if (!empty($data)) { $_offset += 1; $data = array_column($data, null, 'USER_ID'); $userIds = array_keys($data); //获取用户信息相关的数据 在下方循环内以名为$user的变量整合 $userInfo = array_column(self::getUserInfoByUserIds($userIds), null, 'ID'); //获取用户积分相关的数据 $userPointsInfo = array_column(self::getUserPointsByUserIds($userIds), null, 'ID'); foreach ($data as $userId => $value) { if (isset($userInfo[$userId])) { $user = $userInfo[$userId]; $user['LAST_REC_USER_NAME'] = $user['recUserName']['USER_NAME'] ?? ''; $user['LAST_REC_REAL_NAME'] = $user['recRealName']['REAL_NAME'] ?? ''; $user['LAST_CON_USER_NAME'] = $user['conUserName']['USER_NAME'] ?? ''; $user['LAST_CON_REAL_NAME'] = $user['conRealName']['REAL_NAME'] ?? ''; $user['LAST_SYSTEM_ID'] = $user['LAST_SYSTEM_ID'] ?? ''; $user['EXCHANGE_POINTS'] = $userPointsInfo[$userId] ?? 0; //将ori开头的奖金项目赋值到实发奖金字段 $value = self::formatDataByParams($value, $repeatField); unset($user['ID'], $user['REC_UID'], $user['CON_UID'], $user['recUserName'], $user['recRealName'], $user['conUserName'], $user['conRealName'], $value['ORI_BONUS_TOURISM'], $value['ORI_BONUS_GARAGE'], $value['ORI_BONUS_VILLA']); //整合数据 $data[$userId] = array_merge($value, $user); } } $fieldArray = array_keys(current($data)); \Yii::$app->db->createCommand()->batchInsert($table, $fieldArray, $data)->execute(); $userInfo = null; $data = null; goto calcMonth; } $data = null; return true; } /** * @param array $userIds * @return array|\yii\db\ActiveRecord[] */ public static function getUserInfoByUserIds(array $userIds): array { if (empty($userIds)) { return []; } return UserInfo::find()->alias('UI') ->join('INNER JOIN', User::tableName() . ' AS U', 'U.ID = UI.USER_ID') ->where(['in', 'U.ID', $userIds]) ->select(' U.ID,U.STATUS AS LAST_STATUS,U.MOBILE AS LAST_MOBILE,U.PERIOD_AT AS LAST_PERIOD_AT,U.CREATED_AT AS LAST_CREATED_AT, U.SUB_COM_ID AS LAST_SUB_COM_ID,U.PROVINCE AS LAST_PROVINCE,U.CITY AS LAST_CITY,U.COUNTY AS LAST_COUNTY, U.IS_DIRECT_SELLER AS LAST_IS_DIRECT_SELLER,UI.SYSTEM_ID AS LAST_SYSTEM_ID,UI.REC_UID,UI.CON_UID') ->with([ 'recUserName' => function ($query) { $query->select('USER_ID,USER_NAME'); }, 'recRealName' => function ($query) { $query->select('ID,REAL_NAME'); }, 'conUserName' => function ($query) { $query->select('USER_ID,USER_NAME'); }, 'conRealName' => function ($query) { $query->select('ID,REAL_NAME'); } ]) ->asArray() ->all(); } /** * @param array $userIds * @return array|\yii\db\ActiveRecord[] */ public static function getUserPointsByUserIds(array $userIds): array { if (empty($userIds)) { return []; } return UserBonus::find()->where(['in', 'USER_ID', $userIds]) ->select('EXCHANGE_POINTS,USER_ID') ->asArray() ->all(); } /** * @param array $data * @param array $format * @return array */ public static function formatDataByParams(array $data, array $format): array { if (empty($data) || empty($format)) { return []; } foreach ($data as $field => $v) { if (isset($format[$field])) { foreach ($format[$field] as $formatField) { $data[$formatField] = $v; } } } return $data; } /** * 清空相关表数据 */ public function clearCalcTableData() { // 奖金表 CalcBonus::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusBS::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusQuarter::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusBD::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusQY::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusTG::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); // CalcBonusYJ::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); // CalcBonusGX::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); // CalcBonusGL::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); // 月结时要清空的数据 if ($this->_isCalcMonth) { // CalcBonusYC::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); // CalcBonusVIP::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); // CalcBonusStandard::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); // ScoreMonth::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusTourism::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusGarage::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); CalcBonusVilla::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum); } } }