AddonsCouponGoodStatistics.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace addons\Coupon\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. /**
  6. * This is the model class for table "{{%addons_coupon_good_statistics}}".
  7. *
  8. * @property int $id
  9. * @property int $mall_id
  10. * @property int $coupon_id
  11. * @property int $good_id
  12. * @property int $booking_number 该商品使用该优惠券下单的总数量
  13. * @property int $payed_number 使用该优惠券已支付的该商品数量
  14. * @property int $booking_user_number 该商品使用该优惠券下单的总人数
  15. * @property int $buyed_user_number 使用该优惠券已购买该商品人数
  16. * @property int $created_at 创建时间
  17. * @property int $updated_at
  18. */
  19. class AddonsCouponGoodStatistics extends \common\models\base\BaseActiveRecord
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static function tableName()
  25. {
  26. return '{{%addons_coupon_good_statistics}}';
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['mall_id', 'coupon_id', 'good_id'], 'required'],
  35. [['mall_id', 'coupon_id', 'good_id', 'booking_number', 'payed_number', 'booking_user_number', 'buyed_user_number', 'created_at', 'updated_at'], 'integer'],
  36. ];
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function attributeLabels()
  42. {
  43. return [
  44. 'id' => 'ID',
  45. 'mall_id' => 'Mall ID',
  46. 'coupon_id' => 'Coupon ID',
  47. 'good_id' => 'Good ID',
  48. 'booking_number' => 'Booking Number',
  49. 'payed_number' => 'Payed Number',
  50. 'booking_user_number' => 'Booking User Number',
  51. 'buyed_user_number' => 'Buyed User Number',
  52. 'created_at' => 'Created At',
  53. 'updated_at' => 'Updated At',
  54. ];
  55. }
  56. public static function setData($params)
  57. {
  58. try {
  59. if (empty($params['mall_id'])) return false;
  60. $model = self::findOne(['mall_id' => $params['mall_id'],'coupon_id' => $params['coupon_id'],'good_id' => $params['good_id']]);
  61. if (empty($model)) {
  62. $model = new self();
  63. $model->mall_id = $params['mall_id'];
  64. $model->coupon_id = $params['coupon_id'];
  65. $model->good_id = $params['good_id'];
  66. $model->loadDefaultValues();
  67. }
  68. if (!empty($params['payed_number'])){
  69. $model->payed_number += $params['payed_number'];
  70. if($model->payed_number<0) $model->payed_number = 0;
  71. }
  72. if (!empty($params['buyed_user_number'])){
  73. $model->buyed_user_number += $params['buyed_user_number'];
  74. if($model->buyed_user_number<0) $model->buyed_user_number = 0;
  75. }
  76. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  77. return $model;
  78. } catch (\Exception $e) {
  79. self::$static_error = $e->getMessage();
  80. return false;
  81. }
  82. }
  83. }