StoreCouponGoodStatistics.php 3.2 KB

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