package com.xhkjedu.sapi.controller.appversion;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sapi.model.appversion.TAppMonitor;
import com.xhkjedu.sapi.service.appversion.AppMonitorService;
import com.xhkjedu.sapi.vo.appversion.AppMonitorPVo;
import com.xhkjedu.sapi.vo.appversion.AppMonitorVo;
import com.xhkjedu.sapi.vo.appversion.AppSearchVo;
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 TAppMonitorController
* @description
* @date 2020/11/10 10:04
**/
@RestController
@RequestMapping("/app_monitor")
public class AppMonitorController {
    @Resource
    private AppMonitorService appMonitorService;

    @PostMapping("/add")
    public ResultVo add(@RequestBody TAppMonitor monitor) {
        if (N_Utils.isEmpty(monitor.getModulecode())) return new ResultVo(1,"模块编码不能为空");
        Integer id = appMonitorService.save(monitor);
        return new ResultVo(0, "保存成功",id);
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody AppSearchVo monitor) {
        List<AppMonitorVo> list = appMonitorService.listAll(monitor);
        return new ResultVo(0,"获取按应用统计列表成功",list);
    }

    @PostMapping("/people")
    public ResultVo people(@RequestBody AppSearchVo monitor) {
        Integer schoolid = monitor.getSchoolid();
        Integer page = monitor.getPage();
        Integer pageSize = monitor.getPageSize();
        N_Utils.validation(new Object[]{schoolid,"学校id",1});
        Integer year = appMonitorService.getYear(schoolid);
        if (N_Utils.isEmptyInteger(page)) {//页码不传不分页导出用
            List<AppMonitorPVo> list = appMonitorService.listPeople(monitor, year);
            return new ResultVo(0, "获取按人统计列表成功", list);
        } else {
            PageHelper.startPage(page, pageSize);
            List<AppMonitorPVo> list = appMonitorService.listPeople(monitor, year);
            PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
            return new ResultVo(0, "获取按人统计列表成功", pageResult);
        }
    }
}
