package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.School;
import com.winsun.mapper.SchoolMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 获取学校信息（因为manager使用未鉴权注解，导致访问401）
 * @author chengfengluo
 * @date 2021-03-09 10:15
 */
@RequestMapping("/appSchool")
@RestController
public class SchoolController {

    @Autowired
    private SchoolMapper schoolMapper;

    @RequestMapping(name = "获取县分下拉列表", value = "substList", method = RequestMethod.POST)
    public ResponseData<List<Map<String, Object>>> substList() {
//		ShiroUser user = getShiroUser();
//		if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
//			return ResponseData.error("无数据权限");
//		}

        // 查询县分列表
        Wrapper<School> wrapperSubName = new EntityWrapper<School>();
        wrapperSubName.setSqlSelect("sub_name")
                .ne("sub_name", "null")
                .groupBy("sub_name");
        List<Map<String, Object>> substNameList = schoolMapper.selectMaps(wrapperSubName);

        return ResponseData.success(substNameList, "查询完毕！");
    }

    @RequestMapping(name = "获取学校下拉列表", value = "schoolList", method = RequestMethod.POST)
    public ResponseData<List<Map<String, Object>>> schoolList(@RequestParam("substName") String substName) {
//		ShiroUser user = getShiroUser();
//		if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
//			return ResponseData.error("无数据权限");
//		}

        // 查询县分列表
        Wrapper<School> wrapper = new EntityWrapper<School>();
        wrapper.setSqlSelect("id,school_name")
                .eq("sub_name", substName)
                .groupBy("school_name");
        List<Map<String, Object>> schoolList = schoolMapper.selectMaps(wrapper);

        return ResponseData.success(schoolList, "查询完毕！");
    }

}
