123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\model;
- use app\common\library\helper;
- /**
- * 用户优惠券模型
- * Class UserCoupon
- * @package app\common\model
- */
- class UserCoupon extends BaseModel
- {
- // 定义表名
- protected $name = 'user_coupon';
- // 定义主键
- protected $pk = 'user_coupon_id';
- /**
- * 追加字段
- * @var array
- */
- protected $append = ['state'];
- /**
- * 关联用户表
- * @return \think\model\relation\BelongsTo
- */
- public function user()
- {
- return $this->belongsTo('User');
- }
- /**
- * 一对多关联 优惠券除外商品表
- */
- public function userCouponGoodsExcept()
- {
- return $this->hasMany('UserCouponGoods', 'user_coupon_id')->where('is_except', UserCouponGoods::EXCEPT_YES);
- }
- /**
- * 优惠券状态
- * @param $value
- * @param $data
- * @return array
- */
- public function getStateAttr($value, $data)
- {
- if (isset($data['is_use'])&&$data['is_use']) {
- return ['text' => '已使用', 'value' => 0];
- }
- if (isset($data['expire_time'])&&$data['expire_time']<date('Y-m-d H:i:s')){
- return ['text' => '已过期', 'value' => 0];
- }
- return ['text' => '正常', 'value' => 1];
- }
- /**
- * 获取器:格式化折扣率
- * @param $value
- * @return mixed
- */
- // public function getDiscountAttr($value)
- // {
- // return $value / 10;
- // }
- /**
- * 有效期-开始时间
- * @param $value
- * @return string
- */
- // public function getStartTimeAttr($value)
- // {
- // return date('Y/m/d', $value);
- // }
- /**
- * 有效期-结束时间
- * @param $value
- * @return string
- */
- // public function getEndTimeAttr($value)
- // {
- // return date('Y/m/d', $value);
- // }
- /**
- * 获取器:适用范围配置
- * @param $value
- * @return mixed
- */
- public function getApplyRangeConfigAttr($value)
- {
- return $value ? helper::jsonDecode($value) : [];
- }
- /**
- * 修改器:格式化折扣率
- * @param $value
- * @return mixed
- */
- // public function setDiscountAttr($value)
- // {
- // return $value * 10;
- // }
- /**
- * 修改器:适用范围配置
- * @param $array
- * @return mixed
- */
- public function setApplyRangeConfigAttr($array)
- {
- return helper::jsonEncode($array);
- }
- /**
- * 优惠券详情
- * @param $couponId
- * @return null|static
- */
- public static function detail($couponId)
- {
- return static::get($couponId);
- }
- /**
- * 设置优惠券使用状态
- * @param int $couponId 用户的优惠券id
- * @param bool $isUse 是否已使用
- * @return false|int
- */
- public static function setIsUse(int $couponId, $isUse = true)
- {
- return static::updateBase(['is_use' => (int)$isUse,'use_time'=>time()], ['user_coupon_id' => $couponId]);
- }
- /**
- * 设置优惠券关联的福利
- * @param $couponId
- * @param int $useStatus
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function setWelfareStatus($couponId,$useStatus=1){
- //会员专享券的用户福利领取记录要变更
- $userCoupon = self::where('user_coupon_id','=',$couponId)
- ->field('user_coupon_id,coupon_type,member_welfare_draw_id')->find();
- if ($userCoupon && ($userCoupon['coupon_type'] == Coupon::MEMBER_COUPON) && $userCoupon['member_welfare_draw_id'])
- {
- member\MemberWelfareDraw::where('id',$userCoupon['member_welfare_draw_id'])
- ->update(['use_status'=>$useStatus,'update_time'=>time()]);
- }
- return true;
- }
- }
|