jinpengyong
2023-08-25 f9f8f90ac63d6ce3274410d3721b173f40db6e41
screen-manage/src/main/java/com/moral/api/service/impl/VersionServiceImpl.java
@@ -4,10 +4,9 @@
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.config.mybatis.wrapper.NullFilterWrapper;
import com.moral.api.entity.Sensor;
import com.moral.api.entity.User;
import com.moral.api.entity.Version;
import com.moral.api.entity.VersionSensorUnit;
import com.moral.api.entity.*;
import com.moral.api.mapper.OrganizationUnitAlarmMapper;
import com.moral.api.mapper.SensorMapper;
import com.moral.api.mapper.VersionMapper;
import com.moral.api.mapper.VersionSensorUnitMapper;
import com.moral.api.pojo.dto.version.VersionDTO;
@@ -15,6 +14,7 @@
import com.moral.api.pojo.form.version.*;
import com.moral.api.service.VersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.util.CacheUtils;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import com.moral.util.ConvertUtils;
@@ -41,6 +41,10 @@
    VersionMapper versionMapper;
    @Autowired
    VersionSensorUnitMapper versionSensorUnitMapper;
    @Autowired
    SensorMapper sensorMapper;
    @Autowired
    OrganizationUnitAlarmMapper organizationUnitAlarmMapper;
    @Override
    public VersionQueryDTO query(VersionQueryForm form) {
@@ -97,7 +101,7 @@
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        return dto;
    }
    @Override
    @Transactional
    public VersionDTO update(VersionUpdateForm form) {
@@ -162,6 +166,17 @@
        VersionDTO dto = new VersionDTO();
        //取参
        Integer id = form.getId();
        //查询型号是否被使用,如果被使用则无法删除
        QueryWrapper<OrganizationUnitAlarm> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        queryWrapper.eq("is_delete", Constants.NOT_DELETE);
        queryWrapper.eq("version_id", id);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryWrapper);
        if (!ObjectUtils.isEmpty(organizationUnitAlarms)) {
            dto.setCode(ResponseCodeEnum.VERSION_USED.getCode());
            dto.setMsg(ResponseCodeEnum.VERSION_USED.getMsg());
            return dto;
        }
        //查询要删除的版本用于记录日志
        Version oldVersion = versionMapper.selectById(id);
        //执行删除
@@ -188,16 +203,73 @@
        //取参
        List<VersionSensorUnit> sensorUnits = form.getSensorUnits();
        Integer versionId = form.getVersionId();
        //删除之前分配的单位和因子
        UpdateWrapper deleteWrapper = new UpdateWrapper();
        deleteWrapper.eq("version_id", versionId);
        deleteWrapper.set("is_delete", Constants.DELETE);
        versionSensorUnitMapper.update(null, deleteWrapper);
        //添加新分配的单位和因子
        //将前端传来的id转为code
        for (VersionSensorUnit sensorUnit : sensorUnits) {
            sensorUnit.setVersionId(versionId);
            Sensor sensor = sensorMapper.selectById(sensorUnit.getSensorId());
            sensorUnit.setSensorCode(sensor.getCode());
        }
        //查询之前分配的单位和因子
        QueryWrapper<VersionSensorUnit> queryOldWrapper = new QueryWrapper<>();
        queryOldWrapper.eq("version_id", versionId);
        queryOldWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<VersionSensorUnit> oldSensorUnits = versionSensorUnitMapper.selectList(queryOldWrapper);
        //判断出变动的因子(要插入和要删除的)
        List<VersionSensorUnit> insertList = new ArrayList<>();
        List<VersionSensorUnit> deleteList = new ArrayList<>();
        sensorUnits.forEach(value -> {
            value.setVersionId(versionId);
            if (!oldSensorUnits.contains(value))
                insertList.add(value);
        });
        oldSensorUnits.forEach(value -> {
            if (!sensorUnits.contains(value))
                deleteList.add(value);
        });
        //删除记录
        if (!ObjectUtils.isEmpty(deleteList)) {
            UpdateWrapper deleteWrapper = new UpdateWrapper();
            List<Integer> deleteIds = new ArrayList<>();
            deleteList.forEach(value -> deleteIds.add(value.getId()));
            deleteWrapper.in("id", deleteIds);
            deleteWrapper.set("is_delete", Constants.DELETE);
            versionSensorUnitMapper.update(null, deleteWrapper);
        }
        //添加记录
        for (VersionSensorUnit sensorUnit : insertList) {
            versionSensorUnitMapper.insert(sensorUnit);
        }
        //维护组织型号关系表
        QueryWrapper<OrganizationUnitAlarm> queryOrgUnitAlarmWrapper = new QueryWrapper<>();
        queryOrgUnitAlarmWrapper.select("distinct organization_id");
        queryOrgUnitAlarmWrapper.eq("version_id", versionId);
        queryOrgUnitAlarmWrapper.eq("is_delete", Constants.NOT_DELETE);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryOrgUnitAlarmWrapper);
        List<Integer> organizationIds = new ArrayList<>();
        organizationUnitAlarms.forEach(value -> organizationIds.add(value.getOrganizationId()));
        for (Integer organizationId : organizationIds) {
            //删除因子
            for (VersionSensorUnit versionSensorUnit : deleteList) {
                UpdateWrapper deleteOrganizationUnitAlarmWrapper = new UpdateWrapper();
                deleteOrganizationUnitAlarmWrapper.eq("organization_id", organizationId);
                deleteOrganizationUnitAlarmWrapper.eq("version_id", versionId);
                deleteOrganizationUnitAlarmWrapper.eq("sensor_code", versionSensorUnit.getSensorCode());
                deleteOrganizationUnitAlarmWrapper.set("is_delete", Constants.DELETE);
                organizationUnitAlarmMapper.update(null, deleteOrganizationUnitAlarmWrapper);
            }
            //新增因子
            for (VersionSensorUnit versionSensorUnit : insertList) {
                OrganizationUnitAlarm organizationUnitAlarm = new OrganizationUnitAlarm();
                organizationUnitAlarm.setOrganizationId(organizationId);
                organizationUnitAlarm.setVersionId(versionId);
                organizationUnitAlarm.setSensorCode(versionSensorUnit.getSensorCode());
                organizationUnitAlarm.setUnitKey(versionSensorUnit.getUnitKey());
                organizationUnitAlarm.setShowUnitKey(versionSensorUnit.getUnitKey());
                organizationUnitAlarmMapper.insert(organizationUnitAlarm);
            }
        }
        //刷新deviceInfo缓存
        CacheUtils.refreshDeviceAlarmInfo();
        CacheUtils.refreshSpecialDeviceAlarmInfo();
        //封装返回结果
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
@@ -238,11 +310,11 @@
        if (ObjectUtils.isEmpty(querySensors)) {
            dto.setTotal(0);
            dto.setPages(0);
        }else{
        } else {
            dto.setTotal(querySensors.size());
            double querySize = (double)querySensors.size();
            double dSize = (double)size;
            dto.setPages((int)Math.ceil(querySize/dSize));
            double querySize = (double) querySensors.size();
            double dSize = (double) size;
            dto.setPages((int) Math.ceil(querySize / dSize));
        }
        dto.setSize(size);
        dto.setCurrent(page);
@@ -252,5 +324,30 @@
        return dto;
    }
    @Override
    public VersionQueryDTO queryByOrganizationId(Integer organizationId) {
        //创建返回对象
        VersionQueryDTO dto = new VersionQueryDTO();
        //查询型号id
        QueryWrapper<OrganizationUnitAlarm> queryVersionIdsWrapper = new QueryWrapper<>();
        queryVersionIdsWrapper.select("DISTINCT version_id").eq("is_delete", Constants.NOT_DELETE).eq("organization_id", organizationId);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(queryVersionIdsWrapper);
        List<Integer> versionIds = new ArrayList<>();
        for (OrganizationUnitAlarm organizationUnitAlarm : organizationUnitAlarms) {
            versionIds.add(organizationUnitAlarm.getVersionId());
        }
        //根据型号id查询型号
        List<Version> versions = new ArrayList<>();
        if (!ObjectUtils.isEmpty(versionIds))
            versions = versionMapper.selectBatchIds(versionIds);
        //封装返回对象
        List<VersionDTO> versionDTOS = new ArrayList<>();
        versions.forEach(value -> versionDTOS.add(new VersionDTO(value)));
        dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
        dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
        dto.setVersionDTOS(versionDTOS);
        return dto;
    }
}