| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 | <?php// +----------------------------------------------------------------------// | Niucloud-admin 企业快速开发的多应用管理平台// +----------------------------------------------------------------------// | 官方网址:https://www.niucloud.com// +----------------------------------------------------------------------// | niucloud团队 版权所有 开源版本可自由商用// +----------------------------------------------------------------------// | Author: Niucloud Team// +----------------------------------------------------------------------namespace addon\mall\app\model\coupon;use addon\mall\app\dict\coupon\CouponDict;use app\model\site\Site;use core\base\BaseModel;use think\contract\Jsonable;use think\db\exception\DataNotFoundException;use think\db\exception\DbException;use think\db\exception\ModelNotFoundException;use think\db\Query;use think\model\relation\HasMany;use think\model\relation\hasOne;/** * 优惠券模型 */class Coupon extends BaseModel{    /**     * 数据表主键     * @var string     */    protected $pk = 'id';    /**     * 模型名称     * @var string     */    protected $name = 'mall_coupon';    protected $type = [        'start_time' => 'timestamp',        'end_time' => 'timestamp',        'valid_start_time' => 'timestamp',        'valid_end_time' => 'timestamp',    ];    // 设置json类型字段    protected $json = [ 'join_site_ids' ];    // 设置JSON数据返回数组    protected $jsonAssoc = true;    /**     * 优惠券商品项     * @return HasMany     */    public function goods()    {        return $this->hasMany(CouponGoods::class, 'coupon_id', 'id');    }    /**     * 关联站点表     * @return hasOne     */    public function site()    {        return $this->hasOne(Site::class, 'site_id', 'site_id');    }    /**     * 关联商品项     * @param $value     * @param $data     * @return int|string     * @throws DataNotFoundException     * @throws DbException     * @throws ModelNotFoundException     *///    public function getGoodsDataAttr($value, $data)//    {//        $coupon_id = $data['id'] ?? 0;//        $list = [];//        if($coupon_id > 0) {//            $coupon_id = $data['id'];//            $list = (new CouponGoods())->where([['coupon_id', '=', $coupon_id]])->select();//        }//        return $list;//    }    public function getCouponPriceAttr($value, $data)    {        if (empty($data['price']))        {            return 0;        }        return rtrim(rtrim($data['price'], '0'), '.');    }    public function getCouponMinPriceAttr($value, $data)    {        if (empty($data['min_condition_money']))        {            return 0;        }        return rtrim(rtrim($data['min_condition_money'], '0'), '.');    }    /**     * 搜索器:标题     * @param $value     * @param $data     */    public function searchTitleAttr($query, $value, $data)    {        if ($value) {            $query->where('title', 'like', '%'.$value.'%');        }    }    /**     * 搜索器:状态     * @param $value     * @param $data     */    public function searchStatusAttr($query, $value, $data)    {        if ($value != '') {            $query->where('status', '=', $value);        }    }    /**     * 搜索器:优惠券类型     * @param $value     * @param $data     */    public function searchTypeAttr($query, $value, $data)    {        if ($value) {            $query->where('type', '=', $value);        }    }    /**     * 搜索器:领取方式     * @param $value     * @param $data     */    public function searchReceiveTypeAttr($query, $value, $data)    {        if ($value) {            $query->where('receive_type', '=', $value);        }    }    /**     * 搜索器:站点id     * @param $value     * @param $data     */    public function searchSiteIdAttr($query, $value, $data)    {        if ($value != '') {            if (is_array($value)) {                $query->where('site_id', 'in', $value);            } else {                $query->where('site_id', '=', $value);            }        }    }    /**     * 类型     * @param $value     * @param $data     * @return string     */    public function getTypeNameAttr($value, $data)    {        if (empty($data['type']))            return '';        return CouponDict::getType()[$data['type']] ?? '';    }    /**     * 领取类型     * @param $value     * @param $data     * @return string     */    public function getReceiveTypeNameAttr($value, $data)    {        if (empty($data['receive_type']))            return '';        return CouponDict::getReceiveType()[$data['receive_type']] ?? '';    }    /**     * 优惠券状态     * @param $value     * @param $data     * @return string     */    public function getStatusNameAttr($value, $data)    {        return CouponDict::getStatus()[$data['status']] ?? '';    }    /**     * 活动开始时间搜索器     * @param Query $query     * @param $value     * @param $data     */    public function searchStartTimeAttr(Query $query, $value, $data)    {        $start_time = empty($value[0]) ? 0 : strtotime($value[0]);        $end_time = empty($value[1]) ? 0 : strtotime($value[1]);        if ($start_time > 0 && $end_time > 0) {            $query->whereBetweenTime('start_time', $start_time, $end_time);        } elseif ($start_time > 0 && $end_time == 0) {            $query->where([['start_time', '>=', $start_time]]);        } elseif ($start_time == 0 && $end_time > 0) {            $query->where([['start_time', '<=', $end_time]]);        }    }    /**     * 活动结束时间搜索器     * @param Query $query     * @param $value     * @param $data     */    public function searchEndTimeAttr(Query $query, $value, $data)    {        $start_time = empty($value[0]) ? 0 : strtotime($value[0]);        $end_time = empty($value[1]) ? 0 : strtotime($value[1]);        if ($start_time > 0 && $end_time > 0) {            $query->whereBetweenTime('end_time', $start_time, $end_time);        } elseif ($start_time > 0 && $end_time == 0) {            $query->where([['end_time', '>=', $start_time]]);        } elseif ($start_time == 0 && $end_time > 0) {            $query->where([['end_time', '<=', $end_time]]);        }    }    /**     * 领取生效时间范围搜索器(查询生效时间范围内的优惠券)     * @param Query $query     * @param $value     * @param $data     */    public function searchReceiveTimeAttr(Query $query, $value, $data)    {        $now_time = time();        $time_where = [            [                [ 'start_time', '<=', $now_time ],                [ 'end_time', '>=', $now_time ],            ],            [                [ 'start_time', '=', 0 ],                [ 'end_time', '=', 0 ],            ]        ];        $query->where(function ($query) use ($time_where){            $query->whereOr($time_where);        });    }}
 |