| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace common\models\statistics;
- use common\helpers\FormatHelper;
- use common\models\base\BaseActiveRecord;
- use Exception;
- use RuntimeException;
- use Yii;
- abstract class BaseUserStatistics extends BaseActiveRecord
- {
- /**
- * 是否是根据日期统计
- *
- * @var bool
- */
- const IS_BY_DATE = false;
- /**
- * 允许自增的字段
- *
- * @var string[]
- */
- const ALLOW_INCR_FIELD = [
- 'order_num',
- 'order_money',
- 'pay_money',
- 'pay_goods_num',
- 'average_money',
- 'finish_order_num',
- 'finish_order_money',
- 'finish_goods_num',
- 'original_order_money',
- 'balance_pay',
- 'wechat_pay',
- 'ali_pay',
- 'pay_selling_price',
- 'finish_selling_price',
- 'user_goods_price_total',
- 'user_order_num',
- ];
- /**
- * 允许自减的字段
- *
- * @var string[]
- */
- const ALLOW_DECR_FIELD = [
- 'order_num',
- 'order_money',
- 'pay_money',
- 'pay_goods_num',
- 'average_money',
- 'original_order_money',
- 'balance_pay',
- 'wechat_pay',
- 'ali_pay',
- 'pay_selling_price',
- 'finish_selling_price',
- 'user_goods_price_total',
- 'user_order_num',
- ];
- /**
- * 创建数据
- *
- * @param array $data
- *
- * @return bool
- * @throws RuntimeException
- */
- protected static function setData(array $data): bool
- {
- try {
- $model = new static();
- $model->loadDefaultValues();
- $model->load($data, '');
- $model->save();
- } catch (Exception $e) {
- return false;
- }
- return true;
- }
- /**
- * 保存统计数据
- *
- * @param array $baseData
- * @param array $data
- * @param bool $isIncr
- *
- * @return bool
- */
- public static function saveUserStatisticsData(array $baseData, array $data, bool $isIncr = true): bool
- {
- $baseWhere = [
- 'user_id' => $baseData['user_id']
- ];
- if (static::IS_BY_DATE) {
- $baseWhere['date'] = $baseData['date'];
- }
- $isExist = static::find()->where($baseWhere)->count();
- if (!$isExist) {
- $saveData = [
- 'user_id' => $baseData['user_id'],
- 'mall_id' => $baseData['mall_id'],
- ];
- if (static::IS_BY_DATE) {
- $saveData['date'] = $baseData['date'];
- }
- static::setData($saveData);
- }
- // 用户获取field
- $newModel = new static;
- // 保存的数据
- $updateData = [];
- // 允许操作的字段
- $allowUpdateField = $isIncr ? static::ALLOW_INCR_FIELD : static::ALLOW_DECR_FIELD;
- // 循环赋值
- foreach ($newModel as $field => $_) {
- !empty($data[$field]) && in_array($field, $allowUpdateField, true) && $updateData[$field] = $data[$field];
- }
- if (empty($updateData)) {
- return true;
- }
- try {
- static::updateAllCounters($updateData, $baseWhere);
- } catch (Exception $e) {
- $desc = sprintf(
- '保存用户统计数据失败,data: %s',
- json_encode([
- 'class' => static::class,
- 'agrs' => func_get_args(),
- 'saveData' => compact('updateData')
- ])
- );
- Yii::error(FormatHelper::exception($e, $desc));
- return false;
- }
- return true;
- }
- }
|