You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\discount\model;
use app\base\model\user\User;
use think\model\concern\SoftDelete;
class DiscountCouponMy extends Base
{
use SoftDelete;
protected $type = [
'use_time' => 'timestamp',
'remark' => 'serialize'
];
/**
* 获取有效期展示文案
* @date 2022-10-17
*/
public function getValidityTextAttr($value, $data)
{
if ($data['is_forever'] == 1) {
return '永久可用';
} else {
return date('Y.m.d', $data['start_time']) . '~' . date('Y.m.d', $data['end_time']) . '可用';
}
}
/**
* 获取我的优惠券列表-带分页
* @param array $where 查询条件
* @param int $page 第X页
* @param string $field 字段
* @param string $order 排序
* @date 2022-10-17
*/
public function listMyCouponWithPage($where, $page, $field = '*', $order = '')
{
// 排序
if (empty($order)) {
$order = 'id desc';
}
return $this->field($field)->where($where)->order($order)
->paginate(['list_rows' => 50, 'page' => $page], false)->each(function ($item, $key) {
// 有效期显示文案
$item['validity_text'] = $item->validity_text;
// 价钱根据情况显示整数或1位小数
if ($item['price'] == round($item['price'])) {
$item['price'] = (string)round($item['price']);
}
return $item;
});
}
/**
* 获取我的优惠券列表
* @date 2022-10-17
*/
public function listMyCoupon($where, $field = '*', $order = '')
{
// 排序
if (empty($order)) {
$order = 'id desc';
}
return $this->field($field)->where($where)->order($order)
->select()->each(function ($item, $key) {
// 有效期显示文案
$item['validity_text'] = $item->validity_text;
// 价钱根据情况显示整数或1位小数
if ($item['price'] == round($item['price'])) {
$item['price'] = (string)round($item['price']);
}
return $item;
});
}
/**
* 获取我领取的优惠券以及张数
* @date 2022-10-17
*/
public function countMyCouponByCoupon()
{
$result = $this->where([
['user_id', '=', $this->userId]
])->group('coupon_id')->column('count(id)', 'coupon_id');
return $result;
}
/**
* 用户是否获得过首次注册优惠券
* @date 2022-10-17
*/
public function isUserReceiveFirstRegisterMyCoupon()
{
$number = $this->where([
['uid', '=', $this->mid],
['user_id', '=', $this->userId],
['coupon_type', '=', 2]
])->count();
$res = $number > 0 ? true : false;
return $res;
}
/**
* 获取优惠券是否有效
* @param int $coupon_my_id 我的优惠券ID
* @date 2022-05-13
*/
public function isAvailable($coupon_my_id)
{
$coupon_my = $this->getOneData([
['id', '=', $coupon_my_id]
], 'is_forever,end_time');
$is_available = ($coupon_my['is_forever'] == 0 && $coupon_my['end_time'] <= time()) ? false : true;
return $is_available;
}
/**
* 领取用户
*/
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id')->bind(['mobile_phone']);
}
}