'balance', self::ASSETS_SCORE => 'score', self::ASSETS_COMMISSION => 'commission', self::ASSETS_BALANCE_FROZEN => 'balance_frozen', self::ASSETS_SCORE_FROZEN => 'score_frozen', self::ASSETS_COMMISSION_FROZEN => 'commission_frozen', self::ASSETS_WITHDRAW => 'withdraw', ]; //每个指标需要查询的表 const ASSETS_ARR = [ self::ASSETS_BALANCE => [self::ASSETS_BALANCE, self::ASSETS_BALANCE_FROZEN, self::ASSETS_WITHDRAW], self::ASSETS_COMMISSION => [self::ASSETS_COMMISSION, self::ASSETS_COMMISSION_FROZEN, self::ASSETS_WITHDRAW], self::ASSETS_SCORE => [self::ASSETS_SCORE, self::ASSETS_SCORE_FROZEN] ]; /** * 执行统计操作 * * @author hpei * @return bool */ public function execute() { $mall_list = Mall::getEnableMallList(true); if (!empty($mall_list)) { $date = date('Ymd'); foreach ($mall_list as $mall) { $mallId = $mall['id']; try { $lastStatistics = CorntabAssetsStatistics::find()->select(['type', 'last_id', 'id'])->where(['mall_id' => $mallId])->indexBy('type')->asArray()->all(); $trans = Yii::$app->db->beginTransaction(); foreach (self::ASSETS_ARR as $type => $actions) { $statisticsData = []; //循环每一张表 foreach ($actions as $assetsType) { $lastId = isset($lastStatistics[$assetsType]) ? $lastStatistics[$assetsType]['last_id'] : 0; $name = self::ASSETS_ENUM[$assetsType]; //积分没有提现,不需要type if ($type == self::ASSETS_ENUM[self::ASSETS_SCORE]) { $data = $this->getData($mallId, $name, $lastId); } else { $data = $this->getData($mallId, $name, $lastId, $type); } $statisticsData = array_merge($statisticsData, $data); unset($statisticsData['last_id']); if ($name == self::ASSETS_ENUM[self::ASSETS_WITHDRAW] && $type == self::ASSETS_COMMISSION) continue; if (isset($lastStatistics[$assetsType])) { $id = $lastStatistics[$assetsType]['id']; $updateData = []; $updateData['updated_at'] = time(); if ($data['last_id'] > 0) $updateData['last_id'] = $data['last_id']; $res = CorntabAssetsStatistics::updateAll($updateData, ['id' => $id]); } else { $object = new CorntabAssetsStatistics(); $object->updated_at = time(); $object->created_at = time(); $object->mall_id = $mallId; $object->type = $assetsType; $object->last_id = $data['last_id']; $res = $object->save(); } if (!$res) { $trans->rollBack(); throw new \Exception($object->getErrorMessage()); } } $capitalStatistics = CapitalStatistics::find()->where(['mall_id' => $mallId, 'date' => $date, 'type' => $type])->one(); if (empty($capitalStatistics)) { $capitalStatistics = new CapitalStatistics(); $capitalStatistics->date = $date; $capitalStatistics->type = $type; $capitalStatistics->mall_id = $mallId; } if (!empty($statisticsData)) { foreach ($statisticsData as $key => $value) { if ($value) $capitalStatistics->$key = $value; } if (!$capitalStatistics->save()) { $trans->rollBack(); throw new \Exception($capitalStatistics->getErrorMessage()); } } } $trans->commit(); } catch (\Exception $e) { $trans->rollBack(); $exception = FormatHelper::exception($e, "统计资金定时任务失败,行数". $e->getLine() . $e->getFile()); echo $exception; Yii::error($exception); $this->setError($exception); return false; } } } return true; } /** * 查询每个统计项数据 * * @param int $mall_id * @param string $name * @param int $last_id 上次执行最后一条记录ID * @param int $from 提现来源1佣金2余额 */ public function getData($mall_id, $name, $last_id, $from = 0) { switch ($name) { //积分 余额 佣金 case ($name == self::ASSETS_ENUM[self::ASSETS_SCORE] || $name == self::ASSETS_ENUM[self::ASSETS_COMMISSION] || $name == self::ASSETS_ENUM[self::ASSETS_BALANCE]): CapitalLog::$capitalType = $name; $scoreList = CapitalLog::find()->select(['id', 'change_type', 'change_num'])->where(['mall_id' => $mall_id])->andWhere(['>', 'id', $last_id])->limit(self::LIMIT)->all(); $last_id = 0; $capitalTotal = 0; //发放的资产 $expendCapitalTotal = 0; //支出的资产 if (!empty($scoreList)) { foreach ($scoreList as $score) { $last_id = $score['id']; if ($score['change_type'] == CapitalLog::CHANGE_TYPE_ADD) $capitalTotal += $score['change_num']; if ($score['change_type'] == CapitalLog::CHANGE_TYPE_SUB) $expendCapitalTotal += $score['change_num']; } } return ['capital_total' => $capitalTotal, 'expend_capital_total' => $expendCapitalTotal, 'last_id' => $last_id]; //冻结余额 冻结积分 冻结佣金 case ($name == self::ASSETS_ENUM[self::ASSETS_SCORE_FROZEN] || $name == self::ASSETS_ENUM[self::ASSETS_COMMISSION_FROZEN] || $name == self::ASSETS_ENUM[self::ASSETS_BALANCE_FROZEN]): CapitalLog::$capitalType = $name; $scoreList = CapitalLog::find()->select(['id', 'change_type', 'change_num', 'status'])->where(['mall_id' => $mall_id])->andWhere(['>', 'id', $last_id])->asArray()->limit(self::LIMIT)->all(); $last_id = 0; $incomeCapitalTotal = 0;//总资产,包括已经冻结的 $invalidCapitalTotal = 0;//无效资产 if (!empty($scoreList)) { foreach ($scoreList as $score) { $last_id = $score['id']; if ($score['status'] == -1 && $score['change_type'] == CapitalLog::CHANGE_TYPE_ADD) $invalidCapitalTotal += $score['change_num']; if ($score['status'] != -1 && $score['change_type'] == CapitalLog::CHANGE_TYPE_ADD) $incomeCapitalTotal += $score['change_num']; } } return ['income_capital_total' => $incomeCapitalTotal, 'invalid_capital_total' => $invalidCapitalTotal, 'last_id' => $last_id]; //提现 case self::ASSETS_ENUM[self::ASSETS_WITHDRAW]: //资金表1:余额,3:佣金;提现表1:佣金,2:余额 if ($from == 1) $from = 2; if ($from == 3) $from = 1; $where = ['mall_id' => $mall_id, 'status' => [UserWithdrawLog::STATUS_AGREE, UserWithdrawLog::STATUS_ALREADY_TRANS]]; $withdrawLog = UserWithdrawLog::find()->select(['id', 'from', 'actual_num', 'poundage_num'])->where($where)->andWhere(['>', 'id', $last_id])->asArray()->limit(self::LIMIT)->all(); $last_id = 0; $cashOutTotal = 0;//提现 $serviceChargesTatoal = 0;//手续费 if (!empty($withdrawLog)) { foreach ($withdrawLog as $log) { $last_id = $log['id']; if ($log['from'] == $from) { $cashOutTotal += $log['actual_num'] ?? 0; $serviceChargesTatoal += $log['poundage_num'] ?? 0; } } } return ['cash_out_total' => $cashOutTotal, 'service_charges_tatoal' => $serviceChargesTatoal, 'last_id' => $last_id]; } } }