123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php namespace App\Http\Controllers\Api\Lottery;
- use App\Http\Controllers\Api\Api;
- use App\Models\Lottery\Order as Model;
- use App\Models\Lottery\OrderUsed as OrderUsed;
- use App\Models\Custom;
- use App\Models\CustomCoupon;
- use App\Models\CustomScore;
- use App\Models\Lottery\OrderProduct as LotteryOrderProduct;
- use App\Models\Lottery\OrderRecord;
- use App\Models\Lottery\OrderReward as OrderReward;
- use App\Models\OrdersProduct;
- use App\Models\WeiBan\Tags as WeiBanTags;
- use Illuminate\Support\Facades\DB;
- use App\Models\CustomAmount;
- /**
- * 积分抽奖
- *
- * @author 刘相欣
- *
- * */
- class Order extends Api{
- /**
- * 获取抽奖配置 /api/lottery_order/get_detail
- *
- *
- * */
- public function get_detail(Model $Model,Custom $Custom,OrderReward $OrderReward,LotteryOrderProduct $LotteryOrderProduct,WeiBanTags $WeiBanTags,OrdersProduct $OrdersProduct,OrderUsed $OrderUsed){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 获取客户信息
- $custom = $Custom->getOne($uid);
- // 如果存在的话
- if( !$custom ) return json_send(['code'=>'no_login','msg'=>'请登录','data'=>['error'=>'无对应客户']]);
- // 接收参数
- $id = request('id',0);
- // 获取客户城市的数据
- $data = $Model->getOne($id);
- // 如果存在的话
- if( !$data ) return json_send(['code'=>'error','msg'=>'暂无活动','data'=>$data]);
- // 默认可以参加活动
- $data['allow_join'] = 1;
- // 判断是不是可以参与
- if( $data['tag_scope'] ) {
- // 解析数组
- $data['tag_scope'] = explode(',',$data['tag_scope']);
- // 查询用户标签
- $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
- // 标签范围限定时,默认不能参与
- $data['allow_join'] = 0;
- // 判断标签是不是存在
- foreach ($tags as $value) {
- // 标签范围内,允许参加
- if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
- }
- // 如果不能参与
- if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在标签范围','data'=>['error'=>'不在标签范围内']]);
- }
- // 判断是不是可以参与
- if( $data['city_ids'] ) {
- // 解析数组
- $data['city_ids'] = explode(',',$data['city_ids']);
- // 如果不在城市范围
- if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
- // 如果不能参与
- if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
- }
- // 奖品
- $reward = $OrderReward->getListByLottery($data['id']);
- // 活动暂无奖品
- if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
- // logo
- $data['logo'] = $data['logo'] ? path_compat($data['logo']) : '';
- // 通过活动ID,查询奖品
- $data['reward_list'] = [];
- // 奖品数据
- foreach ($reward as $value) {
- // 奖项
- $data['reward_list'][] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
- }
- // 商品范围
- $productScope = $LotteryOrderProduct->query()->where([['lottery_id','=',$data['id']]])->pluck('product_id')->toArray();
- // 查询结果
- $query = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$data['start_time']],['insert_time','<=',$data['end_time']]]);
- // 如果商品存在
- if( $productScope ) $query = $query->whereIn('product_id',$productScope);
- // 获取时间段内下单数量
- $orderTotal = $query->groupBy('order_id')->count();
- // 是否已经抽过奖
- $lotterUsed = $OrderUsed->query()->where([['lottery_id','=',$data['id']],['custom_uid','=',$uid]])->count();
- // 计算可用次数
- $data['join_num'] = $data['join_num'] = ($orderTotal > 0 ? 1 - $lotterUsed : 0);
- // 最少为0,避免显示异常
- $data['join_num'] = $data['join_num'] < 0 ? 0 : $data['join_num'];
- // 时间处理
- $data['start_date'] = date('Y/m/d H:i',$data['start_time']);
- // 时间处理
- $data['end_date'] = date('Y/m/d H:i',$data['end_time']);
- // 删除不必要的数据
- unset($data['tag_scope'],$data['city_ids']);
- // 返回结果
- return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
- }
- /**
- * 积分抽奖 /api/lottery_score/get_reward
- *
- * */
- public function get_reward(Model $Model,Custom $Custom,OrderReward $OrderReward,WeiBanTags $WeiBanTags,CustomScore $CustomScore,LotteryOrderProduct $LotteryOrderProduct,CustomCoupon $CustomCoupon,OrderRecord $OrderRecord,OrdersProduct $OrdersProduct,OrderUsed $OrderUsed,CustomAmount $CustomAmount){
- // 接口验签
- // $this->verify_sign();
- // 检查登录
- $uid = $this->checkLogin();
- // 获取客户信息
- $lotteryId = request('lottery_id',0);
- // 如果存在的话
- if( !$lotteryId ) 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($lotteryId);
- // 如果存在的话
- 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]);
- // 奖品
- $reward = $OrderReward->getListByLottery($data['id']);
- // 活动暂无奖品
- if( !$reward ) return json_send(['code'=>'error','msg'=>'活动暂未配置奖品','data'=>$data]);
- // 默认可以参加活动
- $data['allow_join'] = 1;
- // 判断是不是可以参与
- if( $data['tag_scope'] ) {
- // 解析数组
- $data['tag_scope'] = explode(',',$data['tag_scope']);
- // 查询用户标签
- $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
- // 标签范围限定时,默认不能参与
- $data['allow_join'] = 0;
- // 判断标签是不是存在
- foreach ($tags as $value) {
- // 标签范围内,允许参加
- if( in_array($value['name'],$data['tag_scope']) ) $data['allow_join'] = 1;
- }
- // 如果不能参与
- if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'不符合参与条件','data'=>['error'=>'不符合参与条件']]);
- }
- // 判断是不是可以参与
- if( $data['city_ids'] ) {
- // 解析数组
- $data['city_ids'] = explode(',',$data['city_ids']);
- // 如果不在城市范围
- if( !in_array($custom['city_id'],$data['city_ids']) ) $data['allow_join'] = 0;
- // 如果不能参与
- if( !$data['allow_join'] ) return json_send(['code'=>'error','msg'=>'账号不在活动城市','data'=>['error'=>'账号不在活动城市']]);
- }
- // 商品范围
- $productScope = $LotteryOrderProduct->query()->where([['lottery_id','=',$data['id']]])->pluck('product_id')->toArray();
- // 查询结果
- $query = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$data['start_time']],['insert_time','<=',$data['end_time']]]);
- // 如果商品存在
- if( $productScope ) $query = $query->whereIn('product_id',$productScope);
- // 获取时间段内下单数量
- $orderTotal = $query->groupBy('order_id')->count();
- // 是否已经抽过奖
- $lotterUsed = $OrderUsed->query()->where([['lottery_id','=',$data['id']],['custom_uid','=',$uid]])->count();
- // 计算可用次数
- $data['join_num'] = $data['join_num'] = ($orderTotal > 0 ? 1 - $lotterUsed : 0);
- // 如果次数不够
- if( $data['join_num'] <= 0 ) return json_send(['code'=>'error','msg'=>'抽奖次数已用完','data'=>['error'=>'抽奖次数已用完']]);
- // 组合数据,写入订单表,子表
- DB::beginTransaction();
- try{
- // 扣减积分
- $result = $OrderUsed->add(['lottery_id'=>$data['id'],'custom_uid'=>$uid]);
- // 如果积分扣减失败
- if( !$result ) {
- // 回退数据
- DB::rollBack();
- return json_send(['code'=>'error','msg'=>'抽奖次数扣减失败','data'=>['error'=>'抽奖次数扣减失败']]);
- }
- // 获取奖励结果
- $rewardIndex = $OrderReward->getRewardResult($reward);
- // 如果中奖,下标不是0
- if( $rewardIndex ) {
- // 获取奖品
- $rewardResult = $reward[$rewardIndex];
- // 奖品记录ID
- if( !empty($rewardResult['id']) ){
- // 记录,默认状态为1,进行中
- $record = ['custom_uid'=>$uid,'lottery_id'=>$lotteryId,'reward_id'=>$rewardResult['id'],'reward_name'=>$rewardResult['reward_name'],'status'=>1];
- // 如果是积分
- if( $rewardResult['reward_type'] == 1 ){
- // 积分大于0
- if( $rewardResult['reward_info'] > 0 ){
- // 积分发放
- $result = $CustomScore->trade($uid,$lotteryId,$rewardResult['reward_info'],7,3);
- // 发放失败,改为未中奖
- if( isset($result['error']) ) $rewardIndex = 0;
- // 发放成功,状态为已完成
- $record['status']= 8;
- }
- }
- // 优惠券,先进行发放
- if( $rewardResult['reward_type'] == 2 ){
- // 优惠券存在ID
- if( $rewardResult['reward_info'] > 0 ){
- // 积分给与
- $result = $CustomCoupon->giveCoupon($rewardResult['reward_info'],$uid);
- // 发放失败,改为未中奖
- if( !$result ) $rewardIndex = 0;
- // 发放成功,状态为已完成
- $record['status']= 8;
- }
- }
- // 红包
- if( $rewardResult['reward_type'] == 3 ){
- // 积分大于0
- if( $rewardResult['reward_info'] > 0 ){
- // 积分发放
- $result = $CustomAmount->trade($uid,$lotteryId,$rewardResult['reward_info'],6,1,'下单抽奖');
- // 发放失败,改为未中奖
- if( isset($result['error']) ) $rewardIndex = 0;
- // 发放成功,状态为已完成
- $record['status']= 8;
- }
- }
- // 如果是实物,要求填写地址,状态设置为0
- if( $rewardResult['reward_type'] == 5 ) $record['status'] = 0;
- // 中奖才进行记录
- if( $rewardIndex ) {
- // 奖品数量减少
- $OrderReward->edit($rewardResult['id'],['reward_total'=>DB::raw('reward_total+-1')]);
- // 扣减数量
- $OrderRecord->add($record);
- }
- }
- }
- // 提交事务
- DB::commit();
- // 通过活动ID,查询奖品
- $rewardList = [];
- // 奖品数据
- foreach ($reward as $value) {
- $rewardList[] = ['id'=>$value['id'],'name'=>$value['reward_name'],'img'=>$value['reward_thumb'],'reward_type'=>$value['reward_type']];
- }
- // 返回结果
- 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()]]);
- }
- }
-
- }
|