AddonsCouponShareLog.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace addons\Coupon\common\models;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use common\enums\StatusEnum;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_coupon_share_log}}".
  8. *
  9. * @property int $id
  10. * @property int $mall_id
  11. * @property int $user_coupon_id
  12. * @property int $get_user_coupon_id
  13. * @property int $coupon_id
  14. * @property int $giving_user_id 转出会员id,值为 0 说明是平台赠送的优惠券
  15. * @property int $receive_user_id 受赠会员id,尚未被领取此字段值为0
  16. * @property int $giving_status 转赠状态,1:转赠中(已分享出去,未被领取),2:已被领取
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class AddonsCouponShareLog extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_coupon_share_log}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id','user_coupon_id','get_user_coupon_id', 'coupon_id', 'giving_user_id'], 'required'],
  36. [['mall_id', 'coupon_id', 'giving_user_id', 'receive_user_id', 'giving_status', 'created_at', 'updated_at'], 'integer'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'mall_id' => 'Mall ID',
  47. 'user_coupon_id' => 'user_coupon_id',
  48. 'get_user_coupon_id' => 'get_user_coupon_id',
  49. 'coupon_id' => 'Coupon ID',
  50. 'giving_user_id' => 'Giving User ID',
  51. 'receive_user_id' => 'Receive User ID',
  52. 'giving_status' => 'Giving Status',
  53. 'created_at' => 'Created At',
  54. 'updated_at' => 'Updated At',
  55. ];
  56. }
  57. public static function setData($params)
  58. {
  59. $transaction = \Yii::$app->db->beginTransaction();
  60. try {
  61. if (!empty($params['coupon_share_log_id'])) {
  62. // 领取
  63. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['coupon_share_log_id']]);
  64. if (empty($model)) throw new \Exception('服务不存在或已删除');
  65. $model->get_user_coupon_id = $params['get_user_coupon_id'];
  66. $model->receive_user_id = $params['receive_user_id'];
  67. $model->giving_status = AddonsCouponEnum::GIVING_STATUS2;
  68. $model->updated_at = time();
  69. $user_coupon = AddonsCouponUserRelate::findOne(['mall_id' => $params['mall_id'], 'id' => $model->user_coupon_id]);
  70. if(empty($user_coupon)) throw new \Exception('此优惠券暂不支持转赠');
  71. if($user_coupon->coupon_status==4) throw new \Exception('已失效');
  72. $user_coupon->is_giving = AddonsCouponEnum::GIVING;
  73. $user_coupon->giving_status = AddonsCouponEnum::GIVING_STATUS2;
  74. $get_user_coupon = AddonsCouponUserRelate::findOne(['mall_id' => $params['mall_id'], 'id' => $params['get_user_coupon_id']]);
  75. $get_user_coupon->give_time=$user_coupon->give_time+1;//转赠加次数
  76. $get_user_coupon->save();
  77. } else {
  78. // 转赠
  79. $model = new self();
  80. $model->mall_id = $params['mall_id'];
  81. $model->coupon_id = $params['coupon_id'];
  82. $model->user_coupon_id = $params['user_coupon_id'];
  83. $model->giving_user_id = $params['giving_user_id'];
  84. $model->get_user_coupon_id = 0;
  85. $model->receive_user_id = 0;
  86. $model->giving_status = 1;
  87. $model->created_at = time();
  88. $user_coupon = AddonsCouponUserRelate::findOne(['mall_id' => $params['mall_id'], 'id' => $params['user_coupon_id']]);
  89. if(empty($user_coupon)) throw new \Exception('会员优惠券不存在');
  90. if($user_coupon->coupon_status==4) throw new \Exception('已失效');
  91. $user_coupon->is_giving = AddonsCouponEnum::GIVING;
  92. $user_coupon->giving_status = AddonsCouponEnum::GIVING_STATUS1;
  93. $addonCoupon=AddonsCoupon::findOne(['mall_id' => $params['mall_id'], 'id' => $params['coupon_id']]);
  94. if($addonCoupon->give_time>0&&$user_coupon->give_time>=$addonCoupon->give_time)
  95. {
  96. throw new \Exception('已达到转赠次数上限');
  97. }
  98. }
  99. $res_user_coupon = $user_coupon->save();
  100. $res_share_log = $model->save();
  101. if ($res_share_log === false || $res_user_coupon === false) throw new \Exception('res_share_log => ' . var_export($model->getErrorMessage(), true) . ', res_user_coupon => ' . var_export($res_user_coupon, true));
  102. $transaction->commit();
  103. return $model;
  104. } catch (\Exception $e) {
  105. $transaction->rollback();
  106. self::$static_error = $e->getMessage();
  107. return false;
  108. }
  109. }
  110. }