UserCoupon.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use app\common\library\helper;
  14. /**
  15. * 用户优惠券模型
  16. * Class UserCoupon
  17. * @package app\common\model
  18. */
  19. class UserCoupon extends BaseModel
  20. {
  21. // 定义表名
  22. protected $name = 'user_coupon';
  23. // 定义主键
  24. protected $pk = 'user_coupon_id';
  25. /**
  26. * 追加字段
  27. * @var array
  28. */
  29. protected $append = ['state'];
  30. /**
  31. * 关联用户表
  32. * @return \think\model\relation\BelongsTo
  33. */
  34. public function user()
  35. {
  36. return $this->belongsTo('User');
  37. }
  38. /**
  39. * 一对多关联 优惠券除外商品表
  40. */
  41. public function userCouponGoodsExcept()
  42. {
  43. return $this->hasMany('UserCouponGoods', 'user_coupon_id')->where('is_except', UserCouponGoods::EXCEPT_YES);
  44. }
  45. /**
  46. * 优惠券状态
  47. * @param $value
  48. * @param $data
  49. * @return array
  50. */
  51. public function getStateAttr($value, $data)
  52. {
  53. if (isset($data['is_use'])&&$data['is_use']) {
  54. return ['text' => '已使用', 'value' => 0];
  55. }
  56. if (isset($data['expire_time'])&&$data['expire_time']<date('Y-m-d H:i:s')){
  57. return ['text' => '已过期', 'value' => 0];
  58. }
  59. return ['text' => '正常', 'value' => 1];
  60. }
  61. /**
  62. * 获取器:格式化折扣率
  63. * @param $value
  64. * @return mixed
  65. */
  66. // public function getDiscountAttr($value)
  67. // {
  68. // return $value / 10;
  69. // }
  70. /**
  71. * 有效期-开始时间
  72. * @param $value
  73. * @return string
  74. */
  75. // public function getStartTimeAttr($value)
  76. // {
  77. // return date('Y/m/d', $value);
  78. // }
  79. /**
  80. * 有效期-结束时间
  81. * @param $value
  82. * @return string
  83. */
  84. // public function getEndTimeAttr($value)
  85. // {
  86. // return date('Y/m/d', $value);
  87. // }
  88. /**
  89. * 获取器:适用范围配置
  90. * @param $value
  91. * @return mixed
  92. */
  93. public function getApplyRangeConfigAttr($value)
  94. {
  95. return $value ? helper::jsonDecode($value) : [];
  96. }
  97. /**
  98. * 修改器:格式化折扣率
  99. * @param $value
  100. * @return mixed
  101. */
  102. // public function setDiscountAttr($value)
  103. // {
  104. // return $value * 10;
  105. // }
  106. /**
  107. * 修改器:适用范围配置
  108. * @param $array
  109. * @return mixed
  110. */
  111. public function setApplyRangeConfigAttr($array)
  112. {
  113. return helper::jsonEncode($array);
  114. }
  115. /**
  116. * 优惠券详情
  117. * @param $couponId
  118. * @return null|static
  119. */
  120. public static function detail($couponId)
  121. {
  122. return static::get($couponId);
  123. }
  124. /**
  125. * 设置优惠券使用状态
  126. * @param int $couponId 用户的优惠券id
  127. * @param bool $isUse 是否已使用
  128. * @return false|int
  129. */
  130. public static function setIsUse(int $couponId, $isUse = true)
  131. {
  132. return static::updateBase(['is_use' => (int)$isUse,'use_time'=>time()], ['user_coupon_id' => $couponId]);
  133. }
  134. /**
  135. * 设置优惠券关联的福利
  136. * @param $couponId
  137. * @param int $useStatus
  138. * @throws \think\db\exception\DataNotFoundException
  139. * @throws \think\db\exception\DbException
  140. * @throws \think\db\exception\ModelNotFoundException
  141. */
  142. public static function setWelfareStatus($couponId,$useStatus=1){
  143. //会员专享券的用户福利领取记录要变更
  144. $userCoupon = self::where('user_coupon_id','=',$couponId)
  145. ->field('user_coupon_id,coupon_type,member_welfare_draw_id')->find();
  146. if ($userCoupon && ($userCoupon['coupon_type'] == Coupon::MEMBER_COUPON) && $userCoupon['member_welfare_draw_id'])
  147. {
  148. member\MemberWelfareDraw::where('id',$userCoupon['member_welfare_draw_id'])
  149. ->update(['use_status'=>$useStatus,'update_time'=>time()]);
  150. }
  151. return true;
  152. }
  153. }