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.

77 lines
2.2 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 think\model\concern\SoftDelete;
class DiscountCoupon extends Base
{
use SoftDelete;
protected $type = [
'publish_time' => 'timestamp'
];
/**
* 获取有效期展示文案
* @date 2022-10-17
*/
public function getValidityTextAttr($value, $data)
{
if ($data['validity_type'] == 1) {
return date('Y.m.d', $data['start_time']) . '~' . date('Y.m.d', $data['end_time']) . '可用';
} else if ($data['validity_type'] == 2) {
return '自领取' . $data['validity_days'] . '天可用';
} else if ($data['validity_type'] == 3) {
return '永久可用';
}
}
/**
* 获取优惠券列表(分页)
* @param array $where 查询条件
* @param int $page 第X页
* @param string $field 字段
* @param string $order 排序
* @date 2022-10-17
*/
public function listCouponWithPage($where, $page, $field = '*', $order = '')
{
// 排序
if (empty($order)) {
$order = 'id desc';
}
return $this->field($field)->where($where)->where(function ($query) {
// 没有过期的
$query->whereOr([['end_time', '=', 0]])->whereOr([['end_time', '>', time()]]);
})->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 listCoupon($where, $field = '*', $order = '')
{
// 排序
if (empty($order)) {
$order = 'id desc';
}
return $this->field($field)->where($where)->where(function ($query) {
$query->whereOr([['end_time', '=', 0]])->whereOr([['end_time', '>', time()]]);
})->order($order)->select();
}
}