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.domain.DcPublishManage;
import com.zc.business.enums.UniversalEnum;
import com.zc.business.service.IDcDeviceService;
import com.zc.business.service.IDcOperLogService;
import com.zc.business.service.IDcPublishManageService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

@Aspect
@Component
public class OperationLogAspect {


    private final static Map<String, Object> DEVICEFUNCTIONNAME = new HashMap<String, Object>() {{
        put(UniversalEnum.ONE.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.TWO.getValue(), new HashMap<String, Object>() {{
            put(UniversalEnum.ELEVEN.getValue(), UniversalEnum.FILE_NAME_SENDING.getValue());
            put(UniversalEnum.THIRTEEN.getValue(), UniversalEnum.FILE_SENDING.getValue());
            put(UniversalEnum.ONE_B.getValue(), UniversalEnum.SPECIFIES_THE_LIST_THAT_CURRENTLY_NEEDS_TO_BE_PLAYED.getValue());
        }});
        put(UniversalEnum.THREE.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.FOUR.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.FIVE.getValue(), new HashMap<String, Object>() {{
            put(UniversalEnum.EMPTY_STRING.getValue(), UniversalEnum.RELEASE_VOICE_BROADCAST.getValue());
        }});
        put(UniversalEnum.SIX.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.SEVEN.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.EIGHT.getValue(), new HashMap<String, Object>() {{
            put(UniversalEnum.ZERO_ONE.getValue(), UniversalEnum.COMMUNICATION_CONNECTION_CHECK.getValue());
            put(UniversalEnum.ZERO_B.getValue(), UniversalEnum.CONFIGURATION_DEVICE_PARAMETER.getValue());
            put(UniversalEnum.ZERO_D.getValue(), UniversalEnum.READ_DEVICE_PARAMETER.getValue());
            put(UniversalEnum.ZERO_F.getValue(), UniversalEnum.READ_DEVICE_STATUS_DATA.getValue());
        }});
        put(UniversalEnum.NINE.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.TEN.getValue(), new HashMap<String, Object>() {{
            put(UniversalEnum.SETMD.getValue(), UniversalEnum.MODE_SETTING_COMMAND.getValue());
            put(UniversalEnum.ASKMD.getValue(), UniversalEnum.PATTERN_QUERY_COMMAND.getValue());
            put(UniversalEnum.SETTM.getValue(), UniversalEnum.WORK_TIME_SETTING_COMMAND.getValue());
            put(UniversalEnum.ASKTM.getValue(), UniversalEnum.THIS_COMMAND_IS_USED_TO_QUERY_WORKING_HOURS.getValue());
            put(UniversalEnum.SETDF.getValue(), UniversalEnum.CUSTOMIZE_MODE_SETTINGS.getValue());
            put(UniversalEnum.ASKDF.getValue(), UniversalEnum.USER_DEFINED_MODE_QUERY_COMMAND.getValue());
        }});
        put(UniversalEnum.ELEVEN.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.TWELVE.getValue(), new HashMap<String, Object>() {{
            put(UniversalEnum.FIFTY_ONE.getValue(), UniversalEnum.DEVICE_CONTROL_MODE.getValue());
            put(UniversalEnum.FIFTY_TWO.getValue(), UniversalEnum.READ_THE_DEVICE_CONTROL_MODE.getValue());
            put(UniversalEnum.THIRTY.getValue(), UniversalEnum.SET_THE_WORKING_STATE_OF_AUTOMATIC_CONTROL_MODE.getValue());
            put(UniversalEnum.THREE_D.getValue(), UniversalEnum.READ_THE_WORKING_STATUS_OF_AUTOMATIC_CONTROL_MODE.getValue());
            put(UniversalEnum.THREE_C.getValue(), UniversalEnum.READ_THE_CURRENT_DEVICE_STATUS.getValue());
        }});
        put(UniversalEnum.THIRTEEN.getValue(), new HashMap<String, Object>() {{
            put("102", "设备开关");
        }});
        put(UniversalEnum.FOURTEEN.getValue(), new HashMap<String, Object>() {{
        }});
        put(UniversalEnum.FIFTEEN.getValue(), new HashMap<String, Object>() {{
            put(UniversalEnum.A_ONE.getValue(), UniversalEnum.QUERY_DATA.getValue());
            put(UniversalEnum.A_TWO.getValue(), UniversalEnum.QUERY_DATA.getValue());
            put(UniversalEnum.A_THREE.getValue(), UniversalEnum.QUERY_DATA.getValue());
            put(UniversalEnum.A_FOUR.getValue(), UniversalEnum.QUERY_DATA.getValue());
            put(UniversalEnum.A_FIVE.getValue(), UniversalEnum.QUERY_DATA.getValue());
            put(UniversalEnum.A_SIX.getValue(), UniversalEnum.QUERY_DATA.getValue());
        }});
        put(UniversalEnum.SIXTEEN.getValue(), new HashMap<String, Object>() {{
            put("102", "设备开关");
        }});
    }};


    @AfterReturning(pointcut = "@annotation(operationLog)", returning = "jsonResult")
    public void AfterReturning(JoinPoint joinPoint, OperationLog operationLog, Object jsonResult) throws Throwable {
        around((ProceedingJoinPoint) joinPoint, operationLog, UniversalEnum.ZERO.getValue(), jsonResult, null);
    }

    @AfterThrowing(value = "@annotation(operationLog)", throwing = "e")
    public void AfterThrowing(JoinPoint joinPoint, OperationLog operationLog, Exception e) throws Throwable {
        around((ProceedingJoinPoint) joinPoint, operationLog, UniversalEnum.ONE.getValue(), AjaxResult.error(), e);
    }

    public void around(ProceedingJoinPoint joinPoint, OperationLog operationLog, String state, Object jsonResult, Exception exception) throws Throwable {

        ISysDeptService deptService = SpringUtils.getBean(ISysDeptService.class);
        IDcDeviceService dcDeviceService = SpringUtils.getBean(IDcDeviceService.class);
        IDcPublishManageService dcPublishManageService = SpringUtils.getBean(IDcPublishManageService.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();


            //String operType = String.valueOf(pointArgs[pointArgs.length - 1]);
            //operLog.setOperType(operType);


            HttpServletRequest request;
            try {

                request = ServletUtils.getRequest();
            } catch (Exception e) {
                request = null;
            }


            //SimpleDateFormat df = new SimpleDateFormat(UniversalEnum.TIME_FORMAT_ALL.getValue());
            //String date = df.format(new Date());
            //String sta = UniversalEnum.EMPTY_STRING.getValue();
            AtomicBoolean status = new AtomicBoolean(true);
            AjaxResult ajaxResult = (AjaxResult) jsonResult;
            try {
                JSONArray datas = (JSONArray) JSON.toJSON(ajaxResult.get("data"));
                datas.forEach(item -> {
                    JSONObject json = (JSONObject) JSON.toJSON(item);
                    if (json.getJSONObject("result").getInteger("code") != UniversalEnum.TWO_HUNDRED.getNumber()) {
                        status.set(false);
                    }
                });
            } catch (Exception e) {
                status.set(false);
            }
//            List<Object> result = datas.stream().filter(item -> {
//                JSONObject json = (JSONObject) JSON.toJSON(item);
//                return Objects.equals(json.getString("device"), "2");
//            }).collect(Collectors.toList());
            if (Objects.equals(state, UniversalEnum.ZERO.getValue()) && Objects.equals(String.valueOf(ajaxResult.get("code")), UniversalEnum.TWO_HUNDRED.getValue()) && !Objects.equals(String.valueOf(ajaxResult.get("msg")), UniversalEnum.FIVE_HUNDRED.getValue()) && status.get()) {
                operLog.setStatus(UniversalEnum.ZERO.getNumber());
                //sta = UniversalEnum.SUCCESSFUL.getValue();
            } else {
                operLog.setStatus(UniversalEnum.ONE.getNumber());
                //sta = UniversalEnum.LOSE.getValue();
            }

            ArrayList<String> deviceIds = new ArrayList<>();
            ArrayList<String> deviceNames = new ArrayList<>();
            String deviceType = UniversalEnum.EMPTY_STRING.getValue();
            StringBuilder remark = new StringBuilder();
            if (operUrl.contains(UniversalEnum.BATCH_FUNCTIONS.getValue())) {
                //批量调用
                JSONObject pointArg = null;
                if (pointArgs[UniversalEnum.ZERO.getNumber()] instanceof JSONObject) {
                    pointArg = (JSONObject) pointArgs[UniversalEnum.ZERO.getNumber()];
                } else {
                    pointArg = (JSONObject) JSON.toJSON(pointArgs[UniversalEnum.ZERO.getNumber()]);
                }
                //String contentDetails = UniversalEnum.EMPTY_STRING.getValue();

                JSONArray devices = pointArg.getJSONArray("devices");
                JSONArray functions = pointArg.getJSONArray("functions");

                for (int i = UniversalEnum.ZERO.getNumber(); i < devices.size(); i++) {

                    JSONObject device = (JSONObject) JSON.toJSON(devices.get(i));

                    LambdaQueryWrapper<DcDevice> 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);

                        for (int i1 = UniversalEnum.ZERO.getNumber(); i1 < functions.size(); i1++) {

                            JSONObject function = (JSONObject) JSON.toJSON(functions.get(i1));

                            String functionId = function.getString("functionId");


                            if(Objects.equals(deviceType, UniversalEnum.SEVENTEEN.getValue())){ //机柜的读取不记录
                               if (Objects.equals(functionId,"04")){
                                   return;
                                }
                                if (Objects.equals(functionId,"03")){
                                    return;
                                }
                            }
                            if(Objects.equals(deviceType, UniversalEnum.EIGHTEEN.getValue())){ //机柜空调的读取不记录
                                    return;
                            }
                            if (Objects.equals(functionId, UniversalEnum.A_ONE.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.A_TWO.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.A_THREE.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.A_FOUR.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.A_FIVE.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.A_SIX.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.ASKMD.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.ASKTM.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.ASKDF.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.FIFTY_TWO.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.THREE_D.getValue()) ||
                                    Objects.equals(functionId, UniversalEnum.THREE_C.getValue())) {
                                return;
                            }


                            //String hashMap = Objects.equals(deviceType, UniversalEnum.FIVE.getValue()) ?
                            //        UniversalEnum.RELEASE_VOICE_BROADCAST.getValue() :
                            //        String.valueOf(
                            //                ((HashMap<String, Object>) (DEVICEFUNCTIONNAME.get(deviceType)))
                            //                        .get(functionId) == null ?
                            //                        UniversalEnum.UNKNOWN_METHOD.getValue() :
                            //                        ((HashMap<String, Object>) (DEVICEFUNCTIONNAME.get(deviceType)))
                            //                                .get(functionId));
                            //remark.append(hashMap).append(":");

                            JSONObject params = function.getJSONObject("params");


                            if (Objects.equals(deviceType, UniversalEnum.TWO.getValue())) {

                                //if (Objects.equals(functionId, UniversalEnum.ELEVEN.getValue())) {
                                //
                                //    remark.append("文件名称:").append(params.getString("fileName")).append("、文件大小:").append(params.getString("size")).append("KB");
                                //
                                //} else
                                if (Objects.equals(functionId, UniversalEnum.THIRTEEN.getValue())) {

                                    JSONArray parameters = params.getJSONArray("parameters");

                                    for (int i2 = UniversalEnum.ZERO.getNumber(); i2 < parameters.size(); i2++) {

                                        remark.append(((JSONObject) JSON.toJSON(parameters.get(i2))).getString("CONTENT"));
                                        //remark.append("文件内容:").append(((JSONObject) JSON.toJSON(parameters.get(i2))).getString("CONTENT"));

                                        if (i2 < parameters.size() - UniversalEnum.ONE.getNumber()) {
                                            remark.append(UniversalEnum.SEMICOLON.getValue());
                                        }
                                    }

                                }
                                //else if (Objects.equals(functionId, UniversalEnum.ONE_B.getValue())) {
                                //
                                //    remark.append("文件标识:").append(params.getString("fileId"));
                                //
                                //}

                            } else if (Objects.equals(deviceType, UniversalEnum.FIVE.getValue())) {

                                remark.append(params.getString("text"));

                            } else if (Objects.equals(deviceType, UniversalEnum.TEN.getValue())) {

                                String set = params.getString(UniversalEnum.SET.getValue());
                                if (Objects.equals(functionId, UniversalEnum.SETMD.getValue())) {

                                    if (Objects.equals(set, UniversalEnum.ZERO.getValue())) {

                                        remark.append(UniversalEnum.OFF.getValue());

                                    } else if (Objects.equals(set, UniversalEnum.ONE.getValue())) {

                                        remark.append(UniversalEnum.STEADY_ON.getValue());

                                    } else if (Objects.equals(set, UniversalEnum.TWO.getValue())) {

                                        remark.append(UniversalEnum.BLINK_AT_AN_INTERVAL_OF_100_MS.getValue());

                                    } else if (Objects.equals(set, UniversalEnum.THREE.getValue())) {

                                        remark.append(UniversalEnum.BLINK_AT_AN_INTERVAL_OF_200_MS.getValue());

                                    } else if (Objects.equals(set, UniversalEnum.FOUR.getValue())) {

                                        remark.append(UniversalEnum.BLINK_AT_AN_INTERVAL_OF_500_MS.getValue());

                                    } else if (Objects.equals(set, UniversalEnum.FIVE.getValue())) {

                                        remark.append(UniversalEnum.DOUBLE_FLICKER.getValue());

                                    } else if (Objects.equals(set, UniversalEnum.SIX.getValue())) {

                                        remark.append(UniversalEnum.SOS.getValue());

                                    } else if (Objects.equals(set, UniversalEnum.SEVEN.getValue())) {

                                        remark.append(UniversalEnum.CUSTOMIZE_THE_BLINKING_TIME.getValue());

                                    }

                                    remark.append(UniversalEnum.MODE.getValue());

                                } else if (Objects.equals(functionId, UniversalEnum.SETTM.getValue())) {

                                    remark.append(set).append(UniversalEnum.MINUTES.getValue());
                                }

                            } else if (Objects.equals(deviceType, UniversalEnum.TWELVE.getValue())) {

                                if (Objects.equals(functionId, UniversalEnum.FIFTY_ONE.getValue())) {

                                    //remark.append("模式:");

                                    String mode = params.getString("mode");

                                    if (Objects.equals(mode, UniversalEnum.ZERO_ZERO.getValue())) {

                                        remark.append("远程手动控制模式:上行:");
                                        String onWorkStatus = params.getString("onWorkStatus");
                                        String inWorkStatus = params.getString("inWorkStatus");

                                        if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_ZERO.getValue())) {

                                            remark.append("不更新状态,下行:");

                                        } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_ONE.getValue())) {

                                            remark.append("常亮,下行:");

                                        } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_TWO.getValue())) {

                                            remark.append("流水,下行:");

                                        } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_THREE.getValue())) {

                                            remark.append("闪烁,下行:");

                                        } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_FOUR.getValue())) {

                                            remark.append("关闭,下行:");

                                        }

                                        if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_ZERO.getValue())) {

                                            remark.append("不更新状态");

                                        } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_ONE.getValue())) {

                                            remark.append("常亮");

                                        } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_TWO.getValue())) {

                                            remark.append("流水");

                                        } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_THREE.getValue())) {

                                            remark.append("闪烁");

                                        } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_FOUR.getValue())) {

                                            remark.append("关闭");

                                        }

                                    } else if (Objects.equals(mode, UniversalEnum.ZERO_ONE.getValue())) {

                                        remark.append("时间段自动控制模式:")
                                                .append(params.getString("startDisplayTime"))
                                                .append("-")
                                                .append(params.getString("endDisplayTime"));

                                    } else if (Objects.equals(mode, UniversalEnum.ZERO_TWO.getValue())) {

                                        remark.append("万年历自动控制模式");

                                    } else if (Objects.equals(mode, UniversalEnum.ZERO_THREE.getValue())) {

                                        remark.append("能见度自动控制模式");

                                    }

                                } else if (Objects.equals(functionId, UniversalEnum.THIRTY.getValue())) {

                                    String onWorkStatus = params.getString("onWorkStatus");
                                    String inWorkStatus = params.getString("inWorkStatus");

                                    if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_ZERO.getValue())) {

                                        remark.append("上行:不更新状态,下行:");

                                    } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_ONE.getValue())) {

                                        remark.append("上行:常亮,下行:");

                                    } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_TWO.getValue())) {

                                        remark.append("上行:流水,下行:");

                                    } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_THREE.getValue())) {

                                        remark.append("上行:闪烁,下行:");

                                    } else if (Objects.equals(onWorkStatus, UniversalEnum.ZERO_FOUR.getValue())) {

                                        remark.append("上行:关闭,下行:");

                                    }

                                    if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_ZERO.getValue())) {

                                        remark.append("不更新状态");

                                    } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_ONE.getValue())) {

                                        remark.append("常亮");

                                    } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_TWO.getValue())) {

                                        remark.append("流水");

                                    } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_THREE.getValue())) {

                                        remark.append("闪烁");

                                    } else if (Objects.equals(inWorkStatus, UniversalEnum.ZERO_FOUR.getValue())) {

                                        remark.append("关闭");

                                    }
                                }

                            }else if (Objects.equals(deviceType, UniversalEnum.ONE.getValue())) {
                                JSONObject videoCamera = (JSONObject) JSON.toJSON(functions.get(0));
                                String cmdType = videoCamera.getString("cmdType");
                                if (cmdType.equals("48")) {
                                    remark.append("雨刷关闭");
                                }
                                if (cmdType.equals("49")) {
                                    remark.append("雨刷开启");
                                }

                            }else if(Objects.equals(deviceType, UniversalEnum.SEVENTEEN.getValue())){ //动环机柜
                                if(Objects.equals(functionId, "SL")){
                                    String aSwitch = params.getString("switch");
                                     if ("1".equals(aSwitch)){
                                         remark.append("门禁打开");
                                     }else if ("0".equals(aSwitch)){
                                         remark.append("门禁关闭");
                                     }else {
                                         remark.append("操作门禁");
                                     }
                                }
                                if (Objects.equals(functionId, "TL")){
                                    remark.append("雷击清零");
                                }
                                if (Objects.equals(functionId, "SR")){
                                    remark.append("系统复位");
                                }
                                if (Objects.equals(functionId, "RS")){
                                    remark.append("令重合闸复位");
                                }
                            }else if (Objects.equals(deviceType, UniversalEnum.THIRTEEN.getValue()) || Objects.equals(deviceType, UniversalEnum.SIXTEEN.getValue())) {

                                String identification = params.getString("deviceName");
                                String value = params.getString("value");

                                if (Objects.equals(identification, "ac_out_1_en")) {

                                    remark.append("220V 输出 1").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                                } else if (Objects.equals(identification, "ac_out_2_en")) {

                                    remark.append("220V 输出 2").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                                } else if (Objects.equals(identification, "dc_out_1_en")) {

                                    remark.append("12V 输出 1").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                                } else if (Objects.equals(identification, "dc_out_2_en")) {

                                    remark.append("12V 输出 2").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                                } else if (Objects.equals(identification, "fan_out_en")) {

                                    remark.append("风扇").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                                }

                            }


                            //remark.append(deviceName);

                            //String hashMap = Objects.equals(deviceType, UniversalEnum.FIVE.getValue()) ?
                            //        UniversalEnum.RELEASE_VOICE_BROADCAST.getValue() :
                            //        String.valueOf(
                            //                ((HashMap<String, Object>) (DEVICEFUNCTIONNAME.get(deviceType)))
                            //                        .get(functionId) == null ?
                            //                        UniversalEnum.UNKNOWN_METHOD.getValue() :
                            //                        ((HashMap<String, Object>) (DEVICEFUNCTIONNAME.get(deviceType)))
                            //                                .get(functionId));

                            //remark.append(UniversalEnum.INVOKE.getValue()).append(hashMap).append(UniversalEnum.FEATURE.getValue());

                            if (i1 < functions.size() - UniversalEnum.ONE.getNumber() && !Objects.equals(deviceType, "2")) {
                                remark.append(UniversalEnum.COMMA.getValue());
                            }


                            //if (Objects.equals(function.getString("functionId"), UniversalEnum.THIRTEEN.getValue()) && Objects.equals(deviceType, UniversalEnum.TWO.getValue())) {
                            //    JSONArray parameters = function.getJSONObject("params").getJSONArray("parameters");
                            //    List<String> content = parameters.stream().map(item -> ((JSONObject) item).getString("CONTENT")).collect(Collectors.toList());
                            //    contentDetails = content.toString().replace(UniversalEnum.LEFT_CENTER_BRACKET.getValue(), UniversalEnum.EMPTY_STRING.getValue()).replace(UniversalEnum.CLOSE_CENTER_BRACKET.getValue(), UniversalEnum.EMPTY_STRING.getValue());
                            //}
                            //
                            //
                            //if (Objects.equals(deviceType, UniversalEnum.FIVE.getValue())) {
                            //    contentDetails = function.getJSONObject("params").getString("text");
                            //}
                        }

                        if (i < (devices.size() - UniversalEnum.ONE.getNumber())) {
                            remark.append(UniversalEnum.UNDERLINE.getValue());
                        }

                    }
                }

                if (loginUser != null) {
                    SysDept sysDept = deptService.selectDeptById(loginUser.getDeptId());
                    if (sysDept != null) {
                        operLog.setDeptName(sysDept.getDeptName());
                    }
                    operLog.setOperName(loginUser.getUsername());
                    operLog.setOperLocation(loginUser.getLoginLocation());
                    if (devices.size() > UniversalEnum.ONE.getNumber()) {
                        operLog.setOperType(UniversalEnum.TWO.getValue());
                    } else {
                        operLog.setOperType(UniversalEnum.ZERO.getValue());
                    }
                } else {
                    operLog.setOperName(UniversalEnum.SERVER.getValue());
                    operLog.setDeptName(UniversalEnum.SERVER.getValue());
                    operLog.setOperLocation(UniversalEnum.SERVER.getValue());
                    operLog.setOperType(UniversalEnum.ONE.getValue());
                }

                //if (Objects.equals(deviceType, UniversalEnum.FIFTEEN.getValue())) {
                //    return;
                //}

                saveOperLog(((JSONObject) JSON.toJSON(jsonResult)).getJSONArray("data"), operLog, pointArgs, request, deviceIds, deviceNames, deviceType, remark);

                if ((Objects.equals(deviceType, UniversalEnum.TWO.getValue()) || Objects.equals(deviceType, UniversalEnum.FIVE.getValue())) && Objects.nonNull(operLog.getId())) {

                    for (int i = UniversalEnum.ZERO.getNumber(); i < devices.size(); i++) {

                        JSONObject device = (JSONObject) JSON.toJSON(devices.get(i));

                        LambdaQueryWrapper<DcDevice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper.eq(DcDevice::getId, device.get("id"));
                        DcDevice dcDevice = dcDeviceService.getById(device.getString("id"));

                        DcPublishManage dcPublishManage = new DcPublishManage();

                        dcPublishManage.setPosition(dcDevice.getDeviceName());

                        if (Objects.equals(state, UniversalEnum.ZERO.getValue()) && Objects.equals(String.valueOf(((AjaxResult) jsonResult).get("code")), UniversalEnum.TWO_HUNDRED.getValue()) && !Objects.equals(String.valueOf(((AjaxResult) jsonResult).get("msg")), UniversalEnum.FIVE_HUNDRED.getValue()) && status.get()) {
                            dcPublishManage.setPublishStatus(UniversalEnum.ONE.getNumber());//发布状态1-成功2-失败
                        } else {
                            dcPublishManage.setPublishStatus(UniversalEnum.TWO.getNumber());//发布状态1-成功2-失败
                        }

                        if (loginUser != null) {
                            dcPublishManage.setPublisher(loginUser.getUsername());//发布人
                        } else {
                            dcPublishManage.setPublisher(UniversalEnum.SYSTEM_TIMING_CALL.getValue());//发布人
                        }
                        dcPublishManage.setPublishTime(new Date());//发布时间
                        dcPublishManage.setCreateTime(new Date());//创建时间

                        dcPublishManage.setLogId(Long.valueOf(operLog.getId()));//发布的如果的情报板传对应id
                        if (Objects.equals(deviceType, UniversalEnum.TWO.getValue())) {
                            dcPublishManage.setPublishChannels(UniversalEnum.FOUR.getNumber());
                        } else {
                            dcPublishManage.setPublishChannels(UniversalEnum.SEVEN.getNumber());
                        }

                        dcPublishManage.setContentDetails(JSON.toJSONString(pointArgs));
                        dcPublishManageService.insertDcPublishManage(dcPublishManage);
                    }
                }

            } else if (operUrl.contains(UniversalEnum.FUNCTIONS.getValue())) {

                LambdaQueryWrapper<DcDevice> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(DcDevice::getIotDeviceId, pointArgs[UniversalEnum.ZERO.getNumber()]);
                List<DcDevice> list = dcDeviceService.list(lambdaQueryWrapper);
                DcDevice dcDevice = list.get(UniversalEnum.ZERO.getNumber());

                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[UniversalEnum.ONE.getNumber()]);

                    JSONObject params = (JSONObject) JSON.toJSON(pointArgs[UniversalEnum.TWO.getNumber()]);

                    if(Objects.equals(deviceType, UniversalEnum.SEVENTEEN.getValue())){ //机柜的读取不记录
                        if (Objects.equals(functionId,"04")){
                            return;
                        }
                        if (Objects.equals(functionId,"03")){
                            return;
                        }
                    }
                    if(Objects.equals(deviceType, UniversalEnum.EIGHTEEN.getValue())){ //机柜空调的不记录
                            return;

                    }
                    if (Objects.equals(functionId, UniversalEnum.A_ONE.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.A_TWO.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.A_THREE.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.A_FOUR.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.A_FIVE.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.A_SIX.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.ASKMD.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.ASKTM.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.ASKDF.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.FIFTY_TWO.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.THREE_D.getValue()) ||
                            Objects.equals(functionId, UniversalEnum.THREE_C.getValue())) {
                        return;
                    }

                    //String hashMap = Objects.equals(deviceType, UniversalEnum.FIVE.getValue())
                    //        ? UniversalEnum.RELEASE_VOICE_BROADCAST.getValue()
                    //        : String.valueOf(((HashMap<String, Object>) (DEVICEFUNCTIONNAME.get(deviceType))).get(functionId));
                    //
                    //remark.append(hashMap).append(":");
                    if (Objects.equals(deviceType, UniversalEnum.THIRTEEN.getValue()) || Objects.equals(deviceType, UniversalEnum.SIXTEEN.getValue())) {

                        String identification = params.getString("deviceName");
                        String value = params.getString("value");

                        if (Objects.equals(identification, "ac_out_1_en")) {

                            remark.append("220V 输出 1").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                        } else if (Objects.equals(identification, "ac_out_2_en")) {

                            remark.append("220V 输出 2").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                        } else if (Objects.equals(identification, "dc_out_1_en")) {

                            remark.append("12V 输出 1").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                        } else if (Objects.equals(identification, "dc_out_2_en")) {

                            remark.append("12V 输出 2").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                        } else if (Objects.equals(identification, "fan_out_en")) {

                            remark.append("风扇").append(Objects.equals(value, UniversalEnum.ONE.getValue()) ? UniversalEnum.UNPACK.getValue() : UniversalEnum.OFF.getValue());

                        }

                    }else if(Objects.equals(deviceType, UniversalEnum.SEVENTEEN.getValue())){ //动环机柜
                        if(Objects.equals(functionId, "SL")){
                            String aSwitch = params.getString("switch");
                            if ("1".equals(aSwitch)){
                                remark.append("门禁打开");
                            }else if ("0".equals(aSwitch)){
                                remark.append("门禁关闭");
                            }else {
                                remark.append("操作门禁");
                            }
                        }
                        if (Objects.equals(functionId, "TL")){
                            remark.append("雷击清零");
                        }
                        if (Objects.equals(functionId, "SR")){
                            remark.append("系统复位");
                        }
                        if (Objects.equals(functionId, "RS")){
                            remark.append("令重合闸复位");
                        }

                    }

                    if (loginUser != null) {
                        SysDept sysDept = deptService.selectDeptById(loginUser.getDeptId());
                        if (sysDept != null) {
                            operLog.setDeptName(sysDept.getDeptName());
                        }
                        operLog.setOperName(loginUser.getUsername());
                        operLog.setOperLocation(loginUser.getLoginLocation());
                        operLog.setOperType(UniversalEnum.ZERO.getValue());
                    } else {
                        operLog.setOperType(UniversalEnum.ONE.getValue());
                    }

                    saveOperLog(((JSONObject) JSON.toJSON(jsonResult)).getJSONArray("data"), operLog, pointArgs, request, deviceIds, deviceNames, deviceType, remark);


                    //if ((Objects.equals(deviceType, "2") || Objects.equals(deviceType, "5")) && Objects.nonNull(operLog.getId())) {
                    //    DcPublishManage dcPublishManage = new DcPublishManage();
                    //
                    //    if (Objects.equals(state, "0") && Objects.equals(String.valueOf(((AjaxResult) jsonResult).get("code")), "200")) {
                    //        dcPublishManage.setPublishStatus(1);//发布状态1-成功2-失败
                    //    } else {
                    //        dcPublishManage.setPublishStatus(2);//发布状态1-成功2-失败
                    //    }
                    //
                    //    if (loginUser != null) {
                    //        dcPublishManage.setPublisher(loginUser.getUsername());//发布人
                    //    } else {
                    //        dcPublishManage.setPublisher("系统定时调用");//发布人
                    //    }
                    //    dcPublishManage.setPublishTime(new Date());//发布时间
                    //    dcPublishManage.setCreateTime(new Date());//创建时间
                    //
                    //    dcPublishManage.setLogId(Long.valueOf(operLog.getId()));//发布的如果的情报板传对应id
                    //    if (Objects.equals(deviceType, "2")) {
                    //        dcPublishManage.setPublishChannels(4);
                    //    } else {
                    //        dcPublishManage.setPublishChannels(7);
                    //    }
                    //
                    //    dcPublishManage.setContentDetails(remark.toString());
                    //    dcPublishManageService.insertDcPublishManage(dcPublishManage);
                    //}

                }
            }

        } catch (Exception e) {
            throw e;
        }
    }

    private void saveOperLog(JSONArray jsonResult, DcOperLog operLog, Object[] pointArgs, HttpServletRequest request, ArrayList<String> deviceIds, ArrayList<String> deviceNames, String deviceType, StringBuilder remark) {
        IDcOperLogService dcOperLogService = SpringUtils.getBean(IDcOperLogService.class);
        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 != null ? jsonResult.toJSONString() : JSON.toJSON(AjaxResult.error()).toString());
        dcOperLogService.save(operLog);
    }
}