123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Facades\Servers\Redis\Redis;
- use App\Models\CustomScore;
- use App\Models\Score\Clockin;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\DB;
- /**
- * 打卡活动
- *
- */
- class CustomClockinRecord extends Model
- {
- use HasFactory;
- // 与模型关联的表名
- protected $table = 'custom_clockin_record';
- // 是否主动维护时间戳
- public $timestamps = false;
- // 定义时间戳字段名
- // const CREATED_AT = 'insert_time';
- // const UPDATED_AT = 'update_time';
- /**
- * 添加数据
- *
- */
- public function add($data)
- {
- // 时间
- $data['insert_time'] = time();
- $data['update_time'] = time();
- // 写入数据表
- $id = $this->query()->insertGetId($data);
- // 返回结果
- return $id;
- }
- /**
- * 添加数据
- *
- */
- public function edit($id,$data)
- {
- // 更新时间
- $data['update_time'] = time();
- // 写入数据表
- $result = $this->query()->where(['id'=>$id])->update($data);
- // 如果操作失败
- if( !$result ) return $result;
- // 更新缓存
- $this->getList(true);
- // 返回结果
- return $result;
- }
- /**
- * 获取列表
- * @param Bool $force 是否强制更新
- *
- */
- public function getList($force = false)
- {
- // 结果数据
- $list = $force ? [] : cache('admin:clockin:active:list');
- $where[] = ['status'=>0];
- $where[] = ['end_time','>=',time()];
- // 不存在数据
- if ( !$list ) {
- // 从数据库获取数据
- $data = $this->query()->where($where)->get(['id','name','banner_img','active_rule','status','start_time','end_time','tag_scope','city_ids']);
- // 是否有数据
- $data = $data ? $data->toArray() : [];
- // 循环处理数据
- $list = [];
- // 进行更新
- foreach ($data as $value) {
- // 重组数据
- $list[$value['id']] = $value;
- }
- // 存起来
- cache(['admin:clockin:active:list'=>$list]);
- }
- // 返回结果
- return $list;
- }
- /**
- * 今日是否已签到
- *
- * @param int $uid 用户ID
- *
- */
- public function isMarkClock($uid,$activeId){
- // 如果没有id
- if( !$uid ) return ['is_clockin'=>0,'finish_day'=>0];
- // 获取缓存
- //$record = cache('score:clockin:mark:uid:'.$uid,[]);
- // 返回结果
- // if( $record ) return $record;
- // 通过查询今日打卡积分记录
- $record = $this->query()->where([['active_id','=',$activeId],['custom_uid','=',$uid]])->orderBy('id','desc')->first(['id','custom_uid','clockin_day','clockin_time']);
- // 如果没有记录
- if ($record){
- if ($record['clockin_time'] >= Carbon::now()->startOfDay()->getTimestamp()){
- $record = ['is_clockin'=>1,'finish_day'=>$record['clockin_day']];
- }elseif($record['clockin_time'] >= Carbon::now()->addDays(-1)->startOfDay()->getTimestamp()){
- $record = ['is_clockin'=>0,'finish_day'=>$record['clockin_day']];
- }else{
- $record = ['is_clockin'=>0,'finish_day'=>0];
- }
- }else{
- $record = ['is_clockin'=>0,'finish_day'=>0];
- }
- // 如果没有打卡天数
- // 标记
- //$this->setMarkClock($uid,$record);
- // 返回结果
- return $record;
- }
- /**
- * 打卡操作
- *
- * @param int $uid 用户ID
- *
- */
- public function finish($uid,$activeId)
- {
- // 获取打卡任务列表,并获取最大天数
- $maxDay = $this->getMaxDay($activeId);
- // 如果是0,没有需要签到的任务
- if( !$maxDay ) return ['error'=>'无签到任务'];
- // 是否已签到
- $isMark = $this->isMarkClock($uid,$activeId);
- // 如果已经签到,不进行后续操作,返回已打卡
- if( $isMark['is_clockin'] ) return ['error'=>'今日已打卡'];
- // 如果当前已签到天数大于最大天数,从1开始计算签到天数
- $finishDay = $isMark['finish_day'] >= $maxDay ? 1 : $isMark['finish_day'] +1;
- // 通过今日天数查找对应的打卡奖励
- $list = $this->getOne($finishDay,$activeId);
- if (!$list) return ['error'=>'今日签到没有奖励'];
- $reward = $list['reward'] ?? 0;
- $coupon_id = $list['coupon_id'] ?? 0;
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- // 写入数据
- try {
- // 成功继续
- if ($reward){
- $result = (new CustomScore())->trade($uid,0,$reward,2,$finishDay);
- // 失败结束
- if( isset($result['error']) ) {
- // 回退数据
- DB::rollBack();
- // 下单失败提示
- return ['error'=>$result['error']];
- }
- }
- if ($coupon_id){
- $result = (new CustomCoupon())->giveCoupon($coupon_id,$uid);
- // 失败结束
- if( !$result ) {
- // 回退数据
- DB::rollBack();
- // 下单失败提示
- return ['error'=>'签到赠送优惠券失败'];
- }
- }
- //打卡签到记录
- $recordInfo = [
- 'custom_uid' => $uid,
- 'active_id' => $activeId,
- 'clockin_day' => $finishDay,
- 'clockin_time' => time(),
- 'score' => $reward,
- 'coupon_id' => $coupon_id,
- ];
- $re = (new CustomClockinRecord())->add($recordInfo);;
- // 失败结束
- if( !$re ) {
- // 回退数据
- DB::rollBack();
- // 下单失败提示
- return ['error'=>'打卡签到记录失败'];
- }
- // 提交数据
- DB::commit();
- // 打卡通过
- $isMark['is_clockin'] = 1;
- $isMark['finish_day'] = $finishDay;
- // 奖励返回
- $isMark['reward'] = $reward;
- $isMark['coupon'] = $coupon_id;
- // 返回打卡结果与奖励
- return $isMark;
- } catch (\Throwable $th) {
- // 回退数据
- DB::rollBack();
- // 下单失败提示
- return ['error'=>'打卡失败'];
- }
- }
- /**
- * 获取最大的打卡天数
- *
- */
- public function getMaxDay($activeId){
- // 查询数据
- $data = (new Clockin())->getActiveList($activeId);
- // 获取天数字段
- $days = array_column($data,'what_day');
- // 获取第一个
- $maxDay = $days ? max($days) : 0;
- // 返回结果
- return (int) $maxDay;
- }
- /**
- * 根据天数获取
- *
- */
- public function getOne($whatDay,$activeId=0,$field=''){
- // 获取列表数据
- $list = (new Clockin())->getActiveList($activeId);
- // 获取数据
- $one = isset($list[$whatDay]) ? $list[$whatDay] : [];
- // 返回值
- return empty($field) ? $one : ( isset($one[$field]) ? $one[$field] : null);
- }
- }
|