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.TAppScan;
import com.xhkjedu.sapi.service.appversion.AppScanService;
import com.xhkjedu.sapi.vo.appversion.AppversionVo;
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;

/**
 * @author ywx
 * @classname AppScanController
 * @description
 * @date 2021/1/26 11:23
 **/
@RestController
@RequestMapping("/appscan")
public class AppScanController {
    @Resource
    private AppScanService appScanService;

    @PostMapping("/add")
    @Action("扫描客户端-增加")
    public ResultVo add(@RequestBody TAppScan appclient) {
        N_Utils.validation(new Object[]{appclient.getVersionname(),"版本名称",2,
                appclient.getVersioncode(),"版本号",2,appclient.getVersionpath(),"应用文件",2});
        Integer id = appScanService.save(appclient);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/delete")
    @Action("扫描客户端-删除")
    public ResultVo delete(@RequestBody TAppScan appclient) {
        N_Utils.validation(new Object[]{appclient.getVersionid(),"版本id",1});
        appScanService.deleteById(appclient.getVersionid());
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/update")
    @Action("扫描客户端-修改")
    public ResultVo update(@RequestBody TAppScan appclient) {
        N_Utils.validation(new Object[]{appclient.getVersionid(),"版本id",1});
        appScanService.update(appclient);
        return new ResultVo(0,"修改成功");
    }

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

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

    @PostMapping("/get_new")
    public ResultVo getNew() {
        TAppScan appclient = appScanService.getNew();
        return new ResultVo(0,"获取最新版本成功",appclient);
    }
}
