package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.bean.Student;
import com.winsun.mapper.SchoolMapper;
import com.winsun.mapper.StudentMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author xuede
 * @Date 2020/3/10 10:16
 * @Version 1.0
 */
@Slf4j
@RestController
public class StartSelectController {

    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/findStartData")
    public ResponseData<Map<String, Object>> findStartData(@RequestParam("id") String id){
        if (StringUtils.isEmpty(id)){
            return ResponseData.error("请输入合伙人id");
        }
        List<Map<String, Object>> mapList = schoolMapper.getuserToSchool(id);
        if (CollectionUtils.isEmpty(mapList)){
            return ResponseData.error("没有查询相关的合伙人id的信息");
        }
        Map<String, Object> map = mapList.get(0);
        return ResponseData.success(map);
    }

    /**
     *通过name和身份证查询学生信息
     * @param stuId 学生ID
     * @param name 用户名
     * @param idCard 身份证号
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkStudent", method = RequestMethod.POST)
    public ResponseData<Map<String, Object>> checkStudent(String stuId,String name,String idCard){

        Wrapper<Student> studentWrapper = new EntityWrapper<>();
        studentWrapper.setSqlSelect("id,stu_name AS stuName,id_card AS idCard,worker,aClass");
        if (StringUtils.isNotBlank(stuId)){
            studentWrapper.addFilter("id = '"+stuId+"'");
        }
        if (StringUtils.isNotBlank(name)){
            studentWrapper.addFilter("stu_name = '"+name+"'");
        }
        if (StringUtils.isNotBlank(idCard)){
            studentWrapper.addFilter("id_card = '"+idCard+"'");
        }
        List<Map<String, Object>> studentMap = studentMapper.selectMaps(studentWrapper);
        if (studentMap == null || studentMap.size() == 0) {
            return ResponseData.error("该学生信息不存在！");
        }
        return ResponseData.success(studentMap.get(0),"验证通过");
    }
}
