package com.ruoyi.web.controller.system; 
 | 
  
 | 
import com.ruoyi.common.annotation.Log; 
 | 
import com.ruoyi.common.annotation.RepeatSubmit; 
 | 
import com.ruoyi.common.core.controller.BaseController; 
 | 
import com.ruoyi.common.core.domain.AjaxResult; 
 | 
import com.ruoyi.common.core.page.TableDataInfo; 
 | 
import com.ruoyi.common.enums.BusinessType; 
 | 
import com.ruoyi.common.utils.StringUtils; 
 | 
import com.ruoyi.common.utils.poi.ExcelUtil; 
 | 
import com.ruoyi.system.domain.SysStudent; 
 | 
import com.ruoyi.system.mapper.SysStudentMapper; 
 | 
import com.ruoyi.system.service.ISysStudentService; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.security.access.prepost.PreAuthorize; 
 | 
import org.springframework.web.bind.annotation.*; 
 | 
  
 | 
import java.util.*; 
 | 
import java.util.stream.Collectors; 
 | 
  
 | 
/** 
 | 
 * 学生信息Controller 
 | 
 * 
 | 
 * @author ruoyi 
 | 
 * @date 2021-10-28 
 | 
 */ 
 | 
@RestController 
 | 
@RequestMapping("/system/student") 
 | 
public class SysStudentController extends BaseController { 
 | 
    @Autowired 
 | 
    private ISysStudentService sysStudentService; 
 | 
  
 | 
    @Autowired 
 | 
    SysStudentMapper sysStudentMapper; 
 | 
  
 | 
    /** 
 | 
     * 查询学生信息列表 
 | 
     */ 
 | 
    // @PreAuthorize("@ss.hasPermi('system:student:list')") 
 | 
    @GetMapping("/list") 
 | 
    public TableDataInfo list(SysStudent sysStudent) { 
 | 
        startPage(); 
 | 
        List<SysStudent> list = sysStudentService.queryList(sysStudent); 
 | 
        //List<SysStudent> list = sysStudentMapper.selectSysStudentList(sysStudent); 
 | 
        //List<SysStudent> list = sysStudentMapper.getStudentByName("测试"); 
 | 
  
 | 
  
 | 
        //利用stream流进行foreach遍历 
 | 
        list.stream().forEach(student -> { 
 | 
            //处理逻辑 打印出所有学生的名单和年龄 
 | 
            System.out.println(student.getStudentName() + student.getStudentAge()); 
 | 
        }); 
 | 
  
 | 
        //利用filter函数进行筛选 获取符合条件的 
 | 
        List<SysStudent> lista = list.stream().filter(student -> StringUtils.equals(student.getStudentSex(), "1")).collect(Collectors.toList()); 
 | 
  
 | 
        lista.stream().forEach(student -> { 
 | 
            //处理逻辑 打印出所有学生的名单和年龄 
 | 
            System.out.println(student.getStudentName() + student.getStudentAge()); 
 | 
        }); 
 | 
  
 | 
        //对List集合进行去重 
 | 
        //将username相同的 进行去重 
 | 
        List<SysStudent> unique = list.stream().collect(Collectors.collectingAndThen( 
 | 
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(SysStudent::getStudentName))), ArrayList::new)); 
 | 
        unique.stream().forEach(student -> { 
 | 
            System.out.println("-------------------" + student.getStudentName()); 
 | 
        }); 
 | 
  
 | 
        //取出list集合对象中的某一个属性(取出list中的每一个对象的名字组成一个新的集合) 
 | 
        List<String> usernames = list.stream().map(s -> s.getStudentName()).collect(Collectors.toList());//不取重的 
 | 
        usernames.stream().forEach(username -> { 
 | 
            System.out.println("不取重的-------------------" + username); 
 | 
        }); 
 | 
  
 | 
        List<String> names = list.stream().map(SysStudent::getStudentName).distinct().collect(Collectors.toList());//这个是将名字取重之后的 
 | 
        names.stream().forEach(username -> { 
 | 
            System.out.println("取重之后-------------------" + username); 
 | 
        }); 
 | 
  
 | 
  
 | 
        //查询姓名不为空的记录 
 | 
        List<String> IsEmptyUsernameList = list.stream().map(s -> s.getStudentName()).filter(s -> !s.isEmpty()).collect(Collectors.toList()); 
 | 
        System.out.println(IsEmptyUsernameList); 
 | 
        //根据其中的某一属性值进行计算 
 | 
        //(获取年龄的最大值、最小值、平均值、综合、个数) 
 | 
        IntSummaryStatistics resultNum =  list.stream().mapToInt((s) -> s.getStudentAge()).summaryStatistics(); 
 | 
        //个数 
 | 
        System.out.println(resultNum.getCount()); 
 | 
        //总大小 
 | 
        System.out.println(resultNum.getSum()); 
 | 
        //最大值 
 | 
        System.out.println(resultNum.getMax()); 
 | 
        //最小值 
 | 
        System.out.println(resultNum.getMin()); 
 | 
        //平均值 
 | 
        System.out.println(resultNum.getAverage()); 
 | 
  
 | 
        return getDataTable(list); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 导出学生信息列表 
 | 
     */ 
 | 
    // @PreAuthorize("@ss.hasPermi('system:student:export')") 
 | 
    @Log(title = "学生信息", businessType = BusinessType.EXPORT) 
 | 
    @GetMapping("/export") 
 | 
    public AjaxResult export(SysStudent sysStudent) { 
 | 
        List<SysStudent> list = sysStudentService.queryList(sysStudent); 
 | 
        ExcelUtil<SysStudent> util = new ExcelUtil<SysStudent>(SysStudent.class); 
 | 
        return util.exportExcel(list, "学生信息数据"); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 获取学生信息详细信息 
 | 
     */ 
 | 
    // @PreAuthorize("@ss.hasPermi('system:student:query')") 
 | 
    @GetMapping(value = "/{studentId}") 
 | 
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId) { 
 | 
        return AjaxResult.success(sysStudentService.getById(studentId)); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 新增学生信息 
 | 
     */ 
 | 
    // @PreAuthorize("@ss.hasPermi('system:student:add')") 
 | 
    @Log(title = "学生信息", businessType = BusinessType.INSERT) 
 | 
    @PostMapping 
 | 
    @RepeatSubmit 
 | 
    public AjaxResult add(@RequestBody SysStudent sysStudent) { 
 | 
        return toAjax(sysStudentService.save(sysStudent)); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 修改学生信息 
 | 
     */ 
 | 
    // @PreAuthorize("@ss.hasPermi('system:student:edit')") 
 | 
    @Log(title = "学生信息", businessType = BusinessType.UPDATE) 
 | 
    @PostMapping("/edit") 
 | 
    @RepeatSubmit 
 | 
    public AjaxResult edit(@RequestBody SysStudent sysStudent) { 
 | 
        return toAjax(sysStudentService.updateById(sysStudent)); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 删除学生信息 
 | 
     */ 
 | 
    // @PreAuthorize("@ss.hasPermi('system:student:remove')") 
 | 
    @Log(title = "学生信息", businessType = BusinessType.DELETE) 
 | 
    @GetMapping("/remove/{studentIds}") 
 | 
    public AjaxResult remove(@PathVariable Long[] studentIds) { 
 | 
        return toAjax(sysStudentService.removeByIds(Arrays.asList(studentIds))); 
 | 
    } 
 | 
} 
 |