diff --git a/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java b/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java index e93f2e177..ff97e7722 100644 --- a/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java +++ b/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/controllers/ApiUserController.java @@ -1,11 +1,13 @@ package com.fangjia.bkoff.controllers; +import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.ExcelWriter; +import com.alibaba.excel.util.DateUtils; +import com.alibaba.excel.write.metadata.WriteSheet; import com.fangjia.bkoff.model.ApiUserModel; import com.fangjia.bkoff.model.JsonModel; -import com.fangjia.core.util.BeanUtils; -import com.fangjia.core.util.DataUtil; -import com.fangjia.core.util.DateUtil; -import com.fangjia.core.util.StringUtil; +import com.fangjia.core.util.*; +import com.fangjia.dal.QueryParam; import com.fangjia.dal.mongo.fangjia.model.ServerArea; import com.fangjia.dal.mongo.model.ApiInvokeStatistical; import com.fangjia.dal.mongo.model.ApiUserCfg; @@ -27,23 +29,33 @@ import com.fangjia.service.sql.UserService; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; -import org.apache.commons.lang.time.DateUtils; import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.bson.types.ObjectId; import org.json.JSONArray; +import org.sothis.core.util.CollectionUtils; import org.sothis.web.mvc.ActionContext; import org.sothis.web.mvc.annotation.Param; +import scala.Int; +import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.math.BigDecimal; import java.net.URLEncoder; +import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.*; + /** * api用户开通等管理controller * @@ -62,7 +74,6 @@ public class ApiUserController { private ApiVisitCountService apiVisitCountService; private ApiCountCityService apiCountCityService; - /** * 所有api用户的信息展示 * @@ -78,7 +89,21 @@ public class ApiUserController { * @param ApiUser * :用户的appKey username、等资料 */ - List apiUsers = this.apiUserService.findApiUsersByParamsSortDate(null); + ArrayList queryParams = new ArrayList(); + if (model.getStatus() != null) { + queryParams.add(new QueryParam("status", model.getStatus())); + }else { + queryParams.add(new QueryParam("status",0)); + } + if (model.getIsTest()!=null){ + queryParams.add(new QueryParam("isTest",model.getIsTest())); + }else { + queryParams.add(new QueryParam("isTest",0)); + } + if (model.getPurpose()!=null){ + queryParams.add(new QueryParam("purpose",model.getPurpose())); + } + List apiUsers = this.apiUserService.findApiUsersByParamsSortDate(queryParams); List apiUserBoList = new ArrayList(); for (ApiUser apiUser : apiUsers) { User user = this.userService.findById(apiUser.getId()); @@ -184,6 +209,8 @@ public class ApiUserController { apiUser.setDeadTime(model.getDeadTime()); apiUser.setManager(model.getManager()); apiUser.setIsMonitor(model.getIsMonitor()); + apiUser.setContact(model.getContact()); + apiUser.setPhone(model.getPhone()); this.apiUserService.save(apiUser); // 新增用户,默认挂牌价改为默认评估价 model.setPriceType("evaluate"); @@ -227,6 +254,8 @@ public class ApiUserController { bo.setDeadTime(apiUser.getDeadTime()); bo.setManager(apiUser.getManager()); bo.setIsMonitor(apiUser.getIsMonitor()); + bo.setContact(apiUser.getContact()); + bo.setPhone(apiUser.getPhone()); ApiUserCfg apiUserCfg_rate = apiUserCfgService.findByUserId(apiUser.getId(), ApiUserCfg.ApiUserCfgKey.EVALUATE_RATE); if (apiUserCfg_rate != null) { bo.setEvaluateRate((Double) apiUserCfg_rate.getCfgValue()); @@ -280,6 +309,8 @@ public class ApiUserController { dataApiUser.put("deadTime", model.getDeadTime()); dataApiUser.put("manager", model.getManager()); dataApiUser.put("isMonitor", model.getIsMonitor()); + dataApiUser.put("contact", model.getContact()); + dataApiUser.put("phone", model.getPhone()); this.apiUserService.update(dataApiUser, userId); // 更改用户密码 // this.userService.updateById(user,userId); @@ -303,14 +334,15 @@ public class ApiUserController { user.put("password", DigestUtils.md5Hex(model.getPassword())); // // // 更改用户密码 - this.userService.updateById(user,userId); + this.userService.updateById(user, userId); // 刷新缓存 this.apiUserService.clearApiUserAuthorityCache(userId); model.setViewType("redirect"); - model.setViewParam("location", "/apiUser/edit?userId="+userId); + model.setViewParam("location", "/apiUser/edit?userId=" + userId); return model; } + public Object editPwdAction(ApiUserModel model) { Long userId = model.getUserId(); User user = this.userService.findById(userId); @@ -975,7 +1007,21 @@ public class ApiUserController { } else { cities = new ArrayList(); } - List analys = openUriAnalyService.findOpenUriAnalyLogs(model.getUserId(), null, startDate, endDate); + // 只查询正式账号 + List queryParams = Arrays.asList(new QueryParam("isTest", 0)); + List apiUsers = this.apiUserService.findApiUsersByParamsSortDate(queryParams); + // 获取正式账号的ids + List ids = new ArrayList(); + for (ApiUser apiUser : apiUsers) { + ids.add((int) apiUser.getId()); + } + List analyAll = openUriAnalyService.findOpenUriAnalyLogs(model.getUserId(), null, startDate, endDate); + List analys = new ArrayList(); + for (OpenUriAnaly analy : analyAll) { + if (ids.contains(analy.getApiUserId())) { + analys.add(analy); + } + } List bos = null; List eachCityBos = null; if (model.getWay() == null) { @@ -1016,9 +1062,7 @@ public class ApiUserController { } } } - List userIds = new ArrayList(); for (StatAnalyBo boTemp : bos) { - userIds.add(Long.valueOf(boTemp.getUserId())); List apiVisitCounts = apiVisitCountService.findAllApiVisitCount(Long.valueOf(boTemp.getUserId()), null, startDate, endDate); int total = 0; for (ApiVisitCount apiVisitCount : apiVisitCounts) { @@ -1027,7 +1071,6 @@ public class ApiUserController { boTemp.setBillingCount(total); } - List apiUsers = apiUserService.findApiUserByIds(userIds); Map idUserMap = new HashMap(); for (ApiUser apiUser : apiUsers) { idUserMap.put(apiUser.getId(), apiUser); @@ -1051,8 +1094,234 @@ public class ApiUserController { return model; } + public Object exportAction(ApiUserModel model, HttpServletResponse response) throws IllegalAccessException { + Date startDate = null; + Date endDate = null; + if (StringUtils.isBlank(model.getBegDate()) && StringUtils.isBlank(model.getEndDate())) { + startDate = DateUtil.add(DateUtil.formatToYYYYMMDD(new Date()), -7); + endDate = DateUtil.parseStringToDate(DateUtil.formatToYYYYMMDD(new Date())); + } else { + if (StringUtils.isNotBlank(model.getBegDate())) { + startDate = DateUtil.parseStringToDate(model.getBegDate()); + } + if (StringUtils.isNotBlank(model.getEndDate())) { + endDate = DateUtil.parseStringToDate(model.getEndDate()); + } - public Object userVisitStatAction(ApiUserModel model) { + } + if (startDate != null) { + model.setBegDate(DateUtil.formatToYYYYMMDD(startDate)); + } + + if (endDate != null) { + model.setEndDate(DateUtil.formatToYYYYMMDD(endDate)); + } + Date nowDate = DateUtil.parseStringToDate(DateUtil.formatToYYYYMMDD(new Date())); + Date lastWeek = DateUtil.add(DateUtil.formatToYYYYMMDD(new Date()), -7); + Date lastMonth = DateUtil.add(DateUtil.formatToYYYYMMDD(new Date()), -30); + + long diffInMillies = Math.abs(endDate.getTime() - startDate.getTime()); + + // 将毫秒转换为天数 + long days = diffInMillies / (1000 * 60 * 60 * 24); // 使用long来计算天数 + + // 只查询正式账号、未过期、未禁用 + List queryParams = Arrays.asList(new QueryParam("isTest", 0), + new QueryParam("status", 0), + new QueryParam("deadTime", QueryParam.GREATER_THAN_EQUAL_OPERATOR, new Date())); + List apiUsers = this.apiUserService.findApiUsersByParamsSortDate(queryParams); + + //所有API + List allApi = openResourceService.findAll(); + // 所有API的URI与name对应Map + Map apiMap = converMapData(allApi); + List bosAll = new ArrayList(); + if (CollectionUtils.isNotEmpty(apiUsers)) { + // 根据访问遍历用户 + for (ApiUser apiUser : apiUsers) { + long id = apiUser.getId(); + // 获取每个用户的访问 + ApiUserAuthority authority = apiUserAuthorityService.findByUserId(id); + // 用户有的api + List userApi = authority.getOpenResourseUris(); + Map countMap = authority.getTotalCountMap(); + // 获取用户名 + User user = userService.findById(id); + long date = DateUtil.parseStringToDate("2016-11-23").getTime(); + if (startDate.getTime() >= date && endDate.getTime() > date) { + List apiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(id, startDate, endDate); + List weekApiInvokeStatisticalList; + if (lastWeek == startDate && nowDate == endDate) { + weekApiInvokeStatisticalList = apiInvokeStatisticalList; + } else { + weekApiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(id, lastWeek, nowDate); + } + List monthApiInvokeStatisticalList; + if (lastMonth == startDate && nowDate == endDate) { + monthApiInvokeStatisticalList = apiInvokeStatisticalList; + } else { + monthApiInvokeStatisticalList = apiInvokeStatisticalService.findApiInvokeStatisticalList(id, lastMonth, nowDate); + } + Map> resultMap = getCountByUri(apiInvokeStatisticalList); + Map> weekResultMap = getCountByUri(weekApiInvokeStatisticalList); + Map> monthResultMap = getCountByUri(monthApiInvokeStatisticalList); + + // 遍历用户所有的api + for (String url : userApi) { + // 统计用过的api + String urlName = apiMap.get(url); + StatAnalyBo bo = new StatAnalyBo(); + if (resultMap.containsKey(url)) { + // 自定义时间的数据 + bo.setSuccessEffectiveTime(resultMap.get(url).get("successEffectiveCount")); + bo.setSuccessInvalidTime(resultMap.get(url).get("successInvalidCount")); + bo.setDefeatTime(resultMap.get(url).get("defeatCount")); + bo.setCount(resultMap.get(url).get("totalCount")); + double dailyUse = bo.getCount() * 1.0 / days; + bo.setDailyUse(dailyUse); + } + if (weekResultMap.containsKey(url)) { + // 近一周数据 + bo.setWeekSuccessEffectiveTime(weekResultMap.get(url).get("successEffectiveCount")); + bo.setWeekSuccessInvalidTime(weekResultMap.get(url).get("successInvalidCount")); + bo.setWeekDefeatTime(weekResultMap.get(url).get("defeatCount")); + bo.setWeekCount(weekResultMap.get(url).get("totalCount")); + double dailyUse = bo.getWeekCount() * 1.0 / 7; + bo.setWeekDailyUse(dailyUse); + } + if (monthResultMap.containsKey(url)) { + // 成功有效次数 + bo.setMonthSuccessEffectiveTime(monthResultMap.get(url).get("successEffectiveCount")); + // 成功无效次数 + bo.setMonthSuccessInvalidTime(monthResultMap.get(url).get("successInvalidCount")); + // 失败次数 + bo.setMonthDefeatTime(monthResultMap.get(url).get("defeatCount")); + bo.setMonthCount(monthResultMap.get(url).get("totalCount")); + double dailyUse = bo.getMonthCount() * 1.0 / 30; + bo.setMonthDailyUse(dailyUse); + } + + bo.setUrl(url); + bo.setUrlName(urlName); + bo.setDeadline(apiUser.getDeadTime()); + bo.setUserId((int) id); + bo.setUserName(user.getDisplayName()); + //剩余调用量 + long curVistedCount = apiVisitCountService.findApiVisitCountByDay(id, url, DateUtil.truncateDay(new Date())); + long historyCount = apiVisitCountService.findApiVisitAllCount(id, url, DateUtil.truncateDay(new Date())); + if (countMap != null && countMap.containsKey(url)) { + Integer totalCount = countMap.get(url); + long usedCount = curVistedCount + historyCount; + bo.setSurplusCallCount(Integer.valueOf((int) (totalCount - usedCount))); + } + bosAll.add(bo); + } + } + } + this.buildReport(bosAll, response, startDate, endDate); + } + model.setViewType("stream"); + return model; + } + + private void buildReport(List bos, HttpServletResponse response, Date startDate, Date endDate) { + try { + List> dataList = new ArrayList>(); + + for (StatAnalyBo bo : bos) { + List row = new ArrayList(); + row.add(bo.getUserId() != null ? bo.getUserId().toString() : ""); + row.add(bo.getUserName() != null ? bo.getUserName().toString() : ""); + row.add(bo.getDeadline() != null ? DateUtils.format(bo.getDeadline(), "yyyy/MM/dd") : ""); + row.add(bo.getUrl() != null ? bo.getUrl().toString() : ""); + row.add(bo.getUrlName() != null ? bo.getUrlName().toString() : ""); + row.add(bo.getSurplusCallCount() != null ? bo.getSurplusCallCount().toString() : ""); + // 上周 + row.add(bo.getWeekCount() != null ? bo.getWeekCount().toString() : ""); + if (bo.getWeekDailyUse() != null) { + // 格式化为保留一位小数的字符串 + String formattedDailyUse = String.format("%.1f", bo.getWeekDailyUse()); + row.add(formattedDailyUse); + } else { + row.add(""); + } + row.add(bo.getWeekSuccessEffectiveTime() != null ? bo.getWeekSuccessEffectiveTime().toString() : ""); + row.add(bo.getWeekSuccessInvalidTime() != null ? bo.getWeekSuccessInvalidTime().toString() : ""); + row.add(bo.getWeekDefeatTime() != null ? bo.getWeekDefeatTime().toString() : ""); + // 上个月 + row.add(bo.getMonthCount() != null ? bo.getMonthCount().toString() : ""); + if (bo.getMonthDailyUse() != null) { + // 格式化为保留一位小数的字符串 + String formattedDailyUse = String.format("%.1f", bo.getMonthDailyUse()); + row.add(formattedDailyUse); + } else { + row.add(""); + } + row.add(bo.getMonthSuccessEffectiveTime() != null ? bo.getMonthSuccessEffectiveTime().toString() : ""); + row.add(bo.getMonthSuccessInvalidTime() != null ? bo.getMonthSuccessInvalidTime().toString() : ""); + row.add(bo.getMonthDefeatTime() != null ? bo.getMonthDefeatTime().toString() : ""); + // 自定义时间 + row.add(bo.getCount() != null ? bo.getCount().toString() : ""); + if (bo.getDailyUse() != null) { + // 格式化为保留一位小数的字符串 + String formattedDailyUse = String.format("%.1f", bo.getDailyUse()); + row.add(formattedDailyUse); + } else { + row.add(""); + } + row.add(bo.getSuccessEffectiveTime() != null ? bo.getSuccessEffectiveTime().toString() : ""); + row.add(bo.getSuccessInvalidTime() != null ? bo.getSuccessInvalidTime().toString() : ""); + row.add(bo.getDefeatTime() != null ? bo.getDefeatTime().toString() : ""); + dataList.add(row); + } + + String dateStr = "(" + DateUtils.format(startDate, "yyyy/MM/dd") + "—" + DateUtils.format(endDate, "yyyy/MM/dd") + ")"; + String fileName = "用户账号使用统计" + dateStr + ".xlsx"; +// ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build(); + String displayName = URLEncoder.encode(fileName, "UTF-8"); + response.setHeader("Content-Disposition", "attachment; filename=\"" + displayName + "\""); + response.setContentType("application/x-download;charset=utf-8"); + Workbook workbook = new XSSFWorkbook(); + Sheet sheet = workbook.createSheet("用户账号使用统计"); + List head = Arrays.asList( + "用户ID", "账号", "合同到期时间", "接口", "接口名称", "剩余使用次数", + "访问次数(近一周)", "日均访问量(近一周)", "成功并有效(近一周)", "成功并无效(近一周)", "失败次数(近一周)", + "访问次数(近一月)", "日均访问量(近一月)", "成功并有效(近一月)", "成功并无效(近一月)", "失败次数(近一月)", + "访问次数" + dateStr, "日均访问量" + dateStr, "成功并有效" + dateStr, "成功并无效" + dateStr, "失败次数" + dateStr + ); + // 创建表头 + Row headRow = sheet.createRow(0); + for (int i = 0; i < head.size(); i++) { + Cell cell = headRow.createCell(i); + cell.setCellValue(head.get(i)); + } + // 写入数据,从第二行 + for (int i = 0; i < dataList.size(); i++) { + Row row = sheet.createRow(i + 1); + List data = dataList.get(i); + for (int j = 0; j < data.size(); j++) { + Cell cell = row.createCell(j); + cell.setCellValue(data.get(j)); + } + } + ServletOutputStream outputStream = response.getOutputStream(); + workbook.write(outputStream); + } catch (IOException e) { + } + + } + + List> formatHead(String[] headArr) { + List> head = new ArrayList>(); + for (String name : headArr) { + List temp = new ArrayList(); + temp.add(name); + head.add(temp); + } + return head; + } + + public Object userVisitStatAction(ApiUserModel model) throws IllegalAccessException { Date startDate = null; Date endDate = null; if (StringUtils.isNotBlank(model.getBegDate())) { @@ -1075,8 +1344,12 @@ public class ApiUserController { // bos = createAnalyBosByUser(analys); // model.setChartSeries(toChartString(bos)); // }else if(model.getWay() == 2){ + Long userId = model.getUserId(); + ApiUserAuthority authority = apiUserAuthorityService.findByUserId(userId); + Map countMap = authority.getTotalCountMap(); + ApiUser apiUser = apiUserService.findApiUserById(userId); if (model.getWay() == 2) { - User user = userService.findById(model.getUserId()); + User user = userService.findById(userId); if (user != null) { model.setUserName(user.getDisplayName()); } @@ -1120,6 +1393,16 @@ public class ApiUserController { boTemp.setSuccessEffectiveTime(resultMap.get(url).get("successEffectiveCount")); boTemp.setSuccessInvalidTime(resultMap.get(url).get("successInvalidCount")); boTemp.setDefeatTime(resultMap.get(url).get("defeatCount")); + if (apiUser != null) { + boTemp.setDeadline(apiUser.getDeadTime()); + } + long curVistedCount = apiVisitCountService.findApiVisitCountByDay(userId, url, DateUtil.truncateDay(new Date())); + long historyCount = apiVisitCountService.findApiVisitAllCount(userId, url, DateUtil.truncateDay(new Date())); + if (countMap != null && countMap.containsKey(url)) { + Integer totalCount = countMap.get(url); + long usedCount = curVistedCount + historyCount; + boTemp.setSurplusCallCount(Integer.valueOf((int) (totalCount - usedCount))); + } } } } @@ -1625,6 +1908,7 @@ public class ApiUserController { Integer successEffectiveCount = detailMap.get("successEffectiveCount"); Integer successInvalidCount = detailMap.get("successInvalidCount"); Integer defeatCount = detailMap.get("defeatCount"); + Integer totalCount = detailMap.get("totalCount"); if (successEffectiveCount != null) { successEffectiveCount = successEffectiveCount + apiInvokeStatistical.getSuccessEffectiveTime(); } else { @@ -1641,10 +1925,16 @@ public class ApiUserController { } else { defeatCount = apiInvokeStatistical.getDefeatTime(); } + if (totalCount != null) { + totalCount = totalCount + apiInvokeStatistical.getTotalTime(); + } else { + totalCount = apiInvokeStatistical.getTotalTime(); + } detailMap.put("successEffectiveCount", successEffectiveCount); detailMap.put("successInvalidCount", successInvalidCount); detailMap.put("defeatCount", defeatCount); + detailMap.put("totalCount", totalCount); } return resultMap; diff --git a/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/model/ApiUserModel.java b/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/model/ApiUserModel.java index ad264fe9a..f77689d9e 100644 --- a/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/model/ApiUserModel.java +++ b/projects/fangjia-bkoff/src/main/java/com/fangjia/bkoff/model/ApiUserModel.java @@ -40,6 +40,10 @@ public class ApiUserModel extends BaseModel { private Map priceRatingMap; private Integer isMonitor; + private String contact; + + private String phone; + public String getPriceType() { return priceType; } @@ -70,7 +74,7 @@ public class ApiUserModel extends BaseModel { private String type; private String citys;// spell private String unCheckCitys;// 页面反选的city - private int status; + private Integer status; private String uri; private boolean hasAuth; private boolean supportLatLng; @@ -378,15 +382,15 @@ public class ApiUserModel extends BaseModel { this.limit = limit; } - public int getStatus() { + public Integer getStatus() { return status; } - public void setStatus(int status) { + public void setStatus(Integer status) { this.status = status; } - public String getCompany() { + public String getCompany() { return company; } @@ -615,4 +619,20 @@ public class ApiUserModel extends BaseModel { public void setIsMonitor(Integer isMonitor) { this.isMonitor = isMonitor; } + + public String getContact() { + return contact; + } + + public void setContact(String contact) { + this.contact = contact; + } + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } } diff --git a/projects/fangjia-bkoff/src/main/webapp/apiUser/add.ftl b/projects/fangjia-bkoff/src/main/webapp/apiUser/add.ftl index 907506c9f..6520d44c3 100644 --- a/projects/fangjia-bkoff/src/main/webapp/apiUser/add.ftl +++ b/projects/fangjia-bkoff/src/main/webapp/apiUser/add.ftl @@ -34,12 +34,12 @@
鏄电О - + 鐢ㄦ埛鍚� - + 瀵嗙爜 @@ -51,6 +51,7 @@ @@ -58,7 +59,7 @@ 璐︽埛鐢ㄩ�� + 閿�鍞礋璐d汉 - + 鍏徃 - + 浼颁环璋冩暣姣旂巼 @@ -91,13 +92,21 @@ siteName - + 鍒版湡鏃堕棿 - + + 瀹㈡埛鑱旂郴浜� + + + + 瀹㈡埛鑱旂郴浜虹數璇� + + ip鍦板潃