123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355 |
- <?php
- namespace App\Servers;
- use App\Models\MemberCoupon;
- use App\Models\MemberSign;
- use App\Models\MemberSignRecord;
- use App\Models\Sign;
- use App\Models\SignSelect;
- use App\Servers\Coupon\CouponServer;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\DB;
- /**
- * 签到发放
- */
- class SignServer
- {
- private $type_arr = [
- 1 => '签到发放',
- ];
- /**
- * 发放类型
- * @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;
- }
- }
|