package com.xhkjedu.sapi.controller.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xhkjedu.annotation.Action;
import com.xhkjedu.sapi.model.system.TSchool;
import com.xhkjedu.sapi.model.system.TSchoolUpgradeHistory;
import com.xhkjedu.sapi.service.system.SchoolService;
import com.xhkjedu.sapi.vo.analyze.SchoolParams;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.utils.PageUtil;
import com.xhkjedu.vo.PageResult;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
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
 **/
@Slf4j
@RestController
@RequestMapping("/school")
public class SchoolController {
    @Resource
    private SchoolService schoolService;

    @PostMapping("/add")
    @Action("学校管理-增加")
    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")
    @Action("学校管理-删除")
    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")
    @Action("学校管理-修改")
    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<Map<String,Object>> list = schoolService.findAll();
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0,"获取列表成功",pageResult);
    }

    /*
    * @Description 检测学年升级
    * @Author WN
    * @Date 2023/6/15 11:40:10
    */
    @PostMapping("/check_upgrade")
    public ResultVo checkUpgrade(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        N_Utils.validation(new Object[]{schoolid,"学校id",1});

        try {
            Map<String,Object> rtnmap = schoolService.checkSchoolUpgrade(schoolid);
            return new ResultVo(0,"成功检测学年升级",rtnmap);
        } catch (Exception e) {
            log.error("检测学年升级失败：" + e.getMessage());
            return new ResultVo(1,"检测学年升级失败");
        }
    }

    //学年升级
    @PostMapping("/upgrade")
    @Action("学校管理-学年升级")
    public ResultVo upgrade(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        Integer createid = tSchool.getCreateid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, createid, "用户id", 1});
        Integer year = schoolService.getYearById(schoolid);
        Integer nowYear = N_Utils.getNodeDateTime("year");//当前年份
        if (year >= nowYear) return new ResultVo(1,"当前学年不用升级");
        try {
            List<TSchoolUpgradeHistory> histories = schoolService.upgrade(schoolid, year, createid);
            return new ResultVo(0, "学年升级成功", histories);
        } catch (Exception e) {
            log.error("学年升级失败：" + e.getMessage());
            return new ResultVo(1, e.getMessage());
        }
    }

    //学年降级
    @PostMapping("/downgrade")
    @Action("学校管理-学校降级")
    public ResultVo downgrade(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        Integer createid = tSchool.getCreateid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, createid, "用户id", 1});
        Integer year = schoolService.getYearById(schoolid);
        Integer nowYear = N_Utils.getNodeDateTime("year");//当前年份
        if (!year.equals(nowYear)) return new ResultVo(1,"当前学年不用降级");
        try {
            List<TSchoolUpgradeHistory> histories = schoolService.downgrade(schoolid, year, createid);
            return new ResultVo(0,"学年降级成功", histories);
        } catch (Exception e) {
            log.error("学年降级失败：" + e.getMessage());
            return new ResultVo(1, e.getMessage());
        }
    }

    /**
     * @Description 学校升级记录
     * @Param [tSchool]
     * @Author ywx
     * @Date 2023/6/30 9:24
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_up")
    public ResultVo listUpgrade(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        Integer page = tSchool.getPage();
        Integer pageSize = tSchool.getPageSize();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1, page, "显示页码", 1, pageSize, "显示条数", 1});
        PageHelper.startPage(page, pageSize);
        List<Map<String, Object>> list = schoolService.listUpgrade(schoolid);
        PageResult pageResult = PageUtil.getPageResult(new PageInfo(list));
        return new ResultVo(0, "获取学校升级记录成功", pageResult);
    }

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

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

    /**
     * 获取准考证号长度限制
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [tSchool]
     * @Author ywx
     * @Date 2022/9/15 14:19
     **/
    @PostMapping("/get_limitenolen")
    public ResultVo getLimiteNoLen(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        Integer limitenolen = schoolService.getLimiteNoLen(schoolid);
        return new ResultVo(0, "获取成功", limitenolen);
    }

    /**
     * @Description 保存学校关联学校
     * @Param [params]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/9/22 16:24
     **/
    @PostMapping("/sfschool")
    @Action("学校管理-保存关联学校")
    public ResultVo saveSchoolFriend(@RequestBody SchoolParams params) {
        Integer schoolid = params.getSchoolid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        schoolService.saveSchoolFriend(params);
        return new ResultVo(0, "保存成功");
    }

    /**
     * @Description 获取学校关联附属学校id
     * @Param [params]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/9/22 16:40
     **/
    @PostMapping("/getfids")
    public ResultVo getFschoolids(@RequestBody SchoolParams params) {
        Integer schoolid = params.getSchoolid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        List<Map<String,Object>> list = schoolService.listFSchoolidsBySchoolid(schoolid);
        return new ResultVo(0, "获取成功",list);
    }

    /**
     * @Description 学校以及附属学校对应年级班级
     * @Param [params]
     * @Return com.xhkjedu.vo.ResultVo
     * @Author wn
     * @Date 2022/9/22 17:37
     **/
    @PostMapping("/fsclass")
    public ResultVo listSchoolClass(@RequestBody SchoolParams params) {
        Integer schoolid = params.getSchoolid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        List<Map<String,Object>> list = schoolService.listSchoolAndFriendSchools(schoolid);
        return new ResultVo(0, "获取成功",list);
    }



     /* 获取学年列表
     *
     * @return com.xhkjedu.vo.ResultVo
     * @Param [tSchool]
     * @Author ywx
     * @Date 2022/9/20 10:04
     **/
    @PostMapping("/list_year")
    public ResultVo listYear(@RequestBody TSchool tSchool) {
        Integer schoolid = tSchool.getSchoolid();
        N_Utils.validation(new Object[]{schoolid, "学校id", 1});
        List<Integer> list = schoolService.listYear(schoolid);
        return new ResultVo(0, "获取列表成功", list);
    }

    /*
     * 获取区域学年列表
     * @Param []
     * @Author ywx
     * @Date 2022/10/14 10:05
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_cyear")
    public ResultVo listCloudYear() {
        List<Integer> list = schoolService.listCloudYear();
        return new ResultVo(0, "获取列表成功", list);
    }

    /**
     * @Description 伙伴学校选择列表
     * @Param [school]
     * @Author ywx
     * @Date 2023/4/10 15:20
     * @return com.xhkjedu.vo.ResultVo
     **/
    @PostMapping("/list_fsel")
    public ResultVo listFriendForSel(@RequestBody TSchool school) {
        N_Utils.validation(new Object[]{school.getSchoolid(), "学校id", 1});
        List<Map<String,Object>> list = schoolService.listFriendForSel(school);
        return new ResultVo(0, "获取伙伴学校选择列表成功", list);
    }
}
