/*
 * Copyright (ISY Team) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package top.zbeboy.isy.rest.internship.release;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Record;
import org.jooq.Result;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.zbeboy.isy.domain.tables.pojos.Department;
import top.zbeboy.isy.service.data.StaffService;
import top.zbeboy.isy.service.data.StudentService;
import top.zbeboy.isy.service.rest.internship.InternshipReleaseRestService;
import top.zbeboy.isy.web.bean.data.student.StudentBean;
import top.zbeboy.isy.web.bean.internship.release.InternshipReleaseBean;
import top.zbeboy.isy.web.util.AjaxUtils;
import top.zbeboy.isy.web.util.PaginationUtils;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;
import java.util.Optional;

@Slf4j
@RestController
public class InternshipReleaseRestController {

    @Resource
    private InternshipReleaseRestService internshipReleaseRestService;

    @Resource
    private StudentService studentService;

    @Resource
    private StaffService staffService;

    /**
     * 获取最近几条件实习数据
     *
     * @param paginationUtils 分页工具
     * @return 实习数据
     */
    @GetMapping("/rest/internship/release/new/data")
    public AjaxUtils internshipNewData(PaginationUtils paginationUtils) {
        AjaxUtils<InternshipReleaseBean> ajaxUtils = AjaxUtils.of();
        Result<Record> records = internshipReleaseRestService.findAllByPage(paginationUtils);
        List<InternshipReleaseBean> internshipReleaseBeens = internshipReleaseRestService.dealData(records);
        return ajaxUtils.success().msg("获取数据成功").listData(internshipReleaseBeens).paginationUtils(paginationUtils);
    }

    /**
     * 为学生获取最新实习数据
     *
     * @param user            用户凭证
     * @param paginationUtils 分页工具
     * @return 实习数据
     */
    @GetMapping("/rest/internship/release/student/data")
    public AjaxUtils internshipStudentData(Principal user, PaginationUtils paginationUtils) {
        AjaxUtils<InternshipReleaseBean> ajaxUtils = AjaxUtils.of();
        if (!ObjectUtils.isEmpty(user) && StringUtils.isNotBlank(user.getName())) {
            Optional<Record> student = studentService.findByUsernameRelation(user.getName());
            if (student.isPresent()) {
                StudentBean studentBean = student.get().into(StudentBean.class);
                Result<Record> records = internshipReleaseRestService.findAllByPageForStudent(paginationUtils, studentBean.getScienceId(), studentBean.getGrade());
                if (records.isNotEmpty()) {
                    ajaxUtils.success().listData(records.into(InternshipReleaseBean.class)).paginationUtils(paginationUtils).obj(studentBean.getScienceName());
                } else {
                    ajaxUtils.fail().msg("没有任何数据");
                }
            } else {
                ajaxUtils.fail().msg("查询学生信息失败");
            }
        } else {
            ajaxUtils.fail().msg("未获取到用户相关信息");
        }
        return ajaxUtils;
    }

    /**
     * 为教职工获取最新实习数据
     *
     * @param user            用户凭证
     * @param paginationUtils 分页工具
     * @return 实习数据
     */
    @GetMapping("/rest/internship/release/staff/data")
    public AjaxUtils internshipStaffData(Principal user, PaginationUtils paginationUtils) {
        AjaxUtils<InternshipReleaseBean> ajaxUtils = AjaxUtils.of();
        if (!ObjectUtils.isEmpty(user) && StringUtils.isNotBlank(user.getName())) {
            Optional<Record> staff = staffService.findByUsernameRelation(user.getName());
            if (staff.isPresent()) {
                Department department = staff.get().into(Department.class);
                Result<Record> records = internshipReleaseRestService.findAllByPageForStaff(paginationUtils, department.getDepartmentId());
                List<InternshipReleaseBean> internshipReleaseBeens = internshipReleaseRestService.dealData(records);
                if (records.isNotEmpty()) {
                    ajaxUtils.success().listData(internshipReleaseBeens).paginationUtils(paginationUtils);
                } else {
                    ajaxUtils.fail().msg("没有任何数据");
                }
            } else {
                ajaxUtils.fail().msg("查询教职工信息失败");
            }
        } else {
            ajaxUtils.fail().msg("未获取到用户相关信息");
        }
        return ajaxUtils;
    }
}

@@ -1257,16 +1257,44 @@
 ncipal;%0A
+import java.util.ArrayList;%0A
 import j
@@ -3058,99 +3058,290 @@
     
-if (records.isNotEmpty()) %7B%0A                    ajaxUtils.success().listData(records.into(I
+List%3CInternshipReleaseBean%3E internshipReleaseBeens = new ArrayList%3C%3E();%0A                if (records.isNotEmpty()) %7B%0A                    internshipReleaseBeens = records.into(InternshipReleaseBean.class);%0A                %7D%0A                ajaxUtils.success().msg(%22%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE%E6%88%90%E5%8A%9F%22).listData(i
 nter
@@ -3354,25 +3354,19 @@
 eleaseBe
-an.class)
+ens
 ).pagina
@@ -3431,103 +3431,8 @@
 ));%0A
-                %7D else %7B%0A                    ajaxUtils.fail().msg(%22%E6%B2%A1%E6%9C%89%E4%BB%BB%E4%BD%95%E6%95%B0%E6%8D%AE%22);%0A                %7D%0A
     
@@ -4517,56 +4517,8 @@
 s);%0A
-                if (records.isNotEmpty()) %7B%0A    
     
@@ -4549,16 +4549,30 @@
 ccess().
+msg(%22%E8%8E%B7%E5%8F%96%E6%95%B0%E6%8D%AE%E6%88%90%E5%8A%9F%22).
 listData
@@ -4634,103 +4634,8 @@
 s);%0A
-                %7D else %7B%0A                    ajaxUtils.fail().msg(%22%E6%B2%A1%E6%9C%89%E4%BB%BB%E4%BD%95%E6%95%B0%E6%8D%AE%22);%0A                %7D%0A
     

优化实习 restful 内容