Voucher.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. public function voucherinfo()
  43. {
  44. return $this->belongsTo(\app\admin\model\Voucher::class, 'voucher_id', 'id', [], 'LEFT')->setEagerlyType(0);
  45. }
  46. /**
  47. * 过期
  48. * @param $user_id
  49. */
  50. public function passVoucher($user_id)
  51. {
  52. $this->where([
  53. "user_id" => $user_id,
  54. "status" => \E_VOUCHER_STATUS::Normal,
  55. ])->where("expiretime", "<=", time())
  56. ->update(["status" => \E_VOUCHER_STATUS::Expire]);
  57. }
  58. public function fetchVoucher($user_id, $status, $page = 1, $size = 10)
  59. {
  60. return $this
  61. ->with("voucherinfo")
  62. ->where([
  63. "user_id" => $user_id,
  64. ])
  65. ->where("status", "<>", \E_VOUCHER_STATUS::Use)
  66. ->where("status", "in", $status)
  67. ->page($page)
  68. ->paginate($size);
  69. }
  70. public function fetchVoucherCount($user_id)
  71. {
  72. return $this->where([
  73. "user_id" => $user_id,
  74. ])
  75. ->where("status", "<>", \E_VOUCHER_STATUS::Use)
  76. ->count();
  77. }
  78. }