| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace addons\Seed\common\models;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- use yii\db\Expression;
- /**
- * This is the model class for table "{{%addons_seed_user_statistics}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $store_id 门店id
- * @property int $user_id 用户ID
- * @property int $pay_order_num 支付订单总数
- * @property int $finish_order_num 完成订单总数
- * @property int $seed_num 新增种子金数量
- * @property int $collect_seed_num 已收取种子金总数量
- * @property int $refund_seed_num 退款种子金总数量
- * @property int $date 日期,如20220726
- * @property int $status 状态,-1:失效,1:正常,2:冻结
- * @property int $created_at 添加时间戳
- * @property int $updated_at 更新时间戳
- */
- class SeedUserStatistics extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_seed_user_statistics}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['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'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'store_id' => '门店id',
- 'user_id' => '用户ID',
- 'pay_order_num' => '支付订单总数',
- 'finish_order_num' => '完成订单总数',
- 'seed_num' => '新增种子金数量',
- 'collect_seed_num' => '已收取种子金总数量',
- 'refund_seed_num' => '退款种子金总数量',
- 'date' => '日期,如20220726',
- 'status' => '状态,-1:失效,1:正常,2:冻结',
- 'created_at' => '添加时间戳',
- 'updated_at' => '更新时间戳',
- ];
- }
- /**
- * 创建统计
- * @Author: lun
- * @DateTime: 2022/8/1 0001 16:19
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $store_id
- * @param $user_id
- * @param $date
- * @return array|bool
- */
- public static function create($mall_id, $store_id, $user_id, $date)
- {
- try {
- $model = self::find()->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'date' => $date, 'store_id' => $store_id, 'status' => StatusEnum::ENABLED])->one();
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $mall_id;
- $model->store_id = $store_id;
- $model->user_id = $user_id;
- $model->date = $date;
- $model->loadDefaultValues();
- } else {
- $model->updated_at = time();
- }
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- return $model->toArray();
- } catch (\Exception $e) {
- self::setStaticError('创建种子金统计:'.$e->getMessage());
- return false;
- }
- }
- /**
- * 保存数据
- * @Author: lun
- * @DateTime: 2022/8/1 0001 16:16
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $params
- * @param $statistics
- * @throws \Exception
- */
- public static function setData($params, $statistics = [])
- {
- try {
- $date = date('Ymd', time());
- $data = self::create($params['mall_id'], $params['store_id'], $params['user_id'], $date);
- if ($data === false) throw new \Exception(SeedUserStatistics::getStaticError());
- // 修改统计的数据
- if ($statistics) {
- $res = SeedUserStatistics::updateAllCounters($statistics, ['id' => $data['id']]);
- if ($res === false) throw new \Exception(SeedUserStatistics::getStaticError());
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 修改数据
- * @Author: lun
- * @DateTime: 2022/8/15 0015 10:23
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $user_id
- * @param int $store_id
- * @param array $params
- * @throws Exception
- */
- public static function saveStatistics($mall_id, $user_id, $store_id = 0, $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, 'store_id' => $store_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (!$res) throw new Exception('修改用户每日统计数据失败');
- }
- }
|