package com.red.circle.console.adapter.app.sys;


import com.red.circle.console.app.service.app.sys.VersionManageService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.inner.model.cmd.sys.AppBuildVersionCmd;
import com.red.circle.other.inner.model.cmd.sys.VersionManageQryCmd;
import com.red.circle.other.inner.model.dto.sys.VersionManageDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器.
 * </p>
 *
 * @author pengliang
 * @since 2019-05-05
 */
@RestController
@RequestMapping("/sys/version/manage")
@RequiredArgsConstructor
public class VersionManageRestController extends BaseController {

  private final VersionManageService versionManageService;

  /**
   * 查询所有版本.
   */
  @GetMapping()
  public PageResult<VersionManageDTO> page(VersionManageQryCmd cmd) {
    return versionManageService.pageVersionManage(cmd);
  }

  @OpsOperationReqLog(value = "新增APP版本信息")
  @PostMapping
  public Boolean save(@RequestBody VersionManageDTO versionManageDTO) {
    return versionManageService.saveVersionManage(versionManageDTO);
  }

  @OpsOperationReqLog(value = "修改APP版本信息")
  @PostMapping("/update")
  public void update(
      @RequestBody @Validated(VersionManageDTO.Update.class) VersionManageDTO versionManageDTO) {
    versionManageService.updateVersionManage(versionManageDTO);
  }

  @OpsOperationReqLog(value = "删除APP版本信息")
  @GetMapping("/{id}")
  public void delete(@PathVariable("id") Long id) {
    versionManageService.deleteVersionManage(id);
  }

  @OpsOperationReqLog(value = "编辑buildVersion")
  @GetMapping("/add/build-version")
  public void addServerBuildVersion(AppBuildVersionCmd param) {
    versionManageService.addBuildVersion(param.getSysOrigin(), param.getPlatform(),
        param.getChannel(), param.getBuildVersion());
  }

  /**
   * 获取编译版本.
   */
  @GetMapping("/get/build-version")
  public Integer getServerBuildVersion(AppBuildVersionCmd param) {
    return versionManageService.getBuildVersion(param.getSysOrigin(), param.getPlatform(),
        param.getChannel());
  }
}
