123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php namespace App\Http\Controllers\Api\Redpacket;
- use App\Http\Controllers\Api\Api;
- use App\Models\Custom;
- use App\Models\CustomAmount;
- use Illuminate\Support\Facades\DB;
- use App\Models\Redpacket\ActiveRecord;
- use App\Models\Redpacket\ActiveReward;
- use App\Models\Redpacket\Active as Model;
- use App\Models\WeiBan\Tags as WeiBanTags;
- use App\Models\OrdersProduct;
- use App\Models\Product;
- /**
- * 红包活动
- *
- * @author 刘相欣
- *
- * */
- class Active extends Api{
- /**
- * 获取抽奖配置 /api/redpacket_active/get_detail
- *
- * */
- public function get_detail(Model $Model,Custom $Custom,ActiveReward $ActiveReward,ActiveRecord $ActiveRecord,WeiBanTags $WeiBanTags,OrdersProduct $OrdersProduct,Product $Product){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 接收参数
- $id = request('id',0);
- // 获取活动
- $data = $Model->getOne($id);
- // 如果存在的话
- if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>$data]);
- // 获取客户信息
- $custom = $Custom->getOne($uid);
- // 如果存在的话
- if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
- // 如果城市限制并且不在在城市范围内,不允许参加
- if ( $data['city_ids'] && !in_array($custom['city_id'],explode(',',$data['city_ids'])) ) return json_send(['code'=>'error','msg'=>'账号不在城市范围,请核对您的城市','data'=>['error'=>'不在标签范围内']]);
- // 判断用户ID是否在活动参与范围
- if( $data['custom_scope'] && !in_array($uid,explode(',',$data['custom_scope']) ) ) return json_send(['code'=>'error','msg'=>'账号不在指定客户范围','data'=>['error'=>'不在客户范围内']]);
- // 判断用户手机是否在活动参与范围
- if( $data['phone_scope'] && !in_array($custom['phone'],explode(',',$data['phone_scope']) ) ) return json_send(['code'=>'error','msg'=>'账号不在指定手机号范围','data'=>['error'=>'不在手机号范围内']]);
- // 如果限制新老用户参与
- if( $data['custom_type'] ) {
- // 如果客户注册时间不在活动期间
- if( $data['custom_start_time'] > $custom['insert_time'] || $data['custom_end_time'] < $custom['insert_time'] ) return json_send(['code'=>'error','msg'=>'账号不在活动范围','data'=>['error'=>'不在活动时间内注册']]);
- }
- // 是否需要获取用户标签
- $tags = $data['tag_scope'] || $data['tag_except'] ? $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']) : [];
- // 获取用户的标签
- $tags = $tags ? array_column($tags,'name') : [];
- // 判断是不是可以参与
- if( $data['tag_scope'] ) {
- // 解析数组
- $data['tag_scope'] = explode(',',$data['tag_scope']);
- // 计算交集
- $intersect = count(array_intersect($data['tag_scope'],$tags));
- // 判断标签是否必须同时满足
- if( $data['tag_scope_type'] == 1 ) {
- // 判断用户的标签是否全部满足于标签限制范围内
- if( $intersect != count($data['tag_scope']) ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
- }else{
- // 判断标签限制并且不在标签限制范围内,不允许参加
- if( !$intersect ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
- }
- }
- // 判断是不是可以参与
- if( $data['tag_except'] ) {
- // 解析数组
- $data['tag_except'] = explode(',',$data['tag_except']);
- // 计算交集
- $intersect = count(array_intersect($data['tag_except'],$tags));
- // 如果存在交集,在排除范围,即不可参与
- if( $intersect ) return json_send(['code'=>'error','msg'=>'账号不在范围','data'=>['error'=>'在标签排除范围内']]);
- }
- // 默认可以参加活动
- $data['allow_join'] = 1;
- // 获取商品
- $data['product_list'] = [];
- // 如果限制下单
- if( $data['is_order'] ) {
- // 查询结果
- $query = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$data['order_start_time']],['insert_time','<=',$data['order_end_time']]]);
- // 如果商品存在
- if( $data['product_scope'] )$query = $query->whereIn('product_id',explode(',',$data['product_scope']));
- // 获取时间段内下单数量
- $orderTotal = $query->groupBy('order_id')->count();
- // 如果没有下单数据
- if( !$orderTotal ) $data['allow_join'] = 0;
- // 获取产品列表
- if( $data['product_scope'] ) $data['product_list'] = $Product->getListByIds(explode(',',$data['product_scope']));
- }
- // 奖品
- $reward = $ActiveReward->getListByLottery($data['id']);
- // 活动暂无奖品
- if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖项','data'=>$data]);
- // 查询条件
- $map = [];
- // 判断周期
- if( !empty($data['freq']) ) {
- if( $data['freq'] == 1 ) $map = [['insert_time','>=',now()->startOfDay()->getTimestamp()],['insert_time','<=',now()->endOfDay()->getTimestamp()]];
- if( $data['freq'] == 2 ) $map = [['insert_time','>=',now()->startOfWeek()->getTimestamp()],['insert_time','<=',now()->endOfWeek()->getTimestamp()]];
- if( $data['freq'] == 3 ) $map = [['insert_time','>=',now()->startOfMonth()->getTimestamp()],['insert_time','<=',now()->endOfMonth()->getTimestamp()]];
- }
- // logo
- $data['logo'] = $data['logo'] ? path_compat($data['logo']) : '';
- // 查询用户已参与次数
- $data['join_num'] = (int)$ActiveRecord->query()->where([['custom_uid','=',$uid],['active_id','=',$id]])->where($map)->count();
- // 计算剩余次数
- $data['number'] = $data['allow_join'] ? ($data['lucky_num'] - $data['join_num']) : 0;
- // 最少为0,避免显示异常
- $data['number'] = $data['number'] < 0 ? 0 : $data['number'];
- // 时间处理
- $data['start_date'] = date('Y/m/d H:i',$data['start_time']);
- // 时间处理
- $data['end_date'] = date('Y/m/d H:i',$data['end_time']);
- // 通过活动ID,查询奖品
- $data['reward_list'] = [];
- // 奖品数据
- foreach ($reward as $value) {
- // 奖项
- $data['reward_list'][] = ['id'=>$value['id'],'money'=>$value['money']];
- }
- // 返回结果
- return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
- }
- /**
- * 抽奖 /api/redpacket_active/get_reward
- *
- * */
- public function get_reward(Model $Model,Custom $Custom,ActiveRecord $ActiveRecord,ActiveReward $ActiveReward,CustomAmount $CustomAmount,WeiBanTags $WeiBanTags,OrdersProduct $OrdersProduct){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 获取活动
- $activeId = request('active_id',0);
- // 如果存在的话
- if( !$activeId ) return json_send(['code'=>'error','msg'=>'请选择参与的活动','data'=>['error'=>"抽奖活动ID有误"]]);
- // 获取客户信息
- $custom = $Custom->getOne($uid);
- // 如果存在的话
- if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
- // 通过活动ID,查询奖品
- $data = $Model->getOne($activeId);
- // 如果存在的话
- if( !$data ) return json_send(['code'=>'error','msg'=>'活动不存在或未开始','data'=>$data]);
- // 活动时间判断
- if( $data['start_time'] > time() ) return json_send(['code'=>'error','msg'=>'活动暂未开始','data'=>$data]);
- // 活动时间判断
- if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动已结束','data'=>$data]);
- // 如果城市限制并且不在在城市范围内,不允许参加
- if ( $data['city_ids'] && !in_array($custom['city_id'],explode(',',$data['city_ids'])) ) return json_send(['code'=>'error','msg'=>'账号不在城市范围,请核对您的城市','data'=>['error'=>'不在标签范围内']]);
- // 判断用户ID是否在活动参与范围
- if( $data['custom_scope'] && !in_array($uid,explode(',',$data['custom_scope']) ) ) return json_send(['code'=>'error','msg'=>'账号不在指定客户范围','data'=>['error'=>'不在客户范围内']]);
- // 判断用户手机是否在活动参与范围
- if( $data['phone_scope'] && !in_array($custom['phone'],explode(',',$data['phone_scope']) ) ) return json_send(['code'=>'error','msg'=>'账号不在指定手机号范围','data'=>['error'=>'不在手机号范围内']]);
- // 如果限制新老用户参与
- if( $data['custom_type'] ) {
- // 如果客户注册时间不在活动期间
- if( $data['custom_start_time'] > $custom['insert_time'] || $data['custom_end_time'] < $custom['insert_time'] ) return json_send(['code'=>'error','msg'=>'账号不在活动范围','data'=>['error'=>'不在活动时间内注册']]);
- }
- // 如果限制下单
- if( $data['is_order'] ) {
- // 查询结果
- $query = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$data['order_start_time']],['insert_time','<=',$data['order_end_time']]]);
- // 如果商品存在
- if( $data['product_scope'] ) $query = $query->whereIn('product_id',explode(',',$data['product_scope']));
- // 获取时间段内下单数量
- $orderTotal = $query->groupBy('order_id')->count();
- // 如果没有下单数据
- if( !$orderTotal ) return json_send(['code'=>'error','msg'=>'不符合下单条件','data'=>['error'=>'不符合订单条件']]);
- }
- // 是否需要获取用户标签
- $tags = $data['tag_scope'] || $data['tag_except'] ? $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']) : [];
- // 获取用户的标签
- $tags = $tags ? array_column($tags,'name') : [];
- // 判断是不是可以参与
- if( $data['tag_scope'] ) {
- // 解析数组
- $data['tag_scope'] = explode(',',$data['tag_scope']);
- // 计算交集
- $intersect = count(array_intersect($data['tag_scope'],$tags));
- // 判断标签是否必须同时满足
- if( $data['tag_scope_type'] == 1 ) {
- // 判断用户的标签是否全部满足于标签限制范围内
- if( $intersect != count($data['tag_scope']) ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
- }else{
- // 判断标签限制并且不在标签限制范围内,不允许参加
- if( !$intersect ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
- }
- }
- // 判断是不是可以参与
- if( $data['tag_except'] ) {
- // 解析数组
- $data['tag_except'] = explode(',',$data['tag_except']);
- // 计算交集
- $intersect = count(array_intersect($data['tag_except'],$tags));
- // 如果存在交集,在排除范围,即不可参与
- if( $intersect ) return json_send(['code'=>'error','msg'=>'账号在不可参与范围','data'=>['error'=>'账号在标签排除范围内']]);
- }
- // 奖品
- $reward = $ActiveReward->getListByLottery($data['id']);
- // 活动暂无奖品
- if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
- // 查询条件
- $map = [['insert_time','>=',$data['start_time']],['insert_time','<=',$data['end_time']]];
- // 判断周期
- if( !empty($data['freq']) ) {
- if( $data['freq'] == 1 ) $map = [['insert_time','>=',now()->startOfDay()->getTimestamp()],['insert_time','<=',now()->endOfDay()->getTimestamp()]];
- if( $data['freq'] == 2 ) $map = [['insert_time','>=',now()->startOfWeek()->getTimestamp()],['insert_time','<=',now()->endOfWeek()->getTimestamp()]];
- if( $data['freq'] == 3 ) $map = [['insert_time','>=',now()->startOfMonth()->getTimestamp()],['insert_time','<=',now()->endOfMonth()->getTimestamp()]];
- }
- // 查询用户已参与次数
- $data['join_num'] = (int)$ActiveRecord->query()->where([['custom_uid','=',$uid],['active_id','=',$activeId]])->where($map)->count();
- // 计算剩余次数
- $data['join_num'] = $data['lucky_num'] - $data['join_num'];
- // 如果次数不够
- if( $data['join_num'] <= 0 ) return json_send(['code'=>'error','msg'=>'抽奖次数已用完','data'=>['error'=>'抽奖次数已用完']]);
- // 抽奖记录
- $record = ['custom_uid'=>$uid,'active_id'=>$activeId,'reward_id'=>0,'money'=>0,'status'=>8];
- // 开启事务
- DB::beginTransaction();
- try{
- // 限制中奖则获取中奖次数
- $rewarTotal = $data['max_reward'] ? $ActiveRecord->query()->where([['active_id','=',$data['id']],['custom_uid','=',$uid],['reward_id','>',0]])->where($map)->count() : 0;
- // 中奖上限以后不再中奖
- $rewardIndex = ($data['max_reward'] && $rewarTotal >= $data['max_reward']) ? 0 : $ActiveReward->getRewardResult($reward);
- // 如果中奖,下标不是0
- if( $rewardIndex ) {
- // 获取奖品
- $rewardResult = $reward[$rewardIndex];
- // 奖品记录ID
- if( !empty($rewardResult['id']) ){
- // 是否直接发放
- if( $data['is_arrive'] ) {
- // 发放红包
- $result = $CustomAmount->trade($uid,$activeId,$rewardResult['money'],8,1,'红包活动');
- // 发放失败,改为未中奖
- if( isset($result['error']) ) $rewardIndex = 0;
- // 发放成功,状态为已完成
- $record['status'] = 8;
- }else{
- // 发放状态为进行中
- $record['status'] = 1;
- }
- }
- }
- // 中奖记录奖品
- if( $rewardIndex ) {
- // 记录,默认状态为1,进行中
- $record['reward_id'] = $reward[$rewardIndex]['id'];
- $record['money'] = $reward[$rewardIndex]['money'];
- // 扣减次数
- $ActiveReward->edit($rewardResult['id'],['reward_total'=>DB::raw('reward_total+-1')]);
- }
- // 写入记录
- $result = $ActiveRecord->add($record);
- // 写入失败
- if( !$result ) {
- // 回退数据
- DB::rollBack();
- // 提示
- return json_send(['code'=>'error','msg'=>'抽奖记录失败,请重试']);
- }
- // 提交事务
- DB::commit();
- // 通过活动ID,查询奖品
- $rewardList = [];
- // 奖品数据
- foreach ($reward as $value) {
- $rewardList[] = ['id'=>$value['id'],'money'=>$value['money']];
- }
- // 默认可以参加活动
- $data['allow_join'] = 1;
- // 返回结果
- return json_send(['code'=>'success','msg'=>'抽奖成功','data'=>['reward_list'=>$rewardList,'reward_index'=>$rewardIndex,'join_num'=>$data['join_num']-1]]);
- // 异常处理
- } catch (\Throwable $th) {
- // 回退数据
- DB::rollBack();
- // 下单失败提示
- return json_send(['code'=>'error','msg'=>'抽奖失败,请重试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
- }
- }
-
- }
|