123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Master\Enum\RedisKeyEnum;
- use App\Model\Model;
- use App\Utils\RedisUtil;
- use Hyperf\DbConnection\Db;
- class UserCouponModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'user_coupon';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- public function searchUserIdAttribute($query, $value, array $params): mixed
- {
- if (!isset($value)) {
- return $query;
- }
- return $query->where('user_id', $value);
- }
- public function searchTypeAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('type', $value);
- }
- public function searchIsValidAttribute($query, $value, array $params): mixed
- {
- if ($value == 1) {
- return $query->where(function ($where) {
- $where->orWhere('valid_at', '<', time())->orWhere('is_use', '=', 1);
- });
- } else {
- return $query->where('is_use', 0)->where('valid_at', '>=', time());
- }
- }
- public function searchMinMoneyMinAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('min_money', '<=', $value);
- }
- public function searchIsUseAttribute($query, $value, array $params): mixed
- {
- return $query->where('is_use', $value);
- }
- public function dataCreateTimeAttribute($value, $params)
- {
- if (empty($value)) {
- return '---';
- }
- return date('Y-m-d H:i:s', $value);
- }
- /**
- * 获取订单优惠券
- * @param int $user_id
- * @param int $type
- * @param int $coupon_id
- * @return array
- */
- public function getOrderCoupon(int $user_id, int $type, int $coupon_id = 0, $total_amount = 0)
- {
- // 计算优惠券
- $coupons = (new UserCouponModel())->getList(
- params: [
- 'user_id' => $user_id,
- 'type' => $type,
- 'min_money_min' => $total_amount,
- 'is_valid' => 0,
- 'is_use' => 0
- ]);
- $min_money = '0.00';
- $money = '0.00';
- if (!empty($coupon_id)) {
- foreach ($coupons as $key => $val) {
- if ($val['id'] == $coupon_id) {
- $min_money = $val['min_money'];
- $money = $val['money'];
- }
- }
- }
- return [
- 'min_money' => $min_money,
- 'money' => $money,
- 'coupons' => $coupons,
- ];
- }
- }
|