package com.moral.controller; import com.auth0.jwt.exceptions.JWTDecodeException; import com.moral.common.bean.ResultBean; import com.moral.common.exception.WebAuthException; import com.moral.common.util.*; import com.moral.common.webAnno.UserLoginToken; import com.moral.entity.AreaNames; import com.moral.entity.Device; import com.moral.entity.MonitorPoint; import com.moral.entity.Organization; import com.moral.service.*; import com.moral.util.LatLngTransformation; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import jdk.nashorn.internal.runtime.logging.Logger; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import static com.moral.common.util.WebUtils.getParametersStartingWith; @RestController @RequestMapping("/web") @CrossOrigin(origins = "*", maxAge = 3600) @SuppressWarnings({"rawtypes", "unchecked", "unused"}) public class WebController { @Resource AccountService accountService; @Resource DictionaryDataService dictionaryDataService; OrganizationService organizationService; @Resource WebTokenService webTokenService; @Resource RedisHashUtil redisHashUtil; @Resource HistoryMinutelyService historyMinutelyService; @Resource MonitorPointService monitorPointService; @Resource DeviceService deviceService; @UserLoginToken @GetMapping("test") public String add() { return "test success!"; } @PostMapping("login") public Map login(@RequestBody Map parameters) { Map resultMap = new HashMap(); if (!(parameters.containsKey("account") && parameters.containsKey("password"))) { resultMap.put("msg", "用户名及密码不允许为空!"); resultMap.put("accountId", -1); } else { resultMap = accountService.webLogin(parameters); String accountId = String.valueOf(resultMap.get("accountId")); if (!accountId.equals("-1")) { redisHashUtil.deleteMapVal("webToken",accountId); resultMap.put("token", webTokenService.getToken(accountId)); } } return resultMap; } @UserLoginToken @PostMapping("logout") public Map logout(HttpServletRequest request) { Map resultMap = new HashMap<>(); String token = request.getHeader("token"); String id = WebTokenUtils.getIdBytoken(token); redisHashUtil.addMapOne("webToken", String.valueOf(id),token); resultMap.put("msg", "退出成功!"); return resultMap; } @UserLoginToken @GetMapping("getAccountInfo") public Map getAccountInfo(HttpServletRequest request) { String token = request.getHeader("token"); String id = ""; try { id = WebTokenUtils.getIdBytoken(token); } catch (JWTDecodeException e) { throw new WebAuthException("401,token无效"); } Map resultMap = accountService.getAccountInfoById(id); Object orgId = resultMap.get("orgId"); if (resultMap.get("orgId") != null && resultMap.get("orgId") instanceof Integer) { StringBuilder areaNamesBuilder = new StringBuilder("中国"); //判断是否为本公司开发者 if (!((Integer) orgId).equals(dictionaryDataService.querySupperOrgId())) { //不是本公司开发者则获取用户所属地区 Organization organization = organizationService.getOrganizationById((Integer) orgId); if (organization.getAreaNames() != null) { Map areaNameMap = BeanUtils.beanToMap(organization.getAreaNames()); List names = areaNameMap.entrySet().stream().filter(item -> { return item.getValue() != null; }).map(item -> { return item.getValue(); }).collect(Collectors.toList()); AreaNames areaNames = organization.getAreaNames(); areaNamesBuilder.append("/"); areaNamesBuilder.append(String.join("/", names)); } // 企业用户 if (organization.getRank() != null && organization.getRank() == 0) { resultMap.put("type", "enterprise"); } else { resultMap.put("type", "government"); } Number mapAreaCode = null; if (organization.getVillageCode() != null) { mapAreaCode = organization.getVillageCode(); } else if (organization.getTownCode() != null) { mapAreaCode = organization.getTownCode(); } else if (organization.getAreaCode() != null) { mapAreaCode = organization.getAreaCode(); } else if (organization.getCityCode() != null) { mapAreaCode = organization.getCityCode(); } else if (organization.getProvinceCode() != null) { mapAreaCode = organization.getProvinceCode(); } resultMap.put("mapAreaCode", mapAreaCode.toString()); } resultMap.put("mapPath", areaNamesBuilder.toString()); String accountId = String.valueOf(resultMap.get("accountId")); resultMap.put("token", webTokenService.getToken(accountId)); } return resultMap; } @UserLoginToken @GetMapping("report_avg_datas") public ResultBean>> getMonitorPointOrDeviceAvgData(HttpServletRequest request) throws Exception { Map parameters = WebUtils.getParametersStartingWith(request, null); //该方法用于判断时间是具体到年月日 ParameterUtils.getTimeType4Time(parameters); Object sensorKey = parameters.remove("sensorKey"); parameters.put("sensors", Arrays.asList(sensorKey)); List> list = historyMinutelyService.getMonitorPointOrDeviceAvgData(parameters); for (Map map : list) { String time = map.get("time").toString(); time = time.substring(time.length() - 2); map.put("time", Integer.valueOf(time)); if(parameters.get("type").equals("day")){ map.put("time", Integer.valueOf(time)+1); } map.put("value", map.remove(sensorKey)); } return new ResultBean>>(list); } @UserLoginToken @GetMapping("monitorpoints-devices") public ResultBean> getMonitorPointsAndDevicesByRegion(HttpServletRequest request) throws Exception { Map parameters = WebUtils.getParametersStartingWith(request, null); List monitorPoints = monitorPointService.getMonitorPointsAndDevicesByRegion(parameters); return new ResultBean>(monitorPoints); } /* @UserLoginToken*/ @GetMapping("coordinates") public ResultBean> getDeviceCoordinatesAndState(HttpServletRequest request){ Map parameters = WebUtils.getParametersStartingWith(request, null); ParameterUtils.getRegionType4RegionCode(parameters); return new ResultBean>(deviceService.queryDevice(parameters)); } }