Coupon.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\helper;
  4. /**
  5. * 优惠券模型
  6. * Class Coupon
  7. * @package app\common\model
  8. */
  9. class Coupon extends BaseModel
  10. {
  11. protected $name = 'coupon';
  12. /**
  13. * 追加字段
  14. * @var array
  15. */
  16. protected $append = [
  17. 'state'
  18. ];
  19. /**
  20. * 默认数据: 适用范围配置
  21. * @var array[]
  22. */
  23. protected $applyRangeConfig = [
  24. 'applyGoodsIds' => [],
  25. 'excludedGoodsIds' => []
  26. ];
  27. /**
  28. * 优惠券状态 (是否可领取)
  29. * @param $value
  30. * @param $data
  31. * @return array
  32. */
  33. public function getStateAttr($value, $data)
  34. {
  35. if (isset($data['is_receive']) && $data['is_receive']) {
  36. return ['text' => '已领取', 'value' => 0];
  37. }
  38. if ($data['total_num'] > -1 && $data['receive_num'] >= $data['total_num']) {
  39. return ['text' => '已抢光', 'value' => 0];
  40. }
  41. if ($data['expire_type'] == 20 && ($data['end_time'] + 86400) < time()) {
  42. return ['text' => '已过期', 'value' => 0];
  43. }
  44. return ['text' => '', 'value' => 1];
  45. }
  46. /**
  47. * 优惠券颜色
  48. * @param $value
  49. * @return mixed
  50. */
  51. public function getColorAttr($value)
  52. {
  53. $status = [10 => 'blue', 20 => 'red', 30 => 'violet', 40 => 'yellow'];
  54. return ['text' => $status[$value], 'value' => $value];
  55. }
  56. /**
  57. * 优惠券类型
  58. * @param $value
  59. * @return mixed
  60. */
  61. public function getCouponTypeAttr($value)
  62. {
  63. $status = [10 => '满减券', 20 => '折扣券'];
  64. return ['text' => $status[$value], 'value' => $value];
  65. }
  66. /**
  67. * 折扣率
  68. * @param $value
  69. * @return mixed
  70. */
  71. public function getDiscountAttr($value)
  72. {
  73. return $value / 10;
  74. }
  75. /**
  76. * 有效期-开始时间
  77. * @param $value
  78. * @return mixed
  79. */
  80. public function getStartTimeAttr($value)
  81. {
  82. return ['text' => date('Y/m/d', $value), 'value' => $value];
  83. }
  84. /**
  85. * 有效期-结束时间
  86. * @param $value
  87. * @return mixed
  88. */
  89. public function getEndTimeAttr($value)
  90. {
  91. return ['text' => date('Y/m/d', $value), 'value' => $value];
  92. }
  93. /**
  94. * 获取器:适用范围配置
  95. * @param $value
  96. * @return mixed
  97. */
  98. public function getApplyRangeConfigAttr($value)
  99. {
  100. $array = $value ? helper::jsonDecode($value) : [];
  101. return array_merge($this->applyRangeConfig, $array);
  102. }
  103. /**
  104. * 修改器:适用范围配置
  105. * @param $array
  106. * @return mixed
  107. */
  108. public function setApplyRangeConfigAttr($array)
  109. {
  110. return helper::jsonEncode(array_merge($this->applyRangeConfig, $array));
  111. }
  112. /**
  113. * 修改器:折扣率
  114. * @param $value
  115. * @return mixed
  116. */
  117. public function setDiscountAttr($value)
  118. {
  119. return helper::bcmul($value, 10, 0);
  120. }
  121. /**
  122. * 优惠券详情
  123. * @param $coupon_id
  124. * @return null|static
  125. * @throws \think\exception\DbException
  126. */
  127. public static function detail($coupon_id)
  128. {
  129. return self::get($coupon_id);
  130. }
  131. }