package org.nl.acs.ext.wms.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.AcsConfig;
import org.nl.acs.common.IDriverService;
import org.nl.acs.device.domain.Device;
import org.nl.acs.device.service.DeviceService;
import org.nl.acs.device_driver.basedriver.agv.ndcone.AgvNdcOneDeviceDriver;
import org.nl.acs.device_driver.basedriver.hongxiang_conveyor.HongXiangStationDeviceDriver;
import org.nl.acs.device_driver.basedriver.plug_pull_device_site.PlugPullDeviceSiteDeviceDriver;
import org.nl.acs.device_driver.tianneng.guhuashi.GuhuashiSiteDeviceDriver;
import org.nl.acs.ext.wms.data.*;
import org.nl.acs.ext.wms.service.AcsToLiKuService;
import org.nl.acs.ext.wms.service.WmsToAcsService;
import org.nl.acs.instruction.domain.Instruction;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.opc.DeviceExtraManageDto;
import org.nl.acs.route.service.RouteLineService;
import org.nl.acs.storage_cell.domain.StorageCell;
import org.nl.acs.storage_cell.service.mapper.StorageCellMapper;
import org.nl.acs.task.service.TaskService;
import org.nl.acs.task.service.dto.TaskDto;
import org.nl.common.exception.BadRequestException;
import org.nl.common.utils.RedisUtils;
import org.nl.config.SpringContextHolder;
import org.nl.system.service.param.ISysParamService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
@Slf4j
public class WmsToAcsServiceImpl implements WmsToAcsService {

    private final ApplicationContext applicationContext;

    private final InstructionService InstructionService;
    private final TaskService taskService;
    private final DeviceService deviceService;
    private final DeviceAppService deviceAppService;
    private final RouteLineService routeLineService;
    private final AcsToLiKuService acsToLiKuService;
    private final StorageCellMapper storageCellMapper;

    private String log_file_type = "log_file_type";
    private String log_type = "LMS请求ACS";
    @Resource
    private RedissonClient redissonClient;

    @Resource
    private RedisUtils redisUtils;

    @Override
    public CancelTaskResponse cancelFromWms(List<CancelTaskRequest> reqs) throws Exception {
        ISysParamService paramService = SpringContextHolder.getBean(ISysParamService.class);
        InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class);
        CancelTaskResponse response = new CancelTaskResponse();
        JSONArray errArr = new JSONArray();
        for (int i = 0; i < reqs.size(); i++) {
            CancelTaskRequest request = reqs.get(i);
            String task_code = request.getTask_code();
            String vehicle_code = request.getVehicle_code();
            Map<String, String> params = request.getParams();
            if (StrUtil.isEmpty(task_code)) {
                throw new BadRequestException("任务号不能为空");
            }
            TaskDto taskDto = taskService.findByCodeFromCache(task_code);
            String cancelTaskCheck = paramService.findByCode(AcsConfig.CANCELTASKCHECK).getValue();
            //todo acs直接取消任务还是取消指令还是任务
            if (StrUtil.equals(cancelTaskCheck, "1")) {
                taskService.cancelNoSendWms(taskDto.getTask_id());
            } else if (StrUtil.equals(cancelTaskCheck, "0")) {
                Instruction inst = instructionService.findByTaskcode(task_code);
                if (inst == null) {
                    taskService.cancelNoSendWms(taskDto.getTask_id());
                } else {
                    throw new RuntimeException("指令正在执行中,操作失败!");
                }
            }

        }

        if (ObjectUtil.isEmpty(errArr)) {
            response.setStatus(200);
        } else {
            response.setStatus(400);
        }
        response.setMessage("success");
        response.setErrArr(errArr);
        log.info("cancelFromWms--------------:输出参数：" + response);

        return response;
    }

    /**
     * agv信息查询
     */
    @Override
    public JSONObject allDeviceStatus() {
        JSONObject jsonObject = new JSONObject();
        JSONArray agv = (JSONArray) redisUtils.get("AGV");
        if (ObjectUtil.isNotEmpty(agv)) {
            jsonObject.put("agv", agv);
        } else {
            jsonObject.put("agv", new JSONArray());
        }
        return jsonObject;
    }


    @Override
    public Map<String, Object> updateDeviceGoodsFromWms(String param) {
        JSONArray datas = JSONArray.parseArray(param);
        log.info("updateDeviceGoodsFromWms--------------:输入参数" + datas.toString());
        for (int i = 0; i < datas.size(); i++) {
            JSONObject data = datas.getJSONObject(i);
            String device_code = data.getString("point_code");
            String has_goods = data.getString("has_goods");

            JSONObject jo = new JSONObject();
            jo.put("device_code", device_code);
            jo.put("hasGoodStatus", has_goods);
            deviceService.changeDeviceStatus(jo);

        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", HttpStatus.OK);
        resultJson.put("message", "操作成功");
        resultJson.put("data", new JSONObject());
        log.info("updateDeviceGoodsFromWms--------------:输出参数" + resultJson.toString());
        return resultJson;
    }

    @Override
    public Map<String, Object> areaControl(JSONObject form) {
        String device_code = form.getString("device_code");
        String type = form.getString("type");

        Device device = deviceAppService.findDeviceByCode(device_code);

        JSONObject resultJson = new JSONObject();
        resultJson.put("status", HttpStatus.OK);
        resultJson.put("message", "操作成功");
        resultJson.put("data", new JSONObject());
        log.info("updateDeviceGoodsFromWms--------------:输出参数" + resultJson.toString());
        return resultJson;
    }

    @Override
    public PutActionResponse putAction(String jsonObject) throws Exception {
        try {
            MDC.put(log_file_type, log_type);
            log.info("putAction--------------:输出参数" + jsonObject);
            JSONArray datas = JSONArray.parseArray(jsonObject);
            PutActionResponse response = new PutActionResponse();
            JSONArray errArr = new JSONArray();
            for (int i = 0; i < datas.size(); i++) {
                String data = datas.get(i).toString();
                PutActionRequest request = JsonUtl.format(data, PutActionRequest.class);
                String device_code = request.getDevice_code();
                String code = request.getCode();
                String value = request.getValue();
                Device device = deviceAppService.findDeviceByCode(device_code);
                if (ObjectUtil.isEmpty(device)) {
                    throw new Exception("未找到对应设备:" + device_code);
                }
                HongXiangStationDeviceDriver hongXiangStationDeviceDriver;
                PlugPullDeviceSiteDeviceDriver plugPullDeviceSiteDeviceDriver;
                if (device.getDeviceDriver() instanceof HongXiangStationDeviceDriver) {
                    hongXiangStationDeviceDriver = (HongXiangStationDeviceDriver) device.getDeviceDriver();
                    hongXiangStationDeviceDriver.writing(code, value);
                }


            }
            response.setStatus(200);
            response.setMessage("success");
            log.info("putAction--------------:输出参数：" + response);
            return response;
        } finally {
            MDC.remove(log_file_type);
        }

    }

    @Override
    public JSONArray queryDevice(String jsonObject) throws Exception {
        JSONArray agvStatus = new JSONArray();
        List<Device> deviceList = deviceAppService.findAllDevice();
        for (Device device : deviceList) {
            if (device.getDeviceDriver() instanceof AgvNdcOneDeviceDriver) {
                AgvNdcOneDeviceDriver agvNdcOneDeviceDriver = (AgvNdcOneDeviceDriver) device.getDeviceDriver();
                JSONObject json = new JSONObject();
                json.put("device_code", agvNdcOneDeviceDriver.device.getDevice_code());
                json.put("device_name", agvNdcOneDeviceDriver.device.getDevice_code());
                json.put("device_status", agvNdcOneDeviceDriver.getStatus());
                json.put("agv_status", agvNdcOneDeviceDriver.getAgv_status());
                json.put("energyLevel", agvNdcOneDeviceDriver.getElectric_qty());
                json.put("transportOrder", agvNdcOneDeviceDriver.getTransportOrder());
                json.put("positionX", agvNdcOneDeviceDriver.getX());
                json.put("positionY", agvNdcOneDeviceDriver.getY());
                json.put("positionAngle", agvNdcOneDeviceDriver.getAngle());
                agvStatus.add(json);
            }
        }
        return agvStatus;
    }


    @Override
    public Map<String, Object> queryDeviceDBValue(String whereJson) {
        JSONArray datas = JSONArray.parseArray(whereJson);
        log.info("orderStatusUpdate--------------:输入参数" + datas.toString());
        JSONObject map = new JSONObject();
        if (datas.size() > 0) {
            for (int i = 0; i < datas.size(); i++) {
                JSONObject jsonObject = datas.getJSONObject(i);
                String device_code = jsonObject.getString("device_code");
                String dbName = jsonObject.getString("DB");
                Device device = deviceAppService.findDeviceByCode(device_code);
                List<DeviceExtraManageDto> extra = device.getExtra();
                for (int j = 0; j < extra.size(); j++) {
                    DeviceExtraManageDto deviceExtraManageDto = extra.get(j);
                    String deviceCode = deviceExtraManageDto.getDevice_code();
                    String extra_name = deviceExtraManageDto.getExtra_name();
                    if (deviceCode.equals(device_code) && extra_name.equals(dbName)) {
                        String extra_code = deviceExtraManageDto.getExtra_code();
                        String[] split = extra_code.split("\\.");
                        extra_code = split[split.length - 1];
                        extra_code = extra_code.substring(0, 1).toUpperCase() + extra_code.substring(1);
                        IDriverService driverService = applicationContext.getBean(device.getDeviceDriverDefination().getDriverCode(), IDriverService.class);
                        Integer dbValue = driverService.getDbValue(device, extra_code);
                        map.put(dbName, dbValue);
                        break;
                    }
                }
            }
        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", HttpStatus.OK);
        resultJson.put("message", "操作成功");
        resultJson.put("data", map);
        return resultJson;
    }

    @Override
    public Map<String, Object> querydevice(String whereJson) {
        return null;
    }


    @Override
    public Map<String, Object> putPlusPullAction(String param) {
        try {
            MDC.put(log_file_type, log_type);
            log.info("putPlusPullAction-----输入参数{}", param);
            JSONObject jo = JSON.parseObject(param);
            String device_code = jo.getString("device_code");
            String size = jo.getString("size");
            String type = jo.getString("type");
            Device device = deviceAppService.findDeviceByCode(device_code);
            if (ObjectUtil.isEmpty(device)) {
                JSONObject resultJson = new JSONObject();
                resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                resultJson.put("message", "未找到对应的设备:" + device_code);
                return resultJson;
            }
            PlugPullDeviceSiteDeviceDriver plugPullDeviceSiteDeviceDriver;
            if (device.getDeviceDriver() instanceof PlugPullDeviceSiteDeviceDriver) {
                plugPullDeviceSiteDeviceDriver = (PlugPullDeviceSiteDeviceDriver) device.getDeviceDriver();
                // 0 穿轴 1拔轴
                if (StrUtil.equals(type, "1")) {

                    if (plugPullDeviceSiteDeviceDriver.getMode() == 1) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求拔轴，当前设备工作模式未自动");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getAction() == 0) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求拔轴，当前设备未全自动");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getStatus() != 0) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求拔轴，当前设备未待机");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getControl() != 0) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求拔轴，当前设备未远程控制");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getMove() != 0) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求拔轴，当前设备有轴");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getMode() == 0 && plugPullDeviceSiteDeviceDriver.getAction() == 1
                            && plugPullDeviceSiteDeviceDriver.getStatus() == 0 && plugPullDeviceSiteDeviceDriver.getMove() == 0
                            && plugPullDeviceSiteDeviceDriver.getControl() == 0) {

                        List list = new ArrayList();
                        Map map = new HashMap();
                        map.put("code", "to_size");
                        map.put("value", size);
                        list.add(map);
                        Map map2 = new HashMap();
                        map2.put("code", "to_type");
                        map2.put("value", type);
                        list.add(map2);
                        Map map3 = new HashMap();
                        map3.put("code", "to_command");
                        map3.put("value", "1");
                        list.add(map3);
                        plugPullDeviceSiteDeviceDriver.writing(list);

                    } else {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "当前设备状态不满足下发条件");
                        return resultJson;
                    }

                } else if (StrUtil.equals(type, "0")) {

                    if (plugPullDeviceSiteDeviceDriver.getMode() == 1) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求插轴，当前设备工作模式未自动");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getAction() == 0) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求插轴，当前设备未全自动");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getStatus() != 0) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求插轴，当前设备未待机");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getControl() != 0) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求插轴，当前设备未远程控制");
                        return resultJson;
                    }
                    if (plugPullDeviceSiteDeviceDriver.getMove() != 1) {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "请求插轴，当前设备没有轴");
                        return resultJson;
                    }

                    if (plugPullDeviceSiteDeviceDriver.getMode() == 0 && plugPullDeviceSiteDeviceDriver.getAction() == 1
                            && plugPullDeviceSiteDeviceDriver.getStatus() == 0 && plugPullDeviceSiteDeviceDriver.getMove() == 1
                            && plugPullDeviceSiteDeviceDriver.getControl() == 0) {

                        List list = new ArrayList();
                        Map map = new HashMap();
                        map.put("code", "to_size");
                        map.put("value", size);
                        list.add(map);
                        Map map2 = new HashMap();
                        map2.put("code", "to_type");
                        map2.put("value", type);
                        list.add(map2);
                        Map map3 = new HashMap();
                        map3.put("code", "to_command");
                        map3.put("value", "1");
                        list.add(map3);
                        plugPullDeviceSiteDeviceDriver.writing(list);

                    } else {
                        JSONObject resultJson = new JSONObject();
                        resultJson.put("status", HttpStatus.BAD_REQUEST.value());
                        resultJson.put("message", "当前设备状态不满足下发条件");
                        return resultJson;
                    }
                }
            }

            JSONObject resultJson = new JSONObject();
            resultJson.put("status", HttpStatus.OK.value());
            resultJson.put("message", "操作成功");
            log.info("putPlusPullAction--------------:输出参数" + resultJson.toString());
            return resultJson;

        } finally {
            MDC.remove(log_file_type);
        }
    }


    @Override
    @SneakyThrows
    public CreateTaskResponse crateTask(List<CreateTaskRequest> reqs) {
        RLock lock = redissonClient.getLock(reqs.get(0).getTask_code());
        boolean tryLock = lock.tryLock(0, TimeUnit.SECONDS);
        CreateTaskResponse response = new CreateTaskResponse();
        try {
            if (tryLock) {
                MDC.put(log_file_type, log_type);
                log.info("crateTask-----输入参数{}", reqs);
                JSONArray errArr = new JSONArray();
                for (int i = 0; i < reqs.size(); i++) {
                    CreateTaskRequest req = reqs.get(i);
                    String task_code = req.getTask_code();
                    String task_id = req.getTask_id();
                    String start_device_code = req.getStart_device_code();
                    String next_device_code = req.getNext_device_code();
                    String priority = req.getPriority();
                    String vehicle_code = req.getVehicle_code();
                    String vehicle_type = req.getVehicle_type();
                    String task_type = req.getTask_type();
                    String agv_task_type = req.getSys_type();
                    String remark = req.getRemark();
                    Map<String, String> params = req.getParams();

                    String start_point_code = "";
                    String next_point_code = "";
                    //                if (StrUtil.isEmpty(task_id)) {
                    //                    JSONObject json = new JSONObject();
                    //                    json.put("task_id", task_id);
                    //                    json.put("message", "任务号不能为空");
                    //                    errArr.add(json);
                    //                    continue;
                    //                }
                    if (StrUtil.isEmpty(task_code)) {
                        JSONObject json = new JSONObject();
                        json.put("task_code", task_code);
                        json.put("message", "任务号不能为空");
                        errArr.add(json);
                        continue;
                    }
                    if (StrUtil.isEmpty(start_device_code)) {
                        JSONObject json = new JSONObject();
                        json.put("task_code", task_code);
                        json.put("message", "起点不能为空");
                        errArr.add(json);
                        continue;
                    }
                    if (StrUtil.isEmpty(next_device_code)) {
                        JSONObject json = new JSONObject();
                        json.put("task_code", task_code);
                        json.put("message", "终点不能为空");
                        errArr.add(json);
                        continue;
                    }
                    //JSONObject start_device_json = WQLObject.getWQLObject("acs_storage_cell").query("parent_storage_code ='" + start_device_code + "'").uniqueResult(0);
                    StorageCell storageCell_start = new LambdaQueryChainWrapper<>(storageCellMapper)
                            .eq(StorageCell::getParent_storage_code, start_device_code)
                            .one();
                    if (!ObjectUtil.isEmpty(storageCell_start)) {
                        //start_point_code = (String) start_device_json.get("parent_storage_code") == null ? start_device_code : (String) start_device_json.get("storage_code");
                        start_point_code = (String) storageCell_start.getParent_storage_code() == null ? start_device_code : (String) storageCell_start.getStorage_code();
                    }
                    //JSONObject next_device_json = WQLObject.getWQLObject("acs_storage_cell").query("parent_storage_code ='" + next_device_code + "'").uniqueResult(0);
                    StorageCell storageCell_start_next = new LambdaQueryChainWrapper<>(storageCellMapper)
                            .eq(StorageCell::getParent_storage_code, next_device_code)
                            .one();
                    if (!ObjectUtil.isEmpty(storageCell_start_next)) {
                        //next_point_code = (String) next_device_json.get("parent_storage_code") == null ? next_point_code : (String) next_device_json.get("storage_code");
                        next_point_code = (String) storageCell_start_next.getParent_storage_code() == null ? next_point_code : (String) storageCell_start_next.getStorage_code();
                    }
                    if (StrUtil.isNotEmpty(start_point_code) && start_point_code.indexOf("-") > 0) {
                        String str[] = start_point_code.split("-");
                        start_device_code = str[0];
                    } else {
                        start_device_code = start_point_code;
                    }

                    if (StrUtil.isNotEmpty(next_point_code) && next_point_code.indexOf("-") > 0) {
                        String str[] = next_point_code.split("-");
                        next_device_code = str[0];
                    } else {
                        next_device_code = next_point_code;
                    }

                    String route_plan_code = "";

                    if (StrUtil.isEmpty(route_plan_code)) {
                        route_plan_code = "normal";
                    }
                    TaskDto taskDto = taskService.findByCodeFromCache(task_code);
                    if (taskDto != null) {
                        JSONObject json = new JSONObject();
                        json.put("task_code", task_code);
                        json.put("message", "不能存在相同的任务号");
                        errArr.add(json);
                        continue;
                    }
                    if (!StrUtil.isEmpty(vehicle_code)) {
                        TaskDto vehicle_dto = taskService.findByContainer(vehicle_code);
                        if (vehicle_dto != null) {
                            JSONObject json = new JSONObject();
                            json.put("task_code", task_code);
                            json.put("message", "不能存在相同的托盘号");
                            errArr.add(json);
                            continue;
                        }
                    }

                    if (StrUtil.isEmpty(start_point_code)) {
                        JSONObject json = new JSONObject();
                        json.put("task_code", task_code);
                        json.put("message", req.getStart_device_code() + " 该设备号未找到对应点位");
                        errArr.add(json);
                        continue;
                    }
                    if (StrUtil.isEmpty(next_point_code)) {
                        JSONObject json = new JSONObject();
                        json.put("task_code", task_code);
                        json.put("message", req.getNext_device_code() + " 该设备号未找到对应点位");
                        errArr.add(json);
                        continue;
                    }
                    JSONObject jo = new JSONObject();
                    jo.put("ext_task_id", task_id);
                    jo.put("task_code", task_code);
                    jo.put("task_id", IdUtil.simpleUUID());
                    jo.put("start_point_code", start_point_code);
                    jo.put("next_point_code", next_point_code);
                    jo.put("start_parent_code", start_point_code);
                    jo.put("next_parent_code", next_point_code);
                    jo.put("start_device_code", start_device_code);
                    jo.put("next_device_code", next_device_code);
                    jo.put("priority", priority);
                    jo.put("vehicle_code", vehicle_code);
                    jo.put("vehicle_type", vehicle_type);
                    jo.put("agv_system_type", agv_task_type);
                    jo.put("remark", remark);
                    jo.put("params", params);
                    jo.put("task_type", StrUtil.isEmpty(task_type) ? 1 : Integer.parseInt(task_type));
                    TaskDto task_dto = jo.toJavaObject(TaskDto.class);
                    try {
                        taskService.create(task_dto);
                    } catch (Exception e) {
                        // e.printStackTrace();
                        JSONObject json = new JSONObject();
                        json.put("task_code", task_code);
                        json.put("message", e.getMessage());
                        errArr.add(json);
                        continue;
                    }
                }
                if (ObjectUtil.isEmpty(errArr)) {
                    response.setStatus(200);
                    response.setMessage("success");
                } else {
                    response.setStatus(400);
                    if (ObjectUtil.isNotEmpty(errArr)) {
                        response.setMessage(errArr.getJSONObject(0).getString("message"));
                    } else {
                        response.setMessage("false");
                    }
                    response.setErrArr(errArr);
                }
                log.info("createFromWms--------------:输出参数：" + JSON.toJSONString(response));
                return response;
            } else {
                log.info("任务正在创建...");
            }
        } finally {
            if (tryLock) {
                lock.unlock();
            }
        }
        return response;
    }

    @Override
    public JSONObject notifyAcs(JSONObject param) {
        log.info("notifyAcs - 请求参数 {}", param);
        JSONObject result = new JSONObject();
        Integer type = param.getInteger("type");
        String device_code = param.getString("device_code");
        Device device = deviceAppService.findDeviceByCode(device_code);
        ;
        switch (type) {
            case 1:
                //LMS通知固化室极板已进满信号，开始固化
                if (device.getDeviceDriver() instanceof GuhuashiSiteDeviceDriver) {
                    GuhuashiSiteDeviceDriver guhuashiSiteDeviceDriver = (GuhuashiSiteDeviceDriver) device.getDeviceDriver();
                    guhuashiSiteDeviceDriver.writing("VW70043", 1);
                }
                break;
            case 2:
                //LMS通知固化室在下班时未进满信号，开始固化
                if (device.getDeviceDriver() instanceof GuhuashiSiteDeviceDriver) {
                    GuhuashiSiteDeviceDriver guhuashiSiteDeviceDriver = (GuhuashiSiteDeviceDriver) device.getDeviceDriver();
                    guhuashiSiteDeviceDriver.writing("VW70045", 1);
                }
                break;
            case 3:
                //LMS通知固化室极板已出完
                if (device.getDeviceDriver() instanceof GuhuashiSiteDeviceDriver) {
                    GuhuashiSiteDeviceDriver guhuashiSiteDeviceDriver = (GuhuashiSiteDeviceDriver) device.getDeviceDriver();
                    guhuashiSiteDeviceDriver.writing("VW70053", 1);
                }
                break;
            default:
                break;
        }

        result.put("status", HttpStatus.OK.value());
        result.put("message", "通知成功！");
        log.info("notifyAcs - 返回参数 {}", result);
        return result;
    }

}
