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.TAppStuapp;
import com.xhkjedu.sapi.service.appversion.AppStuappService;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import com.xhkjedu.sapi.vo.appversion.AppversionVo;
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 AppStuappController
* @description
* @date 2020/06/17 13:29
**/
@RestController
@RequestMapping("/appversion")
public class AppStuappController {
    @Resource
    private AppStuappService appStuappService;

    @PostMapping("/add")
    @Action("学生平板端-增加")
    public ResultVo add(@RequestBody TAppStuapp tAppStuapp) {
        N_Utils.validation(new Object[]{tAppStuapp.getVersionname(),"版本名称",2,
                tAppStuapp.getVersioncode(),"版本号",2,tAppStuapp.getVersionpath(),"应用文件",2});
        Integer id = appStuappService.save(tAppStuapp);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/delete")
    @Action("学生平板端-删除")
    public ResultVo delete(@RequestBody TAppStuapp tAppStuapp) {
        N_Utils.validation(new Object[]{tAppStuapp.getVersionid(),"版本id",1});
        appStuappService.deleteById(tAppStuapp.getVersionid());
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/update")
    @Action("学生平板端-修改")
    public ResultVo update(@RequestBody TAppStuapp tAppStuapp) {
        N_Utils.validation(new Object[]{tAppStuapp.getVersionid(),"版本id",1});
        appStuappService.update(tAppStuapp);
        return new ResultVo(0,"修改成功");
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TAppStuapp tAppStuapp) {
        N_Utils.validation(new Object[]{tAppStuapp.getVersionid(),"版本id",1});
        TAppStuapp appversion = appStuappService.findById(tAppStuapp.getVersionid());
        return new ResultVo(0,"获取详情成功",appversion);
    }

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

    @PostMapping("/get_new")
    public ResultVo getNew() {
        TAppStuapp appversion = appStuappService.getNew();
        return new ResultVo(0,"获取最新app版本成功",appversion);
    }

    @PostMapping("/list_news")
    public ResultVo listNews() {
        List<Map> list = appStuappService.listNews();
        return new ResultVo(0,"获取app终端最新版本列表成功",list);
    }
}
