package com.hys.app.controller.erp.use;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.model.system.dos.AppVersion;
import com.hys.app.model.system.vo.SmsMailTemplateQuery;
import com.hys.app.service.erp.AppVersionManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "消息配置")
@RestController
@RequestMapping("/admin/erp/appVersion")
@AllArgsConstructor
public class AppVersionController {

    private AppVersionManager appVersionManager;

    @PostMapping("add")
    @ApiOperation(value = "新增版本")
    @Log(client = LogClient.admin, detail = "新增版本号为[${appVersion.version}]的消息")
    public Long addVersion(@Valid @RequestBody AppVersion  appVersion) {
        return appVersionManager.addVersion(appVersion);
    }


    @GetMapping("list")
    @ApiOperation(value = "模板分页")
    public IPage<AppVersion> queryTemplate(SmsMailTemplateQuery query) {
        return appVersionManager.page(new Page<>(query.getPageNo(), query.getPageSize()));
    }


    @GetMapping("checkVersion")
    @ApiOperation(value = "校验版本信息，如果不是最新的版本会返回最新的版本，否则返回null")
    public AppVersion  checkVersion(String version) {
        AppVersion one = appVersionManager.getOne(new LambdaQueryWrapper<AppVersion>().eq(AppVersion::getVersion, version).eq(AppVersion::getStatus, true));
        if (ObjectUtil.isNull(one)){
           return appVersionManager.getOne(new LambdaQueryWrapper<AppVersion>().eq(AppVersion::getStatus, true));
        }
        return null;
    }
}
