package com.zc.business.interfaces; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.entity.SysDept; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.common.utils.ServletUtils; import com.ruoyi.common.utils.ip.IpUtils; import com.ruoyi.common.utils.spring.SpringUtils; import com.ruoyi.system.service.ISysDeptService; import com.zc.business.domain.DcDevice; import com.zc.business.domain.DcOperLog; import com.zc.business.service.IDcDeviceService; import com.zc.business.service.IDcOperLogService; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.*; @Aspect @Component public class OperationLogAspect { private final static Map DEVICEFUNCTIONNAME = new HashMap() {{ put("1", new HashMap() {{ }}); put("2", new HashMap() {{ put("11", "文件名发送"); put("13", "文件发送"); put("1B", "指定当前需要播放的清单"); }}); put("3", new HashMap() {{ }}); put("4", new HashMap() {{ }}); put("5", new HashMap() {{ put("", "发布语音广播"); }}); put("6", new HashMap() {{ }}); put("7", new HashMap() {{ }}); put("8", new HashMap() {{ put("01", "通信连接检查"); put("0b", "配置装置参数"); put("0d", "读取装置参数"); put("0f", "读取装置状态数据"); }}); put("9", new HashMap() {{ }}); put("10", new HashMap() {{ put("SETMD", "模式设定命令"); put("ASKMD", "模式查询命令"); put("SETTM", "工作时间设定命令"); put("ASKTM", "工作时间查询命令"); put("SETDF", "自定义模式设置"); put("ASKDF", "自定义模式查询命令"); }}); put("11", new HashMap() {{ }}); put("12", new HashMap() {{ put("51", "设备控制模式"); put("52", "读取设备控制模式"); put("30", "设置自动控制模式工作状态"); put("3D", "读取自动控制模式工作状态"); put("3C", "读取当前设备工作状态"); }}); put("13", new HashMap() {{ }}); put("14", new HashMap() {{ }}); put("15", new HashMap() {{ put("A1", "查询数据"); put("A2", "查询数据"); put("A3", "查询数据"); put("A4", "查询数据"); put("A5", "查询数据"); put("A6", "查询数据"); }}); put("16", new HashMap() {{ }}); }}; @AfterReturning(pointcut = "@annotation(operationLog)", returning = "jsonResult") public void AfterReturning(JoinPoint joinPoint, OperationLog operationLog, Object jsonResult) throws Throwable { System.out.println("运行成功"); around((ProceedingJoinPoint) joinPoint, operationLog, "0", jsonResult, null); } @AfterThrowing(value = "@annotation(operationLog)", throwing = "e") public void AfterThrowing(JoinPoint joinPoint, OperationLog operationLog, Exception e) throws Throwable { System.out.println("运行失败"); around((ProceedingJoinPoint) joinPoint, operationLog, "1", null, e); } public void around(ProceedingJoinPoint joinPoint, OperationLog operationLog, String state, Object jsonResult, Exception exception) throws Throwable { ISysDeptService deptService = SpringUtils.getBean(ISysDeptService.class); IDcOperLogService dcOperLogService = SpringUtils.getBean(IDcOperLogService.class); IDcDeviceService dcDeviceService = SpringUtils.getBean(IDcDeviceService.class); try { DcOperLog operLog = new DcOperLog(); String operUrl = operationLog.operUrl(); LoginUser loginUser; try { loginUser = SecurityUtils.getLoginUser(); } catch (Exception e) { loginUser = null; } Object[] pointArgs = joinPoint.getArgs(); HttpServletRequest request; try { request = ServletUtils.getRequest(); } catch (Exception e) { request = null; } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = df.format(new Date()); String sta = ""; if (Objects.equals(state, "0") && Objects.equals(String.valueOf(((AjaxResult)jsonResult).get("code")), "200")) { operLog.setStatus(0); sta = "成功"; } else { operLog.setStatus(1); sta = "失败"; } ArrayList deviceIds = new ArrayList<>(); ArrayList deviceNames = new ArrayList<>(); String deviceType = ""; StringBuilder remark = new StringBuilder("在" + date + "调用"); if (operUrl.contains("batchFunctions")) { //批量调用 JSONObject pointArg = null; if (pointArgs[0] instanceof JSONObject){ pointArg = (JSONObject) pointArgs[0]; }else { pointArg = (JSONObject) JSON.toJSON(pointArgs[0]); } JSONArray devices = pointArg.getJSONArray("devices"); JSONArray functions = pointArg.getJSONArray("functions"); for (int i = 0; i < devices.size(); i++) { JSONObject device = (JSONObject) JSON.toJSON(devices.get(i)); LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(DcDevice::getId, device.get("id")); DcDevice dcDevice = dcDeviceService.getById(device.getString("id")); if (!Objects.isNull(dcDevice)) { Long deviceId = dcDevice.getId(); String deviceName = dcDevice.getDeviceName(); deviceType = dcDevice.getDeviceType(); deviceIds.add(String.valueOf(deviceId)); deviceNames.add(deviceName); remark.append(deviceName).append("的"); for (int i1 = 0; i1 < functions.size(); i1++) { JSONObject function = (JSONObject) JSON.toJSON(functions.get(i1)); String hashMap = Objects.equals(deviceType, "5") ? "发布语音广播" : String.valueOf( ((HashMap) (DEVICEFUNCTIONNAME.get(deviceType))) .get(function.getString("functionId"))); remark.append("调用").append(hashMap).append("功能").append(sta); if (i1 < functions.size() - 1) { remark.append("、"); } } if (i < (devices.size() - 1)) { remark.append(","); } } } if (loginUser != null) { SysDept sysDept = deptService.selectDeptById(loginUser.getDeptId()); operLog.setOperName(loginUser.getUsername()); operLog.setDeptName(sysDept.getDeptName()); operLog.setOperLocation(loginUser.getLoginLocation()); operLog.setOperType("2"); } else { operLog.setOperType("1"); } JSONArray jsonArray = (JSONArray) JSON.toJSON(pointArgs); operLog.setOperParam(String.valueOf(jsonArray)); operLog.setDcDeviceId(deviceIds.toString()); operLog.setDcDeviceName(deviceNames.toString()); operLog.setOperIp(IpUtils.getIpAddr(request)); operLog.setOperTime(new Date()); operLog.setDcDeviceType(deviceType); operLog.setRemark(remark.toString()); operLog.setJsonResult(jsonResult.toString()); dcOperLogService.addDcOperLog(operLog); } else if (operUrl.contains("functions")) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(DcDevice::getIotDeviceId, pointArgs[0]); List list = dcDeviceService.list(lambdaQueryWrapper); DcDevice dcDevice = list.get(0); if (!Objects.isNull(dcDevice)) { Long deviceId = dcDevice.getId(); String deviceName = dcDevice.getDeviceName(); deviceType = dcDevice.getDeviceType(); deviceIds.add(String.valueOf(deviceId)); deviceNames.add(deviceName); String functionId = String.valueOf(pointArgs[1]); String hashMap = Objects.equals(deviceType, "5") ? "发布语音广播" : String.valueOf(((HashMap) (DEVICEFUNCTIONNAME.get(deviceType))).get(functionId)); remark.append(deviceName).append("的").append(hashMap).append("功能并且调用").append(sta).append("了"); if (loginUser != null) { SysDept sysDept = deptService.selectDeptById(loginUser.getDeptId()); operLog.setOperName(loginUser.getUsername()); operLog.setDeptName(sysDept.getDeptName()); operLog.setOperLocation(loginUser.getLoginLocation()); operLog.setOperType("0"); } else { operLog.setOperType("1"); } JSONArray jsonArray = (JSONArray) JSON.toJSON(pointArgs); operLog.setOperParam(String.valueOf(jsonArray)); operLog.setDcDeviceId(deviceIds.toString()); operLog.setDcDeviceName(deviceNames.toString()); operLog.setOperIp(IpUtils.getIpAddr(request)); operLog.setOperTime(new Date()); operLog.setDcDeviceType(deviceType); operLog.setJsonResult(remark.toString()); dcOperLogService.addDcOperLog(operLog); } } } catch (Exception e) { throw e; } } }