123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\Store;
- use App\Models\OrderW;
- use App\Models\GiftW;
- use App\Models\GiftOrderW;
- use App\Models\IntegralW;
- use App\Models\ExchangeW;
- use App\Models\User;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- //use Maatwebsite\Excel\Facades\Excel;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- class IntegralController extends Controller
- {
- public function getStoreIntegralOrderBy(Request $request){
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $num=$page_size*($page_index-1);
- $type=$request->input('type');
- $where=[];
- if($type=='yesterday'){
- $where=function($query){
- $date=date("Y-m-d",strtotime('-1 days'));
- $query->whereDate('inte.created_at',$date);
- };
- }
- $count=Store::where('status',0)->count();
- // $list=Store::where('status',0)->orderByDesc('integral')
- // ->skip($num)->take($page_size)->get();
- $list=Store::leftJoin('integral as inte',function($query)use($where){
- $query->on('store.id','=','inte.store_id')
- ->where($where);
- })->where('store.status',0)
- ->groupBy('store.id')
- ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
- ->orderByDesc('integral')->orderBy('store.id')
- ->skip($num)->take($page_size)->get();
- return $this->success_list($list,'',$count);
- }
- public function getStoreOrderBy(Request $request){
- $type=$request->input('type');
- $agent_id=Auth::user()->id;
- $store=Store::where('user_id',$agent_id)->first();
- $store_id=$store->id;
- $where=[];
- $date=date("Y-m-d",strtotime('-1 days'));
- $where=function($query)use($date){
- $query->whereDate('inte.created_at',$date);
- };
- $yes_list=array_flip($this->getOrderBy($where));
- $list=array_flip($this->getOrderBy([]));
- $yes_integral=Store::leftJoin('integral as inte',function($query)use($where){
- $query->on('store.id','=','inte.store_id')->where($where);
- })->where('store.status',0)
- ->where('store_id',$store_id)
- ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
- ->value('integral');
- $integral=Store::leftJoin('integral as inte',function($query)use($where){
- $query->on('store.id','=','inte.store_id');
- })->where('store.status',0)
- ->where('store_id',$store_id)
- ->groupBy('store.id')
- ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
- ->value('integral');
- // $array=[15238617326,15138929587,18838980968,13030314383,15286818088];
- // $array=[];
- // $data['exc_gifts']=0;
- // if(in_array($store->phone,$array)){
- // $data['exc_gifts']=1;
- // }
- // $exchange=ExchangeW::where('status',1)->first();
- // if(empty($exchange)){
- // $data['exc_gifts']=0;
- // }else{
- // $date=date("Y-m-d H:i:s");
- // if($date<$exchange->start_time || $date>$exchange->end_time){
- // $data['exc_gifts']=0;
- // }else{
- // $data['exc_gifts']=1;
- // }
- // }
- $data['exc_gifts']=1;
- $data['yes_sort']=$yes_list[$store_id]+1;
- $data['sort']=$list[$store_id] + 1;
- $data['store']=Store::where('id',$store_id)->select('id','name','img','integral')->first();
- $data['integral']=$integral;
- $data['yes_integral']=$yes_integral;
- return $this->success($data);
- }
- protected function getOrderBy($where){
- $data=Store::leftJoin('integral as inte',function($query)use($where){
- $query->on('store.id','=','inte.store_id')
- ->where($where);
- })->where('store.status',0)
- ->groupBy('store.id')
- ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
- ->orderByDesc('integral')->orderBy('store.id')
- ->pluck('id')->toArray();
- return $data;
- }
- public function getStoreDetail(Request $request){
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $num=$page_size*($page_index-1);
- $agent_id=Auth::user()->id;
- $store_id=Store::where('user_id',$agent_id)->value('id');
- $count=IntegralW::where('store_id',$store_id)->count();
- $list=IntegralW::where('store_id',$store_id)->skip($num)->take($page_size)
- ->orderByDesc('created_at')->get();
- return $this->success_list($list,'',$count);
- }
- //我的积分与兑换礼品
- public function getInteGifts(){
- $store=Store::where('user_id',Auth::user()->id)->select('id','name','phone','img','integral')->first();
- $gifts=GiftW::where('status',1)->orderBy('id')->get();
- $data['is_exc']=1;
- $exchange=ExchangeW::where('status',1)->first();
- if(empty($exchange)){
- $data['is_exc']=0;
- }else{
- $date=date("Y-m-d H:i:s");
- if($date<$exchange->start_time || $date>$exchange->end_time){
- $data['is_exc']=0;
- }
- }
- $data['integral']=$store->integral;
- $data['gifts']=$gifts;
- return $this->success($data);
- }
- //兑换礼品
- public function exchangeGifts(Request $request){
- $date=date("Y-m-d H:i:s");
- $exchange=ExchangeW::where('status',1)->first();
- if(empty($exchange)){
- return $this->error('450001','当前时间不可兑换');
- }
- if($date<$exchange->start_time || $date>$exchange->end_time){
- return $this->error('450001','当前时间不可兑换');
- }
- $store=Store::where('user_id',Auth::user()->id)->select('id','name','phone','img','integral')->first();
- $gift_id=$request->input('gift_id');
- $num=$request->input('num');
- $gift=GiftW::where('id',$gift_id)->first();
- if($gift->gift_num - $gift->exchange_num < $num){
- return $this->error('450001','兑换礼品库存不足,停止兑换');
- }
- $total=$num*$gift->integral;
- if($total>$store->integral){
- return $this->error('450001','兑换礼品总积分超出现有积分,不能兑换');
- }
- try{
- DB::transaction(function()use($gift_id,$num,$exchange){
- $giftInfo=GiftW::where('id',$gift_id)->lockForUpdate()->first();
- // $giftInfo->gift_num -= $num;
- $giftInfo->exchange_num += $num;
- $giftInfo->save();
- $storeInfo=Store::where('user_id',Auth::user()->id)->lockForUpdate()->first();
- $storeInfo->integral -= $num*$giftInfo->integral;
- $storeInfo->save();
- $order_no='DH'.date('ymdHis').substr(microtime(), 2, 6).mt_rand(10000, 99999);
- GiftOrderW::create([
- 'store_id'=>$storeInfo->id,
- 'uid'=>$storeInfo->user_id,
- 'gift_id'=>$gift_id,
- 'shipping_status'=>0,
- 'num'=>$num,
- 'season'=>$exchange->season,
- 'order_number'=>$order_no,
- 'bonus'=>$giftInfo->integral,
- 'name'=>$giftInfo->name,
- 'type'=>0
- ]);
- IntegralW::create([
- 'store_id'=>$storeInfo->id,
- 'order_no'=>$order_no,
- 'num'=> $num,
- 'integral'=> 0-$num*$giftInfo->integral,
- 'integral_double'=> 0-$num*$giftInfo->integral,
- 'type'=>3,
- 'remark'=>'兑换礼品',
- ]);
- },5);
- return $this->success([]);
- }catch(\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- //取消兑换礼品
- public function cancelExchangeGifts(Request $request){
- $id=$request->input('id');
- $date=date("Y-m-d H:i:s");
- $exchange=ExchangeW::where('status',1)->first();
- if(empty($exchange)){
- return $this->error('450001','当前时间不可取消兑换');
- }
- $gift_order=GiftOrderW::where('id',$id)->first();
- if($exchange->season != $gift_order->season){
- return $this->error('450001','当前时间该兑换不可取消');
- }
- if($date<$exchange->start_time || $date>$exchange->end_time){
- return $this->error('450001','当前时间该兑换不可取消');
- }
- if($gift_order->shipping_status==1){
- return $this->error('450001','礼品已发出,不能取消兑换');
- }
- try{
- DB::transaction(function()use($id){
- $gift_order=GiftOrderW::where('id',$id)->lockForUpdate()->first();
- $gift_order->type=1;
- $gift_order->save();
- $giftInfo=GiftW::where('id',$gift_order->gift_id)->lockForUpdate()->first();
- // $giftInfo->gift_num += $gift_order->num;
- $giftInfo->exchange_num -= $gift_order->num;
- $giftInfo->save();
- $storeInfo=Store::where('user_id',Auth::user()->id)->lockForUpdate()->first();
- $storeInfo->integral += $gift_order->num*$giftInfo->integral;
- $storeInfo->save();
- IntegralW::create([
- 'store_id'=>$storeInfo->id,
- 'order_no'=>$gift_order->order_number,
- 'num'=> $gift_order->num,
- 'integral'=> $gift_order->num*$giftInfo->integral,
- 'integral_double'=> $gift_order->num*$giftInfo->integral,
- 'type'=>4,
- 'remark'=>'取消兑换',
- ]);
- },5);
- return $this->success([]);
- }catch(\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- //我的兑换
- public function myExchange(Request $request){
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $num=$page_size*($page_index-1);
- $store=Store::where('user_id',Auth::user()->id)->first();
- $gift_count=GiftOrderW::where('store_id',$store->id)->count();
- $gift_order=GiftOrderW::where('store_id',$store->id)
- ->with('gift')
- ->orderByDesc('id')
- ->skip($num)->take($page_size)->get();
- return $this->success_list($gift_order,'',$gift_count);
- }
- //我的礼品
- public function myGifts(Request $request){
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $num=$page_size*($page_index-1);
- $store=Store::where('user_id',Auth::user()->id)->first();
- $gift_count=GiftOrderW::where('store_id',$store->id)->where('type',0)->sum('num');
- $gift_order=GiftOrderW::where('store_id',$store->id)->where('type',0)
- ->with('gift')
- ->groupBy('gift_id')
- ->select('id','name','gift_id',DB::raw('sum(num) as num'))
- ->skip($num)->take($page_size)->get();
- return $this->success_list($gift_order,'',$gift_count);
- }
- //我的下级兑换
- public function myAgentExchange(Request $request){
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $num=$page_size*($page_index-1);
- $user_id=Auth::user()->id;
- // if($user_id=='32531'){
- // $user_id=14;
- // }
- $user=User::where('id',$user_id)->first();
- $childIds = [];
- if ($user->level == '3') {
- $childAgentIds = User::where('agent_id', $user->id)->pluck('id')->toArray();
- if ($childAgentIds) {
- $childChildAgentIds = User::whereIn('agent_id', $childAgentIds)->pluck('id')->toArray();
- $childIds = array_merge($childAgentIds, $childChildAgentIds);
- } else {
- $childIds = $childAgentIds;
- }
- } elseif ($user->level == '2') {
- $childIds = User::where('agent_id', $user->id)->pluck('id');
- } else {
- $childIds = [];
- }
- $data['gifts_num']=GiftOrderW::whereIn('uid',$childIds)->sum('num');//->where('type',0)
- $count=GiftOrderW::whereIn('uid',$childIds)
- ->leftJoin('store as s','gift_orders.store_id','=','s.id')
- ->select('s.id','s.name','s.phone',DB::raw('sum(num) as num'))
- ->groupBy('uid')->get()->count();
- $data['user_gifts']=GiftOrderW::whereIn('uid',$childIds)
- ->leftJoin('store as s','gift_orders.store_id','=','s.id')
- ->select('s.id','s.name','s.phone','s.img',DB::raw('sum(num) as num'))
- ->groupBy('gift_orders.uid')->orderByDesc('num')->orderBy('gift_orders.store_id')
- ->skip($num)->take($page_size)->get();
- return $this->success_list($data,'',$count);//->where('type',0)
- }
- //下级礼品
- public function getAgentGifts(Request $request){
- $id=$request->input('id');
- $gifts=GiftOrderW::where('store_id',$id)->where('type',0)
- ->leftJoin('store as s','gift_orders.store_id','=','s.id')
- ->select('store_id','gift_id','gift_orders.name',DB::raw('sum(num) as num'))
- ->groupBy('gift_id')->orderByDesc('num')->get();
- return $this->success($gifts);
- }
- public function integral_2(){
- $date=date("Y-m-d",strtotime('-1 days'));
- $where=function($query)use($date){
- $query->where('order.is_pay',1)->whereIn('order.apply_cancel',[0,1,3])->whereDate('order.created_at',$date);
- };
- $store_ids=OrderW::where($where)->leftjoin('store','order.store_id','=','store.id')
- ->select('store.name','store.phone','store.img','store_id',DB::raw('sum(total) as total'),DB::raw('sum(account) as account'))
- ->groupBy('store_id')->having('total','>=',2)->orderByDesc('total')->get();
- return $store_ids;
- $date=date("Y-m-d",strtotime('-1 days'));
- $where=function($query)use($date){
- $query->where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->whereDate('created_at',$date);
- };
- $store_ids=OrderW::where($where)
- ->select('store_id',DB::raw('sum(total) as total'))
- ->groupBy('store_id')->having('total','>=',2)->pluck('store_id');
- $order_nos=OrderW::where($where)->whereIn('store_id',$store_ids)->pluck('order_no');
- IntegralW::whereDate('created_at',$date)->whereIn('order_no',$order_nos)->where('type',1)
- ->update(['integral_double'=>DB::raw('2*integral')]);
- $cancel=IntegralW::whereDate('created_at',$date)->where('type',2)->get();
- foreach($cancel as $key=>$val){
- $inte_2=IntegralW::where('order_no',$val->order_no)->where('type',1)->value('integral_double');
- IntegralW::where('id',$val->id)->update([
- 'integral_double'=>0-$inte_2
- ]);
- }
- return '12222222222';
- // $store=Store::leftJoin('integral as inte',function($query)use($date){
- // $query->on('store.id','=','inte.store_id')->whereDate('created_at',$date);
- // })->where('store.status',0)
- // ->groupBy('store.id')
- // ->select('store.id','store.name','store.img',DB::raw('sum(ifnull(inte.num,0)) as num'),DB::raw('sum(ifnull(inte.integral,0)) as integral'))
- // ->having('num','>=',2)
- // ->get();
- $inte=IntegralW::whereDate('created_at',$date)
- ->select('store_id',DB::raw('sum(ifnull(inte.num,0)) as num'),DB::raw('sum(ifnull(inte.integral,0)) as integral'))
- ->groupBy('store_id')
- ->having('num','>=',2)
- ->get();
- foreach($inte as $key=>$val){
- IntegralW::whereDate('created_at',$date)->where('store_id',$val->store_id)->where('type',1)->update([
- 'integral_double'=>DB::raw('2*integral')
- ]);
- }
- }
- }
|