IndexController.php 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Address;
  5. use App\Models\Admin;
  6. use App\Models\Bank123;
  7. use App\Models\Comment;
  8. use App\Models\GoodSku;
  9. use App\Models\OrderCancel;
  10. use App\Models\OrderDetail;
  11. use App\Models\OrderFinishFails;
  12. use App\Models\OrderRefund;
  13. use App\Models\OrderW;
  14. use App\Models\User;
  15. use App\Models\Store;
  16. use App\Models\Order;
  17. use App\Models\Goods;
  18. use App\Models\Region;
  19. use App\Models\Agent\AgentDwbsUser;
  20. use App\Models\Agent\AgentDwbsWarea;
  21. use App\Models\Agent\AgentDwbsAdmin;
  22. use App\Models\Agent\AgentStore;
  23. use App\Models\DwbsUser;
  24. use App\Models\DwbsWarea;
  25. use App\Models\Integral;
  26. use App\Exports\IntegralExport;
  27. use App\Exports\CompanyInfoExport;
  28. use Illuminate\Support\Facades\Cache;
  29. use Maatwebsite\Excel\Facades\Excel;
  30. use function Couchbase\defaultDecoder;
  31. use Illuminate\Http\Request;
  32. use Carbon\Carbon;
  33. use Illuminate\Support\Facades\Auth;
  34. use Illuminate\Support\Facades\DB;
  35. use Illuminate\Support\Facades\Log;
  36. class IndexController extends Controller
  37. {
  38. public function setBank(Request $request)
  39. {
  40. $region = Region::where('id', '>', '700000')->get();
  41. foreach ($region as $key => $val) {
  42. $banks = Bank123::where('city', 'like', '%' . $val->sname . '%')->first();
  43. if ($banks) {
  44. Log::info($val->sname);
  45. }
  46. }
  47. }
  48. //获取首页信息
  49. public function index()
  50. {
  51. $user_count = User::whereNotNull('openId')->orWhereNotNull('phone')->count();
  52. $store_count = Store::where('is_apply', 3)->count();
  53. $goods_count = Goods::where('is_shelves', 1)->count();
  54. $order_sale = Order::where('is_pay', 1)->sum('account');
  55. $refund_no_auth = OrderRefund::where('status', 0)->count();
  56. $list['user_count'] = $user_count;
  57. $list['store_count'] = $store_count;
  58. $list['goods_count'] = $goods_count;
  59. $list['order_sale'] = $order_sale;
  60. $list['refund_no_auth'] = $refund_no_auth;
  61. $list['expire']=date("YmdHis",strtotime('+120 second'));
  62. $list['ccc']=Order::where('apply_cancel',2)->whereColumn('cancel_at','<','pay_at')->get();
  63. return $this->success($list);
  64. }
  65. //获取团队排行榜
  66. public function getWareaOrderBy()
  67. {
  68. $store_ids = Order::where('is_pay', 1)
  69. ->groupBy('store_id')
  70. ->pluck('store_id');
  71. $user_ids = Store::whereIn('id', $store_ids)->pluck('user_id');
  72. $warea_ids = DwbsUser::whereIn('users.id', $user_ids)
  73. ->join('warea', 'warea.id', '=', 'users.warea_id')->distinct('user.warea_id')
  74. ->orderBy('warea.name')
  75. ->pluck('users.warea_id', 'warea.name');
  76. $list = [];
  77. foreach ($warea_ids as $key => $val) {
  78. $data = [];
  79. $data['warea'] = $key;
  80. // $list[$key]['warea']=DwbsWarea::where('id',$val)->value('name');
  81. $uids = DwbsUser::whereIn('id', $user_ids)->where('warea_id', $val)->pluck('id');
  82. $sids = Store::whereIn('user_id', $uids)->pluck('id');
  83. $data['account'] = Order::where('is_pay', 1)
  84. ->whereIn('store_id', $sids)->sum('account');
  85. $data['num'] = Order::where('is_pay', 1)
  86. ->whereIn('store_id', $sids)->sum('total');
  87. $list[] = $data;
  88. }
  89. return $this->success($list);
  90. }
  91. //获取省级行政单位各个main_Attr销售信息
  92. public function getProvinceSalesAttr($id = 700000)
  93. {
  94. $province = Region::where('level', 1)->where('id', '<', $id)->orderBy('id')->pluck('name');
  95. $goods = Goods::where('is_shelves', 1)->select('id', 'name')->get();
  96. $list = [];
  97. foreach ($province as $key => $val) {
  98. $pro = $val;
  99. switch ($province) {
  100. case '北京':
  101. $pro = '北京市';
  102. break;
  103. case '天津':
  104. $pro = '天津市';
  105. break;
  106. case '上海':
  107. $pro = '上海市';
  108. break;
  109. case '重庆':
  110. $pro = '重庆市';
  111. break;
  112. }
  113. $addr_ids = Address::where('province', $pro)->pluck('id');
  114. $order_nos = Order::whereHas('address', function ($query) use ($addr_ids) {
  115. $query->whereIn('id', $addr_ids);
  116. })->where('is_pay', 1)->pluck('order_no');
  117. $data = OrderDetail::whereIn('order_no', $order_nos);
  118. $data->with('goods:id,name,main_attr');
  119. $data->select('goods_id', DB::raw('sum(num) as total'), DB::raw('sum(account) as amount'));
  120. $data->groupBy('goods_id');
  121. $res = $data->get();
  122. foreach ($goods as $keys => $value) {
  123. if (count($res) > 0) {
  124. $result = 0;
  125. foreach ($res as $k => $v) {
  126. if ($value->name == $v->goods->name) {
  127. $result += 1;
  128. $list[$keys]['data'][$key] = $v->total;
  129. }
  130. }
  131. if ($result == 0) {
  132. $list[$keys]['data'][$key] = 0;
  133. }
  134. } else {
  135. $list[$keys]['data'][$key] = 0;
  136. }
  137. $list[$keys]['name'] = $value->name;
  138. }
  139. }
  140. return $this->success($list);
  141. }
  142. //获取所有省级单位
  143. public function getProvinceList()
  144. {
  145. $province = Region::where('level', 1)->where('id', '<', 700000)->orderBy('id')->pluck('name', 'id');
  146. return $this->success($province);
  147. }
  148. //支付比例
  149. public function getAgentPayRate(Request $request)
  150. {
  151. $page_index=$request->input('page_index');
  152. $page_size=$request->input('page_size');
  153. $num=$page_size*($page_index-1);
  154. // $store_ids = Order::where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('store_id');
  155. $total=Store::where('status', 0)->where('is_test','0')->whereHas('order',function($query){
  156. $query->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  157. })->count();
  158. $stores = Store::where('status', 0)->where('is_test','0')->whereHas('order',function($query){
  159. $query->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  160. })->select('id', 'user_id','name','phone')->orderBy('id')->skip($num)->take($page_size)->get();
  161. foreach ($stores as $key => $val) {
  162. // $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');
  163. $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');
  164. $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');
  165. // $stores[$key]->agent = DwbsUser::where('id', $val->user_id)->select('nickname', 'mobile')->first();
  166. }
  167. return $this->success_list($stores,'',$total);
  168. }
  169. //销量排名
  170. public function getSaleOrderBy(Request $request)
  171. {
  172. Log::info('1---'.microtime());
  173. $page_index = $request->input('page_index');
  174. $page_size = $request->input('page_size');
  175. $num = $page_size * ($page_index - 1);
  176. $level = $request->input('level');
  177. $search_name = $request->input('search_name');
  178. $startime = $request->input('startime');
  179. $endtime = $request->input('endtime');
  180. $sort = $request->input('sort');
  181. $desc = $request->input('desc');
  182. $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  183. $admin_wheres = $level_wheres = $search_wheres = [];
  184. $admin_id = Auth::user()->id;
  185. $admin = Admin::where('id', $admin_id)->first();
  186. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  187. $admin_wheres = [];
  188. } else {
  189. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  190. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  191. $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
  192. $admin_wheres = function ($query) use ($store_ids) {
  193. $query->whereIn('store.id', $store_ids);
  194. };
  195. }
  196. if ($level) {
  197. $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
  198. $user_ids = DwbsUser::where('level', $level)->whereIn('id', $agent_ids)->pluck('id');
  199. $store_ids = Store::whereIn('user_id', $user_ids)->pluck('id');
  200. $data->whereIn('store_id', $store_ids);
  201. $level_wheres = function ($query) use ($store_ids) {
  202. $query->whereIn('store.id', $store_ids);
  203. };
  204. }
  205. if ($search_name) {
  206. $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
  207. ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id');
  208. $data->whereIn('store_id', $store_ids);
  209. $search_wheres = function ($query) use ($store_ids) {
  210. $query->whereIn('store.id', $store_ids);
  211. };
  212. }
  213. if ($startime && $endtime) {
  214. $data->whereBetween('created_at', [$startime, $endtime]);
  215. }
  216. $order_ids = $data->pluck('id');
  217. // if ($sort == 'hard') {
  218. // $where['goods_id'] = 1;
  219. // } elseif ($sort == 'simple') {
  220. // $where['goods_id'] = 2;
  221. // } elseif ($sort == 'old') {
  222. // $where['goods_id'] = 3;
  223. // } elseif ($sort == 'cotton') {
  224. // $where['goods_id'] = 4;
  225. // } else {
  226. // $where = [];
  227. // }
  228. Log::info('2---'.microtime());
  229. $store_ids=Store::where('status', 0)->where('is_apply', 3)
  230. ->where($admin_wheres)->where($level_wheres)->where($search_wheres)->pluck('id');
  231. // $order_details =OrderDetail::query()
  232. // ->whereIn('store_id',$store_ids)
  233. // ->whereIn('order_id', $order_ids)
  234. // ->where($where)
  235. // ->groupBy('store_id')
  236. // ->select('store_id', DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
  237. $order_details = Store::where('status', 0)->where('is_apply', 3)
  238. ->where($admin_wheres)->where($level_wheres)->where($search_wheres)
  239. ->leftJoin('order_detail as od', function ($query) use ($order_ids, $where) {
  240. $query->on('od.store_id', '=', 'store.id')
  241. ->whereIn('order_id', $order_ids)
  242. ->where($where);
  243. })
  244. ->groupBy('store.id')
  245. ->select('store.id as store_id',DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
  246. if ($sort == 'total' || $sort == 'hard' || $sort == 'simple' || $sort == 'old' || $sort == 'cotton') {
  247. if ($desc == 1) {
  248. $order_details->OrderBy('total');
  249. } else {
  250. $order_details->OrderByDesc('total');
  251. }
  252. } elseif ($sort == 'account') {
  253. if ($desc == 1) {
  254. $order_details->OrderBy('account');
  255. } else {
  256. $order_details->OrderByDesc('account');
  257. }
  258. }
  259. $count = $order_details->get()->count();
  260. $list = $order_details->skip($num)->limit($page_size)->get();
  261. // Log::info('3---'.microtime());
  262. $all['all_account'] = OrderDetail::whereIn('order_id', $order_ids)->sum('account');
  263. $all['all_num'] = OrderDetail::whereIn('order_id', $order_ids)->sum('num');
  264. $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');
  265. $all['hard_num'] = $nums[1];
  266. $all['simple_num'] = $nums[2];
  267. $all['old_num'] = $nums[3];
  268. $all['cotton_num'] = $nums[4];
  269. Log::info('4---'.microtime());
  270. $wareas = DwbsWarea::pluck('name', 'id');
  271. $user_ids = Store::pluck('user_id');
  272. $store_user_ids = Store::pluck('user_id', 'id');
  273. $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
  274. $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
  275. Log::info('5---'.microtime());
  276. foreach ($list as $key => $val) {
  277. $second=mt_rand(2880,7200);
  278. $order= Order::where('store_id', $val->store_id)->whereIn('id', $order_ids)
  279. ->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  280. $list[$key]=$order;
  281. if($order->total==0 && $order->account==0){
  282. $list[$key]->goods =[];
  283. }else{
  284. $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
  285. ->where('store_id', $val->store_id)->whereIn('order_id', $order_ids)->groupBy('goods_id')
  286. ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
  287. }
  288. if(Cache::get('store-'.$val->store_id)){
  289. $list[$key]->store=Cache::get('store-'.$val->store_id);
  290. }else{
  291. $store = Store::where('id',$val->store_id)->select('id','name','img','phone','province','city','area')->first();
  292. $list[$key]->store = $store;
  293. Cache::add('store-'.$val->store_id, $store, $second);
  294. }
  295. if(isset($val->store_id)){
  296. $user_id = $store_user_ids[$val->store_id];
  297. if(Cache::get('warea-'.$user_id)){
  298. $list[$key]->warea = Cache::get('warea-'.$user_id);
  299. }else{
  300. $warea_id = $warea_user_ids[$user_id];
  301. $list[$key]->warea = $wareas[$warea_id];
  302. Cache::add('warea-'.$user_id, $wareas[$warea_id], $second);
  303. }
  304. if(Cache::get('level-'.$user_id)){
  305. $list[$key]->level = Cache::get('level-'.$user_id);
  306. }else{
  307. $list[$key]->level = $level_user[$user_id];
  308. Cache::add('level-'.$user_id, $level_user[$user_id], $second);
  309. }
  310. }else{
  311. $list[$key]->warea = '';
  312. $list[$key]->level = '';
  313. }
  314. }
  315. Log::info('6---'.microtime());
  316. $all['list'] = $list;
  317. return $this->success_list($all, '', $count);
  318. }
  319. //总销量排名
  320. public function getSaleOrderByAll(Request $request)
  321. {
  322. Log::info('1---'.microtime());
  323. $page_index = $request->input('page_index');
  324. $page_size = $request->input('page_size');
  325. $num = $page_size * ($page_index - 1);
  326. $level = $request->input('level');
  327. $search_name = $request->input('search_name');
  328. $startime = $request->input('startime');
  329. $endtime = $request->input('endtime');
  330. $sort = $request->input('sort');
  331. $desc = $request->input('desc');
  332. $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  333. $admin_id = Auth::user()->id;
  334. $admin = Admin::where('id', $admin_id)->first();
  335. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  336. if ($level) {
  337. $store_ids = Cache::remember('level-'.$level,mt_rand(1800,3600),function()use($level){
  338. $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
  339. $user_ids = DwbsUser::where('level', $level)->whereIn('id',$agent_ids)->pluck('id');
  340. return Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
  341. });
  342. }else{
  343. $store_ids=[];
  344. }
  345. } else {
  346. if ($level) {
  347. $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
  348. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  349. $agent_ids = DwbsUser::where('level', $level)->whereIn('warea_id', $warea_ids)->pluck('id');
  350. return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
  351. });
  352. }else{
  353. $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
  354. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  355. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  356. return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
  357. });
  358. }
  359. }
  360. if ($search_name) {
  361. if($store_ids){
  362. $store_ids = Store::whereIn('id',$store_ids)
  363. ->where(function($query)use($search_name){
  364. $query->where('name', 'like', '%' . $search_name . '%')
  365. ->orWhere('phone', 'like', '%' . $search_name . '%');
  366. })
  367. ->pluck('id')->toArray();
  368. }else{
  369. $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
  370. ->orWhere('phone', 'like', '%' . $search_name . '%')
  371. ->pluck('id')->toArray();
  372. }
  373. }
  374. $where=[];
  375. if ($startime && $endtime) {
  376. $data->whereBetween('created_at', [$startime, $endtime]);
  377. $where=function($query)use($startime, $endtime){
  378. $query->whereBetween('created_at', [$startime, $endtime]);
  379. };
  380. }
  381. if($store_ids){
  382. $data->whereIn('store_id',$store_ids);
  383. }
  384. Log::info('2---'.microtime());
  385. $data1 = clone $data;
  386. $order_details = $data->with([
  387. 'store'=>function($query){
  388. $query->with(['user'=>function($q){
  389. $q->with(['warea:id,name'])->select('id','nickname','level','warea_id');
  390. }])->select('id','name','img','phone','province','city','area','user_id');
  391. }])
  392. ->groupBy('store_id')->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
  393. Log::info('2-1-0-'.microtime());
  394. $count = $order_details->get()->count();
  395. if($desc==1){
  396. $desc='asc';
  397. }else{
  398. $desc='desc';
  399. }
  400. $list = $order_details->orderBy($sort,$desc)->orderBy('store_id')->skip($num)->limit($page_size)->get();
  401. Log::info('3---'.microtime());
  402. $account_num = $data1->where($where)->select(DB::raw('sum(account) as account'),DB::raw('sum(total) as num'))->first();
  403. $all['all_account'] = $account_num->account;
  404. $all['all_num'] = $account_num->num;
  405. Log::info('3.1---'.microtime());
  406. if($store_ids){
  407. $nums = OrderDetail::whereHas('order',function($query)use($store_ids){
  408. $query->whereIn('store_id', $store_ids)->where('is_pay',1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  409. })->where($where)->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id')->toArray();
  410. }else{
  411. $nums = OrderDetail::whereHas('order',function($query){
  412. $query->where('is_pay',1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  413. })->where($where)->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id')->toArray();
  414. }
  415. $all['hard_num'] =array_key_exists(1,$nums)?$nums[1]:null;
  416. $all['simple_num'] = array_key_exists(2,$nums)?$nums[2]:null;
  417. $all['old_num'] = array_key_exists(3,$nums)?$nums[3]:null;
  418. $all['cotton_num'] = array_key_exists(4,$nums)?$nums[4]:null;
  419. Log::info('4---'.microtime());
  420. foreach ($list as $key => $val) {
  421. $second=mt_rand(2880,7200);
  422. if($val->total==0 && $val->account==0){
  423. $list[$key]->goods =[];
  424. }else{
  425. $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
  426. ->where('store_id', $val->store_id)
  427. ->whereHas('order',function($query)use($where){
  428. $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
  429. })
  430. ->groupBy('goods_id')
  431. ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
  432. }
  433. }
  434. Log::info('6---'.microtime());
  435. $all['list'] = $list;
  436. $all['num'] = $nums;
  437. return $this->success_list($all, '', $count);
  438. }
  439. //销量排名
  440. public function saleBy(Request $request){
  441. //删除没有开通微店代理
  442. // 1.获取所有店铺代理id数组
  443. $user_ids=Store::pluck('user_id');
  444. return counnt($user_ids);
  445. // $user_ids = Store::pluck('user_id');
  446. // $user_ids = AgentStore::pluck('user_id');
  447. // return $user_ids;
  448. $list=Order::with('store:id,name,phone')->where('is_pay',1)
  449. ->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1])->groupby('store_id')
  450. ->select('store_id',DB::raw('sum(total) as total'),DB::raw('sum(account) as account'))
  451. ->orderBy('account')->skip(0)->limit(10)->get();
  452. $user_ids = Store::pluck('user_id');
  453. $store_user_ids = Store::pluck('user_id', 'id')->toArray();
  454. // $wareas = DwbsWarea::pluck('name', 'id');
  455. // $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
  456. // $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
  457. // $wareas = AgentDwbsWarea::pluck('name', 'id');
  458. // $warea_user_ids = AgentDwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
  459. // $level_user = AgentDwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
  460. $wareas = Cache::remember('wareas','60',function(){
  461. return DwbsWarea::pluck('name', 'id');
  462. });
  463. $warea_user_ids = Cache::remember('warea_user_ids','60',function()use($user_ids){
  464. return DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
  465. });
  466. $level_user = Cache::remember('level_user','60',function()use($user_ids){
  467. return DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
  468. });
  469. foreach ($list as $key => $val) {
  470. $second=mt_rand(2880,7200);
  471. if($val->total==0 && $val->account==0){
  472. $list[$key]->goods =[];
  473. }else{
  474. $where=[];
  475. $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
  476. ->where('store_id', $val->store_id)
  477. ->whereHas('order',function($query)use($where){
  478. $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
  479. })
  480. ->groupBy('goods_id')
  481. ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
  482. }
  483. if(array_key_exists($val->store_id,$store_user_ids)){
  484. $user_id = $store_user_ids[$val->store_id];
  485. if(Cache::get('warea-'.$user_id)){
  486. $list[$key]->warea = Cache::get('warea-'.$user_id);
  487. }else{
  488. $warea_id = $warea_user_ids[$user_id];
  489. $list[$key]->warea = $wareas[$warea_id];
  490. Cache::add('warea-'.$user_id, $wareas[$warea_id], $second);
  491. }
  492. if(Cache::get('level-'.$user_id)){
  493. $list[$key]->level = Cache::get('level-'.$user_id);
  494. }else{
  495. $list[$key]->level = $level_user[$user_id];
  496. Cache::add('level-'.$user_id, $level_user[$user_id], $second);
  497. }
  498. }else{
  499. $list[$key]->warea = '';
  500. $list[$key]->level = '';
  501. }
  502. }
  503. return $list;
  504. }
  505. //分商品销量排名
  506. public function getSaleOrderByType(Request $request)
  507. {
  508. Log::info('type1---'.microtime());
  509. $page_index = $request->input('page_index');
  510. $page_size = $request->input('page_size');
  511. $num = $page_size * ($page_index - 1);
  512. $level = $request->input('level');
  513. $search_name = $request->input('search_name');
  514. $startime = $request->input('startime');
  515. $endtime = $request->input('endtime');
  516. $sort = $request->input('sort');
  517. $desc = $request->input('desc');
  518. $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  519. $admin_id = Auth::user()->id;
  520. $admin = Admin::where('id', $admin_id)->first();
  521. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  522. if ($level) {
  523. $store_ids = Cache::remember('level-'.$level,mt_rand(1800,3600),function()use($level){
  524. $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
  525. $user_ids = DwbsUser::where('level', $level)->whereIn('id',$agent_ids)->pluck('id');
  526. return Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
  527. });
  528. }else{
  529. $store_ids=[];
  530. }
  531. } else {
  532. if ($level) {
  533. $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
  534. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  535. $agent_ids = DwbsUser::where('level', $level)->whereIn('warea_id', $warea_ids)->pluck('id');
  536. return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
  537. });
  538. }else{
  539. $store_ids = Cache::remember('stores_admin_' . $admin_id, mt_rand(1800, 3600), function () use ($admin_id,$level) {
  540. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  541. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  542. return Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
  543. });
  544. }
  545. }
  546. if ($search_name) {
  547. if($store_ids){
  548. $store_ids = Store::whereIn('id',$store_ids)
  549. ->where(function($query)use($search_name){
  550. $query->where('name', 'like', '%' . $search_name . '%')
  551. ->orWhere('phone', 'like', '%' . $search_name . '%');
  552. })
  553. ->pluck('id')->toArray();
  554. }else{
  555. $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
  556. ->orWhere('phone', 'like', '%' . $search_name . '%')
  557. ->pluck('id')->toArray();
  558. }
  559. }
  560. $where=[];
  561. if ($startime && $endtime) {
  562. $data->whereBetween('created_at', [$startime, $endtime]);
  563. $where=function($query)use($startime, $endtime){
  564. $query->whereBetween('created_at', [$startime, $endtime]);
  565. };
  566. }
  567. if($store_ids){
  568. $data->whereIn('store_id',$store_ids);
  569. }
  570. $datas= clone $data;
  571. $data1= clone $data;
  572. $order_ids = $data->pluck('id');//->whereIn('store_id', $store_ids)
  573. Log::info('type2---'.microtime());
  574. $order_details=OrderDetail::with([
  575. 'store'=>function($query){
  576. $query->with(['user'=>function($q){
  577. $q->with(['warea:id,name'])->select('id','nickname','level','warea_id');
  578. }])->select('id','name','img','phone','province','city','area','user_id');
  579. }])
  580. ->whereHas('order',function($query)use($where,$order_ids){
  581. $query->has('store')->whereIn('id', $order_ids)->where($where);
  582. })->groupBy('store_id')
  583. ->select('store_id',DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as order_account'));
  584. Log::info('type3---'.microtime());
  585. if($sort == 'hard'){
  586. $order_details->where('goods_id',1);
  587. }
  588. if($sort == 'simple'){
  589. $order_details->where('goods_id',2);
  590. }
  591. if($sort == 'old'){
  592. $order_details->where('goods_id',3);
  593. }
  594. if($sort == 'cotton'){
  595. $order_details->where('goods_id',4);
  596. }
  597. if ($sort == 'hard' || $sort == 'simple' || $sort == 'old' || $sort == 'cotton') {
  598. if ($desc == 1) {
  599. $order_details->OrderBy('num');
  600. } else {
  601. $order_details->OrderByDesc('num');
  602. }
  603. }
  604. $count = $order_details->get()->count();
  605. $list = $order_details->orderBy('store_id')->skip($num)->limit($page_size)->get();
  606. Log::info('type3.2---'.microtime());
  607. $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)
  608. ->select(DB::raw('sum(account) as account'),DB::raw('sum(total) as num'))->first();
  609. $all['all_account'] = $account_num->account;
  610. $all['all_num'] = $account_num->num;
  611. Log::info('type3.3---'.microtime());
  612. $nums = OrderDetail::with('store:id,name,phone')->whereHas('order',function($query)use($where,$order_ids){
  613. $query->has('store')->whereIn('id',$order_ids)->where($where);
  614. })->groupBy('goods_id')->select(DB::raw('sum(num) as num'),'goods_id')->pluck('num','goods_id');
  615. // $all['hard_num'] = $nums[1];
  616. // $all['simple_num'] = $nums[2];
  617. // $all['old_num'] = $nums[3];
  618. // $all['cotton_num'] = $nums[4];
  619. $all['hard_num'] =array_key_exists(1,$nums)?$nums[1]:null;
  620. $all['simple_num'] = array_key_exists(2,$nums)?$nums[2]:null;
  621. $all['old_num'] = array_key_exists(3,$nums)?$nums[3]:null;
  622. $all['cotton_num'] = array_key_exists(4,$nums)?$nums[4]:null;
  623. Log::info('type5---'.microtime());
  624. foreach ($list as $key => $val) {
  625. $second=mt_rand(2880,7200);
  626. $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)
  627. ->select(DB::raw('sum(account) as account'),DB::raw('sum(total) as total'))->first();
  628. $list[$key]->total=$val_order->total;
  629. $list[$key]->account=$val_order->account;
  630. if($val_order->total==0 && $val_order->account==0){
  631. $list[$key]->goods =[];
  632. }else{
  633. $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
  634. ->where('store_id', $val->store_id)
  635. ->whereHas('order',function($query)use($where){
  636. $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
  637. })
  638. ->groupBy('goods_id')
  639. ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
  640. }
  641. }
  642. $all['list'] = $list;
  643. return $this->success_list($all, '', $count);
  644. }
  645. //总销量排名
  646. public function exportSaleOrderByAll(Request $request)
  647. {
  648. ini_set('memory_limit', '4096M');
  649. log::info('gaunjuan');
  650. $level = $request->input('level');
  651. $search_name = $request->input('search_name');
  652. $startime = $request->input('startime');
  653. $endtime = $request->input('endtime');
  654. $admin_id = Auth::user()->id;
  655. Log::info('MINUTE---'.microtime());
  656. $admin = Admin::where('id', $admin_id)->first();
  657. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  658. $store_ids = Store::pluck('id')->toArray();
  659. } else {
  660. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  661. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  662. $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
  663. }
  664. Log::info('MINUTE---'.microtime());
  665. if ($level) {
  666. $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
  667. $user_ids = DwbsUser::where('level', $level)->whereIn('id', $agent_ids)->pluck('id');
  668. $level_store_ids = Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
  669. $store_ids=array_intersect($store_ids,$level_store_ids);
  670. }
  671. Log::info('MINUTE---'.microtime());
  672. if ($search_name) {
  673. $search_name_store_ids = Store::where('name', 'like', '%' . $search_name . '%')
  674. ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id')->toArray();
  675. $store_ids=array_intersect($store_ids,$search_name_store_ids);
  676. }
  677. Log::info('MINUTE---'.microtime());
  678. $where=[];
  679. if ($startime && $endtime) {
  680. $where=function($query)use($startime, $endtime){
  681. $query->whereBetween('created_at', [$startime, $endtime]);
  682. };
  683. }
  684. Log::info('MINUTE---'.microtime());
  685. //<<<<<<< HEAD
  686. // $order_ids=Order::where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where)->pluck('id');
  687. // $store=Store::with(['order_detail'=>function($query)use($where,$order_ids){
  688. // $query->with(['goods:id,main_attr'])->select('id','store_id','goods_id','account','num','size','type')
  689. // ->where($where)->whereIn('order_id',$order_ids);
  690. // },'user:id,level,agent_id,nickname'])
  691. // ->whereIn('id',$store_ids)->select('id','name','phone','province','city','area','user_id')->get();//,'img'
  692. //=======
  693. // $order_ids=Order::where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where)->pluck('id');
  694. $store=Store::whereHas('order',function ($q)use ($where){
  695. $q->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
  696. })->with(['order_detail'=>function($query)use($where){
  697. $query->with(['goods:id,main_attr'])->select('id','store_id','goods_id','account','num','size','type')
  698. ->where($where)->whereHas('order',function($q)use($where){
  699. $q->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
  700. });
  701. },'user:id,level,agent_id,nickname'])
  702. ->whereIn('id',$store_ids)->select('id','name','phone','province','city','area','user_id')->get();//,'img'
  703. // $store=Store::where('is_apply',3)->select('id','name','phone','province','city','area','user_id')->get();
  704. //>>>>>>> 3113a468aec0f8b4a919f7affca6dae9837db628
  705. Log::info('MINUTE---'.microtime());
  706. $wareas = DwbsWarea::pluck('name', 'id');
  707. $user_ids = Store::pluck('user_id');
  708. $store_user_ids = Store::pluck('user_id', 'id')->toArray();
  709. $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id')->toArray();
  710. $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id')->toArray();
  711. Log::info('MINUTE---'.microtime());
  712. foreach($store as $key=>$val){
  713. $store[$key]->company = '';
  714. $store[$key]->account=$val->order_detail->sum('account');
  715. $store[$key]->num=$val->order_detail->sum('num');
  716. $store[$key]->super_num=$val->order_detail->where('goods_id',1)->sum('num');
  717. $store[$key]->simple_num=$val->order_detail->where('goods_id',2)->sum('num');
  718. $store[$key]->old_num=$val->order_detail->where('goods_id',3)->sum('num');
  719. $store[$key]->cotton_num=$val->order_detail->where('goods_id',4)->sum('num');
  720. $second=mt_rand(2880,7200);
  721. if(array_key_exists($val->id,$store_user_ids)){
  722. $user_id = $store_user_ids[$val->id];
  723. if(Cache::get('warea-'.$user_id)){
  724. $store[$key]->warea = Cache::get('warea-'.$user_id);
  725. }else{
  726. if(array_key_exists($user_id,$warea_user_ids)){
  727. $warea_id = $warea_user_ids[$user_id];
  728. if(array_key_exists($warea_id,$wareas)){
  729. $store[$key]->warea = $wareas[$warea_id];
  730. }else{
  731. $store[$key]->warea = '';
  732. }
  733. }else{
  734. $store[$key]->warea = '';
  735. }
  736. Cache::add('warea-'.$user_id, $store[$key]->warea, $second);
  737. }
  738. if(Cache::get('level-'.$user_id)){
  739. $store[$key]->level = Cache::get('level-'.$user_id);
  740. }else{
  741. if(array_key_exists($user_id,$level_user)){
  742. $store[$key]->level = $level_user[$user_id];
  743. }else{
  744. $store[$key]->level = '';
  745. }
  746. Cache::add('level-'.$user_id, $store[$key]->level, $second);
  747. }
  748. }else{
  749. $store[$key]->level = '%';
  750. $store[$key]->warea = '';
  751. }
  752. if(!empty($val->user) && $val->user->level==2){
  753. if(Cache::has('company-'.$val->user->id)){
  754. $store[$key]->company=Cache::get('company-'.$val->user->id);
  755. }else{
  756. $company=DwbsUser::withTrashed()->where('id',$val->user->agent_id)->value('nickname');
  757. $store[$key]->company=$company;
  758. Cache::add('company-'.$val->user->id, $company, $second);
  759. }
  760. }
  761. if(!empty($val->user) && $val->user->level==1){
  762. if(Cache::has('company-'.$val->user->id)){
  763. $store[$key]->company=Cache::get('company-'.$val->user->id);
  764. }else{
  765. $agent=DwbsUser::withTrashed()->with('agent:id,nickname')->where('id',$val->user->agent_id)->first();
  766. if($agent->level==3){
  767. $company=$agent->nickname;
  768. }else{
  769. $company=$agent->agent->nickname;
  770. }
  771. $store[$key]->company=$company;
  772. Cache::put('company-'.$val->user->id, $company, $second);
  773. }
  774. }
  775. if(!empty($val->user) && $val->user->level==3){
  776. $store[$key]->company=$val->user->nickname;
  777. }
  778. }
  779. Log::info('MINUTE---'.microtime());
  780. return $this->success($store);
  781. }
  782. //总销量排名
  783. public function saleOrderByAll(Request $request)
  784. {
  785. Log::info('1---'.microtime());
  786. $page_index = $request->input('page_index');
  787. $page_size = $request->input('page_size');
  788. $num = $page_size * ($page_index - 1);
  789. $level = $request->input('level');
  790. $search_name = $request->input('search_name');
  791. $startime = $request->input('startime');
  792. $endtime = $request->input('endtime');
  793. $sort = $request->input('sort');
  794. $desc = $request->input('desc');
  795. $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  796. $admin_id = Auth::user()->id;
  797. $admin = Admin::where('id', $admin_id)->first();
  798. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  799. $store_ids = Store::query()->pluck('id')->toArray();
  800. } else {
  801. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  802. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  803. $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id')->toArray();
  804. }
  805. if ($level) {
  806. $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
  807. $user_ids = DwbsUser::where('level', $level)->whereIn('id',$agent_ids)->pluck('id');
  808. $level_store_ids = Store::whereIn('user_id', $user_ids)->pluck('id')->toArray();
  809. $store_ids=array_intersect($store_ids,$level_store_ids);
  810. }
  811. if ($search_name) {
  812. $search_name_store_ids = Store::where('name', 'like', '%' . $search_name . '%')
  813. ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id')->toArray();
  814. $store_ids=array_intersect($store_ids,$search_name_store_ids);
  815. }
  816. $where=[];
  817. if ($startime && $endtime) {
  818. $data->whereBetween('created_at', [$startime, $endtime]);
  819. $where=function($query)use($startime, $endtime){
  820. $query->whereBetween('created_at', [$startime, $endtime]);
  821. };
  822. }
  823. $datas= clone $data;
  824. $data1= clone $data;
  825. $order_ids = $data->whereIn('store_id', $store_ids)->pluck('id');
  826. Log::info('2---'.microtime());
  827. $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'));
  828. Log::info('2-1-0-'.microtime());
  829. // $count = $order_details->get()->count();
  830. if($desc==1){
  831. $desc='asc';
  832. }else{
  833. $desc='desc';
  834. }
  835. $list = $order_details->orderBy($sort,$desc)->orderBy('store_id')->get();
  836. Log::info('3---'.microtime());
  837. Log::info('4---'.microtime());
  838. $wareas = DwbsWarea::pluck('name', 'id');
  839. $user_ids = Store::pluck('user_id');
  840. $store_user_ids = Store::pluck('user_id', 'id')->toArray();
  841. $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
  842. $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
  843. Log::info('5---'.microtime());
  844. $list=OrderDetail::whereIn('store_id',$store_ids)->where($where)->groupBy('goods_id')->get();
  845. foreach ($list as $key => $val) {
  846. $second=mt_rand(2880,7200);
  847. if($val->total==0 && $val->account==0){
  848. $list[$key]->goods =[];
  849. }else{
  850. Cache::remember('orderBy_goods');
  851. $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
  852. ->where('store_id', $val->store_id)
  853. ->whereHas('order',function($query)use($where){
  854. $query->has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->where($where);
  855. })
  856. ->groupBy('goods_id')
  857. ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
  858. }
  859. if(array_key_exists($val->store_id,$store_user_ids)){
  860. $user_id = $store_user_ids[$val->store_id];
  861. if(Cache::get('warea-'.$user_id)){
  862. $list[$key]->warea = Cache::get('warea-'.$user_id);
  863. }else{
  864. $warea_id = $warea_user_ids[$user_id];
  865. $list[$key]->warea = $wareas[$warea_id];
  866. Cache::add('warea-'.$user_id, $wareas[$warea_id], $second);
  867. }
  868. if(Cache::get('level-'.$user_id)){
  869. $list[$key]->level = Cache::get('level-'.$user_id);
  870. }else{
  871. $list[$key]->level = $level_user[$user_id];
  872. Cache::add('level-'.$user_id, $level_user[$user_id], $second);
  873. }
  874. }else{
  875. $list[$key]->warea = '';
  876. $list[$key]->level = '';
  877. }
  878. }
  879. Log::info('6---'.microtime());
  880. $all['list'] = $list;
  881. return $this->success($all);
  882. }
  883. //导出销量排名
  884. public function exportSaleOrderBy(Request $request)
  885. {
  886. $level = $request->input('level');
  887. $search_name = $request->input('search_name');
  888. $startime = $request->input('start_time');
  889. $endtime = $request->input('end_time');
  890. $sort = $request->input('sort');
  891. $desc = $request->input('desc');
  892. $data = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1]);
  893. $admin_wheres = $level_wheres = $search_wheres = [];
  894. $admin_id = Auth::user()->id;
  895. $admin = Admin::where('id', $admin_id)->first();
  896. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  897. $admin_wheres = [];
  898. } else {
  899. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  900. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  901. $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
  902. $admin_wheres = function ($query) use ($store_ids) {
  903. $query->whereIn('store.id', $store_ids);
  904. };
  905. }
  906. if ($level) {
  907. $agent_ids = Store::where('status', 0)->where('is_apply', 3)->pluck('user_id');
  908. $user_ids = DwbsUser::where('level', $level)->whereIn('id', $agent_ids)->pluck('id');
  909. $store_ids = Store::whereIn('user_id', $user_ids)->pluck('id');
  910. $data->whereIn('store_id', $store_ids);
  911. $level_wheres = function ($query) use ($store_ids) {
  912. $query->whereIn('store.id', $store_ids);
  913. };
  914. }
  915. if ($search_name) {
  916. $store_ids = Store::where('name', 'like', '%' . $search_name . '%')
  917. ->orWhere('phone', 'like', '%' . $search_name . '%')->pluck('id');
  918. $data->whereIn('store_id', $store_ids);
  919. $search_wheres = function ($query) use ($store_ids) {
  920. $query->whereIn('store.id', $store_ids);
  921. };
  922. }
  923. if ($startime && $endtime) {
  924. $data->whereBetween('created_at', [$startime, $endtime]);
  925. }
  926. $order_ids = $data->pluck('id');
  927. if ($sort == 'hard') {
  928. $where['goods_id'] = 1;
  929. } elseif ($sort == 'simple') {
  930. $where['goods_id'] = 2;
  931. } elseif ($sort == 'old') {
  932. $where['goods_id'] = 3;
  933. } elseif ($sort == 'cotton') {
  934. $where['goods_id'] = 4;
  935. } else {
  936. $where = [];
  937. }
  938. $order_details = Store::where('status', 0)->where('is_apply', 3)
  939. ->where($admin_wheres)->where($level_wheres)->where($search_wheres)
  940. ->leftJoin('order_detail as od', function ($query) use ($order_ids, $where) {
  941. $query->on('od.store_id', '=', 'store.id')
  942. ->whereIn('order_id', $order_ids)
  943. ->where($where);
  944. })
  945. ->groupBy('store.id')
  946. ->select('store.id as store_id',DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'));
  947. if ($sort == 'total' || $sort == 'hard' || $sort == 'simple' || $sort == 'old' || $sort == 'cotton') {
  948. if ($desc == 1) {
  949. $order_details->OrderBy('total');
  950. } else {
  951. $order_details->OrderByDesc('total');
  952. }
  953. } elseif ($sort == 'account') {
  954. if ($desc == 1) {
  955. $order_details->OrderBy('account');
  956. } else {
  957. $order_details->OrderByDesc('account');
  958. }
  959. }
  960. $list = $order_details->get();
  961. $all['all_account'] = OrderDetail::whereIn('order_id', $order_ids)->sum('account');
  962. $all['all_num'] = OrderDetail::whereIn('order_id', $order_ids)->sum('num');
  963. $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');
  964. $all['hard_num'] = $nums[1];
  965. $all['simple_num'] = $nums[2];
  966. $all['old_num'] = $nums[3];
  967. $all['cotton_num'] = $nums[4];
  968. $wareas = DwbsWarea::pluck('name', 'id');
  969. $user_ids = Store::pluck('user_id');
  970. $store_user_ids = Store::pluck('user_id', 'id');
  971. $warea_user_ids = DwbsUser::whereIn('id', $user_ids)->pluck('warea_id', 'id');
  972. $level_user = DwbsUser::whereIn('id', $user_ids)->pluck('level', 'id');
  973. foreach ($list as $key => $val) {
  974. $list[$key] = Order::where('store_id', $val->store_id)->whereIn('id', $order_ids)
  975. ->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  976. $list[$key]->goods = OrderDetail::with('goods:id,name,main_attr')
  977. ->where('store_id', $val->store_id)->whereIn('order_id', $order_ids)->groupBy('goods_id')
  978. ->select('goods_id', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
  979. $detail = OrderDetail::with('goods:id,name,main_attr')
  980. ->where('store_id', $val->store_id)->whereIn('order_id', $order_ids)->groupBy('goods_id', 'type', 'size')
  981. ->select('goods_id', 'type', 'size', DB::raw('ifnull(sum(num),0) as num'), DB::raw('ifnull(sum(account),0) as account'))->get();
  982. $list[$key]->detail = $detail;
  983. $list[$key]->store = Store::where('id',$val->store_id)->select('id','name','img','phone','province','city','area')->first();
  984. if(isset($val->store_id)){
  985. $user_id = $store_user_ids[$val->store_id];
  986. $warea_id = $warea_user_ids[$user_id];
  987. $list[$key]->warea = $wareas[$warea_id];
  988. $list[$key]->level = $level_user[$user_id];
  989. }else{
  990. $list[$key]->warea = '';
  991. $list[$key]->level = '';
  992. }
  993. }
  994. $all['list'] = $list;
  995. return $this->success($all);
  996. }
  997. //获取昨日销量排名
  998. public function yesterdayOrderBy(Request $request)
  999. {
  1000. $page_index=$request->input('page_index')??1;
  1001. $page_size=$request->input('page_size')??20;
  1002. $num=$page_size*($page_index-1);
  1003. $data['super_info'] = $this->orderDetail(1,$num,$page_size);
  1004. $data['simple_info'] = $this->orderDetail(2,$num,$page_size);
  1005. $data['old_info'] = $this->orderDetail(3,$num,$page_size);
  1006. $data['cotton_info'] = $this->orderDetail(4,$num,$page_size);
  1007. return $this->success($data);
  1008. }
  1009. public function orderDetail($id,$num,$index)
  1010. {
  1011. $date = date("Y-m-d", strtotime('-1 days'));
  1012. $order_nos = Order::has('store')->where('is_pay', 1)
  1013. ->whereDate('created_at', $date)
  1014. ->whereIn('apply_cancel', [0, 1, 3])
  1015. ->whereIn('is_refund', [0, 1])->pluck('order_no');
  1016. $stores=Order::has('store')->where('is_pay', 1)
  1017. ->whereDate('created_at', $date)
  1018. ->whereIn('apply_cancel', [0, 1, 3])
  1019. ->whereIn('is_refund', [0, 1])->pluck('store_id');
  1020. $count = $order_details = Store::where('status', 0)->where('is_apply', 3)->whereIn('store_id',$stores)
  1021. ->leftJoin('order_detail as od', function ($query) use ($order_nos, $id) {
  1022. $query->on('od.store_id', '=', 'store.id')
  1023. ->whereIn('od.order_no', $order_nos)
  1024. ->where('od.goods_id',$id);
  1025. })->groupBy('store.id')->get()->count();
  1026. $order_details = Store::where('status', 0)->where('is_apply', 3)->whereIn('store_id',$stores)
  1027. ->leftJoin('order_detail as od', function ($query) use ($order_nos, $id) {
  1028. $query->on('od.store_id', '=', 'store.id')
  1029. ->whereIn('od.order_no', $order_nos)
  1030. ->where('od.goods_id',$id);
  1031. })
  1032. ->groupBy('store.id')
  1033. ->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'))
  1034. ->orderByDesc('total')
  1035. ->orderByDesc('account')
  1036. ->orderBy('store.id')
  1037. ->skip($num)->limit($index)
  1038. ->get();
  1039. $data['total']=$count;
  1040. $data['list']=$order_details;
  1041. return $data;
  1042. }
  1043. //总销售数据排名
  1044. public function allOrderBy(Request $request)
  1045. {
  1046. $page_index=$request->input('page_index')??1;
  1047. $page_size=$request->input('page_size')??20;
  1048. $num=$page_size*($page_index-1);
  1049. $count = Order::query()->whereHas('store')
  1050. ->with(['store:id,name,phone,img'])
  1051. ->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])
  1052. ->groupBy('store_id')
  1053. ->get()->count();
  1054. $order = Order::query()->whereHas('store')
  1055. ->with(['store:id,name,phone,img'])
  1056. ->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])
  1057. ->select('store_id',DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))
  1058. ->groupBy('store_id')
  1059. ->orderByDesc('total')
  1060. ->orderByDesc('account')
  1061. ->skip($num)->limit($page_size)
  1062. ->get();
  1063. return $this->success_list($order,'',$count);
  1064. }
  1065. //各款式每日销量变化曲线图
  1066. public function getSaleInfo(Request $request)
  1067. {
  1068. if ($request->input('startdate') && $request->input('enddate')) {
  1069. $startdate = $request->input('startdate');
  1070. $enddate = $request->input('enddate');
  1071. } else {
  1072. $startdate = date("Y-m-d", strtotime('-7 days'));
  1073. $enddate = date("Y-m-d");
  1074. }
  1075. $dates = $this->getDateFromRange($startdate, $enddate);
  1076. $list = [];
  1077. foreach ($dates as $key => $val) {
  1078. $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');
  1079. $list['super'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 1)
  1080. ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1081. }
  1082. foreach ($dates as $key => $val) {
  1083. $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');
  1084. $list['simple'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 2)
  1085. ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1086. }
  1087. foreach ($dates as $key => $val) {
  1088. $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');
  1089. $list['old'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 3)
  1090. ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1091. }
  1092. foreach ($dates as $key => $val) {
  1093. $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');
  1094. $list['cotton'][$val] = OrderDetail::whereIn('order_no', $order_nos)->where('goods_id', 4)
  1095. ->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1096. }
  1097. return $this->success($list);
  1098. }
  1099. function getDateFromRange($startdate, $enddate)
  1100. {
  1101. $stimestamp = strtotime($startdate);
  1102. $etimestamp = strtotime($enddate);
  1103. // 计算日期段内有多少天
  1104. $days = ($etimestamp - $stimestamp) / 86400 + 1;
  1105. // 保存每天日期
  1106. $date = array();
  1107. for ($i = 0; $i < $days; $i++) {
  1108. $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
  1109. }
  1110. return $date;
  1111. }
  1112. //获取每日新增人数
  1113. public function getEveryAddAgent(Request $request)
  1114. {
  1115. if ($request->input('startdate') && $request->input('enddate')) {
  1116. $startdate = $request->input('startdate');
  1117. $enddate = $request->input('enddate');
  1118. } else {
  1119. $startdate = date("Y-m-d", strtotime('-7 days'));
  1120. $enddate = date("Y-m-d");
  1121. }
  1122. $dates = $this->getDateFromRange($startdate, $enddate);
  1123. $list = [];
  1124. foreach ($dates as $key => $val) {
  1125. $list[$val] = Store::whereDate('created_at', $val)->where('is_apply',3)->count();
  1126. }
  1127. return $this->success($list);
  1128. }
  1129. //获取每日新增人数
  1130. public function getEveryAddAgentSort(Request $request)
  1131. {
  1132. $date=$request->input('date');
  1133. $user_ids=Store::whereDate('created_at', $date)->where('is_apply',3)->pluck('user_id');
  1134. $list=DwbsUser::with('warea:id,name')->whereIn('id',$user_ids)->groupBy('warea_id')
  1135. ->select('warea_id',DB::raw('count(id) as count'))->orderByDesc('count')->get();
  1136. return $this->success($list);
  1137. }
  1138. //获取政委每日新增人数变化曲线图
  1139. public function getEveryAddAgentLine(Request $request)
  1140. {
  1141. if ($request->input('startdate') && $request->input('enddate')) {
  1142. $startdate = $request->input('startdate');
  1143. $enddate = $request->input('enddate');
  1144. } else {
  1145. $startdate = date("Y-m-d", strtotime('-7 days'));
  1146. $enddate = date("Y-m-d");
  1147. }
  1148. $dates = $this->getDateFromRange($startdate, $enddate);
  1149. $list = [];
  1150. $warea=DwbsWarea::all();
  1151. foreach($warea as $k=>$v){
  1152. foreach ($dates as $key => $val) {
  1153. $user_ids=Store::whereDate('created_at', $val)->where('is_apply',3)->pluck('user_id');
  1154. $list[$v->name][$val]=DwbsUser::whereIn('id',$user_ids)->where('warea_id',$v->id)->count();
  1155. }
  1156. }
  1157. return $this->success($list);
  1158. }
  1159. public function getHistoryTotal(Request $request)
  1160. {
  1161. $startime = $request->input('start_time');
  1162. $endtime = $request->input('end_time');
  1163. $where = $wheres = [];
  1164. if ($startime && $endtime) {
  1165. $where = function ($query) use ($startime, $endtime) {
  1166. $query->whereBetween('created_at', [$startime, $endtime]);
  1167. };
  1168. $wheres = function ($query) use ($startime, $endtime) {
  1169. $query->whereBetween('cancel_at', [$startime, $endtime]);
  1170. };
  1171. }
  1172. $data['all'] = Order::where('is_pay', 1)->where($where)
  1173. ->select(DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1174. $data['refund'] = OrderRefund::where($where)->where('status', 1)
  1175. ->select(DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1176. $data['cancel'] = Order::where($wheres)->where('apply_cancel', 2)
  1177. ->select(DB::raw('ifnull(sum(total),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1178. return $this->success($data);
  1179. }
  1180. public function getSkuHistoryNum(Request $request)
  1181. {
  1182. $startdate = $request->input('startdate');
  1183. $enddate = $request->input('enddate');
  1184. $where = $wheres = [];
  1185. if ($startdate && $enddate) {
  1186. $startime = date("Y-m-d 00:00:00", strtotime($startdate));
  1187. $endtime = date("Y-m-d 23:59:59", strtotime($enddate));
  1188. $where = function ($query) use ($startime, $endtime) {
  1189. $query->whereBetween('created_at', [$startime, $endtime]);
  1190. };
  1191. }
  1192. $data = OrderDetail::has('goods')->whereHas('order', function ($query) {
  1193. $query->where('is_pay', 1);
  1194. })->with('goods:id,name,main_attr')->where($where)
  1195. ->select('goods_id', DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))
  1196. ->groupBy('goods_id')->orderBy('goods_id')->get();
  1197. return $this->success($data);
  1198. }
  1199. //获取款式排行
  1200. public function getSkuOrderBy()
  1201. {
  1202. $goods_sku=GoodSku::whereHas('goods',function($query){$query->where('is_shelves', 1);})->with('goods:id,name,main_attr')->get();
  1203. foreach($goods_sku as $key=>$val){
  1204. $goods_sku[$key]->detail=OrderDetail::where('sku_id',$val->id)->whereHas('order',function($query){
  1205. $query->where('is_pay', 1)->whereIn('is_refund', [0, 1])->whereIn('apply_cancel', [0, 1, 3]);
  1206. })->select(DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))->first();
  1207. }
  1208. return $this->success($goods_sku);
  1209. }
  1210. //三天销量为0
  1211. public function get3DaysSales0(Request $request)
  1212. {
  1213. $start_time = date("Y-m-d 00:00:00", strtotime('-3 days'));
  1214. $end_time = date("Y-m-d H:i:s");
  1215. $page_index = $request->input('page_index');
  1216. $page_size = $request->input('page_size');
  1217. $num = $page_size * ($page_index - 1);
  1218. $admin_id = Auth::user()->id;
  1219. $admin = Admin::where('id', $admin_id)->first();
  1220. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  1221. $where = [];
  1222. } else {
  1223. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  1224. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  1225. $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
  1226. $where = function ($query) use ($store_ids) {
  1227. $query->whereIn('store.id', $store_ids);
  1228. };
  1229. }
  1230. $count = Store::where('store.status', 0)->where($where)
  1231. ->leftJoin('order as o', function ($query) use ($start_time, $end_time) {
  1232. $query->on('store.id', '=', 'o.store_id')->where('o.is_pay', 1)
  1233. ->whereIn('o.apply_cancel', [0, 1, 3])->whereIn('o.is_refund', [0, 1])
  1234. ->whereBetween('o.created_at', [$start_time, $end_time]);
  1235. })->select('store.id', 'store.name', 'store.img', 'store.phone', DB::Raw('sum(ifnull(o.total,0)) as total'))
  1236. ->groupBy('store.id')
  1237. ->having('total', '=', 0)->get()->count();
  1238. $list = Store::where('store.status', 0)->where($where)
  1239. ->leftJoin('order as o', function ($query) use ($start_time, $end_time) {
  1240. $query->on('store.id', '=', 'o.store_id')->where('o.is_pay', 1)
  1241. ->whereIn('o.apply_cancel', [0, 1, 3])->whereIn('o.is_refund', [0, 1])
  1242. ->whereBetween('o.created_at', [$start_time, $end_time]);
  1243. })->select('store.id', 'store.name', 'store.img', 'store.phone', DB::Raw('sum(ifnull(o.total,0)) as total'))
  1244. ->groupBy('store.id')
  1245. ->having('total', '=', 0)
  1246. ->orderBy('store.id')
  1247. ->skip($num)->take($page_size)
  1248. ->get();
  1249. return $this->success_list($list, '', $count);
  1250. }
  1251. //三天销量为0
  1252. public function export3DaysSales0(Request $request)
  1253. {
  1254. $start_time = date("Y-m-d 00:00:00", strtotime('-3 days'));
  1255. $end_time = date("Y-m-d H:i:s");
  1256. $admin_id = Auth::user()->id;
  1257. $admin = Admin::where('id', $admin_id)->first();
  1258. if ($admin && in_array($admin->role_id, [22, 24])) {//超级管理员
  1259. $where = [];
  1260. } else {
  1261. $warea_ids = DwbsWarea::where('admin_id', $admin_id)->pluck('id');
  1262. $agent_ids = DwbsUser::whereIn('warea_id', $warea_ids)->pluck('id');
  1263. $store_ids = Store::whereIn('user_id', $agent_ids)->pluck('id');
  1264. $where = function ($query) use ($store_ids) {
  1265. $query->whereIn('store.id', $store_ids);
  1266. };
  1267. }
  1268. $list = Store::where('store.status', 0)->where($where)
  1269. ->leftJoin('order as o', function ($query) use ($start_time, $end_time) {
  1270. $query->on('store.id', '=', 'o.store_id')->where('o.is_pay', 1)
  1271. ->whereIn('o.apply_cancel', [0, 1, 3])->whereIn('o.is_refund', [0, 1])
  1272. ->whereBetween('o.created_at', [$start_time, $end_time]);
  1273. })->select('store.id', 'store.name', 'store.img', 'store.phone', DB::Raw('sum(ifnull(o.total,0)) as total'))
  1274. ->groupBy('store.id')
  1275. ->having('total', '=', 0)->orderBy('store.id')->get();
  1276. return $this->success($list);
  1277. }
  1278. //同步订单和订单详情用户店铺信息
  1279. public function asd(Request $request)
  1280. {
  1281. // OrderCancel::where('reason','代理取消订单')->update(['type'=>2]);//->orWhere('reason','<>','代理取消订单')
  1282. Cache::tags(['store','agent'])->put('bar', 'baz123', 60); // 10 分钟
  1283. Cache::tags('stores')->flush();
  1284. return Cache::tags(['store','agent'])->get('bar');
  1285. $num=$request->input('num');
  1286. return $this->success(is_numeric($num));
  1287. // return $this->success(is_nan($num ));
  1288. // return '123';
  1289. // $order=Order::query()->withTrashed()->whereNull('snapshot')->limit(500)->orderBy('id')->get();
  1290. //// return $order;
  1291. // if(count($order)==0){
  1292. // return '已执行完成';
  1293. // }
  1294. // foreach($order as $key=>$val){
  1295. // $goods=[];
  1296. // $order_details=OrderDetail::where('order_id',$val->id)->get();
  1297. // foreach($order_details as $k=>$v){
  1298. // $sku_info=[];
  1299. // $goods_info=Goods::where('id',$v->goods_id)->first();
  1300. // if(!array_key_exists($v->goods_id,$goods)){
  1301. // $goods[$v->goods_id]['name']=$goods_info->name;
  1302. // $goods[$v->goods_id]['price']=$goods_info->price;
  1303. // $goods[$v->goods_id]['img']=$goods_info->img;
  1304. // $goods[$v->goods_id]['main_attr']=$goods_info->main_attr;
  1305. // $goods[$v->goods_id]['sku']=[];
  1306. // }
  1307. // $sku_info['origin_price']=$goods_info->price;
  1308. // $sku_info['price']=$v->price;
  1309. // $sku_info['sku_id']=$v->sku_id;
  1310. // $sku_info['num']=$v->num;
  1311. // $sku_info['size']=$v->size;
  1312. // $sku_info['type']=$v->type;
  1313. // array_push($goods[$v->goods_id]['sku'],$sku_info);
  1314. // OrderDetail::where('id',$v->id)->update([
  1315. // 'origin_price'=>$goods_info->price
  1316. // ]);
  1317. // }
  1318. // Order::withTrashed()->where('id',$val->id)->update([
  1319. // 'snapshot'=>json_encode($goods)
  1320. // ]);
  1321. // }
  1322. //
  1323. //
  1324. //return '执行了'.count($order).'条,最后一条订单id:'.$order[count($order)-1]->id;
  1325. // $update = DB::table('goods_sku')->where('id',$skuId)->update(['stock_daily'=>DB::raw('stock_daily_max')]);
  1326. // $orders=Order::where('is_pay',1)->where('self_receive',1)->where('apply_cancel','<>',2)->update([
  1327. // 'express_receive_time'=>DB::raw('updated_at'),
  1328. // ]);
  1329. //
  1330. //return $orders;
  1331. $date=date("Y-m-d H:i:s",strtotime('-1 days'));
  1332. Log::info($date);
  1333. // $date=date("Y-m-d H:i:s",strtotime('-1 days'));//测试期间设为一天
  1334. // $date=date("Y-m-d H:i:s",strtotime('-1 hours'));
  1335. $orders=Order::with('address')->whereNotNull('express_info')
  1336. // ->where('status',2)->where('express_state','3')
  1337. ->where('express_receive_time','<',$date)->orderByDesc('express_receive_time')->select('order_no','express_receive_time')->get();
  1338. return $orders;
  1339. $orders=Order::whereNotNull('express_info')->where('express_receive_time','0000-00-00 00:00:00')->get();
  1340. foreach($orders as $key=>$val){
  1341. $express=\GuzzleHttp\json_decode($val->express_info,true);
  1342. foreach($express['Traces'] as $k=>$v){
  1343. $express['Traces'][$k]['AcceptTime']=date("Y-m-d H:i:s",substr($v['AcceptTime'],0,strlen($v['AcceptTime'])-3));
  1344. }
  1345. // return $express;
  1346. // return $this->success(\GuzzleHttp\json_encode($express));
  1347. order::where('id',$val->id)->update([
  1348. 'express_info'=>\GuzzleHttp\json_encode($express),
  1349. 'express_receive_time'=>array_pop($express['Traces'])['AcceptTime']
  1350. ]);
  1351. // $orders[$key]=$express;
  1352. }
  1353. // return $orders;
  1354. return Order::whereNotNull('express_info')->where('express_receive_time','0000-00-00 00:00:00')->get();
  1355. // $apply=Store::query()->groupBy('is_apply')->select(DB::raw('count(id) as num'),'is_apply')->pluck('num','is_apply');
  1356. //return $apply;
  1357. $second=10;
  1358. // Cache::put('type', 1513699, $second);
  1359. // if(Cache::get('type')){
  1360. // return Cache::get('type');
  1361. // }else{
  1362. // return '111161';
  1363. // }
  1364. // $str=Store::where('id','99')->value('name');
  1365. // $str = preg_replace_callback(
  1366. // '/./u',
  1367. // function (array $match) {
  1368. // return strlen($match[0]) >= 4 ? '' : $match[0];
  1369. // },
  1370. // $str);
  1371. // return $str;
  1372. $number = 2;
  1373. $str = "Shanghai";
  1374. $txt = sprintf("There are %u million cars in %s.",$number,$str);
  1375. // return $txt;
  1376. $num='a';
  1377. $name='pig';
  1378. $txt=sprintf("there is %s an %s",$num,$name);
  1379. return $txt;
  1380. $store_name=Store::where('id','7990')->value('name');//7353
  1381. $store_name='VVV(11111111111122)))';
  1382. $store_name=str_replace(' ','',$store_name);
  1383. $store_name=preg_replace("/[^\x{4e00}-\x{9fa5}^0-9^A-Z^a-z^(]+/u", '', $store_name);
  1384. $store_name=mb_substr($store_name,0,16);
  1385. if(mb_strlen(preg_replace("/[^\x{4e00}-\x{9fa5}]+/u", '', $store_name))==0){
  1386. return $this->error('450001','该店铺名称格式不符需重置');
  1387. }
  1388. return $this->success($store_name);
  1389. $phones= DwbsUser::where('level',3)->pluck('mobile');
  1390. $list=Store::whereIn('phone',$phones)->select('username','idCard')->get();
  1391. return Excel::download(new CompanyInfoExport($list), 'company.xlsx');
  1392. return '123';
  1393. // $id=$request->input('id');
  1394. // $integral=Integral::where('store_id',$id)->sum('integral_double');
  1395. //return $integral;
  1396. //return '123123';
  1397. // foreach($integral as $key=>$val){
  1398. // Store::where('id',$val->id)->update(['integral'=>$val->integral]);
  1399. // }
  1400. //
  1401. // return $integral;
  1402. $integral = Store::leftJoin('integral as inte', function ($query) {
  1403. $query->on('store.id', '=', 'inte.store_id');
  1404. })->where('store.status', 0)
  1405. ->groupBy('store.id')
  1406. ->select('store.id', 'store.name', 'store.img', DB::raw('sum(ifnull(inte.integral_double,0)) as integral'))
  1407. ->orderByDesc('integral')
  1408. ->get();
  1409. // return $integral;
  1410. foreach ($integral as $key => $val) {
  1411. if ($val->integral != Store::where('id', $val->id)->value('integral')) {
  1412. Log::info('123-------789');
  1413. info($val->id . '--' . $val->integral . '--' . Store::where('id', $val->id)->value('integral'));
  1414. // Store::where('id',$val->id)->update(['integral'=>$val->integral]);
  1415. };
  1416. }
  1417. return $integral;
  1418. return '店铺积分汇总结束';
  1419. $date = date("Y-m-d", strtotime('-1 days'));
  1420. $where = function ($query) use ($date) {
  1421. $query->where('order.is_pay', 1)->whereIn('order.apply_cancel', [0, 1, 3])->whereDate('order.pay_at', $date);
  1422. };
  1423. $cellData = Order::where($where)->leftjoin('store', 'order.store_id', '=', 'store.id')
  1424. ->select('store.name', 'store.phone', 'store.img', 'store.user_id', 'store_id', DB::raw('sum(total) as total'), DB::raw('sum(account) as account'))
  1425. ->groupBy('store_id')->having('total', '>=', 2)->orderByDesc('total')->orderBy('store_id')->get();//->orderByDesc('account')
  1426. foreach ($cellData as $key => $val) {
  1427. $level = DwbsUser::where('id', $val->user_id)->value('level');
  1428. $cellData[$key]->level = $level;
  1429. }
  1430. return Excel::download(new IntegralExport($cellData), 'users1.xlsx');
  1431. $date = date("Y-m-d", strtotime('-1 days'));
  1432. $where = function ($query) use ($date) {
  1433. $query->where('order.is_pay', 1)->whereIn('order.apply_cancel', [0, 1, 3])->whereDate('order.created_at', $date);
  1434. };
  1435. $cellData = Order::where($where)->leftjoin('store', 'order.store_id', '=', 'store.id')
  1436. ->select('store.name', 'store.phone', 'store.img', 'store.user_id', 'store_id', DB::raw('sum(total) as total'), DB::raw('sum(account) as account'))
  1437. ->groupBy('store_id')->having('total', '>=', 2)->orderByDesc('total')->get()->toArray();
  1438. foreach ($cellData as $key => $val) {
  1439. $level = DwbsUser::where('id', $val['user_id'])->value('level');
  1440. $cellData[$key]['level'] = $level;
  1441. }
  1442. return $this->success($cellData);
  1443. ini_set('memory_limit', '500M');
  1444. set_time_limit(0);//设置超时限制为0分钟
  1445. // $cellData = MemberFollow::select('xt_name','sex','face')->limit(5)->get()->toArray();
  1446. // $cellData[0] = array('昵称','shouji','头像','头像','头像','头像');
  1447. // for($i=0;$i<count($cellData);$i++){
  1448. // $cellData[$i] = array_values($cellData[$i]);
  1449. // $cellData[$i][0] = str_replace('=',' '.'=',$cellData[$i][0]);
  1450. // }
  1451. // //dd($cellData);
  1452. // Excel::create('用户信息',function($excel) use ($cellData){
  1453. // $excel->sheet('score', function($sheet) use ($cellData){
  1454. // $sheet->rows($cellData);
  1455. // });
  1456. // })->export('xls');
  1457. return '123122222222';
  1458. $orders = OrderDetail::orderBy('id')->skip(0)->take(1000)->get();
  1459. // $orders=OrderDetail::all();
  1460. foreach ($orders as $key => $val) {
  1461. $store = Store::where('id', $val->store_id)->first();
  1462. if (empty($store)) {
  1463. Log::info('失败商品' . $val->order_no . '%%%' . $val->store_id);
  1464. continue;
  1465. }
  1466. $agent = DwbsUser::where('mobile', $store->phone)->first();
  1467. if ($agent->level == 1) {
  1468. $price = Goods::where('id', $val->goods_id)->value('super_price');
  1469. // return '0@'.$price*$val->num;
  1470. OrderDetail::where('id', $val->id)->update([
  1471. 'cost_price' => $price,
  1472. 'cost_account' => $price * $val->num
  1473. ]);
  1474. }
  1475. if ($agent->level == 2) {
  1476. // return '1@'.$price*$val->num;
  1477. $price = Goods::where('id', $val->goods_id)->value('top_price');
  1478. OrderDetail::where('id', $val->id)->update([
  1479. 'cost_price' => $price,
  1480. 'cost_account' => $price * $val->num
  1481. ]);
  1482. }
  1483. if ($agent->level == 3) {
  1484. $price = Goods::where('id', $val->goods_id)->value('crown_price');
  1485. OrderDetail::where('id', $val->id)->update([
  1486. 'cost_price' => $price,
  1487. 'cost_account' => $price * $val->num
  1488. ]);
  1489. // return '2@'.$price*$val->num;
  1490. }
  1491. // return $price*$val->num;
  1492. }
  1493. return $this->success('true');
  1494. // $orders=Order::where('is_pay',1)->get();
  1495. // $oo=[];
  1496. // $nn=0;
  1497. // foreach($orders as $key=>$val){
  1498. // $account= OrderDetail::where('order_no',$val->order_no)->sum('account');
  1499. // if($val->account != $account){
  1500. // $details=OrderDetail::where('order_no',$val->order_no)->get();
  1501. // foreach($details as $k=>$v){
  1502. // $goods=Goods::where('id',$v->goods_id)->first();
  1503. // if($goods){
  1504. // OrderDetail::where('id',$v->id)->update([
  1505. // 'price'=>$goods->vip_price,
  1506. // 'account'=>$v->num*$goods->vip_price,
  1507. // ]);
  1508. // }
  1509. // }
  1510. //
  1511. // $data['order_no']=$val->order_no;
  1512. // $data['account']=$val->account;
  1513. // $data['store_id']=$val->store_id;
  1514. // $data['total']=$val->total;
  1515. // $data['acc']=$account;
  1516. // $nn=$nn+$account-$val->account;
  1517. // $data['ann']=$nn;
  1518. // $data['created_at']=$val->created_at;
  1519. // array_push($oo,$data);
  1520. // }
  1521. // }
  1522. // return $oo;
  1523. 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();
  1524. return OrderFinishFails::where('status', 0)->get();
  1525. $order_nos = Order::has('store')->where('is_pay', 1)->whereIn('apply_cancel', [0, 1, 3])->whereIn('is_refund', [0, 1])->pluck('order_no');
  1526. // $order_details=OrderDetail::with('store:id,name,phone')->whereIn('order_no',$order_nos)->groupBy('store_id')
  1527. // ->select('store_id',DB::raw('ifnull(sum(num),0) as total'),DB::raw('ifnull(sum(account),0) as account'))
  1528. // ->where('goods_id',3)->get();
  1529. $order_details = Store::leftJoin('order_detail as od', function ($query) use ($order_nos) {
  1530. $query->on('od.store_id', '=', 'store.id')
  1531. ->whereIn('order_no', $order_nos)
  1532. ->where('goods_id', 3);
  1533. })
  1534. ->groupBy('store.id')
  1535. ->select('store.id', DB::raw('ifnull(sum(num),0) as total'), DB::raw('ifnull(sum(account),0) as account'))
  1536. ->get();
  1537. return count($order_details);
  1538. // $order=Order::select('id','order_no','user_id','store_id')->get();
  1539. //// return $order;
  1540. // foreach($order as $key=>$val){
  1541. // OrderDetail::where('order_no',$val->order_no)->update([
  1542. // 'user_id'=>$val->user_id,
  1543. // 'store_id'=>$val->store_id,
  1544. // ]);
  1545. // }
  1546. }
  1547. public function exportSaleOrderBys(Request $request){
  1548. $level=$request->input('level');
  1549. $search_name=$request->input('search_name');
  1550. $startime=$request->input('start_time');
  1551. $endtime=$request->input('end_time');
  1552. $sort=$request->input('sort');
  1553. $desc=$request->input('desc');
  1554. $data=Order::has('store')->where('is_pay',1)->whereIn('apply_cancel',[0,1,3])->whereIn('is_refund',[0,1]);
  1555. $admin_wheres=$level_wheres=$search_wheres=[];
  1556. $admin_id=Auth::user()->id;
  1557. $admin=Admin::where('id',$admin_id)->first();
  1558. if($admin && in_array($admin->role_id,[22,24])){//超级管理员
  1559. $admin_wheres=[];
  1560. }else{
  1561. $warea_ids=DwbsWarea::where('admin_id',$admin_id)->pluck('id');
  1562. $agent_ids=DwbsUser::whereIn('warea_id',$warea_ids)->pluck('id');
  1563. $store_ids=Store::whereIn('user_id',$agent_ids)->pluck('id');
  1564. $admin_wheres=function($query)use($store_ids){
  1565. $query->whereIn('store.id',$store_ids);
  1566. };
  1567. }
  1568. if($level){
  1569. $agent_ids=Store::where('status',0)->where('is_apply',3)->pluck('user_id');
  1570. $user_ids=DwbsUser::where('level',$level)->whereIn('id',$agent_ids)->pluck('id');
  1571. $store_ids=Store::whereIn('user_id',$user_ids)->pluck('id');
  1572. $data->whereIn('store_id',$store_ids);
  1573. $level_wheres=function($query)use($store_ids){
  1574. $query->whereIn('store.id',$store_ids);
  1575. };
  1576. }
  1577. if($search_name){
  1578. $store_ids=Store::where('name','like','%'.$search_name.'%')
  1579. ->orWhere('phone','like','%'.$search_name.'%')->pluck('id');
  1580. $data->whereIn('store_id',$store_ids);
  1581. $search_wheres=function($query)use($store_ids){
  1582. $query->whereIn('store.id',$store_ids);
  1583. };
  1584. }
  1585. if($startime && $endtime){
  1586. $data->whereBetween('created_at',[$startime,$endtime]);
  1587. }
  1588. $order_nos=$data->pluck('order_no');
  1589. if($sort=='hard'){
  1590. $where['goods_id']=1;
  1591. }elseif($sort=='simple'){
  1592. $where['goods_id']=2;
  1593. }elseif($sort=='old'){
  1594. $where['goods_id']=3;
  1595. }elseif($sort=='cotton'){
  1596. $where['goods_id']=4;
  1597. }else{
  1598. $where=[];
  1599. }
  1600. $order_details=Store::where('status',0)->where('is_apply',3)
  1601. ->where($admin_wheres)->where($level_wheres)->where($search_wheres)
  1602. ->leftJoin('order_detail as od',function($query)use($order_nos,$where){
  1603. $query->on('od.store_id','=','store.id')
  1604. ->whereIn('order_no',$order_nos)
  1605. ->where($where);
  1606. })
  1607. ->groupBy('store.id')
  1608. ->select('store.id as store_id',DB::raw('ifnull(sum(num),0) as total'),DB::raw('ifnull(sum(account),0) as account'));
  1609. if($sort=='total' || $sort=='hard' || $sort=='simple' || $sort=='old' || $sort=='cotton'){
  1610. if($desc==1){
  1611. $order_details->OrderBy('total');
  1612. }else{
  1613. $order_details->OrderByDesc('total');
  1614. }
  1615. }elseif($sort=='account'){
  1616. if($desc==1){
  1617. $order_details->OrderBy('account');
  1618. }else{
  1619. $order_details->OrderByDesc('account');
  1620. }
  1621. }
  1622. $list=$order_details->get();
  1623. $all['all_account']=OrderDetail::whereIn('order_no',$order_nos)->sum('account');
  1624. $all['all_num']=OrderDetail::whereIn('order_no',$order_nos)->sum('num');
  1625. $all['hard_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',1)->whereIn('order_no',$order_nos)->sum('num');
  1626. $all['simple_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',2)->whereIn('order_no',$order_nos)->sum('num');
  1627. $all['old_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',3)->whereIn('order_no',$order_nos)->sum('num');
  1628. $all['cotton_num']=OrderDetail::with('store:id,name,phone')->where('goods_id',4)->whereIn('order_no',$order_nos)->sum('num');
  1629. foreach($list as $key=>$val){
  1630. $order=Order::with('store')->where('store_id',$val->store_id)->whereIn('order_no',$order_nos)
  1631. ->select(DB::raw('ifnull(sum(total),0) as total'),DB::raw('ifnull(sum(account),0) as account'))->first();
  1632. $list[$key]=$order;
  1633. if($order->total==0 && $order->account==0){
  1634. $list[$key]->goods=OrderDetail::with('goods:id,name,main_attr')
  1635. ->where('store_id',$val->store_id)->whereIn('order_no',$order_nos)->groupBy('goods_id')
  1636. ->select('goods_id',DB::raw('ifnull(sum(num),0) as num'),DB::raw('ifnull(sum(account),0) as account'))->get();
  1637. $detail=OrderDetail::with('goods:id,name,main_attr')
  1638. ->where('store_id',$val->store_id)->whereIn('order_no',$order_nos)->groupBy('goods_id','type','size')
  1639. ->select('goods_id','type','size',DB::raw('ifnull(sum(num),0) as num'),DB::raw('ifnull(sum(account),0) as account'))->get();
  1640. $list[$key]->detail=$detail;
  1641. }else{
  1642. $list[$key]->goods=[];
  1643. $list[$key]->detail=[];
  1644. }
  1645. $second=mt_rand(2880,7200);
  1646. if(Cache::has('level-'.$val->store->user_id)){
  1647. $list[$key]->level=Cache::get('level-'.$val->store->user_id);
  1648. }else{
  1649. // $user_id=Store::where('id',$val->store_id)->value('user_id');
  1650. $level=DwbsUser::where('id',$val->store->user_id)->value('level');
  1651. $list[$key]->level=$level;
  1652. Cache::add('level-'.$val->store->user_id, $level, $second);
  1653. }
  1654. if(Cache::has('store-'.$val->store_id)){
  1655. $list[$key]->store=Cache::get('store-'.$val->store->user_id);
  1656. }else{
  1657. $store=Store::where('id',$val->store_id)->select('id','name','phone','province','city','area')->first();
  1658. $list[$key]->store=$store;
  1659. Cache::add('store-'.$val->store_id, $store, $second);
  1660. }
  1661. }
  1662. $all['list']=$list;
  1663. return $this->success($all);
  1664. }
  1665. }