SeedUserStatistics.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace addons\Seed\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. use yii\db\Expression;
  7. /**
  8. * This is the model class for table "{{%addons_seed_user_statistics}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $store_id 门店id
  13. * @property int $user_id 用户ID
  14. * @property int $pay_order_num 支付订单总数
  15. * @property int $finish_order_num 完成订单总数
  16. * @property int $seed_num 新增种子金数量
  17. * @property int $collect_seed_num 已收取种子金总数量
  18. * @property int $refund_seed_num 退款种子金总数量
  19. * @property int $date 日期,如20220726
  20. * @property int $status 状态,-1:失效,1:正常,2:冻结
  21. * @property int $created_at 添加时间戳
  22. * @property int $updated_at 更新时间戳
  23. */
  24. class SeedUserStatistics extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_seed_user_statistics}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'store_id', 'user_id', 'pay_order_num', 'finish_order_num', 'seed_num', 'collect_seed_num', 'refund_seed_num', 'date', 'status', 'created_at', 'updated_at'], 'integer'],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => '商城ID',
  50. 'store_id' => '门店id',
  51. 'user_id' => '用户ID',
  52. 'pay_order_num' => '支付订单总数',
  53. 'finish_order_num' => '完成订单总数',
  54. 'seed_num' => '新增种子金数量',
  55. 'collect_seed_num' => '已收取种子金总数量',
  56. 'refund_seed_num' => '退款种子金总数量',
  57. 'date' => '日期,如20220726',
  58. 'status' => '状态,-1:失效,1:正常,2:冻结',
  59. 'created_at' => '添加时间戳',
  60. 'updated_at' => '更新时间戳',
  61. ];
  62. }
  63. /**
  64. * 创建统计
  65. * @Author: lun
  66. * @DateTime: 2022/8/1 0001 16:19
  67. * @Copyright: copyright (c) 2021 广东七件事集团
  68. * @param $mall_id
  69. * @param $store_id
  70. * @param $user_id
  71. * @param $date
  72. * @return array|bool
  73. */
  74. public static function create($mall_id, $store_id, $user_id, $date)
  75. {
  76. try {
  77. $model = self::find()->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'date' => $date, 'store_id' => $store_id, 'status' => StatusEnum::ENABLED])->one();
  78. if (empty($model)) {
  79. $model = new self();
  80. $model->mall_id = $mall_id;
  81. $model->store_id = $store_id;
  82. $model->user_id = $user_id;
  83. $model->date = $date;
  84. $model->loadDefaultValues();
  85. } else {
  86. $model->updated_at = time();
  87. }
  88. if (!$model->save()) throw new Exception($model->getErrorMessage());
  89. return $model->toArray();
  90. } catch (\Exception $e) {
  91. self::setStaticError('创建种子金统计:'.$e->getMessage());
  92. return false;
  93. }
  94. }
  95. /**
  96. * 保存数据
  97. * @Author: lun
  98. * @DateTime: 2022/8/1 0001 16:16
  99. * @Copyright: copyright (c) 2021 广东七件事集团
  100. * @param $params
  101. * @param $statistics
  102. * @throws \Exception
  103. */
  104. public static function setData($params, $statistics = [])
  105. {
  106. try {
  107. $date = date('Ymd', time());
  108. $data = self::create($params['mall_id'], $params['store_id'], $params['user_id'], $date);
  109. if ($data === false) throw new \Exception(SeedUserStatistics::getStaticError());
  110. // 修改统计的数据
  111. if ($statistics) {
  112. $res = SeedUserStatistics::updateAllCounters($statistics, ['id' => $data['id']]);
  113. if ($res === false) throw new \Exception(SeedUserStatistics::getStaticError());
  114. }
  115. return true;
  116. } catch (\Exception $e) {
  117. self::setStaticError($e->getMessage());
  118. return false;
  119. }
  120. }
  121. /**
  122. * 修改数据
  123. * @Author: lun
  124. * @DateTime: 2022/8/15 0015 10:23
  125. * @Copyright: copyright (c) 2021 广东七件事集团
  126. * @param $mall_id
  127. * @param $user_id
  128. * @param int $store_id
  129. * @param array $params
  130. * @throws Exception
  131. */
  132. public static function saveStatistics($mall_id, $user_id, $store_id = 0, $params = [])
  133. {
  134. $update_data['updated_at'] = time();
  135. foreach ($params as $key => $value) {
  136. $symbol = $value >= 0 ? '+' : '-';
  137. $update_data[$key] = new Expression($key.$symbol.abs($value));
  138. }
  139. $res = self::updateAll($update_data, ['mall_id' => $mall_id, 'store_id' => $store_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  140. if (!$res) throw new Exception('修改用户每日统计数据失败');
  141. }
  142. }