UserCoupon.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\common\model;
  3. use think\Hook;
  4. use app\common\library\helper;
  5. /**
  6. * 用户优惠券模型
  7. * Class UserCoupon
  8. * @package app\common\model
  9. */
  10. class UserCoupon extends BaseModel
  11. {
  12. protected $name = 'user_coupon';
  13. /**
  14. * 追加字段
  15. * @var array
  16. */
  17. protected $append = ['state'];
  18. /**
  19. * 订单模型初始化
  20. */
  21. public static function init()
  22. {
  23. parent::init();
  24. // 监听优惠券处理事件
  25. $static = new static;
  26. Hook::listen('UserCoupon', $static);
  27. }
  28. /**
  29. * 关联用户表
  30. * @return \think\model\relation\BelongsTo
  31. */
  32. public function user()
  33. {
  34. return $this->belongsTo('User');
  35. }
  36. /**
  37. * 优惠券状态
  38. * @param $value
  39. * @param $data
  40. * @return array
  41. */
  42. public function getStateAttr($value, $data)
  43. {
  44. if ($data['is_use']) {
  45. return ['text' => '已使用', 'value' => 0];
  46. }
  47. if ($data['is_expire']) {
  48. return ['text' => '已过期', 'value' => 0];
  49. }
  50. return ['text' => '', 'value' => 1];
  51. }
  52. /**
  53. * 优惠券颜色
  54. * @param $value
  55. * @return mixed
  56. */
  57. public function getColorAttr($value)
  58. {
  59. $status = [10 => 'blue', 20 => 'red', 30 => 'violet', 40 => 'yellow'];
  60. return ['text' => $status[$value], 'value' => $value];
  61. }
  62. /**
  63. * 优惠券类型
  64. * @param $value
  65. * @return mixed
  66. */
  67. public function getCouponTypeAttr($value)
  68. {
  69. $status = [10 => '满减券', 20 => '折扣券'];
  70. return ['text' => $status[$value], 'value' => $value];
  71. }
  72. /**
  73. * 折扣率
  74. * @param $value
  75. * @return mixed
  76. */
  77. public function getDiscountAttr($value)
  78. {
  79. return $value / 10;
  80. }
  81. /**
  82. * 有效期-开始时间
  83. * @param $value
  84. * @return mixed
  85. */
  86. public function getStartTimeAttr($value)
  87. {
  88. return ['text' => date('Y/m/d', $value), 'value' => $value];
  89. }
  90. /**
  91. * 有效期-结束时间
  92. * @param $value
  93. * @return mixed
  94. */
  95. public function getEndTimeAttr($value)
  96. {
  97. return ['text' => date('Y/m/d', $value), 'value' => $value];
  98. }
  99. /**
  100. * 获取器:适用范围配置
  101. * @param $value
  102. * @return mixed
  103. */
  104. public function getApplyRangeConfigAttr($value)
  105. {
  106. return $value ? helper::jsonDecode($value) : [];
  107. }
  108. /**
  109. * 修改器:适用范围配置
  110. * @param $array
  111. * @return mixed
  112. */
  113. public function setApplyRangeConfigAttr($array)
  114. {
  115. return helper::jsonEncode($array);
  116. }
  117. /**
  118. * 优惠券详情
  119. * @param $coupon_id
  120. * @return null|static
  121. * @throws \think\exception\DbException
  122. */
  123. public static function detail($coupon_id)
  124. {
  125. return static::get($coupon_id);
  126. }
  127. /**
  128. * 设置优惠券使用状态
  129. * @param int $couponId 用户的优惠券id
  130. * @param bool $isUse 是否已使用
  131. * @return false|int
  132. */
  133. public static function setIsUse($couponId, $isUse = true)
  134. {
  135. return (new static)->save(['is_use' => (int)$isUse], ['user_coupon_id' => $couponId]);
  136. }
  137. }