'签到发放', ]; /** * 发放类型 * @var string[] */ private $grantType_arr = [ 1 => '仅发放一次', 2 => '每次触发即发放', 3 => '自然月循环发放', ]; /** * 状态 * @var string[] */ private $status_arr = [ 1 => '进行中', 2 => '未开始', 3 => '已结束', ]; static private $server = ''; private function __construct() { } /** * 创建对象 * @return SignServer */ static function creatServer() { if (empty(self::$server)) { self::$server = new SignServer(); } return self::$server; } /** * 获取优惠券类型 * @param $type * @return array|string */ public function getTypeStr($type) { return empty($this->type_arr[$type]) ? '' : $this->type_arr[$type]; } /** * 获取优惠券发放类型 * @param $type * @return array|string */ public function getGrantTypeStr($type) { return empty($this->grantType_arr[$type]) ? '' : $this->grantType_arr[$type]; } /** * 获取状态 * @param $status * @return string */ public function getStatusArr($status) { return empty($this->status_arr[$status]) ? '' : $this->status_arr[$status]; } /** * 获取签到发放规则 * @param $info * @return string */ public function getRuleStr($info) { if($info->{'rule'} == 1){ $info->{'rule_str'} = '累计签到'.$info->{'day'}.'天可领'.$info->{'num'}.'张'; }elseif ($info->{'rule'} == 2){ $info->{'rule_str'} = '连续签到'.$info->{'day'}.'天可领'.$info->{'num'}.'张'; }elseif ($info->{'rule'} == 3){ $info->{'rule_str'} = date('Y.n.d',strtotime($info->{'date_time'})).'可领'.$info->{'num'}.'张'; }elseif ($info->{'rule'} == 4){ $week = ['日','一','二','三','四','五','六']; $info->{'rule_str'} = '周'.$week[$info->{'week'}].'签到可领'.$info->{'num'}.'张'; } return $info; } function saveInfo($signInfo,$couponList){ DB::beginTransaction(); if (empty($signInfo['id'])) { $signInfo = Sign::create($signInfo); if (empty($signInfo->{'id'})) { DB::rollBack(); return false; } } else { $res = Sign::where('id', $signInfo['id'])->first()->update($signInfo); if (empty($res)) { DB::rollBack(); return false; } } //记录优惠券信息 foreach ($couponList as $coupon) { $couponData = [ 'sign_id' => $signInfo['id'], 'coupon_id' => $coupon['id'], 'num' => 1, ]; $selectInfo = SignSelect::where('sign_id', $couponData['sign_id'])->where('coupon_id', $coupon['id'])->first(); if (empty($selectInfo)) { $couponData['receive_num'] = 0; $couponData['use_num'] = 0; SignSelect::create($couponData); } else { $selectInfo->update($couponData); } } DB::commit(); return true; } /** * 获取签到活动详情 * @param $id * @return mixed */ function getInfo($id) { //查询数据条件 $where = [['id', $id], ['is_del', 0]]; $info = Sign::where($where) ->select(['id', 'name', 'type', 'start_time', 'end_time', 'rule', 'status', 'day', 'num', 'date_time', 'week', 'grant_type']) ->first(); $info['type_str'] = $this->getTypeStr($info->{'type'}); $info['grant_type_str'] = $this->getGrantTypeStr($info->{'grant_type'}); $info = $this->getRuleStr($info); return $info; } /** * 获取领取、使用数量 * @param $sign_id * @return mixed */ function NumStatistics($sign_id) { $where = [['sign_id', '=', $sign_id]]; $list = SignSelect::where($where)->select('receive_num', 'use_num', 'coupon_id')->get(); $data = [ 'receive_num' => 0, 'use_num' => 0, ]; $data['coupon_ids'] = []; foreach ($list as $value) { $data['receive_num'] += $value['receive_num']; $data['use_num'] += $value['use_num']; $data['coupon_ids'][] = $value['coupon_id']; } return $data; } /** * 处理签到 * @param $m_id * @param array $signInfo * @return array */ function MemberSign($m_id,$signInfo=[]){ $time = date('Y-m-d'); $start_month = Carbon::now()->startOfMonth()->timestamp; DB::beginTransaction(); if($signInfo){ $last_time = date('Y-m-d',strtotime($signInfo['last_time'])); //获取前一天日期;比对是否是连续签到 $yesterday = date('Y-m-d',strtotime("-1 day")); if($last_time == $yesterday){ $series_count = $signInfo['series_count'] + 1; }else{ $series_count = 1; } if($time == date('Y-m-d',$start_month)){//破月重置累计.连续签到 $series_count = 1; $sign_count = 1; }else{ //累计签到天数加1 $sign_count = $signInfo['sign_count'] + 1; } $info = [ 'm_id' => $m_id, 'last_time' => $time, 'sign_count' => $sign_count, 'series_count' => $series_count, ]; //更新数据 $res = $signInfo::where('m_id',$m_id)->update($info); }else{ $sign_count = 1; $series_count = 1; $info = [ 'm_id' => $m_id, 'last_time' => $time, 'sign_count' => $sign_count, 'series_count' => $series_count, ]; $res = MemberSign::create($info); } if($res){ //存入详细记录 $record = MemberSignRecord::create([ 'm_id' => $m_id, 'sign_time' => $time, ]); $coupon = []; //获取可领取优惠卷 $date = date('Y-m-d'); $week = date('w') == 0 ? 7 : date('w'); //获取今日可领优惠卷 $list = $this->getCouponList($m_id,$sign_count,$series_count,$date,$week); if($list){ //领取优惠卷 foreach ($list as $value){ CouponServer::creatServer()->handleReceive($m_id,$value); } $count = count($list); $coupon['num'] = $count; $coupon['full_type'] = $list[0]['full_type']; $coupon['full_money'] = $list[0]['full_money']; $coupon['money'] = $list[0]['money']; } $info['is_sign'] = 1; $data['info'] = $info; $data['coupon'] = $coupon; if($record){ DB::commit(); return ['code'=>1,'data'=>$data]; }else{ DB::rollBack(); return ['code'=>0,'data'=>[]]; } }else{ DB::rollBack(); return ['code'=>0,'data'=>[]]; } } /** * 获取可领取的优惠卷 * @param $m_id * @param $sign_count * @param $series_count * @param $date * @param $week * @return array */ function getCouponList($m_id,$sign_count,$series_count,$date,$week){ $time = date('Y-m-d H:i:s'); //获取开启活动的优惠卷 $list = Sign::from('signs as g') ->leftJoin('sign_selects as s', 'g.id', '=', 's.sign_id') ->leftJoin('coupons as c', 'c.id', '=', 's.coupon_id') ->where('g.on_off',1) ->where('g.status',1) ->where('g.end_time','>=',$time) ->where('c.status','<>',3) ->orderBy('c.money','desc') ->select(['s.id', 's.num', 's.sign_id', 'c.name', 'c.type', 'c.desc', 'c.money', 'c.full_money', 'c.time_type', 'c.start_time', 'c.end_time', 'c.valid_day', 'c.wait_day', 'c.is_share', 'c.no_range', 'c.full_type', 'c.range_rule', 's.coupon_id', 'g.grant_type', 'g.rule', 'g.day', 'g.date_time', 'g.week']) ->get(); $data = []; foreach ($list as $value){ $res = false; if($value['rule'] == 3 && $date == date('Y-m-d',strtotime($value['date_time']))){//指定日期发放 $res = $this->checkCanCoupon($m_id,$value); }elseif ($value['rule'] == 4 && $week == $value['week']){//指定周几发放 $res = $this->checkCanCoupon($m_id,$value); }elseif ($value['rule'] == 2 && $series_count == $value['day']){//连续签到 $res = $this->checkCanCoupon($m_id,$value); }elseif ($value['rule'] == 1 && $sign_count == $value['day']){//累计签到 $res = $this->checkCanCoupon($m_id,$value); } if($res){ $data[] = $value; } } return $data; } /** * 检查可领取的优惠卷是否符合发放标准 * @param $m_id * @param $value * @return bool */ function checkCanCoupon($m_id,$value){ if($value['grant_type'] == 1){//仅触发一次就检查该用户领取过没有 $where = [['m_id',$m_id], ['coupon_id',$value['coupon_id']], ['select_id',$value['id']], ['sign_id',$value['sign_id']]]; $find = MemberCoupon::where($where)->select(['id'])->first(); if(!$find){//没有领取过就可领 return true; } }elseif ($value['grant_type'] == 2){//每次触发就领取 return true; }elseif ($value['grant_type'] == 3){//每月触发一次 //获取每月开头时间和结束时间 $start = Carbon::now()->startOfMonth(); $end = Carbon::now()->endOfMonth(); $where = [['m_id',$m_id], ['coupon_id',$value['coupon_id']], ['select_id',$value['id']], ['sign_id',$value['sign_id']]]; $find = MemberCoupon::where($where)->where('created_at','>=',$start)->where('created_at','<=',$end)->select(['id'])->first(); if(!$find){//没有领取过就可领 return true; } } return false; } }