BaseUserStatistics.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace common\models\statistics;
  3. use common\helpers\FormatHelper;
  4. use common\models\base\BaseActiveRecord;
  5. use Exception;
  6. use RuntimeException;
  7. use Yii;
  8. abstract class BaseUserStatistics extends BaseActiveRecord
  9. {
  10. /**
  11. * 是否是根据日期统计
  12. *
  13. * @var bool
  14. */
  15. const IS_BY_DATE = false;
  16. /**
  17. * 允许自增的字段
  18. *
  19. * @var string[]
  20. */
  21. const ALLOW_INCR_FIELD = [
  22. 'order_num',
  23. 'order_money',
  24. 'pay_money',
  25. 'pay_goods_num',
  26. 'average_money',
  27. 'finish_order_num',
  28. 'finish_order_money',
  29. 'finish_goods_num',
  30. 'original_order_money',
  31. 'balance_pay',
  32. 'wechat_pay',
  33. 'ali_pay',
  34. 'pay_selling_price',
  35. 'finish_selling_price',
  36. 'user_goods_price_total',
  37. 'user_order_num',
  38. ];
  39. /**
  40. * 允许自减的字段
  41. *
  42. * @var string[]
  43. */
  44. const ALLOW_DECR_FIELD = [
  45. 'order_num',
  46. 'order_money',
  47. 'pay_money',
  48. 'pay_goods_num',
  49. 'average_money',
  50. 'original_order_money',
  51. 'balance_pay',
  52. 'wechat_pay',
  53. 'ali_pay',
  54. 'pay_selling_price',
  55. 'finish_selling_price',
  56. 'user_goods_price_total',
  57. 'user_order_num',
  58. ];
  59. /**
  60. * 创建数据
  61. *
  62. * @param array $data
  63. *
  64. * @return bool
  65. * @throws RuntimeException
  66. */
  67. protected static function setData(array $data): bool
  68. {
  69. try {
  70. $model = new static();
  71. $model->loadDefaultValues();
  72. $model->load($data, '');
  73. $model->save();
  74. } catch (Exception $e) {
  75. return false;
  76. }
  77. return true;
  78. }
  79. /**
  80. * 保存统计数据
  81. *
  82. * @param array $baseData
  83. * @param array $data
  84. * @param bool $isIncr
  85. *
  86. * @return bool
  87. */
  88. public static function saveUserStatisticsData(array $baseData, array $data, bool $isIncr = true): bool
  89. {
  90. $baseWhere = [
  91. 'user_id' => $baseData['user_id']
  92. ];
  93. if (static::IS_BY_DATE) {
  94. $baseWhere['date'] = $baseData['date'];
  95. }
  96. $isExist = static::find()->where($baseWhere)->count();
  97. if (!$isExist) {
  98. $saveData = [
  99. 'user_id' => $baseData['user_id'],
  100. 'mall_id' => $baseData['mall_id'],
  101. ];
  102. if (static::IS_BY_DATE) {
  103. $saveData['date'] = $baseData['date'];
  104. }
  105. static::setData($saveData);
  106. }
  107. // 用户获取field
  108. $newModel = new static;
  109. // 保存的数据
  110. $updateData = [];
  111. // 允许操作的字段
  112. $allowUpdateField = $isIncr ? static::ALLOW_INCR_FIELD : static::ALLOW_DECR_FIELD;
  113. // 循环赋值
  114. foreach ($newModel as $field => $_) {
  115. !empty($data[$field]) && in_array($field, $allowUpdateField, true) && $updateData[$field] = $data[$field];
  116. }
  117. if (empty($updateData)) {
  118. return true;
  119. }
  120. try {
  121. static::updateAllCounters($updateData, $baseWhere);
  122. } catch (Exception $e) {
  123. $desc = sprintf(
  124. '保存用户统计数据失败,data: %s',
  125. json_encode([
  126. 'class' => static::class,
  127. 'agrs' => func_get_args(),
  128. 'saveData' => compact('updateData')
  129. ])
  130. );
  131. Yii::error(FormatHelper::exception($e, $desc));
  132. return false;
  133. }
  134. return true;
  135. }
  136. }