1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Models\Address;
- use App\Models\Admin;
- use App\Models\Bank123;
- use App\Models\Comment;
- use App\Models\GoodSku;
- use App\Models\OrderCancel;
- use App\Models\OrderDetail;
- use App\Models\OrderFinishFails;
- use App\Models\OrderRefund;
- use App\Models\OrderW;
- use App\Models\User;
- use App\Models\Store;
- use App\Models\Order;
- use App\Models\Goods;
- use App\Models\Region;
- use App\Models\Agent\AgentDwbsUser;
- use App\Models\Agent\AgentDwbsWarea;
- use App\Models\Agent\AgentDwbsAdmin;
- use App\Models\Agent\AgentStore;
- use App\Models\DwbsUser;
- use App\Models\DwbsWarea;
- use App\Models\Integral;
- use App\Exports\IntegralExport;
- use App\Exports\CompanyInfoExport;
- use Illuminate\Support\Facades\Cache;
- use Maatwebsite\Excel\Facades\Excel;
- use function Couchbase\defaultDecoder;
- use Illuminate\Http\Request;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class IndexController extends Controller
- {
- public function setBank(Request $request)
- {
- $region = Region::where('id', '>', '700000')->get();
- foreach ($region as $key => $val) {
- $banks = Bank123::where('city', 'like', '%' . $val->sname . '%')->first();
- if ($banks) {
- Log::info($val->sname);
- }
- }
- }
- //获取首页信息
- public function index()
- {
- $user_count = User::whereNotNull('openId')->orWhereNotNull('phone')->count();
- $store_count = Store::where('is_apply', 3)->count();
- $goods_count = Goods::where('is_shelves', 1)->count();
- $order_sale = Order::where('is_pay', 1)->sum('account');
- $refund_no_auth = OrderRefund::where('status', 0)->count();
- $list['user_count'] = $user_count;
- $list['store_count'] = $store_count;
- $list['goods_count'] = $goods_count;
- $list['order_sale'] = $order_sale;
- $list['refund_no_auth'] = $refund_no_auth;
- $list['expire']=date("YmdHis",strtotime('+120 second'));
- $list['ccc']=Order::where('apply_cancel',2)->whereColumn('cancel_at','<','pay_at')->get();
- return $this->success($list);
- }
- //获取团队排行榜
- public function getWareaOrderBy()
- {
- $store_ids = Order::where('is_pay', 1)
- ->groupBy('store_id')
- ->pluck('store_id');
- $user_ids = Store::whereIn('id', $store_ids)->pluck('user_id');
- $warea_ids = DwbsUser::whereIn('users.id', $user_ids)
- ->join('warea', 'warea.id', '=', 'users.warea_id')->distinct('user.warea_id')
- ->orderBy('warea.name')
- ->pluck('users.warea_id', 'warea.name');
- $list = [];
- foreach ($warea_ids as $key => $val) {
- $data = [];
- $data['warea'] = $key;
- // $list[$key]['warea']=DwbsWarea::where('id',$val)->value('name');
- $uids = DwbsUser::whereIn('id', $user_ids)->where('warea_id', $val)->pluck('id');
- $sids = Store::whereIn('user_id', $uids)->pluck('id');
- $data['account'] = Order::where('is_pay', 1)
- ->whereIn('store_id', $sids)->sum('account');
- $data['num'] = Order::where('is_pay', 1)
- ->whereIn('store_id', $sids)->sum('total');
- $list[] = $data;
- }
- return $this->success($list);
- }
- //获取省级行政单位各个main_Attr销售信息
- public function getProvinceSalesAttr($id = 700000)
- {
- $province = Region::where('level', 1)->where('id', '<', $id)->orderBy('id')->pluck('name');
- $goods = Goods::where('is_shelves', 1)->select('id', 'name')->get();
- $list = [];
- foreach ($province as $key => $val) {
- $pro = $val;
- switch ($province) {
- case '北京':
- $pro = '北京市';
- break;
- case '天津':
- $pro = '天津市';
- break;
- case '上海':
- $pro = '上海市';
- break;
- case '重庆':
- $pro = '重庆市';
- break;
- }
- $addr_ids = Address::where('province', $pro)->pluck('id');
- $order_nos = Order::whereHas('address', function ($query) use ($addr_ids) {
- $query->whereIn('id', $addr_ids);
- })->where('is_pay', 1)->pluck('order_no');
- $data = OrderDetail::whereIn('order_no', $order_nos);
- $data->with('goods:id,name,main_attr');
- $data->select('goods_id', DB::raw('sum(num) as total'), DB::raw('sum(account) as amount'));
- $data->groupBy('goods_id');
- $res = $data->get();
- foreach ($goods as $keys => $value) {
- if (count($res) > 0) {
- $result = 0;
- foreach ($res as $k => $v) {
- if ($value->name == $v->goods->name) {
- $result += 1;
- $list[$keys]['data'][$key] = $v->total;
- }
- }
- if ($result == 0) {
- $list[$keys]['data'][$key] = 0;
- }
- } else {
- $list[$keys]['data'][$key] = 0;
- }
- $list[$keys]['name'] = $value->name;
- }
- }
- return $this->success($list);
- }
- //获取所有省级单位
- public function getProvinceList()
- {
- $province = Region::where('level', 1)->where('id', '<', 700000)->orderBy('id')->pluck('name', 'id');
- return $this->success($province);
- }
- //支付比例
- public function getAgentPayRate(Request $request)
- {
- $page_index=$request->input('page_index');
- $page_size=$request->input('page_size');
- $num=$page_size*($page_index-1);
- // $store_ids = Order::where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('store_id');
- $total=Store::where('status', 0)->where('is_test','0')->whereHas('order',function($query){
- $query->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- })->count();
- $stores = Store::where('status', 0)->where('is_test','0')->whereHas('order',function($query){
- $query->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- })->select('id', 'user_id','name','phone')->orderBy('id')->skip($num)->take($page_size)->get();
- foreach ($stores as $key => $val) {
- // $stores[$key]->total = Order::where('store_id', $val->id)->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->sum('total');
- $stores[$key]->pay_total = Order::where('store_id', $val->id)->where('self_pay',0)->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->sum('total');
- $stores[$key]->no_self_pay_total = Order::where('store_id', $val->id)->where('self_pay', 1)->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->sum('total');
- // $stores[$key]->agent = DwbsUser::where('id', $val->user_id)->select('nickname', 'mobile')->first();
- }
- return $this->success_list($stores,'',$total);
- }
- //销量排名
- public function getSaleOrderBy(Request $request)
- {
- Log::info('1---'.microtime());
- $page_index = $request->input('page_index');
- $page_size = $request->input('page_size');
- $num = $page_size * ($page_index - 1);
- $level = $request->input('level');
- $search_name = $request->input('search_name');
- $startime = $request->input('startime');
- $endtime = $request->input('endtime');
- $sort = $request->input('sort');
- $desc = $request->input('desc');
- $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- $admin_wheres = $level_wheres = $search_wheres = [];
- $admin_id = Auth::user()->id;
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- $admin_wheres = [];
- } else {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
- $admin_wheres = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- if ($level) {
- $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
- $user_ids = DwbsUser::where('level', $level)->whereIn('id', $agent_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $user_ids)->pluck('id');
- $data->whereIn('store_id', $store_ids);
- $level_wheres = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- if ($search_name) {
- $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id');
- $data->whereIn('store_id', $store_ids);
- $search_wheres = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- if ($startime && $endtime) {
- $data->whereBetween('created_at', [$startime, $endtime]);
- }
- $order_ids = $data->pluck('id');
- // if ($sort == 'hard') {
- // $where['goods_id'] = 1;
- // } elseif ($sort == 'simple') {
- // $where['goods_id'] = 2;
- // } elseif ($sort == 'old') {
- // $where['goods_id'] = 3;
- // } elseif ($sort == 'cotton') {
- // $where['goods_id'] = 4;
- // } else {
- // $where = [];
- // }
- Log::info('2---'.microtime());
- $store_ids=Store::where('status', 0)->where('is_apply', 3)
- ->where($admin_wheres)->where($level_wheres)->where($search_wheres)->pluck('id');
- // $order_details =OrderDetail::query()
- // ->whereIn('store_id',$store_ids)
- // ->whereIn('order_id', $order_ids)
- // ->where($where)
- // ->groupBy('store_id')
- // ->select('store_id', DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
- $order_details = Store::where('status', 0)->where('is_apply', 3)
- ->where($admin_wheres)->where($level_wheres)->where($search_wheres)
- ->leftJoin('order_detail as od', function ($query) use ($order_ids, $where) {
- $query->on('od.store_id', '=', 'store.id')
- ->whereIn('order_id', $order_ids)
- ->where($where);
- })
- ->groupBy('store.id')
- ->select('store.id as store_id',DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
- if ($sort == 'total' || $sort == 'hard' || $sort == 'simple' || $sort == 'old' || $sort == 'cotton') {
- if ($desc == 1) {
- $order_details->OrderBy('total');
- } else {
- $order_details->OrderByDesc('total');
- }
- } elseif ($sort == 'account') {
- if ($desc == 1) {
- $order_details->OrderBy('account');
- } else {
- $order_details->OrderByDesc('account');
- }
- }
- $count = $order_details->get()->count();
- $list = $order_details->skip($num)->limit($page_size)->get();
- // Log::info('3---'.microtime());
- $all['all_account'] = OrderDetail::whereIn('order_id', $order_ids)->sum('account');
- $all['all_num'] = OrderDetail::whereIn('order_id', $order_ids)->sum('num');
- $nums = OrderDetail::with('store:id,name,phone')->whereIn('order_id', $order_ids)->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id');
- $all['hard_num'] = $nums[1];
- $all['simple_num'] = $nums[2];
- $all['old_num'] = $nums[3];
- $all['cotton_num'] = $nums[4];
- Log::info('4---'.microtime());
- $wareas = DwbsWarea::pluck('name', 'id');
- $user_ids = Store::pluck('user_id');
- $store_user_ids = Store::pluck('user_id', 'id');
- $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
- $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
- Log::info('5---'.microtime());
- foreach ($list as $key => $val) {
- $second=mt_rand(2880,7200);
- $order= Order::where('store_id', $val->store_id)->whereIn('id', $order_ids)
- ->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- $list[$key]=$order;
- if($order->total==0 && $order->account==0){
- $list[$key]->goods =[];
- }else{
- $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id', $val->store_id)->whereIn('order_id', $order_ids)->groupBy('goods_id')
- ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
- }
- if(Cache::get('store-'.$val->store_id)){
- $list[$key]->store=Cache::get('store-'.$val->store_id);
- }else{
- $store = Store::where('id',$val->store_id)->select('id','name','img','phone','province','city','area')->first();
- $list[$key]->store = $store;
- Cache::add('store-'.$val->store_id, $store, $second);
- }
- if(isset($val->store_id)){
- $user_id = $store_user_ids[$val->store_id];
- if(Cache::get('warea-'.$user_id)){
- $list[$key]->warea = Cache::get('warea-'.$user_id);
- }else{
- $warea_id = $warea_user_ids[$user_id];
- $list[$key]->warea = $wareas[$warea_id];
- Cache::add('warea-'.$user_id, $wareas[$warea_id], $second);
- }
- if(Cache::get('level-'.$user_id)){
- $list[$key]->level = Cache::get('level-'.$user_id);
- }else{
- $list[$key]->level = $level_user[$user_id];
- Cache::add('level-'.$user_id, $level_user[$user_id], $second);
- }
- }else{
- $list[$key]->warea = '';
- $list[$key]->level = '';
- }
- }
- Log::info('6---'.microtime());
- $all['list'] = $list;
- return $this->success_list($all, '', $count);
- }
- //总销量排名
- public function getSaleOrderByAll(Request $request)
- {
- Log::info('1---'.microtime());
- $page_index = $request->input('page_index');
- $page_size = $request->input('page_size');
- $num = $page_size * ($page_index - 1);
- $level = $request->input('level');
- $search_name = $request->input('search_name');
- $startime = $request->input('startime');
- $endtime = $request->input('endtime');
- $sort = $request->input('sort');
- $desc = $request->input('desc');
- $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- $admin_id = Auth::user()->id;
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- if ($level) {
- $store_ids = Cache::remember('level-'.$level,mt_rand(1800,3600),function()use($level){
- $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
- $user_ids = DwbsUser::where('level', $level)->whereIn('id',$agent_ids)->pluck('id');
- return Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
- });
- }else{
- $store_ids=[];
- }
- } else {
- if ($level) {
- $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::where('level', $level)->whereIn('warea_id', $warea_ids)->pluck('id');
- return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
- });
- }else{
- $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
- });
- }
- }
- if ($search_name) {
- if($store_ids){
- $store_ids = Store::whereIn('id',$store_ids)
- ->where(function($query)use($search_name){
- $query->where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%');
- })
- ->pluck('id')->toArray();
- }else{
- $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%')
- ->pluck('id')->toArray();
- }
- }
- $where=[];
- if ($startime && $endtime) {
- $data->whereBetween('created_at', [$startime, $endtime]);
- $where=function($query)use($startime, $endtime){
- $query->whereBetween('created_at', [$startime, $endtime]);
- };
- }
- if($store_ids){
- $data->whereIn('store_id',$store_ids);
- }
- Log::info('2---'.microtime());
- $data1 = clone $data;
- $order_details = $data->with([
- 'store'=>function($query){
- $query->with(['user'=>function($q){
- $q->with(['warea:id,name'])->select('id','nickname','level','warea_id');
- }])->select('id','name','img','phone','province','city','area','user_id');
- }])
- ->groupBy('store_id')->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
- Log::info('2-1-0-'.microtime());
- $count = $order_details->get()->count();
- if($desc==1){
- $desc='asc';
- }else{
- $desc='desc';
- }
- $list = $order_details->orderBy($sort,$desc)->orderBy('store_id')->skip($num)->limit($page_size)->get();
- Log::info('3---'.microtime());
- $account_num = $data1->where($where)->select(DB::raw('sum(account) as account'),DB::raw('sum(total) as num'))->first();
- $all['all_account'] = $account_num->account;
- $all['all_num'] = $account_num->num;
- Log::info('3.1---'.microtime());
- if($store_ids){
- $nums = OrderDetail::whereHas('order',function($query)use($store_ids){
- $query->whereIn('store_id', $store_ids)->where('is_pay',1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- })->where($where)->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id')->toArray();
- }else{
- $nums = OrderDetail::whereHas('order',function($query){
- $query->where('is_pay',1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- })->where($where)->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id')->toArray();
- }
- $all['hard_num'] =array_key_exists(1,$nums)?$nums[1]:null;
- $all['simple_num'] = array_key_exists(2,$nums)?$nums[2]:null;
- $all['old_num'] = array_key_exists(3,$nums)?$nums[3]:null;
- $all['cotton_num'] = array_key_exists(4,$nums)?$nums[4]:null;
- Log::info('4---'.microtime());
- foreach ($list as $key => $val) {
- $second=mt_rand(2880,7200);
- if($val->total==0 && $val->account==0){
- $list[$key]->goods =[];
- }else{
- $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id', $val->store_id)
- ->whereHas('order',function($query)use($where){
- $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
- })
- ->groupBy('goods_id')
- ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
- }
- }
- Log::info('6---'.microtime());
- $all['list'] = $list;
- $all['num'] = $nums;
- return $this->success_list($all, '', $count);
- }
- //销量排名
- public function saleBy(Request $request){
- //删除没有开通微店代理
- // 1.获取所有店铺代理id数组
- $user_ids=Store::pluck('user_id');
- return counnt($user_ids);
- // $user_ids = Store::pluck('user_id');
- // $user_ids = AgentStore::pluck('user_id');
- // return $user_ids;
- $list=Order::with('store:id,name,phone')->where('is_pay',1)
- ->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1])->groupby('store_id')
- ->select('store_id',DB::raw('sum(total) as total'),DB::raw('sum(account) as account'))
- ->orderBy('account')->skip(0)->limit(10)->get();
- $user_ids = Store::pluck('user_id');
- $store_user_ids = Store::pluck('user_id', 'id')->toArray();
- // $wareas = DwbsWarea::pluck('name', 'id');
- // $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
- // $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
- // $wareas = AgentDwbsWarea::pluck('name', 'id');
- // $warea_user_ids = AgentDwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
- // $level_user = AgentDwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
- $wareas = Cache::remember('wareas','60',function(){
- return DwbsWarea::pluck('name', 'id');
- });
- $warea_user_ids = Cache::remember('warea_user_ids','60',function()use($user_ids){
- return DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
- });
- $level_user = Cache::remember('level_user','60',function()use($user_ids){
- return DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
- });
- foreach ($list as $key => $val) {
- $second=mt_rand(2880,7200);
- if($val->total==0 && $val->account==0){
- $list[$key]->goods =[];
- }else{
- $where=[];
- $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id', $val->store_id)
- ->whereHas('order',function($query)use($where){
- $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
- })
- ->groupBy('goods_id')
- ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
- }
- if(array_key_exists($val->store_id,$store_user_ids)){
- $user_id = $store_user_ids[$val->store_id];
- if(Cache::get('warea-'.$user_id)){
- $list[$key]->warea = Cache::get('warea-'.$user_id);
- }else{
- $warea_id = $warea_user_ids[$user_id];
- $list[$key]->warea = $wareas[$warea_id];
- Cache::add('warea-'.$user_id, $wareas[$warea_id], $second);
- }
- if(Cache::get('level-'.$user_id)){
- $list[$key]->level = Cache::get('level-'.$user_id);
- }else{
- $list[$key]->level = $level_user[$user_id];
- Cache::add('level-'.$user_id, $level_user[$user_id], $second);
- }
- }else{
- $list[$key]->warea = '';
- $list[$key]->level = '';
- }
- }
- return $list;
- }
- //分商品销量排名
- public function getSaleOrderByType(Request $request)
- {
- Log::info('type1---'.microtime());
- $page_index = $request->input('page_index');
- $page_size = $request->input('page_size');
- $num = $page_size * ($page_index - 1);
- $level = $request->input('level');
- $search_name = $request->input('search_name');
- $startime = $request->input('startime');
- $endtime = $request->input('endtime');
- $sort = $request->input('sort');
- $desc = $request->input('desc');
- $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- $admin_id = Auth::user()->id;
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- if ($level) {
- $store_ids = Cache::remember('level-'.$level,mt_rand(1800,3600),function()use($level){
- $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
- $user_ids = DwbsUser::where('level', $level)->whereIn('id',$agent_ids)->pluck('id');
- return Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
- });
- }else{
- $store_ids=[];
- }
- } else {
- if ($level) {
- $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::where('level', $level)->whereIn('warea_id', $warea_ids)->pluck('id');
- return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
- });
- }else{
- $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
- });
- }
- }
- if ($search_name) {
- if($store_ids){
- $store_ids = Store::whereIn('id',$store_ids)
- ->where(function($query)use($search_name){
- $query->where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%');
- })
- ->pluck('id')->toArray();
- }else{
- $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%')
- ->pluck('id')->toArray();
- }
- }
- $where=[];
- if ($startime && $endtime) {
- $data->whereBetween('created_at', [$startime, $endtime]);
- $where=function($query)use($startime, $endtime){
- $query->whereBetween('created_at', [$startime, $endtime]);
- };
- }
- if($store_ids){
- $data->whereIn('store_id',$store_ids);
- }
- $datas= clone $data;
- $data1= clone $data;
- $order_ids = $data->pluck('id');//->whereIn('store_id', $store_ids)
- Log::info('type2---'.microtime());
- $order_details=OrderDetail::with([
- 'store'=>function($query){
- $query->with(['user'=>function($q){
- $q->with(['warea:id,name'])->select('id','nickname','level','warea_id');
- }])->select('id','name','img','phone','province','city','area','user_id');
- }])
- ->whereHas('order',function($query)use($where,$order_ids){
- $query->has('store')->whereIn('id', $order_ids)->where($where);
- })->groupBy('store_id')
- ->select('store_id',DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as order_account'));
- Log::info('type3---'.microtime());
- if($sort == 'hard'){
- $order_details->where('goods_id',1);
- }
- if($sort == 'simple'){
- $order_details->where('goods_id',2);
- }
- if($sort == 'old'){
- $order_details->where('goods_id',3);
- }
- if($sort == 'cotton'){
- $order_details->where('goods_id',4);
- }
- if ($sort == 'hard' || $sort == 'simple' || $sort == 'old' || $sort == 'cotton') {
- if ($desc == 1) {
- $order_details->OrderBy('num');
- } else {
- $order_details->OrderByDesc('num');
- }
- }
- $count = $order_details->get()->count();
- $list = $order_details->orderBy('store_id')->skip($num)->limit($page_size)->get();
- Log::info('type3.2---'.microtime());
- $account_num= Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where)->whereIn('id',$order_ids)
- ->select(DB::raw('sum(account) as account'),DB::raw('sum(total) as num'))->first();
- $all['all_account'] = $account_num->account;
- $all['all_num'] = $account_num->num;
- Log::info('type3.3---'.microtime());
- $nums = OrderDetail::with('store:id,name,phone')->whereHas('order',function($query)use($where,$order_ids){
- $query->has('store')->whereIn('id',$order_ids)->where($where);
- })->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id');
- // $all['hard_num'] = $nums[1];
- // $all['simple_num'] = $nums[2];
- // $all['old_num'] = $nums[3];
- // $all['cotton_num'] = $nums[4];
- $all['hard_num'] =array_key_exists(1,$nums)?$nums[1]:null;
- $all['simple_num'] = array_key_exists(2,$nums)?$nums[2]:null;
- $all['old_num'] = array_key_exists(3,$nums)?$nums[3]:null;
- $all['cotton_num'] = array_key_exists(4,$nums)?$nums[4]:null;
- Log::info('type5---'.microtime());
- foreach ($list as $key => $val) {
- $second=mt_rand(2880,7200);
- $val_order=Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where('store_id',$val->store_id)->where($where)
- ->select(DB::raw('sum(account) as account'),DB::raw('sum(total) as total'))->first();
- $list[$key]->total=$val_order->total;
- $list[$key]->account=$val_order->account;
- if($val_order->total==0 && $val_order->account==0){
- $list[$key]->goods =[];
- }else{
- $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id', $val->store_id)
- ->whereHas('order',function($query)use($where){
- $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
- })
- ->groupBy('goods_id')
- ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
- }
- }
- $all['list'] = $list;
- return $this->success_list($all, '', $count);
- }
- //总销量排名
- public function exportSaleOrderByAll(Request $request)
- {
- ini_set('memory_limit', '4096M');
- log::info('gaunjuan');
- $level = $request->input('level');
- $search_name = $request->input('search_name');
- $startime = $request->input('startime');
- $endtime = $request->input('endtime');
- $admin_id = Auth::user()->id;
- Log::info('MINUTE---'.microtime());
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- $store_ids = Store::pluck('id')->toArray();
- } else {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
- }
- Log::info('MINUTE---'.microtime());
- if ($level) {
- $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
- $user_ids = DwbsUser::where('level', $level)->whereIn('id', $agent_ids)->pluck('id');
- $level_store_ids = Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
- $store_ids=array_intersect($store_ids,$level_store_ids);
- }
- Log::info('MINUTE---'.microtime());
- if ($search_name) {
- $search_name_store_ids = Store::where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id')->toArray();
- $store_ids=array_intersect($store_ids,$search_name_store_ids);
- }
- Log::info('MINUTE---'.microtime());
- $where=[];
- if ($startime && $endtime) {
- $where=function($query)use($startime, $endtime){
- $query->whereBetween('created_at', [$startime, $endtime]);
- };
- }
- Log::info('MINUTE---'.microtime());
- //<<<<<<< HEAD
- // $order_ids=Order::where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where)->pluck('id');
- // $store=Store::with(['order_detail'=>function($query)use($where,$order_ids){
- // $query->with(['goods:id,main_attr'])->select('id','store_id','goods_id','account','num','size','type')
- // ->where($where)->whereIn('order_id',$order_ids);
- // },'user:id,level,agent_id,nickname'])
- // ->whereIn('id',$store_ids)->select('id','name','phone','province','city','area','user_id')->get();//,'img'
- //=======
- // $order_ids=Order::where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where)->pluck('id');
- $store=Store::whereHas('order',function ($q)use ($where){
- $q->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
- })->with(['order_detail'=>function($query)use($where){
- $query->with(['goods:id,main_attr'])->select('id','store_id','goods_id','account','num','size','type')
- ->where($where)->whereHas('order',function($q)use($where){
- $q->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
- });
- },'user:id,level,agent_id,nickname'])
- ->whereIn('id',$store_ids)->select('id','name','phone','province','city','area','user_id')->get();//,'img'
- // $store=Store::where('is_apply',3)->select('id','name','phone','province','city','area','user_id')->get();
- //>>>>>>> 3113a468aec0f8b4a919f7affca6dae9837db628
- Log::info('MINUTE---'.microtime());
- $wareas = DwbsWarea::pluck('name', 'id');
- $user_ids = Store::pluck('user_id');
- $store_user_ids = Store::pluck('user_id', 'id')->toArray();
- $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id')->toArray();
- $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id')->toArray();
- Log::info('MINUTE---'.microtime());
- foreach($store as $key=>$val){
- $store[$key]->company = '';
- $store[$key]->account=$val->order_detail->sum('account');
- $store[$key]->num=$val->order_detail->sum('num');
- $store[$key]->super_num=$val->order_detail->where('goods_id',1)->sum('num');
- $store[$key]->simple_num=$val->order_detail->where('goods_id',2)->sum('num');
- $store[$key]->old_num=$val->order_detail->where('goods_id',3)->sum('num');
- $store[$key]->cotton_num=$val->order_detail->where('goods_id',4)->sum('num');
- $second=mt_rand(2880,7200);
- if(array_key_exists($val->id,$store_user_ids)){
- $user_id = $store_user_ids[$val->id];
- if(Cache::get('warea-'.$user_id)){
- $store[$key]->warea = Cache::get('warea-'.$user_id);
- }else{
- if(array_key_exists($user_id,$warea_user_ids)){
- $warea_id = $warea_user_ids[$user_id];
- if(array_key_exists($warea_id,$wareas)){
- $store[$key]->warea = $wareas[$warea_id];
- }else{
- $store[$key]->warea = '';
- }
- }else{
- $store[$key]->warea = '';
- }
- Cache::add('warea-'.$user_id, $store[$key]->warea, $second);
- }
- if(Cache::get('level-'.$user_id)){
- $store[$key]->level = Cache::get('level-'.$user_id);
- }else{
- if(array_key_exists($user_id,$level_user)){
- $store[$key]->level = $level_user[$user_id];
- }else{
- $store[$key]->level = '';
- }
- Cache::add('level-'.$user_id, $store[$key]->level, $second);
- }
- }else{
- $store[$key]->level = '%';
- $store[$key]->warea = '';
- }
- if(!empty($val->user) && $val->user->level==2){
- if(Cache::has('company-'.$val->user->id)){
- $store[$key]->company=Cache::get('company-'.$val->user->id);
- }else{
- $company=DwbsUser::withTrashed()->where('id',$val->user->agent_id)->value('nickname');
- $store[$key]->company=$company;
- Cache::add('company-'.$val->user->id, $company, $second);
- }
- }
- if(!empty($val->user) && $val->user->level==1){
- if(Cache::has('company-'.$val->user->id)){
- $store[$key]->company=Cache::get('company-'.$val->user->id);
- }else{
- $agent=DwbsUser::withTrashed()->with('agent:id,nickname')->where('id',$val->user->agent_id)->first();
- if($agent->level==3){
- $company=$agent->nickname;
- }else{
- $company=$agent->agent->nickname;
- }
- $store[$key]->company=$company;
- Cache::put('company-'.$val->user->id, $company, $second);
- }
- }
- if(!empty($val->user) && $val->user->level==3){
- $store[$key]->company=$val->user->nickname;
- }
- }
- Log::info('MINUTE---'.microtime());
- return $this->success($store);
- }
- //总销量排名
- public function saleOrderByAll(Request $request)
- {
- Log::info('1---'.microtime());
- $page_index = $request->input('page_index');
- $page_size = $request->input('page_size');
- $num = $page_size * ($page_index - 1);
- $level = $request->input('level');
- $search_name = $request->input('search_name');
- $startime = $request->input('startime');
- $endtime = $request->input('endtime');
- $sort = $request->input('sort');
- $desc = $request->input('desc');
- $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- $admin_id = Auth::user()->id;
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- $store_ids = Store::query()->pluck('id')->toArray();
- } else {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
- }
- if ($level) {
- $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
- $user_ids = DwbsUser::where('level', $level)->whereIn('id',$agent_ids)->pluck('id');
- $level_store_ids = Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
- $store_ids=array_intersect($store_ids,$level_store_ids);
- }
- if ($search_name) {
- $search_name_store_ids = Store::where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id')->toArray();
- $store_ids=array_intersect($store_ids,$search_name_store_ids);
- }
- $where=[];
- if ($startime && $endtime) {
- $data->whereBetween('created_at', [$startime, $endtime]);
- $where=function($query)use($startime, $endtime){
- $query->whereBetween('created_at', [$startime, $endtime]);
- };
- }
- $datas= clone $data;
- $data1= clone $data;
- $order_ids = $data->whereIn('store_id', $store_ids)->pluck('id');
- Log::info('2---'.microtime());
- $order_details = $datas->with('store:id,name,img,phone,province,city,area')->whereIn('store_id',$store_ids)->groupBy('store_id')->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
- Log::info('2-1-0-'.microtime());
- // $count = $order_details->get()->count();
- if($desc==1){
- $desc='asc';
- }else{
- $desc='desc';
- }
- $list = $order_details->orderBy($sort,$desc)->orderBy('store_id')->get();
- Log::info('3---'.microtime());
- Log::info('4---'.microtime());
- $wareas = DwbsWarea::pluck('name', 'id');
- $user_ids = Store::pluck('user_id');
- $store_user_ids = Store::pluck('user_id', 'id')->toArray();
- $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
- $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
- Log::info('5---'.microtime());
- $list=OrderDetail::whereIn('store_id',$store_ids)->where($where)->groupBy('goods_id')->get();
- foreach ($list as $key => $val) {
- $second=mt_rand(2880,7200);
- if($val->total==0 && $val->account==0){
- $list[$key]->goods =[];
- }else{
- Cache::remember('orderBy_goods');
- $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id', $val->store_id)
- ->whereHas('order',function($query)use($where){
- $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
- })
- ->groupBy('goods_id')
- ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
- }
- if(array_key_exists($val->store_id,$store_user_ids)){
- $user_id = $store_user_ids[$val->store_id];
- if(Cache::get('warea-'.$user_id)){
- $list[$key]->warea = Cache::get('warea-'.$user_id);
- }else{
- $warea_id = $warea_user_ids[$user_id];
- $list[$key]->warea = $wareas[$warea_id];
- Cache::add('warea-'.$user_id, $wareas[$warea_id], $second);
- }
- if(Cache::get('level-'.$user_id)){
- $list[$key]->level = Cache::get('level-'.$user_id);
- }else{
- $list[$key]->level = $level_user[$user_id];
- Cache::add('level-'.$user_id, $level_user[$user_id], $second);
- }
- }else{
- $list[$key]->warea = '';
- $list[$key]->level = '';
- }
- }
- Log::info('6---'.microtime());
- $all['list'] = $list;
- return $this->success($all);
- }
- //导出销量排名
- public function exportSaleOrderBy(Request $request)
- {
- $level = $request->input('level');
- $search_name = $request->input('search_name');
- $startime = $request->input('start_time');
- $endtime = $request->input('end_time');
- $sort = $request->input('sort');
- $desc = $request->input('desc');
- $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
- $admin_wheres = $level_wheres = $search_wheres = [];
- $admin_id = Auth::user()->id;
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- $admin_wheres = [];
- } else {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
- $admin_wheres = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- if ($level) {
- $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
- $user_ids = DwbsUser::where('level', $level)->whereIn('id', $agent_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $user_ids)->pluck('id');
- $data->whereIn('store_id', $store_ids);
- $level_wheres = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- if ($search_name) {
- $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
- ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id');
- $data->whereIn('store_id', $store_ids);
- $search_wheres = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- if ($startime && $endtime) {
- $data->whereBetween('created_at', [$startime, $endtime]);
- }
- $order_ids = $data->pluck('id');
- if ($sort == 'hard') {
- $where['goods_id'] = 1;
- } elseif ($sort == 'simple') {
- $where['goods_id'] = 2;
- } elseif ($sort == 'old') {
- $where['goods_id'] = 3;
- } elseif ($sort == 'cotton') {
- $where['goods_id'] = 4;
- } else {
- $where = [];
- }
- $order_details = Store::where('status', 0)->where('is_apply', 3)
- ->where($admin_wheres)->where($level_wheres)->where($search_wheres)
- ->leftJoin('order_detail as od', function ($query) use ($order_ids, $where) {
- $query->on('od.store_id', '=', 'store.id')
- ->whereIn('order_id', $order_ids)
- ->where($where);
- })
- ->groupBy('store.id')
- ->select('store.id as store_id',DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
- if ($sort == 'total' || $sort == 'hard' || $sort == 'simple' || $sort == 'old' || $sort == 'cotton') {
- if ($desc == 1) {
- $order_details->OrderBy('total');
- } else {
- $order_details->OrderByDesc('total');
- }
- } elseif ($sort == 'account') {
- if ($desc == 1) {
- $order_details->OrderBy('account');
- } else {
- $order_details->OrderByDesc('account');
- }
- }
- $list = $order_details->get();
- $all['all_account'] = OrderDetail::whereIn('order_id', $order_ids)->sum('account');
- $all['all_num'] = OrderDetail::whereIn('order_id', $order_ids)->sum('num');
- $nums = OrderDetail::with('store:id,name,phone')->whereIn('order_id', $order_ids)->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id');
- $all['hard_num'] = $nums[1];
- $all['simple_num'] = $nums[2];
- $all['old_num'] = $nums[3];
- $all['cotton_num'] = $nums[4];
- $wareas = DwbsWarea::pluck('name', 'id');
- $user_ids = Store::pluck('user_id');
- $store_user_ids = Store::pluck('user_id', 'id');
- $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
- $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
- foreach ($list as $key => $val) {
- $list[$key] = Order::where('store_id', $val->store_id)->whereIn('id', $order_ids)
- ->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id', $val->store_id)->whereIn('order_id', $order_ids)->groupBy('goods_id')
- ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
- $detail = OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id', $val->store_id)->whereIn('order_id', $order_ids)->groupBy('goods_id', 'type', 'size')
- ->select('goods_id', 'type', 'size', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
- $list[$key]->detail = $detail;
- $list[$key]->store = Store::where('id',$val->store_id)->select('id','name','img','phone','province','city','area')->first();
- if(isset($val->store_id)){
- $user_id = $store_user_ids[$val->store_id];
- $warea_id = $warea_user_ids[$user_id];
- $list[$key]->warea = $wareas[$warea_id];
- $list[$key]->level = $level_user[$user_id];
- }else{
- $list[$key]->warea = '';
- $list[$key]->level = '';
- }
- }
- $all['list'] = $list;
- return $this->success($all);
- }
- //获取昨日销量排名
- public function yesterdayOrderBy(Request $request)
- {
- $page_index=$request->input('page_index')??1;
- $page_size=$request->input('page_size')??20;
- $num=$page_size*($page_index-1);
- $data['super_info'] = $this->orderDetail(1,$num,$page_size);
- $data['simple_info'] = $this->orderDetail(2,$num,$page_size);
- $data['old_info'] = $this->orderDetail(3,$num,$page_size);
- $data['cotton_info'] = $this->orderDetail(4,$num,$page_size);
- return $this->success($data);
- }
- public function orderDetail($id,$num,$index)
- {
- $date = date("Y-m-d", strtotime('-1 days'));
- $order_nos = Order::has('store')->where('is_pay', 1)
- ->whereDate('created_at', $date)
- ->whereIn('apply_cancel', [0, 1, 3])
- ->whereIn('is_refund', [0, 1])->pluck('order_no');
- $stores=Order::has('store')->where('is_pay', 1)
- ->whereDate('created_at', $date)
- ->whereIn('apply_cancel', [0, 1, 3])
- ->whereIn('is_refund', [0, 1])->pluck('store_id');
- $count = $order_details = Store::where('status', 0)->where('is_apply', 3)->whereIn('store_id',$stores)
- ->leftJoin('order_detail as od', function ($query) use ($order_nos, $id) {
- $query->on('od.store_id', '=', 'store.id')
- ->whereIn('od.order_no', $order_nos)
- ->where('od.goods_id',$id);
- })->groupBy('store.id')->get()->count();
- $order_details = Store::where('status', 0)->where('is_apply', 3)->whereIn('store_id',$stores)
- ->leftJoin('order_detail as od', function ($query) use ($order_nos, $id) {
- $query->on('od.store_id', '=', 'store.id')
- ->whereIn('od.order_no', $order_nos)
- ->where('od.goods_id',$id);
- })
- ->groupBy('store.id')
- ->select('store.id as store_id', 'store.name', 'store.phone', 'store.img', DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))
- ->orderByDesc('total')
- ->orderByDesc('account')
- ->orderBy('store.id')
- ->skip($num)->limit($index)
- ->get();
- $data['total']=$count;
- $data['list']=$order_details;
- return $data;
- }
- //总销售数据排名
- public function allOrderBy(Request $request)
- {
- $page_index=$request->input('page_index')??1;
- $page_size=$request->input('page_size')??20;
- $num=$page_size*($page_index-1);
- $count = Order::query()->whereHas('store')
- ->with(['store:id,name,phone,img'])
- ->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])
- ->groupBy('store_id')
- ->get()->count();
- $order = Order::query()->whereHas('store')
- ->with(['store:id,name,phone,img'])
- ->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])
- ->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))
- ->groupBy('store_id')
- ->orderByDesc('total')
- ->orderByDesc('account')
- ->skip($num)->limit($page_size)
- ->get();
- return $this->success_list($order,'',$count);
- }
- //各款式每日销量变化曲线图
- public function getSaleInfo(Request $request)
- {
- if ($request->input('startdate') && $request->input('enddate')) {
- $startdate = $request->input('startdate');
- $enddate = $request->input('enddate');
- } else {
- $startdate = date("Y-m-d", strtotime('-7 days'));
- $enddate = date("Y-m-d");
- }
- $dates = $this->getDateFromRange($startdate, $enddate);
- $list = [];
- foreach ($dates as $key => $val) {
- $order_nos = Order::whereDate('created_at', $val)->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('order_no');
- $list['super'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 1)
- ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- }
- foreach ($dates as $key => $val) {
- $order_nos = Order::whereDate('created_at', $val)->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('order_no');
- $list['simple'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 2)
- ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- }
- foreach ($dates as $key => $val) {
- $order_nos = Order::whereDate('created_at', $val)->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('order_no');
- $list['old'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 3)
- ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- }
- foreach ($dates as $key => $val) {
- $order_nos = Order::whereDate('created_at', $val)->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('order_no');
- $list['cotton'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 4)
- ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- }
- return $this->success($list);
- }
- function getDateFromRange($startdate, $enddate)
- {
- $stimestamp = strtotime($startdate);
- $etimestamp = strtotime($enddate);
- // 计算日期段内有多少天
- $days = ($etimestamp - $stimestamp) / 86400 + 1;
- // 保存每天日期
- $date = array();
- for ($i = 0; $i < $days; $i++) {
- $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
- }
- return $date;
- }
- //获取每日新增人数
- public function getEveryAddAgent(Request $request)
- {
- if ($request->input('startdate') && $request->input('enddate')) {
- $startdate = $request->input('startdate');
- $enddate = $request->input('enddate');
- } else {
- $startdate = date("Y-m-d", strtotime('-7 days'));
- $enddate = date("Y-m-d");
- }
- $dates = $this->getDateFromRange($startdate, $enddate);
- $list = [];
- foreach ($dates as $key => $val) {
- $list[$val] = Store::whereDate('created_at', $val)->where('is_apply',3)->count();
- }
- return $this->success($list);
- }
- //获取每日新增人数
- public function getEveryAddAgentSort(Request $request)
- {
- $date=$request->input('date');
- $user_ids=Store::whereDate('created_at', $date)->where('is_apply',3)->pluck('user_id');
- $list=DwbsUser::with('warea:id,name')->whereIn('id',$user_ids)->groupBy('warea_id')
- ->select('warea_id',DB::raw('count(id) as count'))->orderByDesc('count')->get();
- return $this->success($list);
- }
- //获取政委每日新增人数变化曲线图
- public function getEveryAddAgentLine(Request $request)
- {
- if ($request->input('startdate') && $request->input('enddate')) {
- $startdate = $request->input('startdate');
- $enddate = $request->input('enddate');
- } else {
- $startdate = date("Y-m-d", strtotime('-7 days'));
- $enddate = date("Y-m-d");
- }
- $dates = $this->getDateFromRange($startdate, $enddate);
- $list = [];
- $warea=DwbsWarea::all();
- foreach($warea as $k=>$v){
- foreach ($dates as $key => $val) {
- $user_ids=Store::whereDate('created_at', $val)->where('is_apply',3)->pluck('user_id');
- $list[$v->name][$val]=DwbsUser::whereIn('id',$user_ids)->where('warea_id',$v->id)->count();
- }
- }
- return $this->success($list);
- }
- public function getHistoryTotal(Request $request)
- {
- $startime = $request->input('start_time');
- $endtime = $request->input('end_time');
- $where = $wheres = [];
- if ($startime && $endtime) {
- $where = function ($query) use ($startime, $endtime) {
- $query->whereBetween('created_at', [$startime, $endtime]);
- };
- $wheres = function ($query) use ($startime, $endtime) {
- $query->whereBetween('cancel_at', [$startime, $endtime]);
- };
- }
- $data['all'] = Order::where('is_pay', 1)->where($where)
- ->select(DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- $data['refund'] = OrderRefund::where($where)->where('status', 1)
- ->select(DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- $data['cancel'] = Order::where($wheres)->where('apply_cancel', 2)
- ->select(DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- return $this->success($data);
- }
- public function getSkuHistoryNum(Request $request)
- {
- $startdate = $request->input('startdate');
- $enddate = $request->input('enddate');
- $where = $wheres = [];
- if ($startdate && $enddate) {
- $startime = date("Y-m-d 00:00:00", strtotime($startdate));
- $endtime = date("Y-m-d 23:59:59", strtotime($enddate));
- $where = function ($query) use ($startime, $endtime) {
- $query->whereBetween('created_at', [$startime, $endtime]);
- };
- }
- $data = OrderDetail::has('goods')->whereHas('order', function ($query) {
- $query->where('is_pay', 1);
- })->with('goods:id,name,main_attr')->where($where)
- ->select('goods_id', DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))
- ->groupBy('goods_id')->orderBy('goods_id')->get();
- return $this->success($data);
- }
- //获取款式排行
- public function getSkuOrderBy()
- {
- $goods_sku=GoodSku::whereHas('goods',function($query){$query->where('is_shelves', 1);})->with('goods:id,name,main_attr')->get();
- foreach($goods_sku as $key=>$val){
- $goods_sku[$key]->detail=OrderDetail::where('sku_id',$val->id)->whereHas('order',function($query){
- $query->where('is_pay', 1)->whereIn('is_refund', [0, 1])->whereIn('apply_cancel', [0, 1, 3]);
- })->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
- }
- return $this->success($goods_sku);
- }
- //三天销量为0
- public function get3DaysSales0(Request $request)
- {
- $start_time = date("Y-m-d 00:00:00", strtotime('-3 days'));
- $end_time = date("Y-m-d H:i:s");
- $page_index = $request->input('page_index');
- $page_size = $request->input('page_size');
- $num = $page_size * ($page_index - 1);
- $admin_id = Auth::user()->id;
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- $where = [];
- } else {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
- $where = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- $count = Store::where('store.status', 0)->where($where)
- ->leftJoin('order as o', function ($query) use ($start_time, $end_time) {
- $query->on('store.id', '=', 'o.store_id')->where('o.is_pay', 1)
- ->whereIn('o.apply_cancel', [0, 1, 3])->whereIn('o.is_refund', [0, 1])
- ->whereBetween('o.created_at', [$start_time, $end_time]);
- })->select('store.id', 'store.name', 'store.img', 'store.phone', DB::Raw('sum(ifnull(o.total,0)) as total'))
- ->groupBy('store.id')
- ->having('total', '=', 0)->get()->count();
- $list = Store::where('store.status', 0)->where($where)
- ->leftJoin('order as o', function ($query) use ($start_time, $end_time) {
- $query->on('store.id', '=', 'o.store_id')->where('o.is_pay', 1)
- ->whereIn('o.apply_cancel', [0, 1, 3])->whereIn('o.is_refund', [0, 1])
- ->whereBetween('o.created_at', [$start_time, $end_time]);
- })->select('store.id', 'store.name', 'store.img', 'store.phone', DB::Raw('sum(ifnull(o.total,0)) as total'))
- ->groupBy('store.id')
- ->having('total', '=', 0)
- ->orderBy('store.id')
- ->skip($num)->take($page_size)
- ->get();
- return $this->success_list($list, '', $count);
- }
- //三天销量为0
- public function export3DaysSales0(Request $request)
- {
- $start_time = date("Y-m-d 00:00:00", strtotime('-3 days'));
- $end_time = date("Y-m-d H:i:s");
- $admin_id = Auth::user()->id;
- $admin = Admin::where('id', $admin_id)->first();
- if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
- $where = [];
- } else {
- $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
- $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
- $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
- $where = function ($query) use ($store_ids) {
- $query->whereIn('store.id', $store_ids);
- };
- }
- $list = Store::where('store.status', 0)->where($where)
- ->leftJoin('order as o', function ($query) use ($start_time, $end_time) {
- $query->on('store.id', '=', 'o.store_id')->where('o.is_pay', 1)
- ->whereIn('o.apply_cancel', [0, 1, 3])->whereIn('o.is_refund', [0, 1])
- ->whereBetween('o.created_at', [$start_time, $end_time]);
- })->select('store.id', 'store.name', 'store.img', 'store.phone', DB::Raw('sum(ifnull(o.total,0)) as total'))
- ->groupBy('store.id')
- ->having('total', '=', 0)->orderBy('store.id')->get();
- return $this->success($list);
- }
- //同步订单和订单详情用户店铺信息
- public function asd(Request $request)
- {
- // OrderCancel::where('reason','代理取消订单')->update(['type'=>2]);//->orWhere('reason','<>','代理取消订单')
- Cache::tags(['store','agent'])->put('bar', 'baz123', 60); // 10 分钟
- Cache::tags('stores')->flush();
- return Cache::tags(['store','agent'])->get('bar');
- $num=$request->input('num');
- return $this->success(is_numeric($num));
- // return $this->success(is_nan($num ));
- // return '123';
- // $order=Order::query()->withTrashed()->whereNull('snapshot')->limit(500)->orderBy('id')->get();
- //// return $order;
- // if(count($order)==0){
- // return '已执行完成';
- // }
- // foreach($order as $key=>$val){
- // $goods=[];
- // $order_details=OrderDetail::where('order_id',$val->id)->get();
- // foreach($order_details as $k=>$v){
- // $sku_info=[];
- // $goods_info=Goods::where('id',$v->goods_id)->first();
- // if(!array_key_exists($v->goods_id,$goods)){
- // $goods[$v->goods_id]['name']=$goods_info->name;
- // $goods[$v->goods_id]['price']=$goods_info->price;
- // $goods[$v->goods_id]['img']=$goods_info->img;
- // $goods[$v->goods_id]['main_attr']=$goods_info->main_attr;
- // $goods[$v->goods_id]['sku']=[];
- // }
- // $sku_info['origin_price']=$goods_info->price;
- // $sku_info['price']=$v->price;
- // $sku_info['sku_id']=$v->sku_id;
- // $sku_info['num']=$v->num;
- // $sku_info['size']=$v->size;
- // $sku_info['type']=$v->type;
- // array_push($goods[$v->goods_id]['sku'],$sku_info);
- // OrderDetail::where('id',$v->id)->update([
- // 'origin_price'=>$goods_info->price
- // ]);
- // }
- // Order::withTrashed()->where('id',$val->id)->update([
- // 'snapshot'=>json_encode($goods)
- // ]);
- // }
- //
- //
- //return '执行了'.count($order).'条,最后一条订单id:'.$order[count($order)-1]->id;
- // $update = DB::table('goods_sku')->where('id',$skuId)->update(['stock_daily'=>DB::raw('stock_daily_max')]);
- // $orders=Order::where('is_pay',1)->where('self_receive',1)->where('apply_cancel','<>',2)->update([
- // 'express_receive_time'=>DB::raw('updated_at'),
- // ]);
- //
- //return $orders;
- $date=date("Y-m-d H:i:s",strtotime('-1 days'));
- Log::info($date);
- // $date=date("Y-m-d H:i:s",strtotime('-1 days'));//测试期间设为一天
- // $date=date("Y-m-d H:i:s",strtotime('-1 hours'));
- $orders=Order::with('address')->whereNotNull('express_info')
- // ->where('status',2)->where('express_state','3')
- ->where('express_receive_time','<',$date)->orderByDesc('express_receive_time')->select('order_no','express_receive_time')->get();
- return $orders;
- $orders=Order::whereNotNull('express_info')->where('express_receive_time','0000-00-00 00:00:00')->get();
- foreach($orders as $key=>$val){
- $express=\GuzzleHttp\json_decode($val->express_info,true);
- foreach($express['Traces'] as $k=>$v){
- $express['Traces'][$k]['AcceptTime']=date("Y-m-d H:i:s",substr($v['AcceptTime'],0,strlen($v['AcceptTime'])-3));
- }
- // return $express;
- // return $this->success(\GuzzleHttp\json_encode($express));
- order::where('id',$val->id)->update([
- 'express_info'=>\GuzzleHttp\json_encode($express),
- 'express_receive_time'=>array_pop($express['Traces'])['AcceptTime']
- ]);
- // $orders[$key]=$express;
- }
- // return $orders;
- return Order::whereNotNull('express_info')->where('express_receive_time','0000-00-00 00:00:00')->get();
- // $apply=Store::query()->groupBy('is_apply')->select(DB::raw('count(id) as num'),'is_apply')->pluck('num','is_apply');
- //return $apply;
- $second=10;
- // Cache::put('type', 1513699, $second);
- // if(Cache::get('type')){
- // return Cache::get('type');
- // }else{
- // return '111161';
- // }
- // $str=Store::where('id','99')->value('name');
- // $str = preg_replace_callback(
- // '/./u',
- // function (array $match) {
- // return strlen($match[0]) >= 4 ? '' : $match[0];
- // },
- // $str);
- // return $str;
- $number = 2;
- $str = "Shanghai";
- $txt = sprintf("There are %u million cars in %s.",$number,$str);
- // return $txt;
- $num='a';
- $name='pig';
- $txt=sprintf("there is %s an %s",$num,$name);
- return $txt;
- $store_name=Store::where('id','7990')->value('name');//7353
- $store_name='VVV(11111111111122)))';
- $store_name=str_replace(' ','',$store_name);
- $store_name=preg_replace("/[^\x{4e00}-\x{9fa5}^0-9^A-Z^a-z^(]+/u", '', $store_name);
- $store_name=mb_substr($store_name,0,16);
- if(mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $store_name))==0){
- return $this->error('450001','该店铺名称格式不符需重置');
- }
- return $this->success($store_name);
- $phones= DwbsUser::where('level',3)->pluck('mobile');
- $list=Store::whereIn('phone',$phones)->select('username','idCard')->get();
- return Excel::download(new CompanyInfoExport($list), 'company.xlsx');
- return '123';
- // $id=$request->input('id');
- // $integral=Integral::where('store_id',$id)->sum('integral_double');
- //return $integral;
- //return '123123';
- // foreach($integral as $key=>$val){
- // Store::where('id',$val->id)->update(['integral'=>$val->integral]);
- // }
- //
- // return $integral;
- $integral = Store::leftJoin('integral as inte', function ($query) {
- $query->on('store.id', '=', 'inte.store_id');
- })->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')
- ->get();
- // return $integral;
- foreach ($integral as $key => $val) {
- if ($val->integral != Store::where('id', $val->id)->value('integral')) {
- Log::info('123-------789');
- info($val->id . '--' . $val->integral . '--' . Store::where('id', $val->id)->value('integral'));
- // Store::where('id',$val->id)->update(['integral'=>$val->integral]);
- };
- }
- return $integral;
- return '店铺积分汇总结束';
- $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.pay_at', $date);
- };
- $cellData = Order::where($where)->leftjoin('store', 'order.store_id', '=', 'store.id')
- ->select('store.name', 'store.phone', 'store.img', 'store.user_id', 'store_id', DB::raw('sum(total) as total'), DB::raw('sum(account) as account'))
- ->groupBy('store_id')->having('total', '>=', 2)->orderByDesc('total')->orderBy('store_id')->get();//->orderByDesc('account')
- foreach ($cellData as $key => $val) {
- $level = DwbsUser::where('id', $val->user_id)->value('level');
- $cellData[$key]->level = $level;
- }
- return Excel::download(new IntegralExport($cellData), 'users1.xlsx');
- $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);
- };
- $cellData = Order::where($where)->leftjoin('store', 'order.store_id', '=', 'store.id')
- ->select('store.name', 'store.phone', 'store.img', 'store.user_id', 'store_id', DB::raw('sum(total) as total'), DB::raw('sum(account) as account'))
- ->groupBy('store_id')->having('total', '>=', 2)->orderByDesc('total')->get()->toArray();
- foreach ($cellData as $key => $val) {
- $level = DwbsUser::where('id', $val['user_id'])->value('level');
- $cellData[$key]['level'] = $level;
- }
- return $this->success($cellData);
- ini_set('memory_limit', '500M');
- set_time_limit(0);//设置超时限制为0分钟
- // $cellData = MemberFollow::select('xt_name','sex','face')->limit(5)->get()->toArray();
- // $cellData[0] = array('昵称','shouji','头像','头像','头像','头像');
- // for($i=0;$i<count($cellData);$i++){
- // $cellData[$i] = array_values($cellData[$i]);
- // $cellData[$i][0] = str_replace('=',' '.'=',$cellData[$i][0]);
- // }
- // //dd($cellData);
- // Excel::create('用户信息',function($excel) use ($cellData){
- // $excel->sheet('score', function($sheet) use ($cellData){
- // $sheet->rows($cellData);
- // });
- // })->export('xls');
- return '123122222222';
- $orders = OrderDetail::orderBy('id')->skip(0)->take(1000)->get();
- // $orders=OrderDetail::all();
- foreach ($orders as $key => $val) {
- $store = Store::where('id', $val->store_id)->first();
- if (empty($store)) {
- Log::info('失败商品' . $val->order_no . '%%%' . $val->store_id);
- continue;
- }
- $agent = DwbsUser::where('mobile', $store->phone)->first();
- if ($agent->level == 1) {
- $price = Goods::where('id', $val->goods_id)->value('super_price');
- // return '0@'.$price*$val->num;
- OrderDetail::where('id', $val->id)->update([
- 'cost_price' => $price,
- 'cost_account' => $price * $val->num
- ]);
- }
- if ($agent->level == 2) {
- // return '1@'.$price*$val->num;
- $price = Goods::where('id', $val->goods_id)->value('top_price');
- OrderDetail::where('id', $val->id)->update([
- 'cost_price' => $price,
- 'cost_account' => $price * $val->num
- ]);
- }
- if ($agent->level == 3) {
- $price = Goods::where('id', $val->goods_id)->value('crown_price');
- OrderDetail::where('id', $val->id)->update([
- 'cost_price' => $price,
- 'cost_account' => $price * $val->num
- ]);
- // return '2@'.$price*$val->num;
- }
- // return $price*$val->num;
- }
- return $this->success('true');
- // $orders=Order::where('is_pay',1)->get();
- // $oo=[];
- // $nn=0;
- // foreach($orders as $key=>$val){
- // $account= OrderDetail::where('order_no',$val->order_no)->sum('account');
- // if($val->account != $account){
- // $details=OrderDetail::where('order_no',$val->order_no)->get();
- // foreach($details as $k=>$v){
- // $goods=Goods::where('id',$v->goods_id)->first();
- // if($goods){
- // OrderDetail::where('id',$v->id)->update([
- // 'price'=>$goods->vip_price,
- // 'account'=>$v->num*$goods->vip_price,
- // ]);
- // }
- // }
- //
- // $data['order_no']=$val->order_no;
- // $data['account']=$val->account;
- // $data['store_id']=$val->store_id;
- // $data['total']=$val->total;
- // $data['acc']=$account;
- // $nn=$nn+$account-$val->account;
- // $data['ann']=$nn;
- // $data['created_at']=$val->created_at;
- // array_push($oo,$data);
- // }
- // }
- // return $oo;
- return Order::where('status', 3)->where('is_finish', 0)->select('id', 'order_no', 'status', 'is_finish', 'created_at', 'apply_cancel', 'is_refund', 'pay_at')->get();
- return OrderFinishFails::where('status', 0)->get();
- $order_nos = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('order_no');
- // $order_details=OrderDetail::with('store:id,name,phone')->whereIn('order_no',$order_nos)->groupBy('store_id')
- // ->select('store_id',DB::raw('ifnull(sum(num),0) as total'),DB::raw('ifnull(sum(account),0) as account'))
- // ->where('goods_id',3)->get();
- $order_details = Store::leftJoin('order_detail as od', function ($query) use ($order_nos) {
- $query->on('od.store_id', '=', 'store.id')
- ->whereIn('order_no', $order_nos)
- ->where('goods_id', 3);
- })
- ->groupBy('store.id')
- ->select('store.id', DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))
- ->get();
- return count($order_details);
- // $order=Order::select('id','order_no','user_id','store_id')->get();
- //// return $order;
- // foreach($order as $key=>$val){
- // OrderDetail::where('order_no',$val->order_no)->update([
- // 'user_id'=>$val->user_id,
- // 'store_id'=>$val->store_id,
- // ]);
- // }
- }
- public function exportSaleOrderBys(Request $request){
- $level=$request->input('level');
- $search_name=$request->input('search_name');
- $startime=$request->input('start_time');
- $endtime=$request->input('end_time');
- $sort=$request->input('sort');
- $desc=$request->input('desc');
- $data=Order::has('store')->where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1]);
- $admin_wheres=$level_wheres=$search_wheres=[];
- $admin_id=Auth::user()->id;
- $admin=Admin::where('id',$admin_id)->first();
- if($admin && in_array($admin->role_id,[22,24])){//超级管理员
- $admin_wheres=[];
- }else{
- $warea_ids=DwbsWarea::where('admin_id',$admin_id)->pluck('id');
- $agent_ids=DwbsUser::whereIn('warea_id',$warea_ids)->pluck('id');
- $store_ids=Store::whereIn('user_id',$agent_ids)->pluck('id');
- $admin_wheres=function($query)use($store_ids){
- $query->whereIn('store.id',$store_ids);
- };
- }
- if($level){
- $agent_ids=Store::where('status',0)->where('is_apply',3)->pluck('user_id');
- $user_ids=DwbsUser::where('level',$level)->whereIn('id',$agent_ids)->pluck('id');
- $store_ids=Store::whereIn('user_id',$user_ids)->pluck('id');
- $data->whereIn('store_id',$store_ids);
- $level_wheres=function($query)use($store_ids){
- $query->whereIn('store.id',$store_ids);
- };
- }
- if($search_name){
- $store_ids=Store::where('name','like','%'.$search_name.'%')
- ->orWhere('phone','like','%'.$search_name.'%')->pluck('id');
- $data->whereIn('store_id',$store_ids);
- $search_wheres=function($query)use($store_ids){
- $query->whereIn('store.id',$store_ids);
- };
- }
- if($startime && $endtime){
- $data->whereBetween('created_at',[$startime,$endtime]);
- }
- $order_nos=$data->pluck('order_no');
- if($sort=='hard'){
- $where['goods_id']=1;
- }elseif($sort=='simple'){
- $where['goods_id']=2;
- }elseif($sort=='old'){
- $where['goods_id']=3;
- }elseif($sort=='cotton'){
- $where['goods_id']=4;
- }else{
- $where=[];
- }
- $order_details=Store::where('status',0)->where('is_apply',3)
- ->where($admin_wheres)->where($level_wheres)->where($search_wheres)
- ->leftJoin('order_detail as od',function($query)use($order_nos,$where){
- $query->on('od.store_id','=','store.id')
- ->whereIn('order_no',$order_nos)
- ->where($where);
- })
- ->groupBy('store.id')
- ->select('store.id as store_id',DB::raw('ifnull(sum(num),0) as total'),DB::raw('ifnull(sum(account),0) as account'));
- if($sort=='total' || $sort=='hard' || $sort=='simple' || $sort=='old' || $sort=='cotton'){
- if($desc==1){
- $order_details->OrderBy('total');
- }else{
- $order_details->OrderByDesc('total');
- }
- }elseif($sort=='account'){
- if($desc==1){
- $order_details->OrderBy('account');
- }else{
- $order_details->OrderByDesc('account');
- }
- }
- $list=$order_details->get();
- $all['all_account']=OrderDetail::whereIn('order_no',$order_nos)->sum('account');
- $all['all_num']=OrderDetail::whereIn('order_no',$order_nos)->sum('num');
- $all['hard_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',1)->whereIn('order_no',$order_nos)->sum('num');
- $all['simple_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',2)->whereIn('order_no',$order_nos)->sum('num');
- $all['old_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',3)->whereIn('order_no',$order_nos)->sum('num');
- $all['cotton_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',4)->whereIn('order_no',$order_nos)->sum('num');
- foreach($list as $key=>$val){
- $order=Order::with('store')->where('store_id',$val->store_id)->whereIn('order_no',$order_nos)
- ->select(DB::raw('ifnull(sum(total),0) as total'),DB::raw('ifnull(sum(account),0) as account'))->first();
- $list[$key]=$order;
- if($order->total==0 && $order->account==0){
- $list[$key]->goods=OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id',$val->store_id)->whereIn('order_no',$order_nos)->groupBy('goods_id')
- ->select('goods_id',DB::raw('ifnull(sum(num),0) as num'),DB::raw('ifnull(sum(account),0) as account'))->get();
- $detail=OrderDetail::with('goods:id,name,main_attr')
- ->where('store_id',$val->store_id)->whereIn('order_no',$order_nos)->groupBy('goods_id','type','size')
- ->select('goods_id','type','size',DB::raw('ifnull(sum(num),0) as num'),DB::raw('ifnull(sum(account),0) as account'))->get();
- $list[$key]->detail=$detail;
- }else{
- $list[$key]->goods=[];
- $list[$key]->detail=[];
- }
- $second=mt_rand(2880,7200);
- if(Cache::has('level-'.$val->store->user_id)){
- $list[$key]->level=Cache::get('level-'.$val->store->user_id);
- }else{
- // $user_id=Store::where('id',$val->store_id)->value('user_id');
- $level=DwbsUser::where('id',$val->store->user_id)->value('level');
- $list[$key]->level=$level;
- Cache::add('level-'.$val->store->user_id, $level, $second);
- }
- if(Cache::has('store-'.$val->store_id)){
- $list[$key]->store=Cache::get('store-'.$val->store->user_id);
- }else{
- $store=Store::where('id',$val->store_id)->select('id','name','phone','province','city','area')->first();
- $list[$key]->store=$store;
- Cache::add('store-'.$val->store_id, $store, $second);
- }
- }
- $all['list']=$list;
- return $this->success($all);
- }
- }
|