MemberCoupon.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace app\common\models;
  3. use app\common\traits\CreateOrderSnTrait;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. /**
  8. * Class MemberCoupon
  9. * @package app\common\models
  10. * @property Coupon belongsToCoupon
  11. * @property int used
  12. * @property int coupon_id
  13. * @property Carbon get_time
  14. * @property int id
  15. * @property int uid
  16. * @property Member member
  17. */
  18. class MemberCoupon extends BaseModel
  19. {
  20. use CreateOrderSnTrait;
  21. use SoftDeletes;
  22. public $table = 'yz_member_coupon';
  23. public $timestamps = false;
  24. public $dates = ['deleted_at'];
  25. protected $casts = ['get_time' => 'date'];
  26. protected $guarded = [];
  27. protected $appends = ['time_start', 'time_end', 'timestamp_end'];
  28. public $selected;
  29. protected $hidden = ['uniacid', 'get_type', 'send_uid', 'order_sn', 'back', 'back_time', 'deleted_at'];
  30. /**
  31. * 定义字段名
  32. * @return array
  33. */
  34. public function atributeNames()
  35. { //todo typo
  36. return [
  37. 'uniacid' => '公众号 ID',
  38. 'uid' => '用户 ID',
  39. 'coupon_id' => '优惠券 ID',
  40. 'get_type' => '获取优惠券的方式',
  41. 'used' => '是否已经使用',
  42. 'use_time' => '使用优惠券的时间',
  43. 'get_time' => '获取优惠券的时间',
  44. 'send_uid' => '手动发放优惠券的操作人员的 uid',
  45. 'order_sn' => '使用优惠券的订单号',
  46. 'back' => '返现',
  47. 'back_time' => '返现时间',
  48. ];
  49. }
  50. public function getTimeStartAttribute()
  51. {
  52. if ($this->belongsToCoupon->time_limit == false) {
  53. $result = $this->get_time;
  54. } else {
  55. $result = $this->belongsToCoupon->time_start;
  56. }
  57. return $result->toDateString();
  58. }
  59. public function getTimeEndAttribute()
  60. {
  61. if ($this->belongsToCoupon->time_limit == false) {
  62. if ($this->belongsToCoupon->time_days == false) {
  63. $result = '不限时间';
  64. } else {
  65. $result = $this->get_time->addDays($this->belongsToCoupon->time_days);
  66. }
  67. } else {
  68. $result = $this->belongsToCoupon->time_end;
  69. }
  70. if ($result instanceof Carbon) {
  71. $result = $result->toDateString();
  72. }
  73. return $result;
  74. }
  75. public function getTimestampEndAttribute()
  76. {
  77. if ($this->belongsToCoupon->time_limit == false) {
  78. if ($this->belongsToCoupon->time_days == false) {
  79. $result = '不限时间';
  80. } else {
  81. $result = Carbon::createFromTimestamp($this->getOriginal('get_time'))->addDays($this->belongsToCoupon->time_days);
  82. }
  83. } else {
  84. $result = Carbon::createFromTimestamp($this->belongsToCoupon->getOriginal('time_end'));
  85. }
  86. if ($result instanceof Carbon) {
  87. $result = $result->toDateTimeString();
  88. }
  89. return $result;
  90. }
  91. /*
  92. * 字段规则
  93. * @return array */
  94. public function rules()
  95. {
  96. return [
  97. 'uniacid' => 'required|integer',
  98. 'uid' => 'required|integer',
  99. 'coupon_id' => 'required|integer',
  100. 'get_type' => 'integer|between:0,2',
  101. 'used' => 'integer|between:0,1',
  102. 'use_time' => 'numeric',
  103. 'get_time' => 'required|numeric',
  104. 'send_uid' => 'integer',
  105. 'order_sn' => 'string',
  106. // 'back' => '',
  107. 'back_time' => 'numeric',
  108. ];
  109. }
  110. /**
  111. * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
  112. */
  113. public function member(){
  114. return $this->belongsTo(Member::class,'uid');
  115. }
  116. public function belongsToCoupon()
  117. {
  118. return $this->belongsTo('app\frontend\modules\coupon\models\OrderCoupon', 'coupon_id', 'id');
  119. }
  120. public function belongsToCommonCoupon()
  121. {
  122. return $this->belongsTo('\app\common\models\Coupon', 'coupon_id', 'id');
  123. }
  124. public function scopeCoupons(Builder $order_builder, $params)
  125. {
  126. $order_builder->with([
  127. 'belongsToCoupon' => function (Builder $query) {
  128. $query->where('status', 0);
  129. }
  130. ]
  131. )->where('used', 0);
  132. }
  133. public static function getMemberCoupon(Member $MemberModel, $param = [])
  134. {
  135. return static::with(['belongsToCoupon' => function (Builder $query) use ($param) {
  136. if (isset($param['coupon']['coupon_method'])) {
  137. //$query->where('coupon_method', $param['coupon']['coupon_method']);
  138. }
  139. return $query->where('status', 0);
  140. }])->where('member_id', $MemberModel->uid)->where('used', 0);
  141. }
  142. public static function getExpireCoupon()
  143. {
  144. $model = self::uniacid();
  145. $model->where('used', 0);
  146. return $model;
  147. }
  148. public function save(array $options = [])
  149. {
  150. // todo 紧急修复优惠券bug 保存和使用bug
  151. unset($this->valid);
  152. unset($this->checked);
  153. //dd(debug_backtrace());
  154. return parent::save($options); // TODO: Change the autogenerated stub
  155. }
  156. public static function search($search)
  157. {
  158. $model = self::uniacid();
  159. if ($search['member_id']) {
  160. $model->where('yz_member_coupon.uid', $search['member_id']);
  161. }
  162. if ($search['member']) {
  163. $model->join('mc_members', 'yz_member_coupon.uid', 'mc_members.uid')
  164. ->where(function ($query) use ($search) {
  165. $query->where('mc_members.mobile', 'like', '%'.$search['member'].'%')
  166. ->orWhere('mc_members.realname', 'like', '%'.$search['member'].'%')
  167. ->orWhere('mc_members.nickname', 'like', '%'.$search['member'].'%');
  168. });
  169. }
  170. if ($search['coupon_name']) {
  171. $model->join('yz_coupon', 'yz_member_coupon.coupon_id','yz_coupon.id')
  172. ->where('yz_coupon.name', 'like', '%'.$search['coupon_name'].'%');
  173. }
  174. return $model;
  175. }
  176. }