| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- <?php
- namespace addons\Seed\common\models;
- use addons\Seed\common\enums\SeedEnum;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%addons_seed_capital_statistics}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int|null $total_seed_num 累计种子金总数量
- * @property int|null $collect_seed_num 已收取种子金总数量
- * @property int|null $refund_seed_num 退款种子金总数量
- * @property int|null $bad_seed_num 坏账种子金数量
- * @property float|null $total_energy_pool 当前能量池总额
- * @property float|null $cumulative_energy_pool 累计能量池总额
- * @property float|null $release_energy_pool 已释放能量池总额
- * @property int|null $release_num 已释放能量池次数
- * @property int $last_release_at 上次释放时间
- * @property int $status 状态,-1:失效,1:正常,2:冻结
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- */
- class SeedCapitalStatistics extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_seed_capital_statistics}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['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'],
- [['total_energy_pool', 'cumulative_energy_pool', 'release_energy_pool'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'total_seed_num' => '累计种子金总数量',
- 'collect_seed_num' => '已收取种子金总数量',
- 'refund_seed_num' => '退款种子金总数量',
- 'bad_seed_num' => '坏账种子金数量',
- 'total_energy_pool' => '当前能量池总额',
- 'cumulative_energy_pool' => '累计能量池总额',
- 'release_energy_pool' => '已释放能量池总额',
- 'release_num' => '已释放能量池次数',
- 'last_release_at' => '上次释放时间',
- 'status' => '状态,-1:失效,1:正常,2:冻结',
- 'created_at' => '添加时间戳',
- 'updated_at' => '更新时间戳',
- ];
- }
- /**
- * 初始化一条统计数据
- * @Author: lun
- * @DateTime: 2022/7/27 0027 18:06
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @return bool
- */
- public static function create($mall_id)
- {
- try {
- $model = self::findOne(['mall_id' => $mall_id]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $mall_id;
- $model->loadDefaultValues();
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 数据统计修改
- * @Author: lun
- * @DateTime: 2022/8/1 0001 16:28
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $params
- * @throws Exception
- */
- public static function saveStatistics($mall_id, $params)
- {
- $update_data['updated_at'] = time();
- foreach ($params as $key => $value) {
- $symbol = $value >= 0 ? '+' : '-';
- $update_data[$key] = new Expression($key.$symbol.abs($value));
- }
- $res = self::updateAll($update_data, ['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
- if (!$res) throw new Exception('修改统计数据失败');
- }
- /**
- * 种子金释放失败处理
- * @Author: lun
- * @DateTime: 2022/8/3 0003 14:11
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @param string $reason
- * @return bool
- */
- public static function handleReleaseFail($params, $reason = '')
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $pool_log = SeedEnergyPool::findOne(['id' => $params['pool_id']]);
- // 不等于释放中直接退出,避免重复修改数据
- if ($pool_log->release_status != SeedEnum::RELEASEING) {
- $trans->commit();
- return true;
- }
- // 释放失败返还种子金奖金池
- $model = self::findOne(['mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
- $model->total_energy_pool += $pool_log->release_energy_pool;
- $model->release_energy_pool -= $pool_log->release_energy_pool;
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- $pool_log->reason = $reason;
- $pool_log->release_status = SeedEnum::RELEASE_FAIL;
- if (!$pool_log->save()) throw new Exception($pool_log->getErrorMessage());
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- Yii::error('种子金返还失败:' . $e->getMessage());
- return false;
- }
- }
- }
|