package com.red.circle.other.app.inner.endpoint.user.device;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.dto.ResultResponse;
import com.red.circle.other.app.inner.service.user.device.RegisterDeviceClientService;
import com.red.circle.other.inner.endpoint.user.device.api.RegisterDeviceClientApi;
import com.red.circle.other.inner.model.cmd.user.device.ArchiveDeviceQryCmd;
import com.red.circle.other.inner.model.cmd.user.device.DeviceRegisterQuantityQryCmd;
import com.red.circle.other.inner.model.cmd.user.device.PageLatestMobileDeviceQryCmd;
import com.red.circle.other.inner.model.dto.user.UserLatestDeviceAssociatedDTO;
import com.red.circle.other.inner.model.dto.user.UserLatestMobileDeviceHistoryDTO;
import com.red.circle.other.inner.model.dto.user.device.ArchiveDeviceDTO;
import com.red.circle.other.inner.model.dto.user.device.DeviceRegisterQuantityDTO;
import com.red.circle.other.inner.model.dto.user.device.LatestMobileDeviceDTO;
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登记设备相关 服务.
 *
 * @author pengliang on 2023/5/24
 */
@Validated
@RestController
@RequestMapping(value = RegisterDeviceClientApi.API_PREFIX, produces = MediaType.APPLICATION_JSON_VALUE)
@RequiredArgsConstructor
public class RegisterDeviceClientEndpoint implements RegisterDeviceClientApi {

  private final RegisterDeviceClientService registerDeviceClientService;

  @Override
  public ResultResponse<Boolean> existsArchiveDevice(String deviceNo, String sysOrigin) {
    return ResultResponse.success(registerDeviceClientService.existsDevice(deviceNo, sysOrigin));
  }

  @Override
  public ResultResponse<Void> addArchiveDevice(ArchiveDeviceDTO cmd) {
    registerDeviceClientService.addDeviceNo(cmd.getDeviceNo(), cmd.getSysOrigin());
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> removeArchiveDevice(ArchiveDeviceDTO cmd) {
    registerDeviceClientService.removeDeviceNo(cmd.getDeviceNo(), cmd.getSysOrigin());
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> delByDeviceNo(Long id) {
    registerDeviceClientService.delByDeviceNo(id);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<Void> uploadLatestDeviceNoAsync(LatestMobileDeviceDTO cmd) {
    registerDeviceClientService.uploadLatestDeviceNo(cmd);
    return ResultResponse.success();
  }

  @Override
  public ResultResponse<String> getImeiByUserId(Long userId) {
    return ResultResponse.success(registerDeviceClientService.getImeiByUserId(userId));
  }

  @Override
  public ResultResponse<List<Long>> listUserIdByImei(String imei, String sysOrigin) {
    return ResultResponse.success(registerDeviceClientService.listUserIdByImei(imei, sysOrigin));
  }

  @Override
  public ResultResponse<List<UserLatestMobileDeviceHistoryDTO>> listByImei(String imei,
      String sysOrigin) {
    return ResultResponse.success(registerDeviceClientService.listByImei(imei, sysOrigin));
  }


  @Override
  public ResultResponse<List<Long>> listLatestAssociationUserIds(Long userId) {
    return ResultResponse.success(registerDeviceClientService.listLatestAssociationUserIds(userId));
  }

  @Override
  public ResultResponse<Set<Long>> getIpOrImeiUserId(Long userId) {
    return ResultResponse.success(registerDeviceClientService.getIpOrImeiUserId(userId));
  }

  @Override
  public ResultResponse<UserLatestDeviceAssociatedDTO> getDeviceByUserId(Long userId) {
    return ResultResponse.success(registerDeviceClientService.getDeviceByUserId(userId));
  }

  @Override
  public ResultResponse<PageResult<UserLatestMobileDeviceHistoryDTO>> pageLatestMobileDevice(
      PageLatestMobileDeviceQryCmd query) {
    return ResultResponse.success(registerDeviceClientService.pageLatestMobileDevice(query));
  }

  @Override
  public ResultResponse<Map<String, DeviceRegisterQuantityDTO>> mapDeviceRegisterQuantity(
      DeviceRegisterQuantityQryCmd query) {
    return ResultResponse.success(registerDeviceClientService.mapDeviceRegisterQuantity(query));
  }


  @Override
  public ResultResponse<Map<Long, String>> mapImeiByIds(Set<Long> userIds) {
    return ResultResponse.success(registerDeviceClientService.mapImeiByIds(userIds));
  }


  @Override
  public ResultResponse<PageResult<ArchiveDeviceDTO>> pageList(ArchiveDeviceQryCmd query) {
    return ResultResponse.success(registerDeviceClientService.pageList(query));
  }
}
