package com.store.dao;
import com.store.entity.Staff;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.sql.Insert;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import java.text.SimpleDateFormat;
import java.util.List;
/**
* 作用标注
*
* @date: 2023/3/5
*/
public class StaffDao extends HibernateDaoSupport {
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public List<Staff> getList() {
Session session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
String sql = "select * from staff";
Query query = session.createSQLQuery(sql).addEntity(Staff.class);
List<Staff> result = (List<Staff>) query.list();
transaction.commit();
session.close();
return result;
}
public Staff login(String un, String pw) {
Session session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
String sql = "select * from staff where (account = '" + un + "' or phone = '" + un + "') and password = '" + pw +"' and state = 1";
Query query = session.createSQLQuery(sql).addEntity(Staff.class);
List<Staff> result = (List<Staff>) query.list();
transaction.commit();
session.close();
if(result.size() != 0){
return result.get(0);
}else {
Staff s = new Staff();
return s;
}
}
public String register(String un, String pw,String dep) {
int flag = isHave(un);
if(flag>0){
return "fail";
}else{
Session session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
Staff s = new Staff();
s.setAccount(un);
s.setPassWord(pw);
s.setDep(dep);
s.setCreateDate(sdf.format(System.currentTimeMillis()));
s.setState("1");
session.save(s);
transaction.commit();
session.close();
return "success";
}
}
public int isHave(String un){
Session session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
String sql = "select * from staff where account = '" + un + "'";
Query query = session.createSQLQuery(sql).addEntity(Staff.class);
List<Staff> result = (List<Staff>) query.list();
transaction.commit();
session.close();
return result.size();
}
public String update(Staff staff) {
Session session = getSessionFactory().openSession();
Transaction transaction=session.getTransaction();
transaction.begin();
String sql="update Staff set updatedate = '" + sdf.format(System.currentTimeMillis()) +"'";
if(staff.getUsername()!="" && staff.getUsername()!=null){
sql += ",username = '"+staff.getUsername()+"'";
}
if(staff.getSex()!="" && staff.getSex()!=null){
sql += ",sex = '"+staff.getSex()+"'";
}
if(staff.getAge()!=0){
sql += ",age = "+staff.getAge();
}
if(staff.getPhone()!="" && staff.getPhone()!=null){
sql += ",phone = '"+staff.getPhone()+"'";
}
if(staff.getAddress()!="" && staff.getAddress()!=null){
sql += ",address = '"+staff.getAddress()+"'";
}
if(staff.getAccount()!="" && staff.getAccount()!=null){
sql += ",account = '"+staff.getAccount()+"'";
}
if(staff.getPassWord()!="" && staff.getPassWord()!=null){
sql += ",password = '"+staff.getPassWord()+"'";
}
if(staff.getState()!="" && staff.getState()!=null){
sql += ",state = '"+staff.getState()+"'";
}
if(staff.getDep()!="" && staff.getDep()!=null){
sql += ",dep = '"+staff.getDep()+"'";
}
Query query = session.createQuery(sql+"where id = "+staff.getId());
int r=query.executeUpdate();
transaction.commit();
session.close();
if(r>0){
return "success";
}else{
return "fail";
}
// Session session = getSessionFactory().openSession();
// Transaction transaction=session.getTransaction();
// transaction.begin();
// Staff s =session.get(Staff.class, staff.getId());
// if(staff.getUsername()!="" && staff.getUsername()!=null){
// s.setUsername(staff.getUsername());
// }
// if(staff.getSex()!="" && staff.getSex()!=null){
// s.setSex(staff.getSex());
// }
// if(staff.getAge()!=0){
// s.setAge(staff.getAge());
// }
// if(staff.getPhone()!="" && staff.getPhone()!=null){
// s.setPhone(staff.getPhone());
// }
// if(staff.getAddress()!="" && staff.getAddress()!=null){
// s.setAddress(staff.getAddress());
// }
// if(staff.getAccount()!="" && staff.getAccount()!=null){
// s.setAccount(staff.getAccount());
// }
// if(staff.getPassWord()!="" && staff.getPassWord()!=null){
// s.setPassWord(staff.getPassWord());
// }
// if(staff.getState()!="" && staff.getState()!=null){
// s.setState(staff.getState());
// }
// if(staff.getDep()!="" && staff.getDep()!=null){
// s.setDep(staff.getDep());
// }
// s.setUpdateDate(sdf.format(System.currentTimeMillis()));
//
// session.update(s);
// transaction.commit();
// session.close();
// return "success";
}
}