| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- namespace app\api\model\user;
- use app\api\model\BaseModel;
- class Voucher extends BaseModel
- {
- // 表名
- protected $name = 'user_voucher';
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = 'integer';
- // 定义时间戳字段名
- protected $createTime = 'createtime';
- protected $updateTime = 'updatetime';
- protected $deleteTime = false;
- // 追加属性
- protected $append = [
- 'status_text',
- 'expiretime_text'
- ];
- public function getStatusList()
- {
- return ['normal' => __('Normal'), 'use' => __('Use'), 'expire' => __('Expire')];
- }
- public function getStatusTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
- $list = $this->getStatusList();
- return isset($list[$value]) ? $list[$value] : '';
- }
- public function getExpiretimeTextAttr($value, $data)
- {
- $value = $value ? $value : (isset($data['expiretime']) ? $data['expiretime'] : '');
- return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
- }
- protected function setExpiretimeAttr($value)
- {
- return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
- }
- public function user()
- {
- return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- public function voucherinfo()
- {
- return $this->belongsTo(\app\admin\model\Voucher::class, 'voucher_id', 'id', [], 'LEFT')->setEagerlyType(0);
- }
- /**
- * 过期
- * @param $user_id
- */
- public function passVoucher($user_id)
- {
- $this->where([
- "user_id" => $user_id,
- "status" => \E_VOUCHER_STATUS::Normal,
- ])->where("expiretime", "<=", time())
- ->update(["status" => \E_VOUCHER_STATUS::Expire]);
- }
- public function fetchVoucher($user_id, $status, $page = 1, $size = 10)
- {
- return $this
- ->with("voucherinfo")
- ->where([
- "user_id" => $user_id,
- ])
- ->where("status", "<>", \E_VOUCHER_STATUS::Use)
- ->where("status", "in", $status)
- ->page($page)
- ->paginate($size);
- }
- public function fetchVoucherCount($user_id)
- {
- return $this->where([
- "user_id" => $user_id,
- ])
- ->where("status", "<>", \E_VOUCHER_STATUS::Use)
- ->count();
- }
- }
|