BossGoodsStatistics.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace addons\Boss\common\models;
  3. use common\enums\StatusEnum;
  4. use common\helpers\FormatHelper;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "{{%addons_boss_goods_statistics}}".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城ID
  12. * @property int $user_id 用户id
  13. * @property int $goods_id 商品ID
  14. * @property int $buy_num 购买数量
  15. * @property float $money 实付金额
  16. * @property int $date 日期
  17. * @property int $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int|null $created_at 创建时间
  19. * @property int|null $updated_at 更新时间
  20. */
  21. class BossGoodsStatistics extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_boss_goods_statistics}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['mall_id', 'user_id', 'goods_id', 'buy_num', 'date', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['money'], 'number'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => 'ID',
  47. 'mall_id' => '商城ID',
  48. 'user_id' => '用户id',
  49. 'goods_id' => '商品ID',
  50. 'buy_num' => '购买数量',
  51. 'money' => '实付金额',
  52. 'date' => '日期',
  53. 'status' => '状态[-1:删除;0:禁用;1启用]',
  54. 'created_at' => '创建时间',
  55. 'updated_at' => '更新时间',
  56. ];
  57. }
  58. /**
  59. * 保存统计
  60. * @Author: lun
  61. * @DateTime: 2022/9/12 0012 17:45
  62. * @Copyright: copyright (c) 2021 广东七件事集团
  63. * @param $order_detail
  64. * @return bool
  65. */
  66. public static function setData($order_detail)
  67. {
  68. $trans = Yii::$app->db->beginTransaction();
  69. try {
  70. $date = date('Ymd', time());
  71. foreach ($order_detail as $detail) {
  72. $model = self::findOne(['user_id' => $detail['user_id'], 'goods_id' => $detail['goods_id'], 'date' => $date, 'status' => StatusEnum::ENABLED]);
  73. if (empty($model)) {
  74. $model = new self();
  75. $model->mall_id = $detail['mall_id'];
  76. $model->user_id = $detail['user_id'];
  77. $model->goods_id = $detail['goods_id'];
  78. $model->buy_num = $detail['num'];
  79. $model->money = $detail['goods_price'];
  80. $model->date = $date;
  81. $model->loadDefaultValues();
  82. } else {
  83. $model->buy_num += $detail['num'];
  84. $model->money += $detail['goods_price'];
  85. }
  86. if (!$model->save()) throw new Exception($model->getErrorMessage());
  87. }
  88. $trans->commit();
  89. return true;
  90. } catch (\Exception $e) {
  91. $trans->rollBack();
  92. $exception = FormatHelper::exception($e, "股东分红:用户购物统计");
  93. self::setStaticError($exception);
  94. return false;
  95. }
  96. }
  97. }