Voucher.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\api\model\user;
  3. use app\api\model\BaseModel;
  4. class Voucher extends BaseModel
  5. {
  6. // 表名
  7. protected $name = 'user_voucher';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'integer';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. // 追加属性
  15. protected $append = [
  16. 'status_text',
  17. 'expiretime_text'
  18. ];
  19. public function getStatusList()
  20. {
  21. return ['normal' => __('Normal'), 'use' => __('Use'), 'expire' => __('Expire')];
  22. }
  23. public function getStatusTextAttr($value, $data)
  24. {
  25. $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
  26. $list = $this->getStatusList();
  27. return isset($list[$value]) ? $list[$value] : '';
  28. }
  29. public function getExpiretimeTextAttr($value, $data)
  30. {
  31. $value = $value ? $value : (isset($data['expiretime']) ? $data['expiretime'] : '');
  32. return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
  33. }
  34. protected function setExpiretimeAttr($value)
  35. {
  36. return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
  37. }
  38. public function user()
  39. {
  40. return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  41. }
  42. /**
  43. * 过期
  44. * @param $user_id
  45. */
  46. public function passVoucher($user_id)
  47. {
  48. $this->where([
  49. "user_id" => $user_id,
  50. "status" => \E_VOUCHER_STATUS::Normal,
  51. ])->where("expiretime", "<=", time())
  52. ->update(["status" => \E_VOUCHER_STATUS::Expire]);
  53. }
  54. public function fetchVoucher($user_id, $status, $page = 1, $size = 10)
  55. {
  56. return $this->where([
  57. "user_id" => $user_id,
  58. ])
  59. ->where("status", "<>", \E_VOUCHER_STATUS::Use)
  60. ->where("status", "in", $status)
  61. ->page($page)
  62. ->paginate($size);
  63. }
  64. public function fetchVoucherCount($user_id)
  65. {
  66. return $this->where([
  67. "user_id" => $user_id,
  68. ])
  69. ->where("status", "<>", \E_VOUCHER_STATUS::Use)
  70. ->count();
  71. }
  72. }