package com.railway.web.controller.business.app;

import com.railway.business.app.domain.AppUpgrade;
import com.railway.business.app.domain.vo.AppUpgradeQuery;
import com.railway.business.app.service.IAppUpgradeService;
import com.railway.common.core.controller.BaseController;
import com.railway.common.core.domain.AjaxResult;
import com.railway.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhaomn
 */
@Api(value = "rest/app", tags = "应用升级 - apk版本升级表")
@RestController
@Validated
@RequestMapping(value = "app/upgrade")
public class AppUpgradeController extends BaseController {

  private final IAppUpgradeService appUpgradeService;

  public AppUpgradeController(
      IAppUpgradeService appUpgradeService) {
    this.appUpgradeService = appUpgradeService;
  }

  @ApiOperation(value = "新增")
  @PostMapping("/add")
  public AjaxResult add(@Validated @RequestBody AppUpgrade appUpgrade) {
    return toAjax(appUpgradeService.create(appUpgrade));
  }

  @ApiOperation(value = "删除")
  @DeleteMapping("/{ids}")
  public AjaxResult delete(@RequestParam String[] ids) {
    return toAjax(appUpgradeService.delete(ids));
  }

  @ApiOperation(value = "更新")
  @PutMapping("/update")
  public AjaxResult update(@RequestBody @Valid AppUpgrade appUpgrade) {
    return toAjax(appUpgradeService.update(appUpgrade));
  }

  @ApiOperation(value = "单个")
  @GetMapping(value = {"/", "/{id}"})
  public AjaxResult getInfo(String id) {
    AppUpgrade info = appUpgradeService.getInfo(id);
    AjaxResult ajax = AjaxResult.success();
    ajax.put("info", info);
    return ajax;
  }

  @ApiOperation(value = "列表")
  @GetMapping(value = "list")
  public TableDataInfo getList(AppUpgrade appUpgrade) {
    startPage();
    List<AppUpgrade> list = appUpgradeService.getList(appUpgrade);
    return getDataTable(list);
  }

  @ApiOperation(value = "APK更新版本检查")
  @GetMapping("/upgrade")
  public AjaxResult upgrade(AppUpgradeQuery query) {
    AppUpgrade info = appUpgradeService.getNewestInfo();
    AjaxResult ajax = AjaxResult.success();
    if (null != info && !info.getApkVersion().equals(query.getApkVersion())) {
      String android = "android";
      String url;
      if (android.equals(query.getOsType().toLowerCase())) {
        url = info.getUrl();
      } else {
        url = "https://www.baidu.com";
      }
      ajax.put("url", url);
      ajax.put("apkVersion", info.getApkVersion());
      ajax.put("description", info.getDescription());
    }
    return ajax;
  }

}
