ElectronCouponGiveLogs.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. use yii\db\Exception;
  7. /**
  8. * This is the model class for table "qimall_addons_electron_coupon_give_logs".
  9. *
  10. * @property int $id
  11. * @property int $mall_id 商城id
  12. * @property int $e_coupon_id 电子券id
  13. * @property int $user_e_coupon_id 赠送者会员电子券id
  14. * @property int $accept_user_e_coupon_id 接收者会员电子券id
  15. * @property int $user_id 接收者id
  16. * @property int $give_user_id 赠送者id
  17. * @property int $active_time 激活时间
  18. * @property int $use_time 使用时间
  19. * @property int $share_expiry 分享有效期,单位小时
  20. * @property int $created_at
  21. * @property int $updated_at
  22. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  23. */
  24. class ElectronCouponGiveLogs extends BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_electron_coupon_give_logs}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id', 'e_coupon_id', 'user_id', 'give_user_id', 'active_time', 'use_time'], 'required'],
  40. [['mall_id', 'e_coupon_id', 'user_e_coupon_id','accept_user_e_coupon_id', 'user_id', 'give_user_id', 'active_time', 'use_time','share_expiry', 'created_at', 'updated_at', 'status'], 'integer'],
  41. ];
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function attributeLabels()
  47. {
  48. return [
  49. 'id' => 'ID',
  50. 'mall_id' => 'Mall ID',
  51. 'e_coupon_id' => 'E Coupon ID',
  52. 'user_e_coupon_id' => 'User E Coupon ID',
  53. 'accept_user_e_coupon_id' => 'Accept User E Coupon ID',
  54. 'user_id' => 'User ID',
  55. 'give_user_id' => 'Give User ID',
  56. 'active_time' => 'Active Time',
  57. 'use_time' => 'Use Time',
  58. 'share_expiry' => 'Share Expiry',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. 'status' => 'Status',
  62. ];
  63. }
  64. public static function setData(array $params,int $amount)
  65. {
  66. try{
  67. $field = ['mall_id', 'e_coupon_id','user_e_coupon_id','accept_user_e_coupon_id','user_id','give_user_id','active_time','use_time','share_expiry','created_at','updated_at','status'];
  68. $insert = [];
  69. for ($i=0;$i<$amount;$i++){
  70. $insert[]=$params;
  71. }
  72. if(empty($insert)) throw new Exception('数据为空');
  73. return Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  74. }catch(\Exception $e){
  75. self::setStaticError($e->getMessage());
  76. return false;
  77. }
  78. }
  79. //激活后的操作
  80. public static function activeAfter($number,$user_id,$accept_user_e_coupon_id,$active_time){
  81. try {
  82. for ($i = 0; $i < $number; $i++) {
  83. $where = [
  84. 'status' => StatusEnum::ENABLED,
  85. 'user_id' => $user_id,
  86. 'accept_user_e_coupon_id' => $accept_user_e_coupon_id,
  87. ];
  88. $model = ElectronCouponGiveLogs::find()->where($where)->orderBy('created_at asc')->one();
  89. if (!empty($model)) {
  90. $model->status = StatusEnum::DISABLED;
  91. $model->active_time = $active_time;
  92. $res = $model->save();
  93. if ($res == false) throw new Exception($model->getErrorMessage());
  94. }
  95. }
  96. return true;
  97. }catch(\Exception $e){
  98. self::setStaticError($e->getMessage());
  99. return false;
  100. }
  101. }
  102. //使用后的操作
  103. public static function useAfter($user_id,$accept_user_e_coupon_id,$use_time){
  104. try {
  105. $where = [
  106. 'status' => [StatusEnum::ENABLED,StatusEnum::ENABLED],
  107. 'user_id' => $user_id,
  108. 'accept_user_e_coupon_id' => $accept_user_e_coupon_id,
  109. ];
  110. $model = ElectronCouponGiveLogs::find()->where($where)->andWhere(['<>','active_time',0])->orderBy('created_at asc')->one();
  111. if (!empty($model)) {
  112. $model->status = StatusEnum::DELETE;
  113. $model->use_time = $use_time;
  114. $res = $model->save();
  115. if ($res == false) throw new Exception($model->getErrorMessage());
  116. }
  117. return true;
  118. }catch(\Exception $e){
  119. self::setStaticError($e->getMessage());
  120. return false;
  121. }
  122. }
  123. }