package com.xhkjedu.sapi.controller.appversion;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.appversion.TWebVersion;
import com.xhkjedu.sapi.service.appversion.WebVersionService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
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;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname WebVersionController
 * @description 网页端版本管理
 * @date 2022/6/27 14:53
 **/
@RestController
@RequestMapping("/web_version")
public class WebVersionController {
    @Resource
    private WebVersionService webVersionService;

    @PostMapping("/add")
    @Action("校园网页端-增加")
    public ResultVo add(@RequestBody TWebVersion app) {
        N_Utils.validation(new Object[]{app.getVersionname(), "版本名称", 2
                , app.getVersiontype(), "网页端类型", 1, app.getVersioncomm(), "更新内容", 2});
        webVersionService.save(app);
        return new ResultVo(0, "保存成功");
    }

    @PostMapping("/delete")
    @Action("校园网页端-删除")
    public ResultVo delete(@RequestBody TWebVersion app) {
        N_Utils.validation(new Object[]{app.getVersionid(), "版本id", 1});
        webVersionService.deleteById(app.getVersionid());
        return new ResultVo(0, "删除成功");
    }

    @PostMapping("/update")
    @Action("校园网页端-修改")
    public ResultVo update(@RequestBody TWebVersion app) {
        N_Utils.validation(new Object[]{app.getVersionid(), "版本id", 1});
        webVersionService.update(app);
        return new ResultVo(0, "修改成功");
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TWebVersion app) {
        PageHelper.startPage(app.getPage(), app.getPageSize());
        List<Map> list = webVersionService.listAll(app);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取列表成功", pageResult);
    }

    /**
     * 获取教师网页端最新版本
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param []
     * @Author ywx
     * @Date 2022/6/27 15:17
     **/
    @PostMapping("/get_tnew")
    public ResultVo getTeacherNew() {
        Map map = webVersionService.getNew(1);
        return new ResultVo(0, "获取最新版本成功", map);
    }

    /**
     * 获取学生网页端最新版本
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param []
     * @Author ywx
     * @Date 2022/6/27 15:17
     **/
    @PostMapping("/get_snew")
    public ResultVo getStudentNew() {
        Map map = webVersionService.getNew(2);
        return new ResultVo(0, "获取最新版本成功", map);
    }
}
