UserCouponModel.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Master\Enum\RedisKeyEnum;
  5. use App\Model\Model;
  6. use App\Utils\RedisUtil;
  7. use Hyperf\DbConnection\Db;
  8. class UserCouponModel extends Model
  9. {
  10. /**
  11. * The table associated with the model.
  12. *
  13. * @var ?string
  14. */
  15. protected ?string $table = 'user_coupon';
  16. protected ?string $dateFormat = 'U';
  17. public bool $timestamps = false;
  18. protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
  19. protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
  20. /**
  21. * 默认查询字段
  22. *
  23. * @var array|string[]
  24. */
  25. public array $select = [
  26. '*'
  27. ];
  28. public function searchUserIdAttribute($query, $value, array $params): mixed
  29. {
  30. if (!isset($value)) {
  31. return $query;
  32. }
  33. return $query->where('user_id', $value);
  34. }
  35. public function searchTypeAttribute($query, $value, array $params): mixed
  36. {
  37. if (empty($value)) {
  38. return $query;
  39. }
  40. return $query->where('type', $value);
  41. }
  42. public function searchIsValidAttribute($query, $value, array $params): mixed
  43. {
  44. if ($value == 1) {
  45. return $query->where(function ($where) {
  46. $where->orWhere('valid_at', '<', time())->orWhere('is_use', '=', 1);
  47. });
  48. } else {
  49. return $query->where('is_use', 0)->where('valid_at', '>=', time());
  50. }
  51. }
  52. public function searchMinMoneyMinAttribute($query, $value, array $params): mixed
  53. {
  54. if (empty($value)) {
  55. return $query;
  56. }
  57. return $query->where('min_money', '<=', $value);
  58. }
  59. public function searchIsUseAttribute($query, $value, array $params): mixed
  60. {
  61. return $query->where('is_use', $value);
  62. }
  63. public function dataCreateTimeAttribute($value, $params)
  64. {
  65. if (empty($value)) {
  66. return '---';
  67. }
  68. return date('Y-m-d H:i:s', $value);
  69. }
  70. /**
  71. * 获取订单优惠券
  72. * @param int $user_id
  73. * @param int $type
  74. * @param int $coupon_id
  75. * @return array
  76. */
  77. public function getOrderCoupon(int $user_id, int $type, int $coupon_id = 0, $total_amount = 0)
  78. {
  79. // 计算优惠券
  80. $coupons = (new UserCouponModel())->getList(
  81. params: [
  82. 'user_id' => $user_id,
  83. 'type' => $type,
  84. 'min_money_min' => $total_amount,
  85. 'is_valid' => 0,
  86. 'is_use' => 0
  87. ]);
  88. $min_money = '0.00';
  89. $money = '0.00';
  90. if (!empty($coupon_id)) {
  91. foreach ($coupons as $key => $val) {
  92. if ($val['id'] == $coupon_id) {
  93. $min_money = $val['min_money'];
  94. $money = $val['money'];
  95. }
  96. }
  97. }
  98. return [
  99. 'min_money' => $min_money,
  100. 'money' => $money,
  101. 'coupons' => $coupons,
  102. ];
  103. }
  104. }