SeedCapitalStatistics.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace addons\Seed\common\models;
  3. use addons\Seed\common\enums\SeedEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. use yii\db\Exception;
  7. use yii\db\Expression;
  8. /**
  9. * This is the model class for table "{{%addons_seed_capital_statistics}}".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property int|null $total_seed_num 累计种子金总数量
  14. * @property int|null $collect_seed_num 已收取种子金总数量
  15. * @property int|null $refund_seed_num 退款种子金总数量
  16. * @property int|null $bad_seed_num 坏账种子金数量
  17. * @property float|null $total_energy_pool 当前能量池总额
  18. * @property float|null $cumulative_energy_pool 累计能量池总额
  19. * @property float|null $release_energy_pool 已释放能量池总额
  20. * @property int|null $release_num 已释放能量池次数
  21. * @property int $last_release_at 上次释放时间
  22. * @property int $status 状态,-1:失效,1:正常,2:冻结
  23. * @property int $created_at 添加时间戳
  24. * @property int $updated_at 更新时间戳
  25. */
  26. class SeedCapitalStatistics extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_seed_capital_statistics}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'total_seed_num', 'collect_seed_num', 'refund_seed_num', 'bad_seed_num', 'release_num', 'last_release_at', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['total_energy_pool', 'cumulative_energy_pool', 'release_energy_pool'], 'number'],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'mall_id' => '商城ID',
  53. 'total_seed_num' => '累计种子金总数量',
  54. 'collect_seed_num' => '已收取种子金总数量',
  55. 'refund_seed_num' => '退款种子金总数量',
  56. 'bad_seed_num' => '坏账种子金数量',
  57. 'total_energy_pool' => '当前能量池总额',
  58. 'cumulative_energy_pool' => '累计能量池总额',
  59. 'release_energy_pool' => '已释放能量池总额',
  60. 'release_num' => '已释放能量池次数',
  61. 'last_release_at' => '上次释放时间',
  62. 'status' => '状态,-1:失效,1:正常,2:冻结',
  63. 'created_at' => '添加时间戳',
  64. 'updated_at' => '更新时间戳',
  65. ];
  66. }
  67. /**
  68. * 初始化一条统计数据
  69. * @Author: lun
  70. * @DateTime: 2022/7/27 0027 18:06
  71. * @Copyright: copyright (c) 2021 广东七件事集团
  72. * @param $mall_id
  73. * @return bool
  74. */
  75. public static function create($mall_id)
  76. {
  77. try {
  78. $model = self::findOne(['mall_id' => $mall_id]);
  79. if (empty($model)) {
  80. $model = new self();
  81. $model->mall_id = $mall_id;
  82. $model->loadDefaultValues();
  83. if (!$model->save()) throw new Exception($model->getErrorMessage());
  84. }
  85. return true;
  86. } catch (\Exception $e) {
  87. self::setStaticError($e->getMessage());
  88. return false;
  89. }
  90. }
  91. /**
  92. * 数据统计修改
  93. * @Author: lun
  94. * @DateTime: 2022/8/1 0001 16:28
  95. * @Copyright: copyright (c) 2021 广东七件事集团
  96. * @param $mall_id
  97. * @param $params
  98. * @throws Exception
  99. */
  100. public static function saveStatistics($mall_id, $params)
  101. {
  102. $update_data['updated_at'] = time();
  103. foreach ($params as $key => $value) {
  104. $symbol = $value >= 0 ? '+' : '-';
  105. $update_data[$key] = new Expression($key.$symbol.abs($value));
  106. }
  107. $res = self::updateAll($update_data, ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  108. if (!$res) throw new Exception('修改统计数据失败');
  109. }
  110. /**
  111. * 种子金释放失败处理
  112. * @Author: lun
  113. * @DateTime: 2022/8/3 0003 14:11
  114. * @Copyright: copyright (c) 2021 广东七件事集团
  115. * @param $params
  116. * @param string $reason
  117. * @return bool
  118. */
  119. public static function handleReleaseFail($params, $reason = '')
  120. {
  121. $trans = Yii::$app->db->beginTransaction();
  122. try {
  123. $pool_log = SeedEnergyPool::findOne(['id' => $params['pool_id']]);
  124. // 不等于释放中直接退出,避免重复修改数据
  125. if ($pool_log->release_status != SeedEnum::RELEASEING) {
  126. $trans->commit();
  127. return true;
  128. }
  129. // 释放失败返还种子金奖金池
  130. $model = self::findOne(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  131. $model->total_energy_pool += $pool_log->release_energy_pool;
  132. $model->release_energy_pool -= $pool_log->release_energy_pool;
  133. if (!$model->save()) throw new Exception($model->getErrorMessage());
  134. $pool_log->reason = $reason;
  135. $pool_log->release_status = SeedEnum::RELEASE_FAIL;
  136. if (!$pool_log->save()) throw new Exception($pool_log->getErrorMessage());
  137. $trans->commit();
  138. return true;
  139. } catch (\Exception $e) {
  140. $trans->rollBack();
  141. Yii::error('种子金返还失败:' . $e->getMessage());
  142. return false;
  143. }
  144. }
  145. }