首页>代码>基于SpringBoot实现的学生就业管理系统>/源代码/employment/src/main/java/com/project/controller/AgreementController.java
package com.project.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.dto.AgreementDTO;
import com.project.entity.Agreement;
import com.project.entity.Student;
import com.project.entity.Teacher;
import com.project.entity.User;
import com.project.service.AgreementService;
import com.project.service.StudentService;
import com.project.service.TeacherService;
import com.project.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.project.enums.AgreementStatus.APPLY;
import static com.project.enums.AgreementStatus.MAIL;
import static com.project.enums.UserRole.STUDENT;
import static com.project.enums.UserRole.TEACHER;
import static com.project.uitls.Constant.CURRENT_USER;

/**
 * 三方协议 邮寄地址
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/agreement" )
public class AgreementController {

    private final AgreementService baseService;
    @Autowired
    private HttpSession session;
    @Autowired
    private UserService userService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private StudentService studentService;


    /**
     * 分页查询
     * @param page 分页对象
     * @param agreement
     * @return
     */
    @GetMapping("/page" )
    public R getPage(Page page, Agreement agreement) {
        User user = (User) session.getAttribute(CURRENT_USER);
        QueryWrapper<Agreement> wrapper = new QueryWrapper<>();
        if (user.getRole().equals(STUDENT)) {
            wrapper.eq("uid", user.getId());
        } else if (user.getRole().equals(TEACHER)) {
            Teacher teacher = teacherService.getByUid(user);
            Student student = new Student();
            student.setSpecialityId(teacher.getSpecialityId());
            List<Student> students = studentService.list(Wrappers.query(student));
            if (students != null && !students.isEmpty()) {
                List<Integer> collect = students.stream().map(x -> x.getUid()).collect(Collectors.toList());
                wrapper.in("uid", collect);
            }
        }

        IPage<Agreement> page1 = baseService.page(page, wrapper);
        List<AgreementDTO> dtos = new ArrayList<>();
        page1.getRecords().forEach(x  -> {
            AgreementDTO dto = new AgreementDTO();
            BeanUtils.copyProperties(x, dto);
            dto.setUserName(userService.getById(x.getUid()).getUserName());
            dtos.add(dto);
        });
        IPage<AgreementDTO> ipage = new Page<AgreementDTO>();
        ipage.setCurrent(page.getCurrent());
        ipage.setPages(page.getPages());
        ipage.setRecords(dtos);
        ipage.setSize(page.getSize());
        ipage.setTotal(page.getTotal());
        return R.ok(ipage);
    }

    @GetMapping("/getById")
    public Object getById(@RequestParam Integer id) {
        return R.ok(baseService.getById(id));
    }

    /**
     * 新增
     * @return R
     */
    @RequestMapping("/saveOrUpdate")
    public R save(Agreement agreement) {
        if (agreement.getId() == null) {
            User user = (User) session.getAttribute(CURRENT_USER);
            agreement.setUid(user.getId());
            baseService.save(agreement);
        }else {
            baseService.updateById(agreement);
        }
        return R.ok(null);
    }

    @GetMapping("/mail")
    public R mail(@RequestParam Integer id) {
        Agreement agreement = baseService.getById(id);
        agreement.setMailTime(DateUtil.now());
        agreement.setStatus(MAIL);
        baseService.updateById(agreement);
        return R.ok(null);
    }


    @GetMapping("/apply")
    public R apply(@RequestParam Integer id) {
        Agreement agreement = baseService.getById(id);
        agreement.setCreateTime(DateUtil.now());
        agreement.setStatus(APPLY);
        baseService.updateById(agreement);
        return R.ok(null);
    }


    @GetMapping("/deleteById")
    public Object deleteById(@RequestParam Integer id) {
        boolean b = baseService.removeById(id);
        return b ? R.ok(null) : R.failed("删除失败");
    }

}
最近下载更多
15953970869  LV6 11月10日
最代码官方  LV168 11月2日
最近浏览更多
76438949 2小时前
暂无贡献等级
lvllvl  LV4 11月18日
驱蚊器我 11月18日
暂无贡献等级
留下一点点 11月15日
暂无贡献等级
张春青  LV6 11月14日
dongandmin  LV6 11月13日
代盐人 11月12日
暂无贡献等级
3993zby  LV2 11月11日
Rommel  LV27 11月11日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友