package action;

import Bean.*;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.Session;
import tools.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;

public class Student extends HibernateTools implements ServletRequestAware {
    private int sno;
    private Integer age;
    private Integer classid;
    private String name;
    private String sex;
    private String spd;
    private String spdcheck;
    private String dept;
    private String school;
    private File file;//上传文件的file对象,由拦截器自动填充
    private String fileFileName;//上传文件名,由拦截器自动填充
    private String fileContentType;//上传文件类型,由拦截器自动填充

    private Session session;
    private int update;//判断是添加学生还是更新学生信息
    private List<StudentEntity> stuEntities;
    private int delkey;
    private int updatekey;
    public String addStudent(){
        showStudents();
        if (session==null||!session.isOpen()){
            session=init();
        }
        if (sno==0||age==0||classid==0||name==null||sex==null||spd==null||spdcheck==null||school==null||dept==null){
           addActionError("请输入所有必填内容");
            return update==0?ERROR:"manage";
        }
        if (!spd.equals(spdcheck))
        {
           addActionError("两次密码不相同");
            return update==0?ERROR:"manage";
        }
        StudentEntity stuEntity=session.get(StudentEntity.class, sno);
        if (stuEntity!=null&&update==0){
            addActionError("该生已经存在,无法重复添加");
            return update==0?ERROR:"manage";
        }
        if (session.get(ClassEntity.class, classid)==null){
            addActionError("班级不存在,请检查班级号");
            return update==0?ERROR:"manage";
        }
        if (fileContentType==null||(!fileContentType.equals("image/jpeg")&&!fileContentType.equals("image/jpg"))){
            addActionError("图片格式不正确,请上传jpg格式");
            return update==0?ERROR:"manage";
        }
    if (update==0)//避免出现两个相同的实体
        stuEntity=new StudentEntity();
        stuEntity.setStudentId(sno);
        stuEntity.setAge(age);
        stuEntity.setClassId(classid);
        stuEntity.setName(name);
        stuEntity.setSex(sex);
        stuEntity.setPassword(spd);
        stuEntity.setDept(dept);
        stuEntity.setSchool(school);
        stuEntity.setHeadImg(filetobytes.Filetobyte(file));
        session.saveOrUpdate(stuEntity);
        destory();
        showStudents();
        if (update==1){
            addActionError("修改成功");
            return "update";
        }
        addActionError("添加成功");
        return SUCCESS;
    }

    public String delstudent(){
        showStudents();
        if (session==null||!session.isOpen()){
            session=init();
        }
        if (delkey==0){
           addActionError("未指定删除对象");
            return INPUT;
        }
        StudentEntity stuEntity=session.get(StudentEntity.class, delkey);
        if (stuEntity==null){
           addActionError("该学生已经不存在");
            return ERROR;
        }
        session.delete(stuEntity);
        destory();
        delkey=0;
        addActionError("删除成功");
        showStudents();
        return SUCCESS;
    }
    public String showStudents(){
        if (session==null||!session.isOpen()){
            session=init();
        }
        stuEntities=session.createQuery("from StudentEntity order by studentId").getResultList();
        destory();
        return SUCCESS;
    }

public String premanageStudent(){
        //刷新学生信息
    session=init();
    if (updatekey==0){
        return INPUT;
    }
    StudentEntity stuEntity=session.get(StudentEntity.class, updatekey);
    if (stuEntity==null){
        addActionError("用户不存在,请刷新后重试");
        return ERROR;
    }
    sno=stuEntity.getStudentId();
    name=stuEntity.getName();
    sex=stuEntity.getSex();
    spdcheck=spd=stuEntity.getPassword();
    age=stuEntity.getAge();
    classid=stuEntity.getClassId();
    school=stuEntity.getSchool();
    dept=stuEntity.getDept();
    spd=spdcheck=stuEntity.getPassword();
    destory();
       return SUCCESS;
}

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {

    }

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getClassid() {
        return classid;
    }

    public void setClassid(Integer classid) {
        this.classid = classid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getSpd() {
        return spd;
    }

    public void setSpd(String spd) {
        this.spd = spd;
    }

    public int getUpdatekey() {
        return updatekey;
    }

    public void setUpdatekey(int updatekey) {
        this.updatekey = updatekey;
    }

    public String getSpdcheck() {
        return spdcheck;
    }

    public void setSpdcheck(String spdcheck) {
        this.spdcheck = spdcheck;
    }

    public List<StudentEntity> getStuEntities() {
        return stuEntities;
    }

    public void setStuEntities(List<StudentEntity> stuEntities) {
        this.stuEntities = stuEntities;
    }

    public int getDelkey() {
        return delkey;
    }

    public void setDelkey(int delkey) {
        this.delkey = delkey;
    }

    public int getUpdate() {
        return update;
    }

    public void setUpdate(int update) {
        this.update = update;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public File getHeadImg() {
        return file;
    }

    public void setHeadImg(File file) {
        this.file = file;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public String getFileFileName() {
        return fileFileName;
    }

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
        return fileContentType;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    public String getHeadImgContentType() {
        return fileContentType;
    }

    public void setHeadImgContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }
}
最近下载更多
lizhenmrt  LV4 2022年12月12日
这不是一只柚子吗  LV6 2021年5月24日
feiyang991128  LV4 2021年5月12日
hao liong  LV2 2021年5月3日
wensidy  LV10 2021年4月27日
hahaman  LV5 2021年4月25日
lili010126  LV8 2021年4月2日
184773349  LV8 2021年3月6日
cxy2956  LV1 2021年1月10日
李晓晗  LV1 2021年1月7日
最近浏览更多
WBelong  LV7 3月6日
asddwh  LV12 2023年12月29日
微信网友_6790506018131968  LV1 2023年12月23日
wkq888  LV16 2023年11月27日
958546428  LV3 2023年11月10日
mhymhy123  LV1 2023年10月20日
wbx666  LV1 2023年9月18日
灵依ziNing  LV7 2023年7月28日
zhumeng168  LV5 2023年7月6日
asadda  LV2 2023年6月26日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友