package com.xhkjedu.sapi.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.sapi.model.system.TSchool;
import com.xhkjedu.sapi.service.system.SchoolService;
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 TSchoolController
 * @description
 * @date 2020/6/2 15:06
 **/
@RestController
@RequestMapping("/school")
public class SchoolController {
    @Resource
    private SchoolService schoolService;

    @PostMapping("/add")
    public ResultVo add(@RequestBody TSchool tSchool) {
        try {
            String schoolname = tSchool.getSchoolname();
            N_Utils.validation(new Object[]{schoolname,"学校名称",2});
            Integer num = schoolService.countBySchoolName(schoolname);
            if (num != 0) return new ResultVo(1,"该学校名称已存在");
            Integer id = schoolService.save(tSchool);
            return new ResultVo(0, "保存成功",id);
        } catch (Exception e) {
            return new ResultVo(1,e.getMessage());
        }
    }

    @PostMapping("/delete")
    public ResultVo delete(@RequestBody TSchool tSchool) {
        N_Utils.validation(new Object[]{tSchool.getSchoolid(),"学校id",1});
        schoolService.deleteById(tSchool.getSchoolid());
        return new ResultVo(0,"删除成功");
    }

    @PostMapping("/update")
    public ResultVo update(@RequestBody TSchool tSchool) {
        N_Utils.validation(new Object[]{tSchool.getSchoolid(),"学校id",1});
        schoolService.update(tSchool);
        return new ResultVo(0,"修改成功");
    }

    @PostMapping("/list")
    public ResultVo list(@RequestBody TSchool tSchool) {
        Integer page = tSchool.getPage();
        Integer pageSize = tSchool.getPageSize();
        N_Utils.validation(new Object[]{page,"显示页码",1,pageSize,"显示条数",1});
        PageHelper.startPage(page, pageSize);
        List<TSchool> list = schoolService.findAll();
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取列表成功",pageResult);
    }

    @PostMapping("/upgrade")
    public ResultVo upgrade(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        Integer year = schoolService.getYearById(schoolid);
        N_Utils.validation(new Object[]{schoolid,"学校id",1});
        Integer nowYear = N_Utils.getNodeDateTime("year");//当前年份
        if (year >= nowYear) return new ResultVo(1,"当前学年不用升级");
        try {
            schoolService.upgrade(schoolid, year);
            return new ResultVo(0,"学年升级成功");
        } catch (Exception e) {
            return new ResultVo(1,e.getMessage());
        }
    }

    @PostMapping("/downgrade")
    public ResultVo downgrade(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        Integer year = schoolService.getYearById(schoolid);
        N_Utils.validation(new Object[]{schoolid,"学校id",1});
        schoolService.downgrade(schoolid, year);
        return new ResultVo(0,"学年降级成功");
    }

    @PostMapping("/detail")
    public ResultVo detail(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        N_Utils.validation(new Object[]{schoolid,"学校id",1});
        TSchool school = schoolService.finById(schoolid);
        return new ResultVo(0,"获取学校详情成功", school);
    }

    @PostMapping("/list_sel")
    public ResultVo listForSel() {
        List<Map> list = schoolService.listForSel();
        return new ResultVo(0,"获取学校选择列表成功", list);
    }
}
