package com.red.circle.other.app.inner.service.sys.impl;

import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.sys.SysAppVersionManageInnerConvertor;
import com.red.circle.other.app.inner.service.sys.VersionClientService;
import com.red.circle.other.infra.database.cache.service.other.BuildVersionCacheService;
import com.red.circle.other.infra.database.rds.service.sys.VersionManageService;
import com.red.circle.other.infra.database.rds.service.sys.VersionUpdateDescriptionService;
import com.red.circle.other.inner.model.cmd.sys.PageVersionDescQryCmd;
import com.red.circle.other.inner.model.cmd.sys.SysVersionUpdateDescriptionCmd;
import com.red.circle.other.inner.model.cmd.sys.VersionManageQryCmd;
import com.red.circle.other.inner.model.dto.sys.SysVersionUpdateDescriptionDTO;
import com.red.circle.other.inner.model.dto.sys.VersionManageDTO;
import com.red.circle.tool.core.parse.DataTypeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 系统管理员 实现.
 *
 * @author pengliang on 2023/8/11
 */
@Service
@RequiredArgsConstructor
public class VersionClientServiceImpl implements VersionClientService {

  private final VersionManageService versionManageService;
  private final BuildVersionCacheService buildVersionCacheService;
  private final VersionUpdateDescriptionService versionUpdateDescriptionService;
  private final SysAppVersionManageInnerConvertor sysAppVersionManageInnerConvertor;


  @Override
  public PageResult<SysVersionUpdateDescriptionDTO> pageDescription(PageVersionDescQryCmd cmd) {
    return versionUpdateDescriptionService.pageDescription(cmd)
        .convert(sysAppVersionManageInnerConvertor::toSysVersionUpdateDescriptionDTO);
  }

  @Override
  public boolean addDesc(SysVersionUpdateDescriptionCmd param) {
    return versionUpdateDescriptionService.add(param);
  }

  @Override
  public boolean updateDesc(SysVersionUpdateDescriptionCmd param) {
    return versionUpdateDescriptionService.update(param);
  }

  @Override
  public boolean deleteDesc(Long id) {
    return versionUpdateDescriptionService.deleteById(id);
  }


  @Override
  public PageResult<VersionManageDTO> pageVersionManage(VersionManageQryCmd cmd) {
    return versionManageService.pageVersionManage(cmd)
        .convert(sysAppVersionManageInnerConvertor::toVersionManageDTO);
  }

  @Override
  public Boolean saveVersionManage(VersionManageDTO param) {
    param.setCreateUser(DataTypeUtils.toLong(param.getReqUserId()));
    return versionManageService.saveVersionManage(
        sysAppVersionManageInnerConvertor.toVersionManage(param));
  }

  @Override
  public void deleteVersionManage(Long id) {
    versionManageService.deleteVersionManage(id);
  }

  @Override
  public void updateVersionManage(VersionManageDTO param) {
    param.setCreateUser(param.getReqUserId());
    versionManageService.updateVersionManage(
        sysAppVersionManageInnerConvertor.toVersionManage(param));
  }

  @Override
  public void addBuildVersion(String sysOrigin, String platform, String channel,
      Integer buildVersion) {
    buildVersionCacheService.addBuildVersion(sysOrigin, platform, channel, buildVersion);
  }

  @Override
  public Integer getBuildVersion(String sysOrigin, String platform, String channel) {
    return buildVersionCacheService.getBuildVersion(sysOrigin, platform, channel);
  }
}
