123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\model;
- use app\api\service\User as UserService;
- use app\common\exception\BaseException;
- use app\common\library\helper;
- use app\common\model\Coupon as CouponModel;
- use app\common\model\GoodsCategoryRel;
- use think\Db;
- /**
- * 优惠券模型
- * Class Coupon
- * @package app\api\model
- */
- class Coupon extends CouponModel
- {
- /**
- * 隐藏字段
- * @var array
- */
- protected $hidden = [
- 'receive_num',
- 'is_delete',
- 'create_time',
- 'update_time',
- ];
- /**
- * 获取优惠券列表
- * @param int|null $limit 获取的数量
- * @param bool $onlyReceive 只显示可领取
- * @param int $is_limit_hour
- * @return \think\Collection
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getLimitList(int $limit = null, bool $onlyReceive = false,int $is_limit_hour = 0)
- {
- // 查询构造器
- $query = $this->getNewQuery();
- // 只显示可领取(未过期,未发完)的优惠券
- if ($onlyReceive) {
- $query->where('IF ( `limit_total_type` = 10,`receive_num` <= `total_num`, `total_num`=0 )');
- }
- //限时券有公开券和领券中心类的券
- $query->where('public_lev','<',2);
- // 查询数量
- $limit > 0 && $query->limit($limit);
- // 优惠券列表
- $now = Date("Y-m-d H:i:s",time());
- $couponList = $query->where('receive_type','=',10)
- ->where('send_type','=',10)
- ->where('is_delete', '=', 0)
- ->where('audit_status','=',10)
- ->where('status','=',1)
- ->whereIn('is_show_position',[1,2])
- ->where('start_time','<',$now)
- ->where("end_time",">",$now)
- ->where('is_limit_hour','=',$is_limit_hour)
- ->where('activity_type','=',0)
- ->order(['start_hour'=>'asc','coupon_type'=>'asc','reduce_price'=>'desc', 'discount'=>'asc'])//'coupon_type'=>'asc','reduce_price'=>'desc', 'discount'=>'asc',
- ->select();
- // 获取用户已领取的优惠券
- return $this->setIsReceive($couponList);
- }
- /**
- * 获取优惠券列表
- * @param int|null $limit 获取的数量
- * @param bool $onlyReceive 只显示可领取
- * @param int $is_limit_hour
- * @param null $couponType
- * @param int $public_lev
- * @return \think\Collection
- * @throws BaseException
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList(int $limit = null, bool $onlyReceive = false,int $is_limit_hour = 0,$couponType = null,$public_lev=0,$source_id=0)
- {
- // 查询构造器
- $query = $this->getNewQuery();
- // 只显示可领取(未过期,未发完)的优惠券
- if ($onlyReceive) {
- $query->where('IF ( `limit_total_type` = 10,`receive_num` <= `total_num`, `total_num`=0 )');
- }
- //v1.4.0- 补充1.4.1添加开会员定向发放,不显示到领券中心
- if ($couponType){
- $query->where('coupon_type',$couponType);
- }else{
- $query->where('coupon_type','<',self::MEMBER_COUPON);
- }
- //1.4.41版本加入券是否公开类型
- if ($public_lev == 1 || $public_lev == 2){
- $query->whereIn('public_lev',[0,$public_lev]);
- }else{
- $query->where('public_lev',$public_lev);
- }
- if($source_id==1){
- $query->whereIn('is_show_position',[1,3]);
- }
- if($source_id==0){
- $query->whereIn('is_show_position',[1,2]);
- }
- // 查询数量
- $limit > 0 && $query->limit($limit);
- // 优惠券列表
- $now = Date("Y-m-d H:i:s",time());
- $couponList = $query->where('receive_type','=',10)
- ->where('send_type','=',10)
- ->where('is_delete', '=', 0)
- ->where('audit_status','=',10)
- ->where('status','=',1)
- ->where(function ($query) use ($now){//去除已失效的券
- $query->where('expire_type',10)->whereOr(function ($q) use ($now){
- $q->where([['expire_type','=',20],['expire_time','>',$now]]);
- });
- })
- ->where('start_time','<',$now)
- ->where("end_time",">",$now)
- ->where('is_limit_hour','=',$is_limit_hour)
- ->where('activity_type','=',0)
- ->field('* , IF(
- expire_type = 20,
- yoshop_coupon.expire_time,
- DATE_ADD( now(), INTERVAL yoshop_coupon.expire_day DAY )
- ) as tmp_expire_time ')
- ->order(['coupon_type'=>'asc','reduce_price'=>'desc','discount'=>'asc','tmp_expire_time' =>'asc', 'end_time'=>'asc'])
- ->select();
- // 获取用户已领取的优惠券
- return $this->setIsReceive($couponList);
- }
- /**
- * 获取优惠券落地页数据
- *
- * @param $couponId
- * @return mixed
- */
- public static function getDetail($couponId)
- {
- $now = Date("Y-m-d H:i:s",time());
- $data = self::where('coupon_id', $couponId)
- ->where('is_show_position','<>',4)
- // ->where('receive_type','=',10)
- // ->where('send_type','=',10)
- // ->where('is_delete', '=', 0)
- // ->where('audit_status','=',10)
- // ->where('status','=',1)
- // ->where(function ($query) use ($now){//去除已失效的券
- // $query->where('expire_type',10)->whereOr(function ($q) use ($now){
- // $q->where([['expire_type','=',20],['expire_time','>',$now]]);
- // });
- // })
- // ->where('start_time','<',$now)
- // ->where("end_time",">",$now)
- // ->where('activity_type','=',0)
- ->field('* ,IF(
- expire_type = 20,
- yoshop_coupon.expire_time,
- DATE_ADD( now(), INTERVAL yoshop_coupon.expire_day DAY )
- ) as tmp_expire_time ')
- ->find();
- if (empty($data)) {
- return [];
- }
- // 获取用户已领取的优惠券
- $userInfo = UserService::getCurrentLoginUser();
- $data['user_coupon_id'] = 0;
- $data['is_receive'] = false;
- if ($userInfo !== false) {
- $UserCouponModel = new UserCoupon;
- $userCouponIds = $UserCouponModel->getUserCouponIds($userInfo['user_id']);
- $userTodayCouponIds = $UserCouponModel->getTodayUserCouponIds($userInfo['user_id']);
- $data['user_coupon_id'] = 0;
- //如果是会员生日礼券,需要找到这张券
- if ($data['coupon_type'] == self::MEMBER_BIRTH_COUPON){
- $uc = $UserCouponModel->getUserCouponId($userInfo['user_id'],$data['coupon_id']);
- $data['user_coupon_id'] = $uc['user_coupon_id']??0;
- $data['is_receive'] = boolval($uc);
- }else{
- if($data['limit_receive_type']==10){
- $data['is_receive'] = in_array($data['coupon_id'], $userCouponIds);
- }else{
- $data['is_receive'] = in_array($data['coupon_id'], $userTodayCouponIds);
- }
- }
- }
- return $data;
- }
- public function getListTwo(int $limit = null, bool $onlyReceive = false)
- {
- // 只显示可领取(未过期,未发完)的优惠券
- if ($onlyReceive) {
- $this->where(function($query){
- $query->where([
- ['limit_total_type','=',10],
- ['receive_num', 'exp', Db::raw('<=total_num')]
- ])->whereOr([
- ['limit_total_type','=',20],
- ['total_num','=',0]
- ]);
- });
- }
- // 查询数量
- $limit > 0 && $this->limit($limit);
- // 优惠券列表
- $now = Date("Y-m-d H:i:s",time());
- $couponList = $this->where('receive_type','=',10)
- ->where('send_type','=',10)
- ->where('is_delete', '=', 0)
- ->where('audit_status','=',10)
- ->where('status','=',1)
- ->whereIn('is_show_position',[1,2])
- ->where('start_time','<',$now)
- ->where("end_time",">",$now)
- ->order(['reduce_price'=>'desc', 'create_time' => 'desc'])
- ->select();
- // 获取用户已领取的优惠券
- return $this->setIsReceive($couponList);
- }
- /**
- * 获取用户已领取的优惠券
- * @param $couponList
- * @return mixed
- * @throws BaseException
- */
- private function setIsReceive($couponList) {
- // 获取用户已领取的优惠券
- $userInfo = UserService::getCurrentLoginUser();
- if ($userInfo !== false) {
- $UserCouponModel = new UserCoupon;
- $userCouponIds = $UserCouponModel->getUserCouponIds($userInfo['user_id']);
- $userTodayCouponIds = $UserCouponModel->getTodayUserCouponIds($userInfo['user_id']);
- foreach ($couponList as $key => $item) {
- $couponList[$key]['user_coupon_id'] = 0;
- //如果是会员生日礼券,需要找到这张券
- if ($item['coupon_type'] == self::MEMBER_BIRTH_COUPON){
- $uc = $UserCouponModel->getUserCouponId($userInfo['user_id'],$item['coupon_id']);
- $couponList[$key]['user_coupon_id'] = $uc['user_coupon_id']??0;
- $couponList[$key]['is_receive'] = boolval($uc);
- }else{
- if($item['limit_receive_type']==10){
- $couponList[$key]['is_receive'] = in_array($item['coupon_id'], $userCouponIds);
- }else{
- $couponList[$key]['is_receive'] = in_array($item['coupon_id'], $userTodayCouponIds);
- }
- }
-
- }
- }
- return $couponList;
- }
- //查找优惠券中哪些是新用户自动领取的
- public function checkCouponIdBySendType(array $coupon_id_arr = [],int $send_type =20){
- $couponIdArray = $this->where('send_type','=',$send_type)
- ->where('coupon_id','in',$coupon_id_arr)
- ->column('coupon_id');
- return $couponIdArray;
- }
- // /**
- // * 优惠券状态 (是否可领取)
- // * @param $value
- // * @param $data
- // * @return array
- // */
- public function getStateAttr($value, $data)
- {
- if (isset($data['limit_total_type'])&&$data['limit_total_type']==10&&isset($data['total_num'])&&$data['total_num'] > -1 && $data['receive_num'] >= $data['total_num']) {
- return ['text' => '领完啦', 'value' => -1];
- }
- if (isset($data['is_receive']) && $data['is_receive']) {
- //看下是否存在
- $userInfo = UserService::getCurrentLoginUser();
- if($userInfo!=false){
- $userCouponModel = new UserCoupon;
- if($data['limit_receive_type']==10){ //限制总数
- $userCouponIds = $userCouponModel->getUserCouponIds($userInfo['user_id']);
- }
- if($data['limit_receive_type']==20){ //限制每天总数
- $userCouponIds = $userCouponModel->getTodayUserCouponIds($userInfo['user_id']);
- }
- $ac = array_count_values($userCouponIds);
- $coupon_id = $data['coupon_id'];
- if(isset($ac[$coupon_id])){
- if($data['limit_receive_cnt']>$ac[$coupon_id]){
- if($data['is_limit_hour']==0){
- return ['text' => '正常', 'value' => 1];
- }
- if($data['is_limit_hour']==1){
- $now = Date("Y-m-d H:i:s",time());
- $today_start = Date("Y-m-d",time())." ".$data['start_hour'].":00:00";
- $today_end = Date("Y-m-d",time())." ".$data['end_hour'].":00:00";
- if($now > $today_start&&$now < $today_end){
- return ['text'=>'正常','value'=>1];
- }
- if($today_start>$now){
- return ['text'=>'即将开始','value'=>-2];
- }
- }
- }
- return ['text' => '已领取', 'value' => 0];
- }
- }else{
- return ['text' => '已领取', 'value' => 0];
- }
- }
- if (!isset($data['is_limit_hour'])){
- return ['text' => '领完啦', 'value' => -1];
- }
- if(isset($data['is_limit_hour']) && $data['is_limit_hour']==0){
- return ['text' => '正常', 'value' => 1];
- }
- if(isset($data['is_limit_hour']) && $data['is_limit_hour'] == 1){
- $now = Date("Y-m-d H:i:s",time());
- $today_start = Date("Y-m-d",time())." ".$data['start_hour'].":00:00";
- $today_end = Date("Y-m-d",time())." ".$data['end_hour'].":00:00";
- if($now > $today_start&&$now < $today_end){
- return ['text'=>'正常','value'=>1];
- }
- if($today_start > $now){
- return ['text'=>'即将开始','value'=>-2];
- }
- }
- }
- /**
- * 验证优惠券是否可领取
- * @return bool
- */
- public function checkReceive()
- {
- //10 限制优惠券总数量 20 不限制总数量
- if ($this['limit_total_type']==10&&$this['total_num'] > -1 && $this['receive_num'] >= $this['total_num']) {
- $this->error = '优惠券已发完';
- return false;
- }
- return true;
- }
- /**
- * 累计已领取数量 同时减可用数量
- * @return mixed
- */
- public function setIncReceiveNum()
- {
- $this->setInc($this['coupon_id'], 'receive_num', 1);
- if($this['limit_total_type']==10){
- $this->setDec($this['coupon_id'], 'avaiable_num', 1);
- }
- }
- public static function showDetail($couponId){
- return Coupon::where('coupon_id',$couponId)
- ->field('coupon_id,name,reduce_price,min_price,status,audit_status,is_limit_hour,expire_time')->find();
- }
- //获取开通会员礼包优惠券列表
- public function getMemList(){
- $filter[] = ['is_delete','=',0];
- $filter[] = ['end_time','>',date('Y-m-d H:i:s')];
- $filter[] = ['start_time','<',date('Y-m-d H:i:s')];
- $filter[] = ['status','=',1];
- $filter[] = ['audit_status','=',10];
- $filter[] = ['receive_type','=',20];
- $filter[] = ['expire_type','=',10];
- $filter[] = ['coupon_type','=',Coupon::MEMBER_COUPON];
- $list = Coupon::where($filter)->select();
- return $list;
- }
- }
|