package com.stylefeng.guns.modular.system.controller.general; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.stylefeng.guns.GunsApplication; import com.stylefeng.guns.core.base.controller.BaseController; import com.stylefeng.guns.core.common.constant.state.Order; import com.stylefeng.guns.core.shiro.ShiroKit; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.system.controller.resp.TOrderServerResp; import com.stylefeng.guns.modular.system.controller.util.HttpUtils; import com.stylefeng.guns.modular.system.enums.OrderStateEnum; import com.stylefeng.guns.modular.system.enums.StatusEnum; import com.stylefeng.guns.modular.system.model.*; import com.stylefeng.guns.modular.system.pojo.GaoDePoJo; import com.stylefeng.guns.modular.system.service.*; import com.stylefeng.guns.modular.system.util.DateUtil; import com.stylefeng.guns.modular.system.util.RedisUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.text.SimpleDateFormat; import java.time.Duration; import java.time.LocalDate; import java.time.Period; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; /** * 控制器 * * @author fengshuonan * @Date 2023-03-14 15:15:19 */ @Controller @RequestMapping("/tHomePage") public class THomePageController extends BaseController { private final static Logger log = LoggerFactory.getLogger(THomePageController.class); @Autowired private IUserService userService; @Autowired private ITAgentService tAgentService; @Autowired private ITBranchOfficeService branchOfficeService; @Autowired private ITDriverService tDriverService; @Autowired private ITOrderService tOrderService; @Autowired private ITRevenueService tRevenueService; @Autowired private ITDriverWorkService tDriverWorkService; @Autowired private GaoDePoJo gaoDePoJo; @Autowired private HttpUtils httpUtils; @Autowired private RedisUtil redisUtil; private String PREFIX = "/system/tHomePage/"; /** * 跳转到地图 */ @RequestMapping("/map") public String map(Integer agentId,Integer type,Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 3 && Objects.nonNull(objectId)){ agentId = objectId; } // 查询所有代理商 List agentList = branchOfficeService.selectList(new EntityWrapper() .ne("status", StatusEnum.DELETE.getCode()) .ne("principal","")); model.addAttribute("agentList",agentList); // 查询广西省广州市的地址解析 Map map = geocode("广西省柳州市"); model.addAttribute("addressLon",map.get("addressLon")); model.addAttribute("addressLat",map.get("addressLat")); // 查询广西全区域数据 查询广西所有的代理商 Wrapper status = new EntityWrapper().eq("status", 1); if(null != agentId){ status.eq("id", agentId); } List tAgentList = branchOfficeService.selectList(status); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); // 查询统计在线司机,待接单,服务中,已完成,已取消 List onLineDriver = tDriverService.selectList(new EntityWrapper() .in("branchOfficeId", ids)); List tAgentAllList = branchOfficeService.selectList(new EntityWrapper()); List allIds = tAgentAllList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); tOrderService.getDataStatisticsCountByIds(allIds,model); // 查询服务中的订单列表 List orderServerRespList = tOrderService.getDataStatisticsServerListByIds(allIds); List orderList = tOrderService.selectList(new EntityWrapper()); // 查询司机 for (TOrderServerResp tOrderServerResp : orderServerRespList) { TDriver tDriver = tDriverService.selectById(tOrderServerResp.getDriverId()); tOrderServerResp.setAvatar(tDriver.getAvatar()); tOrderServerResp.setDriverName(tDriver.getName()); List collect = orderList.stream().filter(order -> tDriver.getId().equals(order.getDriverId()) && (order.getState().equals(OrderStateEnum.WAIT_PAY.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()) || order.getState().equals(OrderStateEnum.FINISH.getCode()))).collect(Collectors.toList()); // 驾龄 Period period = Period.between(LocalDate.now(), DateUtil.dateToLocalDate(tDriver.getFirstCertificateTime())); tOrderServerResp.setDrivingExperience(Math.max(Math.abs(period.getYears()), 1)); // 代驾次数 tOrderServerResp.setValetDrivingCount(collect.size()); // 行驶时间 long goTime = 0; for (TOrder tOrder : collect) { // 下车时间 Date getoffTime = tOrder.getGetoffTime(); // 上车时间 Date boardingTime = tOrder.getBoardingTime(); long minutes = Duration.between(DateUtil.dateToLocalDate(getoffTime), DateUtil.dateToLocalDate(boardingTime)).toMinutes(); goTime += Math.abs(minutes); } tOrderServerResp.setGoTime(Long.valueOf(goTime).intValue()); // 行驶里程 int sum = collect.stream().mapToInt(TOrder::getActualMileage).sum(); tOrderServerResp.setMileageTraveled(sum/1000); } model.addAttribute("serverList",orderServerRespList); // 查询今天所有订单 List allList = tOrderService.getDataStatisticsAllListByIds(ids,type); List> list = new ArrayList<>(allList.size()); for (TOrder tOrder : allList) { List obj = new ArrayList<>(3); obj.add(tOrder.getStartLng()); obj.add(tOrder.getStartLat()); obj.add(String.valueOf(tOrder.getState())); list.add(obj); } model.addAttribute("allList",list); List> list1 = new ArrayList<>(); AtomicInteger count = new AtomicInteger(0); for (TDriver tDriver : onLineDriver) { String value1 = redisUtil.getValue("DRIVER_" + tDriver.getPhone()); String value2 = redisUtil.getValue(value1); if(ToolUtil.isNotEmpty(value2)){ count.addAndGet(1); } List obj = new ArrayList<>(3); String value = redisUtil.getValue("DRIVER" + tDriver.getId()); if(ToolUtil.isEmpty(value)){ continue; } obj.add(value.split(",")[0]);// 经度 obj.add(value.split(",")[1]);// 纬度 obj.add(String.valueOf(tDriver.getServerStatus()));// 服务状态 list1.add(obj); } model.addAttribute("onLineDriverCount",count.get()); // 拿到所有在线司机地址 model.addAttribute("onLineDriver",list1); return PREFIX + "tHomePageMap.html"; } @RequestMapping("/getMap") @ResponseBody public Object getMap(Integer agentId) { Map maps = new HashMap<>(); Wrapper last = new EntityWrapper().eq("state", 301).eq("status", 1); if(null != agentId){ last.eq("branchOfficeId", agentId); TBranchOffice tBranchOffice = branchOfficeService.selectById(agentId); maps.put("code", tBranchOffice.getCityCode()); } List tOrders = tOrderService.selectList(last.last(" and UNIX_TIMESTAMP(createTime) + 7200 >= UNIX_TIMESTAMP(NOW())")); List> cancel = new ArrayList<>(); for (TOrder order : tOrders) { Map map = new HashMap<>(); map.put("lonLat", order.getStartLng() + "," + order.getStartLat()); cancel.add(map); } maps.put("cancel", cancel); Wrapper eq = new EntityWrapper().eq("serverStatus", 1).eq("approvalStatus", 2).eq("status", 1); if(null != agentId){ eq.eq("branchOfficeId", agentId); } List tDrivers = tDriverService.selectList(eq); List> leisure = new ArrayList<>(); for (TDriver tDriver : tDrivers) { String value = redisUtil.getValue("DRIVER" + tDriver.getId()); if(ToolUtil.isEmpty(value)){ continue; } Map map = new HashMap<>(); map.put("name", tDriver.getName()); map.put("lonLat", value); leisure.add(map); } maps.put("leisure", leisure); Wrapper en = new EntityWrapper().in("state", Arrays.asList(103, 104, 105, 106)).eq("status", 1); if(null != agentId){ en.eq("branchOfficeId", agentId); } List tOrders1 = tOrderService.selectList(en); List> receivedOrder = new ArrayList<>(); for (TOrder order : tOrders1) { String value = redisUtil.getValue("DRIVER" + order.getDriverId()); if(ToolUtil.isEmpty(value)){ continue; } TDriver tDriver = tDriverService.selectById(order.getDriverId()); Map map = new HashMap<>(); map.put("name", tDriver.getName()); map.put("lonLat", value); receivedOrder.add(map); } maps.put("receivedOrder", receivedOrder); Wrapper en1 = new EntityWrapper().eq("state", 102).eq("status", 1); if(null != agentId){ en1.eq("branchOfficeId", agentId); } List tOrders2 = tOrderService.selectList(en1); List> service = new ArrayList<>(); for (TOrder order : tOrders2) { TDriver tDriver = tDriverService.selectById(order.getDriverId()); Map map = new HashMap<>(); map.put("name", tDriver.getName()); map.put("lonLat", order.getStartLng() + "," + order.getStartLat()); service.add(map); } maps.put("service", service); List tOrders3 = tOrderService.selectList(new EntityWrapper().eq("state", 101).eq("hallOrder", 1).eq("status", 1)); List> lobby = new ArrayList<>(); for (TOrder order : tOrders3) { Map map = new HashMap<>(); map.put("lonLat", order.getStartLng() + "," + order.getStartLat()); lobby.add(map); } maps.put("lobby", lobby); return maps; } @RequestMapping("/getServerOrder") @ResponseBody public Object getServerOrder(Integer agentId,Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 3 && Objects.nonNull(objectId)){ agentId = objectId; } HashMap map = new HashMap<>(); if(Objects.nonNull(agentId)){ List tAgentList = branchOfficeService.selectList(new EntityWrapper()); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); // 查询服务中的订单列表 List orderServerRespList = tOrderService.getDataStatisticsServerListByIds(ids); List orderList = tOrderService.selectList(new EntityWrapper()); // 查询司机 for (TOrderServerResp tOrderServerResp : orderServerRespList) { TDriver tDriver = tDriverService.selectById(tOrderServerResp.getDriverId()); tOrderServerResp.setAvatar(tDriver.getAvatar()); tOrderServerResp.setDriverName(tDriver.getName()); List collect = orderList.stream().filter(order -> tDriver.getId().equals(order.getDriverId()) && (order.getState().equals(OrderStateEnum.WAIT_PAY.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()) || order.getState().equals(OrderStateEnum.FINISH.getCode()))).collect(Collectors.toList()); // 驾龄 if(Objects.nonNull(tDriver.getFirstCertificateTime())){ Period period = Period.between(LocalDate.now(), DateUtil.dateToLocalDate(tDriver.getFirstCertificateTime())); tOrderServerResp.setDrivingExperience(Math.max(Math.abs(period.getYears()), 1)); }else { tOrderServerResp.setDrivingExperience(0); } // 代驾次数 tOrderServerResp.setValetDrivingCount(collect.size()); // 行驶时间 long goTime = 0; for (TOrder tOrder : collect) { // 下车时间 Date getoffTime = tOrder.getGetoffTime(); // 上车时间 Date boardingTime = tOrder.getBoardingTime(); long minutes = Duration.between(DateUtil.dateToLocalDateTime(getoffTime), DateUtil.dateToLocalDateTime(boardingTime)).toMinutes(); goTime += Math.abs(minutes); } tOrderServerResp.setGoTime(Long.valueOf(goTime).intValue()); // 行驶里程 if(CollectionUtils.isEmpty(collect)){ tOrderServerResp.setMileageTraveled(0); }else { int sum = collect.stream().mapToInt(TOrder::getActualMileage).sum(); tOrderServerResp.setMileageTraveled(sum/1000); } } map.put("serverList",orderServerRespList); }else { List tAgentList = branchOfficeService.selectList(new EntityWrapper()); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); // 查询服务中的订单列表 List orderServerRespList = tOrderService.getDataStatisticsServerListByIds(ids); List orderList = tOrderService.selectList(new EntityWrapper()); // 查询司机 for (TOrderServerResp tOrderServerResp : orderServerRespList) { TDriver tDriver = tDriverService.selectById(tOrderServerResp.getDriverId()); tOrderServerResp.setAvatar(tDriver.getAvatar()); tOrderServerResp.setDriverName(tDriver.getName()); List collect = orderList.stream().filter(order -> tDriver.getId().equals(order.getDriverId()) && (order.getState().equals(OrderStateEnum.WAIT_PAY.getCode()) || order.getState().equals(OrderStateEnum.WAIT_EVALUATED.getCode()) || order.getState().equals(OrderStateEnum.FINISH.getCode()))).collect(Collectors.toList()); // 驾龄 if(Objects.nonNull(tDriver.getFirstCertificateTime())){ Period period = Period.between(LocalDate.now(), DateUtil.dateToLocalDate(tDriver.getFirstCertificateTime())); tOrderServerResp.setDrivingExperience(Math.max(Math.abs(period.getYears()), 1)); }else { tOrderServerResp.setDrivingExperience(0); } // 代驾次数 tOrderServerResp.setValetDrivingCount(collect.size()); // 行驶时间 long goTime = 0; for (TOrder tOrder : collect) { // 下车时间 Date getoffTime = tOrder.getGetoffTime(); // 上车时间 Date boardingTime = tOrder.getBoardingTime(); long minutes = Duration.between(DateUtil.dateToLocalDateTime(getoffTime), DateUtil.dateToLocalDateTime(boardingTime)).toMinutes(); goTime += Math.abs(minutes); } tOrderServerResp.setGoTime(Long.valueOf(goTime).intValue()); // 行驶里程 if(CollectionUtils.isEmpty(collect)){ tOrderServerResp.setMileageTraveled(0); }else { int sum = collect.stream().mapToInt(TOrder::getActualMileage).sum(); tOrderServerResp.setMileageTraveled(sum/1000); } } map.put("serverList",orderServerRespList); } return map; } /** * 跳转到首页统计 */ @RequestMapping("/statistics") public String statistics(Integer agentId,String dayDate,String yearDate,String monthDate,Integer type,Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 3 && Objects.nonNull(objectId)){ agentId = objectId; } if(Objects.isNull(type)){ type = 1; } model.addAttribute("txt",new SimpleDateFormat("yyyy年MM月dd日").format(new Date()) + DateUtil.getWeekDay(new Date()) + ",欢迎" + Objects.requireNonNull(ShiroKit.getUser()).getName() + "登录"); // 查询所有代理商 List agentList = branchOfficeService.selectList(new EntityWrapper() .ne("status", StatusEnum.DELETE.getCode()) .ne("principal","")); model.addAttribute("agentList",agentList); Map map = new HashMap<>(); if(Objects.nonNull(agentId)){ // 统计代理商 branchOfficeService.getDataStatistics(agentId,model,map); // 统计司机数 tDriverService.getDataStatistics(agentId,model,map); // 订单统计本月 tOrderService.getStatisticsOrderByMonth(agentId,dayDate,model,map); // 订单统计,每年按月份 tOrderService.getDataStatisticsByYear(agentId,yearDate,model,map); // 业绩排名 performanceTable if (1 == type){ // 单量 tOrderService.getDataStatisticsOrderCount(agentId,monthDate,model,map); }else { // 收入、佣金 tRevenueService.getDataStatisticsIncomeOrCommission(agentId,monthDate,type,model,map); } }else { // 查询广西全区域数据 查询广西所有的代理商 List tAgentList = branchOfficeService.selectList(new EntityWrapper() .eq("provinceCode", "450000")); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); // 统计代理商 branchOfficeService.getDataStatisticsByIds(ids,model,map); // 统计司机数 tDriverService.getDataStatisticsByIds(ids,model,map); // 订单统计本月 tOrderService.getStatisticsOrderByMonthIds(ids,dayDate,model,map); // 订单统计,每年按月份 tOrderService.getDataStatisticsByYearByIds(ids,yearDate,model,map); // 业绩排名 performanceTable if (1 == type){ // 单量 tOrderService.getDataStatisticsOrderCountByIds(ids,monthDate,model,map); }else { // 收入、佣金 tRevenueService.getDataStatisticsIncomeOrCommissionByIds(ids,monthDate,type,model,map); } } return PREFIX + "tHomePageStatistics.html"; } /** * 首页统计通过代理商查询 */ @RequestMapping("/statisticsByAgentId") @ResponseBody public Map statisticsByAgentId(Integer agentId, String dayDate, String yearDate, String monthDate, Integer type, Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 2 && Objects.nonNull(objectId)){ agentId = objectId; } Map map = new HashMap<>(); if(Objects.nonNull(agentId)){ // 统计代理商 branchOfficeService.getDataStatistics(agentId,model,map); // 统计司机数 tDriverService.getDataStatistics(agentId,model,map); // 订单统计本月 tOrderService.getStatisticsOrderByMonth(agentId,dayDate,model,map); // 订单统计,每年按月份 tOrderService.getDataStatisticsByYear(agentId,yearDate,model,map); // 业绩排名 performanceTable if (1 == type){ // 单量 tOrderService.getDataStatisticsOrderCount(agentId,monthDate,model,map); }else { // 收入、佣金 tRevenueService.getDataStatisticsIncomeOrCommission(agentId,monthDate,type,model,map); } } return map; } /** * 首页统计通过代理商查询订单量 今天,昨天,本周,本月 */ @RequestMapping("/statisticsMonthByAgentId") @ResponseBody public Map statisticsMonthByAgentId(Integer agentId, String dayDate,Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 2 && Objects.nonNull(objectId)){ agentId = objectId; } dayDate = new SimpleDateFormat("yyyy-MM").format(new Date()); Map map = new HashMap<>(); if(Objects.nonNull(agentId)){ // 订单统计本月 tOrderService.getStatisticsOrderByMonth(agentId,dayDate,model,map); }else { // 查询广西全区域数据 查询广西所有的代理商 List tAgentList = branchOfficeService.selectList(new EntityWrapper() .eq("provinceCode", "450000")); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); // 订单统计本月 tOrderService.getStatisticsOrderByMonthIds(ids,dayDate,model,map); } return map; } /** * 首页统计通过代理商查询订单量 按年 */ @RequestMapping("/statisticsYearByAgentId") @ResponseBody public Map statisticsYearByAgentId(Integer agentId, String yearDate,Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 2 && Objects.nonNull(objectId)){ agentId = objectId; } if(!StringUtils.hasLength(yearDate)){ yearDate = new SimpleDateFormat("yyyy").format(new Date()); } Map map = new HashMap<>(); if(Objects.nonNull(agentId)){ // 订单统计,每年按月份 tOrderService.getDataStatisticsByYear(agentId,yearDate,model,map); }else { // 查询广西全区域数据 查询广西所有的代理商 List tAgentList = branchOfficeService.selectList(new EntityWrapper() .eq("provinceCode", "450000")); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); tOrderService.getDataStatisticsByYearByIds(ids,yearDate,model,map); } return map; } /** * 首页统计通过代理商查询订单量,收入,佣金 按年 */ @RequestMapping("/statisticsOrderByAgentId") @ResponseBody public Map statisticsOrderByAgentId(Integer agentId, String monthDate,Integer type,Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 2 && Objects.nonNull(objectId)){ agentId = objectId; } if(Objects.isNull(type)){ type = 1; } if(!StringUtils.hasLength(monthDate)){ monthDate = new SimpleDateFormat("yyyy-MM").format(new Date()); } Map map = new HashMap<>(); if(Objects.nonNull(agentId)){ // 业绩排名 performanceTable if (1 == type){ // 单量 tOrderService.getDataStatisticsOrderCount(agentId,monthDate,model,map); }else { // 收入、佣金 tRevenueService.getDataStatisticsIncomeOrCommission(agentId,monthDate,type,model,map); } }else { // 业绩排名 performanceTable // 查询广西全区域数据 查询广西所有的代理商 List tAgentList = branchOfficeService.selectList(null); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); if (1 == type){ // 单量 tOrderService.getDataStatisticsOrderCountByIds(ids,monthDate,model,map); }else { // 收入、佣金 tRevenueService.getDataStatisticsIncomeOrCommissionByIds(ids,monthDate,type,model,map); } } return map; } /** * 首页统计通过代理商查询司机数量 */ @RequestMapping("/statisticsDriverByAgentId") @ResponseBody public Map statisticsDriverByAgentId(Integer agentId,Model model) { Integer objectId = ShiroKit.getUser().getObjectId(); Integer roleType = ShiroKit.getUser().getRoleType(); if(roleType == 2 && Objects.nonNull(objectId)){ agentId = objectId; } Map map = new HashMap<>(); if(Objects.nonNull(agentId)){ // 统计司机数 tDriverService.getDataStatistics(agentId,model,map); tAgentService.getDataStatistics(agentId,model,map); }else { // 统计司机数 // 查询广西全区域数据 查询广西所有的代理商 List tAgentList = branchOfficeService.selectList(new EntityWrapper() .eq("provinceCode", "450000")); List ids = tAgentList.stream().map(TBranchOffice::getId).collect(Collectors.toList()); tDriverService.getDataStatisticsByIds(ids,model,map); tAgentService.getDataStatisticsByIds(ids,model,map); } return map; } private Map geocode(String address){ Map map = new HashMap<>(2); try { String url = "https://restapi.amap.com/v3/geocode/geo?address=" + address + "&key=" + gaoDePoJo.getKey(); url = url.trim().replace("#", "").replace(" ", ""); String result = httpUtils.get(url); log.info("地址解析结果:{}", result); JSONObject jsonObject = JSONObject.parseObject(result); if (jsonObject.getString("status").equals("1")) { String geocodes = jsonObject.getString("geocodes"); JSONArray objects = JSONObject.parseArray(geocodes); String string = objects.getString(0); JSONObject geocodesObject = JSONObject.parseObject(string); log.info("地址数据:{}", geocodesObject); String location = geocodesObject.getString("location"); String[] split = location.split(","); map.put("addressLon",split[0]); map.put("addressLat",split[1]); } } catch (Exception e) { log.error("解析地址失败============:{}", e.getMessage()); e.printStackTrace(); } return map; } }