| | |
| | | package com.dji.sample.manage.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.dji.sample.common.error.CommonErrorEnum; |
| | | import com.dji.sample.common.model.Pagination; |
| | | import com.dji.sample.common.model.PaginationData; |
| | | import com.dji.sample.component.mqtt.model.*; |
| | | import com.dji.sample.component.mqtt.service.IMessageSenderService; |
| | | import com.dji.sample.manage.dao.IWorkspaceMapper; |
| | | import com.dji.sample.manage.model.dto.WorkspaceDTO; |
| | | import com.dji.sample.manage.model.entity.WorkspaceEntity; |
| | | import com.dji.sample.manage.model.receiver.OrganizationGetReceiver; |
| | | import com.dji.sample.manage.service.IWorkspaceService; |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.integration.annotation.ServiceActivator; |
| | | import org.springframework.integration.mqtt.support.MqttHeaders; |
| | | import org.springframework.messaging.MessageHeaders; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | import java.util.UUID; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Transactional |
| | |
| | | @Autowired |
| | | private IWorkspaceMapper mapper; |
| | | |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | |
| | | @Autowired |
| | | private IMessageSenderService messageSenderService; |
| | | |
| | | @Override |
| | | public Optional<WorkspaceDTO> getWorkspaceById(int id) { |
| | | return Optional.ofNullable(entityConvertToDto(mapper.selectById(id))); |
| | | public PaginationData<WorkspaceDTO> selectWorkspace(long page, long pageSize) { |
| | | Page<WorkspaceEntity> entityPage = mapper.selectPage( |
| | | new Page<>(page, pageSize), |
| | | new QueryWrapper<>()); |
| | | |
| | | List<WorkspaceDTO> list = entityPage.getRecords() |
| | | .stream() |
| | | .map(this::entityConvertToDto) |
| | | .collect(Collectors.toList()); |
| | | return new PaginationData<>(list, new Pagination(entityPage)); |
| | | } |
| | | |
| | | @Override |
| | | public Boolean createWorkspace(WorkspaceDTO dto) { |
| | | dto.setWorkspaceId(UUID.randomUUID().toString()); |
| | | return mapper.insert(dtoConvertToEntity(dto)) > 0; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean deleteWorkspaceById(Integer id) { |
| | | return mapper.deleteById(id) > 0; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean updateWorkspaceById(WorkspaceDTO dto) { |
| | | return mapper.updateById(dtoConvertToEntity(dto)) > 0; |
| | | } |
| | | |
| | | @Override |
| | |
| | | .eq(WorkspaceEntity::getWorkspaceId, workspaceId)))); |
| | | } |
| | | |
| | | @Override |
| | | public Optional<WorkspaceDTO> getWorkspaceNameByBindCode(String bindCode) { |
| | | return Optional.ofNullable(entityConvertToDto( |
| | | mapper.selectOne(new LambdaQueryWrapper<WorkspaceEntity>().eq(WorkspaceEntity::getBindCode, bindCode)))); |
| | | } |
| | | |
| | | @Override |
| | | @ServiceActivator(inputChannel = ChannelName.INBOUND_REQUESTS_AIRPORT_ORGANIZATION_GET, outputChannel = ChannelName.OUTBOUND) |
| | | public void replyOrganizationGet(CommonTopicReceiver receiver, MessageHeaders headers) { |
| | | OrganizationGetReceiver organizationGet = objectMapper.convertValue(receiver.getData(), OrganizationGetReceiver.class); |
| | | CommonTopicResponse.CommonTopicResponseBuilder<RequestsReply> builder = CommonTopicResponse.<RequestsReply>builder() |
| | | .tid(receiver.getTid()) |
| | | .bid(receiver.getBid()) |
| | | .method(RequestsMethodEnum.AIRPORT_ORGANIZATION_GET.getMethod()) |
| | | .timestamp(System.currentTimeMillis()); |
| | | |
| | | String topic = headers.get(MqttHeaders.RECEIVED_TOPIC).toString() + TopicConst._REPLY_SUF; |
| | | |
| | | if (!StringUtils.hasText(organizationGet.getDeviceBindingCode())) { |
| | | builder.data(RequestsReply.error(CommonErrorEnum.ILLEGAL_ARGUMENT)); |
| | | messageSenderService.publish(topic, builder.build()); |
| | | return; |
| | | } |
| | | |
| | | Optional<WorkspaceDTO> workspace = this.getWorkspaceNameByBindCode(organizationGet.getDeviceBindingCode()); |
| | | if (workspace.isEmpty()) { |
| | | builder.data(RequestsReply.error(CommonErrorEnum.GET_ORGANIZATION_FAILED)); |
| | | messageSenderService.publish(topic, builder.build()); |
| | | return; |
| | | } |
| | | |
| | | builder.data(RequestsReply.success(Map.of(MapKeyConst.ORGANIZATION_NAME, workspace.get().getWorkspaceName()))); |
| | | messageSenderService.publish(topic, builder.build()); |
| | | } |
| | | |
| | | @Override |
| | | public WorkspaceEntity getWorkspaceById(int id) { |
| | | WorkspaceEntity entity=mapper.selectById(id); |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * Convert database entity objects into workspace data transfer object. |
| | | * @param entity |
| | | * @return |
| | | */ |
| | | private WorkspaceDTO entityConvertToDto(WorkspaceEntity entity) { |
| | | WorkspaceDTO.WorkspaceDTOBuilder builder = WorkspaceDTO.builder(); |
| | | if (entity == null) { |
| | | return builder.build(); |
| | | return null; |
| | | } |
| | | return builder |
| | | return WorkspaceDTO.builder() |
| | | .id(entity.getId()) |
| | | .workspaceId(entity.getWorkspaceId()) |
| | | .platformName(entity.getPlatformName()) |
| | | .workspaceDesc(entity.getWorkspaceDesc()) |
| | | .workspaceName(entity.getWorkspaceName()) |
| | | .bindCode(entity.getBindCode()) |
| | | .build(); |
| | | } |
| | | |
| | | private WorkspaceEntity dtoConvertToEntity(WorkspaceDTO dto) { |
| | | if (dto == null) { |
| | | return null; |
| | | } |
| | | return WorkspaceEntity.builder() |
| | | .id(dto.getId()) |
| | | .workspaceId(dto.getWorkspaceId()) |
| | | .platformName(dto.getPlatformName()) |
| | | .workspaceDesc(dto.getWorkspaceDesc()) |
| | | .workspaceName(dto.getWorkspaceName()) |
| | | .bindCode(dto.getBindCode()) |
| | | .build(); |
| | | } |
| | | } |