jinpengyong
2021-05-25 3689936045a84b2c29899bee8680b2129e8ef43a
screen-manage/src/main/java/com/moral/api/service/impl/DeviceServiceImpl.java
@@ -2,7 +2,6 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.Device;
import com.moral.api.entity.ManageAccount;
@@ -20,6 +19,8 @@
import com.moral.api.pojo.vo.device.DeviceVO;
import com.moral.api.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.LogUtils;
import com.moral.constant.Constants;
import com.moral.redis.RedisUtil;
import com.moral.util.ConvertUtils;
@@ -28,11 +29,19 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
/**
 * <p>
@@ -63,12 +72,23 @@
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private LogUtils logUtils;
    @Override
    @Transactional
    public void insert(Device device) {
        Integer orgId = monitorPointMapper.selectById(device.getMonitorPointId()).getOrganizationId();
        device.setOrganizationId(orgId);
        deviceMapper.insert(device);
        Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId());
        //新增设备信息存入redis
        RedisUtil.set("device_" + device.getMac(), deviceInfo);
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("添加了设备:").append(device.getName()).append(";").append("mac:").append(device.getMac());
        logUtils.saveOperationForManage(request, content.toString(), Constants.INSERT_OPERATE_TYPE);
    }
    @Override
@@ -77,18 +97,57 @@
        UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", deviceId).set("is_delete", Constants.DELETE);
        deviceMapper.update(null, updateWrapper);
        String mac = deviceMapper.selectById(deviceId).getMac();
        Device device = deviceMapper.selectById(deviceId);
        String mac = device.getMac();
        //清除redis
        RedisUtil.del("device_" + mac);
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("删除了设备:").append(device.getName()).append(";").append("mac:").append(mac);
        logUtils.saveOperationForManage(request, content.toString(), Constants.DELETE_OPERATE_TYPE);
    }
    @Override
    @Transactional
    public void update(Device device) {
        Integer deviceId = device.getId();
        Device oldDevice = deviceMapper.selectById(deviceId);
        deviceMapper.updateById(device);
        String mac = deviceMapper.selectById(deviceId).getMac();
        //更新redis
        Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId());
        RedisUtil.set("device_" + deviceInfo.get("mac"), deviceInfo);
        RedisUtil.del("device_" + mac);
        Map<String, Object> deviceInfo = selectDeviceInfoById(deviceId);
        RedisUtil.set("device_" + mac, deviceInfo);
        //操作日志记录
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        StringBuilder content = new StringBuilder();
        content.append("修改了设备:").append(mac).append(":");
        Field[] fields = Device.class.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals("id")) {
                continue;
            }
            if ("serialVersionUID".equals(field.getName())) {
                continue;
            }
            String fieldName = field.getName();
            PropertyDescriptor pd = null;
            try {
                pd = new PropertyDescriptor(fieldName, Device.class);
                Method method = pd.getReadMethod();
                Object o1 = method.invoke(oldDevice);
                Object o2 = method.invoke(device);
                if (o2 != null) {
                    content.append(fieldName).append(":").append(o1).append("-->").append(o2).append(";");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        logUtils.saveOperationForManage(request, content.toString(), Constants.UPDATE_OPERATE_TYPE);
    }
    @Override
@@ -132,10 +191,10 @@
    }
    @Override
    public IPage<Device> selectDevices(Map<String, Object> parameters) {
    public Map<String, Object> selectDevices(Map<String, Object> parameters) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        Integer page = (Integer) parameters.get("page");
        Integer size = (Integer) parameters.get("size");
        int page = Integer.parseInt(parameters.get("page").toString());
        int size = Integer.parseInt(parameters.get("size").toString());
        Object order = parameters.get("order");
        Object orderType = parameters.get("orderType");
        Object name = parameters.get("name");
@@ -160,44 +219,64 @@
        queryWrapper.eq("is_delete", Constants.NOT_DELETE);
        Page<Device> devicePage = new Page<>(page, size);
        deviceMapper.selectPage(devicePage, queryWrapper);
        return devicePage;
        List<Device> devices = devicePage.getRecords();
        List<Map<String, Object>> items = new ArrayList<>();
        for (Device device : devices) {
            Map<String, Object> deviceInfo = selectDeviceInfoById(device.getId());
            items.add(deviceInfo);
        }
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("total", devicePage.getTotal());
        result.put("totalPage", devicePage.getPages());
        result.put("current", devicePage.getCurrent());
        result.put("pageSize", devicePage.getSize());
        result.put("item", items);
        return result;
    }
    @Override
    public Map<String, Object> selectDeviceInfoById(Integer deviceId) {
        Map<String, Object> result = new LinkedHashMap<>();
        String mac = deviceMapper.selectById(deviceId).getMac();
        Map<String, Object> deviceInfo = (Map<String, Object>) RedisUtil.get("device_" + mac);
        //先从redis中取
        if (deviceInfo != null) {
            return deviceInfo;
        }
        deviceInfo = new LinkedHashMap<>();
        DeviceVO device = deviceMapper.selectDeviceInfoById(deviceId);
        //设备
        result.put("id", device.getId());
        result.put("name", device.getName());
        result.put("mac", device.getMac());
        result.put("address", device.getAddress());
        result.put("longitude", device.getLongitude());
        result.put("latitude", device.getLatitude());
        result.put("createTime", DateUtils.dateToDateString(device.getCreateTime()));
        deviceInfo.put("id", device.getId());
        deviceInfo.put("name", device.getName());
        deviceInfo.put("mac", device.getMac());
        deviceInfo.put("address", device.getAddress());
        deviceInfo.put("longitude", device.getLongitude());
        deviceInfo.put("latitude", device.getLatitude());
        deviceInfo.put("createTime", DateUtils.dateToDateString(device.getCreateTime()));
        deviceInfo.put("installTime", device.getInstallTime() == null ? null : DateUtils.dateToDateString(device.getInstallTime()));
        //行业
        result.put("profession", device.getProfession());
        result.put("professionName", device.getProfessionName());
        deviceInfo.put("profession", device.getProfession());
        deviceInfo.put("professionName", device.getProfessionName());
        //工艺
        result.put("tech", device.getTech());
        result.put("techName", device.getTechName());
        deviceInfo.put("tech", device.getTech());
        deviceInfo.put("techName", device.getTechName());
        //检测器
        result.put("detector", device.getDetector());
        result.put("detectorName", device.getDetectorName());
        deviceInfo.put("detector", device.getDetector());
        deviceInfo.put("detectorName", device.getDetectorName());
        //采购商
        result.put("purchaser", device.getPurchaser());
        result.put("purchaserName", device.getPurchaserName());
        deviceInfo.put("purchaser", device.getPurchaser());
        deviceInfo.put("purchaserName", device.getPurchaserName());
        //型号
        Map<String, Object> versionInfo = new LinkedHashMap<>();
        Version version = device.getVersion();
        versionInfo.put("id", version.getId());
        versionInfo.put("name", version.getName());
        result.put("version", versionInfo);
        deviceInfo.put("version", versionInfo);
        //维护人
        List<Map<String, Object>> operatorsInfo = new ArrayList<>();
@@ -208,22 +287,23 @@
            operatorMap.put("name", operator.getUserName());
            operatorsInfo.add(operatorMap);
        }
        result.put("operators", operatorsInfo);
        deviceInfo.put("operators", operatorsInfo);
        //组织
        Map<String, Object> orgInfo = new LinkedHashMap<>();
        Organization organization = device.getOrganization();
        orgInfo.put("id", organization.getId());
        orgInfo.put("name", organization.getName());
        result.put("organization", orgInfo);
        deviceInfo.put("organization", orgInfo);
        //站点
        Map<String, Object> mpInfo = new LinkedHashMap<>();
        MonitorPoint monitorPoint = device.getMonitorPoint();
        mpInfo.put("id", monitorPoint.getId());
        mpInfo.put("name", monitorPoint.getName());
        result.put("monitorPoint", mpInfo);
        return result;
        deviceInfo.put("monitorPoint", mpInfo);
        RedisUtil.set("device_" + mac, deviceInfo);
        return deviceInfo;
    }
    @Override