Coupon.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\common\model\plus\coupon;
  3. use app\common\library\helper;
  4. use app\common\model\BaseModel;
  5. /**
  6. * 优惠券模型
  7. */
  8. class Coupon extends BaseModel
  9. {
  10. protected $name = 'coupon';
  11. protected $pk = 'coupon_id';
  12. /**
  13. * 追加字段
  14. * @var array
  15. */
  16. protected $append = [
  17. 'state'
  18. ];
  19. /**
  20. * 关联供应商表
  21. */
  22. public function supplier()
  23. {
  24. return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id')
  25. ->field(['shop_supplier_id', 'name']);
  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 array
  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 array
  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 float|int
  70. */
  71. public function getDiscountAttr($value)
  72. {
  73. return $value / 10;
  74. }
  75. /**
  76. * 有效期-开始时间
  77. * @param $value
  78. * @return array
  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 array
  88. */
  89. public function getEndTimeAttr($value)
  90. {
  91. return ['text' => date('Y-m-d', $value), 'value' => $value];
  92. }
  93. /**
  94. * 折扣率
  95. */
  96. public function setDiscountAttr($value)
  97. {
  98. return helper::bcmul($value, 10, 0);
  99. }
  100. /**
  101. * 优惠券详情
  102. */
  103. public static function detail($coupon_id)
  104. {
  105. return (new static())->find($coupon_id);
  106. }
  107. /**
  108. * 优惠券详情
  109. */
  110. public static function detailWithSupplier($coupon_id)
  111. {
  112. return (new static())->with(['supplier'])->find($coupon_id);
  113. }
  114. }