| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace common\models\census;
- use common\enums\RabbitMqEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\models\base\BaseActiveRecord;
- use Yii;
- /**
- * This is the model class for table "qimall_census_mall_single_month".
- *
- * @property int $id ID
- * @property int $mall_id 商城ID
- * @property float $total_order_money 下单金额
- * @property float $total_pay_money 交易金额
- * @property float $total_refund_money 退款金额
- * @property float $total_deduction_money 抵扣金额
- * @property float $total_recharge_money 充值金额
- * @property string $month 月份
- * @property int $status 状态[-1:删除;0:禁用;1启用;]
- * @property int $created_at 创建时间
- * @property int $updated_at 上次更新数据时间
- */
- class CensusMallSingleMonth extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%census_mall_single_month}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['total_order_money', 'total_pay_money', 'total_refund_money', 'total_deduction_money', 'total_recharge_money'], 'number'],
- [['month'], 'string', 'max' => 20],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'total_order_money' => '下单金额',
- 'total_pay_money' => '交易金额',
- 'total_refund_money' => '退款金额',
- 'total_deduction_money' => '抵扣金额',
- 'total_recharge_money' => '充值金额',
- 'month' => '月份',
- 'status' => '状态[-1:删除;0:禁用;1启用;]',
- 'created_at' => '创建时间',
- 'updated_at' => '上次更新数据时间',
- ];
- }
- public static function setData($params)
- {
- try {
- if (empty($params['mall_id']) && empty($params['month'])) {
- return false;
- }
- $model = self::findOne(['mall_id' => $params['mall_id'], 'month' => $params['month'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->month = $params['month'];
- $model->loadDefaultValues();
- }
- if (!empty($params['total_order_money'])) $model->total_order_money += $params['total_order_money'];
- if (!empty($params['total_pay_money'])) $model->total_pay_money += $params['total_pay_money'];
- if (!empty($params['total_refund_money'])) $model->total_refund_money += $params['total_refund_money'];
- if (!empty($params['total_deduction_money'])) $model->total_deduction_money += $params['total_deduction_money'];
- if (!empty($params['total_recharge_money'])) $model->total_recharge_money += $params['total_recharge_money'];
- if (!$model->save()) throw new \Exception($model->getErrorMessage());
- return $model;
- } catch (\Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- public static function setSingleMonthCache($mall_id, $field, $data, $type = 'total_order_money')
- {
- $redis = \Yii::$app->redis;
- $month = date('Y-m', time());
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $month;
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $result = $redis->hget($redis_key, $field);
- switch ($type) {
- case 'total_order_money':
- $data['total_order_money'] = $data['pay_money'] ?? 0;
- !empty($result) ? $result += $data['pay_money'] : $result = $data['pay_money'];
- break;
- case 'total_deduction_money':
- $data['total_deduction_money'] = $data['total_deduction_money'] ?? 0;
- !empty($result) ? $result += $data['total_deduction_money'] : $result = $data['total_deduction_money'];
- break;
- case 'total_pay_money':
- $data['total_pay_money'] = $data['pay_money'] ?? 0;
- !empty($result) ? $result += $data['pay_money'] : $result = $data['pay_money'];
- break;
- case 'total_refund_money':
- $data['total_refund_money'] = $data['total_refund_money'] ?? 0;
- !empty($result) ? $result += $data['total_refund_money'] : $result = $data['total_refund_money'];
- break;
- case 'total_recharge_money':
- $data['total_recharge_money'] = $data['total_recharge_money'] ?? 0;
- !empty($result) ? $result += $data['total_recharge_money'] : $result = $data['total_recharge_money'];
- break;
- default:
- return;
- }
- $redis->hset($redis_key, $field, $result);
- $data['mall_id'] = $mall_id;
- $data['month'] = $month;
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK_CENSUS, RabbitMqEnum::CENSUS_QUEUE, $data, CensusMallSingleMonth::class, 'setData');
- }
- public static function getCensusMallCache($mall_id, $month)
- {
- $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $month;
- $redis_key = RedisKeyEnum::suffix($key, '', true);
- $data_cache = Yii::$app->redis->hgetall($redis_key);
- $data = [];
- if ($data_cache) {
- foreach ($data_cache as $k => $v) {
- if ($k % 2 == 0) $data[$v] = $data_cache[$k + 1];
- }
- }
- return $data;
- }
- public static function getCensusMall($mall_id, $month)
- {
- return self::getOne([['mall_id' => $mall_id], ['month' => $month]], true);
- }
- }
|