| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
- namespace addons\Boss\common\models;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_boss_goods_statistics}}".
- *
- * @property int $id
- * @property int $mall_id 商城ID
- * @property int $user_id 用户id
- * @property int $goods_id 商品ID
- * @property int $buy_num 购买数量
- * @property float $money 实付金额
- * @property int $date 日期
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int|null $created_at 创建时间
- * @property int|null $updated_at 更新时间
- */
- class BossGoodsStatistics extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_boss_goods_statistics}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['mall_id', 'user_id', 'goods_id', 'buy_num', 'date', 'status', 'created_at', 'updated_at'], 'integer'],
- [['money'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mall_id' => '商城ID',
- 'user_id' => '用户id',
- 'goods_id' => '商品ID',
- 'buy_num' => '购买数量',
- 'money' => '实付金额',
- 'date' => '日期',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * 保存统计
- * @Author: lun
- * @DateTime: 2022/9/12 0012 17:45
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $order_detail
- * @return bool
- */
- public static function setData($order_detail)
- {
- $trans = Yii::$app->db->beginTransaction();
- try {
- $date = date('Ymd', time());
- foreach ($order_detail as $detail) {
- $model = self::findOne(['user_id' => $detail['user_id'], 'goods_id' => $detail['goods_id'], 'date' => $date, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->mall_id = $detail['mall_id'];
- $model->user_id = $detail['user_id'];
- $model->goods_id = $detail['goods_id'];
- $model->buy_num = $detail['num'];
- $model->money = $detail['goods_price'];
- $model->date = $date;
- $model->loadDefaultValues();
- } else {
- $model->buy_num += $detail['num'];
- $model->money += $detail['goods_price'];
- }
- if (!$model->save()) throw new Exception($model->getErrorMessage());
- }
- $trans->commit();
- return true;
- } catch (\Exception $e) {
- $trans->rollBack();
- $exception = FormatHelper::exception($e, "股东分红:用户购物统计");
- self::setStaticError($exception);
- return false;
- }
- }
- }
|