CensusMallSingleMonth.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace common\models\census;
  3. use common\enums\RabbitMqEnum;
  4. use common\enums\RedisKeyEnum;
  5. use common\enums\StatusEnum;
  6. use common\models\base\BaseActiveRecord;
  7. use Yii;
  8. /**
  9. * This is the model class for table "qimall_census_mall_single_month".
  10. *
  11. * @property int $id ID
  12. * @property int $mall_id 商城ID
  13. * @property float $total_order_money 下单金额
  14. * @property float $total_pay_money 交易金额
  15. * @property float $total_refund_money 退款金额
  16. * @property float $total_deduction_money 抵扣金额
  17. * @property float $total_recharge_money 充值金额
  18. * @property string $month 月份
  19. * @property int $status 状态[-1:删除;0:禁用;1启用;]
  20. * @property int $created_at 创建时间
  21. * @property int $updated_at 上次更新数据时间
  22. */
  23. class CensusMallSingleMonth extends BaseActiveRecord
  24. {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function tableName()
  29. {
  30. return '{{%census_mall_single_month}}';
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
  39. [['total_order_money', 'total_pay_money', 'total_refund_money', 'total_deduction_money', 'total_recharge_money'], 'number'],
  40. [['month'], 'string', 'max' => 20],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => '商城ID',
  51. 'total_order_money' => '下单金额',
  52. 'total_pay_money' => '交易金额',
  53. 'total_refund_money' => '退款金额',
  54. 'total_deduction_money' => '抵扣金额',
  55. 'total_recharge_money' => '充值金额',
  56. 'month' => '月份',
  57. 'status' => '状态[-1:删除;0:禁用;1启用;]',
  58. 'created_at' => '创建时间',
  59. 'updated_at' => '上次更新数据时间',
  60. ];
  61. }
  62. public static function setData($params)
  63. {
  64. try {
  65. if (empty($params['mall_id']) && empty($params['month'])) {
  66. return false;
  67. }
  68. $model = self::findOne(['mall_id' => $params['mall_id'], 'month' => $params['month'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  69. if (empty($model)) {
  70. $model = new self();
  71. $model->mall_id = $params['mall_id'];
  72. $model->month = $params['month'];
  73. $model->loadDefaultValues();
  74. }
  75. if (!empty($params['total_order_money'])) $model->total_order_money += $params['total_order_money'];
  76. if (!empty($params['total_pay_money'])) $model->total_pay_money += $params['total_pay_money'];
  77. if (!empty($params['total_refund_money'])) $model->total_refund_money += $params['total_refund_money'];
  78. if (!empty($params['total_deduction_money'])) $model->total_deduction_money += $params['total_deduction_money'];
  79. if (!empty($params['total_recharge_money'])) $model->total_recharge_money += $params['total_recharge_money'];
  80. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  81. return $model;
  82. } catch (\Exception $e) {
  83. self::$static_error = $e->getMessage();
  84. return false;
  85. }
  86. }
  87. public static function setSingleMonthCache($mall_id, $field, $data, $type = 'total_order_money')
  88. {
  89. $redis = \Yii::$app->redis;
  90. $month = date('Y-m', time());
  91. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $month;
  92. $redis_key = RedisKeyEnum::suffix($key, '', true);
  93. $result = $redis->hget($redis_key, $field);
  94. switch ($type) {
  95. case 'total_order_money':
  96. $data['total_order_money'] = $data['pay_money'] ?? 0;
  97. !empty($result) ? $result += $data['pay_money'] : $result = $data['pay_money'];
  98. break;
  99. case 'total_deduction_money':
  100. $data['total_deduction_money'] = $data['total_deduction_money'] ?? 0;
  101. !empty($result) ? $result += $data['total_deduction_money'] : $result = $data['total_deduction_money'];
  102. break;
  103. case 'total_pay_money':
  104. $data['total_pay_money'] = $data['pay_money'] ?? 0;
  105. !empty($result) ? $result += $data['pay_money'] : $result = $data['pay_money'];
  106. break;
  107. case 'total_refund_money':
  108. $data['total_refund_money'] = $data['total_refund_money'] ?? 0;
  109. !empty($result) ? $result += $data['total_refund_money'] : $result = $data['total_refund_money'];
  110. break;
  111. case 'total_recharge_money':
  112. $data['total_recharge_money'] = $data['total_recharge_money'] ?? 0;
  113. !empty($result) ? $result += $data['total_recharge_money'] : $result = $data['total_recharge_money'];
  114. break;
  115. default:
  116. return;
  117. }
  118. $redis->hset($redis_key, $field, $result);
  119. $data['mall_id'] = $mall_id;
  120. $data['month'] = $month;
  121. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK_CENSUS, RabbitMqEnum::CENSUS_QUEUE, $data, CensusMallSingleMonth::class, 'setData');
  122. }
  123. public static function getCensusMallCache($mall_id, $month)
  124. {
  125. $key = RedisKeyEnum::CENSUS_MALL_SINGLE_MONTH . ':' . $mall_id . ':' . $month;
  126. $redis_key = RedisKeyEnum::suffix($key, '', true);
  127. $data_cache = Yii::$app->redis->hgetall($redis_key);
  128. $data = [];
  129. if ($data_cache) {
  130. foreach ($data_cache as $k => $v) {
  131. if ($k % 2 == 0) $data[$v] = $data_cache[$k + 1];
  132. }
  133. }
  134. return $data;
  135. }
  136. public static function getCensusMall($mall_id, $month)
  137. {
  138. return self::getOne([['mall_id' => $mall_id], ['month' => $month]], true);
  139. }
  140. }