package com.tdtech.pdm.create.dao.impl;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import com.tdtech.pdm.create.dao.DQAssemblyDao;
import com.tdtech.pdm.create.pojo.DQAssemblyPojo;
public class DQAssemblyDaoImpl implements DQAssemblyDao{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
/**
* 风险评估时的部件更新
*/
public boolean updateRiskEvaluation(List<DQAssemblyPojo> list){
boolean flag=false;
SessionFactory sessionFactory=null;
Session session =null;
Transaction tx=null;
try{
sessionFactory=this.hibernateTemplate.getSessionFactory();
session = sessionFactory.openSession();
tx=session.beginTransaction();
for(DQAssemblyPojo d:list){
String hql="update DQAssemblyPojo d set d.riskgrade=?,d.risksort=?,d.riskdescrible=?,d.eludemeasureadvice=?,d.recallbill=? where d.id=?";
Query query = session.createQuery(hql);
query.setString(0, d.getRiskgrade());
query.setString(1, d.getRisksort());
query.setString(2, d.getRiskdescrible());
query.setString(3, d.getEludemeasureadvice());
query.setString(4, d.getRecallbill());
query.setInteger(5, d.getId());
query.executeUpdate();
}
tx.commit();
flag=true;
}catch(Exception e){
tx.rollback();
}finally{
session.close();
}
return flag;
}
/**
* 更新部件的部件状态和流程状态
*/
public boolean updateStatus(final String assemblystatus,final String status,final int mateId){
boolean flag = false;
int num=0;
try{
num = this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String sql = "update tb_dqassembly d "+
"set d.assemblystatus=?,d.status=? "+
"where d.nmabid=?";
Query query = session.createSQLQuery(sql);
query.setString(0, assemblystatus);
query.setString(1, status);
query.setInteger(2, mateId);
return query.executeUpdate();
}
});
}catch(Exception e){
e.printStackTrace();
}
if(num>0){
flag=true;
}
return flag;
}
/**
* 保存物料关联部件信息
*/
public boolean saveRelativeInfo(final List<DQAssemblyPojo> list){
boolean flag=false;
int count=0;
try{
count=this.hibernateTemplate.execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int num=0;
for(DQAssemblyPojo d:list){
String hql="update DQAssemblyPojo d set d.adecandtypeflag=?,d.adecribleandtype=?,d.iscomparedoc=?,d.ncdocument=?,d.digest=? where d.dqcode=?";
Query query=session.createQuery(hql);
query.setString(0, d.getAdecandtypeflag());
query.setString(1, d.getAdecribleandtype());
query.setString(2, d.getIscomparedoc());
query.setString(3, d.getNcdocument());
query.setString(4, d.getDigest());
query.setString(5, d.getDqcode());
query.executeUpdate();
num+=1;
}
return num;
}
});
}catch(Exception e){
e.printStackTrace();
}
if(count>0){
flag=true;
}
return flag;
}
/**
* 更新部件信息
*/
public boolean updateDQAssembly(final List<DQAssemblyPojo> list){
boolean flag=false;
int count=0;
try{
count=this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int num=0;
for(int i=0;i<list.size();i++){
DQAssemblyPojo n=list.get(i);
String sql="update tb_dqassembly d set d.adviceworkshop=?,d.advicetype=?,d.lapserate=? where d.dqcode=?";
Query query=session.createSQLQuery(sql);
query.setString(0, n.getAdviceworkshop());
query.setString(1, n.getAdvicetype());
query.setString(2, n.getLapserate());
query.setString(3, n.getDqcode());
query.executeUpdate();
num+=1;
}
return num;
}
});
}catch(Exception e){
e.printStackTrace();
}
System.out.println("更新的行数:"+count);
if(count==list.size()){
flag=true;
}
return flag;
}
/**
* 创建鼎桥部件
* @param dQAssemblyPojo
* @return
*/
public boolean addDQAssembly(DQAssemblyPojo dQAssemblyPojo){
boolean flag=false;
try{
this.hibernateTemplate.save(dQAssemblyPojo);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
/**
* 删除鼎桥部件
* @param id
* @return
*/
public boolean deleteDQAssembly(int id){
boolean flag=false;
try{
DQAssemblyPojo dQAssemblyPojo = this.getHibernateTemplate().get(DQAssemblyPojo.class, id);
this.getHibernateTemplate().delete(dQAssemblyPojo);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
/**
* 修改鼎桥部件
* @param dQAssemblyPojo
* @return
*/
public boolean updateDQAssembly(DQAssemblyPojo dQAssemblyPojo){
boolean flag=false;
try{
this.getHibernateTemplate().update(dQAssemblyPojo);
flag=true;
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
/**
* 修改鼎桥部件
*/
public boolean updateDQAssembly(final Integer id,final Integer newMateAppBillId){
System.out.println("修改鼎桥部件 id="+id+",nmabid="+newMateAppBillId);
boolean flag=false;
try{
int num=0;
num=this.hibernateTemplate.execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String sql="update tb_newMateAppBill set nmabid=? where id=?";
Query query=session.createSQLQuery(sql);
query.setInteger(0, newMateAppBillId);
query.setInteger(1, id);
return query.executeUpdate();
}
});
if(num>0){
flag=true;
}
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
/**
* 查询所有的鼎桥部件
* @return
*/
public List<DQAssemblyPojo> queryAllDQAssembly(){
List<DQAssemblyPojo> list=new ArrayList<DQAssemblyPojo>();
try{
list=this.getHibernateTemplate().find("from DQAssemblyPojo a order by a.id desc");
}catch(Exception e){
e.printStackTrace();
}
return list;
}
/**
* 通过id查询鼎桥部件
* @param id
* @return
*/
public DQAssemblyPojo queryDQAssemblyById(int id){
DQAssemblyPojo dQAssemblyPojo=new DQAssemblyPojo();
//System.out.println("通过id查询鼎桥部件: id="+id);
try{
dQAssemblyPojo=(DQAssemblyPojo)this.getHibernateTemplate().find("from DQAssemblyPojo a where a.id="+id).get(0);
}catch(Exception e){
e.printStackTrace();
}
//System.out.println("通过id查询鼎桥部件-"+dQAssemblyPojo.getDqcode());
return dQAssemblyPojo;
}
/**
* 模糊查询
* @param name
* @return
*/
public List<DQAssemblyPojo> queryDQAssemblyByName(final String name){
List<DQAssemblyPojo> list=new ArrayList<DQAssemblyPojo>();
try{
list=this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql="from DQAssemblyPojo a where a.goods like ? order by a.id desc";
Query query=session.createQuery(hql);
query.setString(0, "%"+name+"%");
return query.list();
}
});
}catch(Exception e){
e.printStackTrace();
}
return list;
}
/**
* 生成华为编码
*/
public String generateHwcode(final String code){
//System.out.println("dao------------------"+code);
String hwcode=null;
List<DQAssemblyPojo> list=new ArrayList<DQAssemblyPojo>();
try{
list=this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql="from DQAssemblyPojo a where a.hwcode like ? order by a.hwcode desc";
Query query=session.createQuery(hql);
query.setString(0, code+"%");
return query.list();
}
});
if(list.size()>0){
hwcode=list.get(0).getHwcode();
System.out.println("dao-华为编码为:"+hwcode);
}else{
hwcode=" ";
}
}catch(Exception e){
e.printStackTrace();
hwcode=" ";
}
StringBuffer s=new StringBuffer();
String currentHwcode=null;
int hwcodeInt=0;
if(hwcode!=null && (!(" ".equals(hwcode)))){
s=s.append(hwcode.substring(4));
hwcodeInt=Integer.parseInt(s.toString());
hwcodeInt+=1;
if(hwcodeInt<10){
currentHwcode=hwcode.substring(0,4)+"0000"+hwcodeInt;
}else if(hwcodeInt<100){
currentHwcode=hwcode.substring(0,4)+"000"+hwcodeInt;
}else if(hwcodeInt<1000){
currentHwcode=hwcode.substring(0,4)+"00"+hwcodeInt;
}else if(hwcodeInt<10000){
currentHwcode=hwcode.substring(0,4)+"0"+hwcodeInt;
}else{
currentHwcode=hwcode.substring(0,4)+hwcodeInt;
}
}else{
currentHwcode=code+"00001";
}
System.out.println("dao-生成华为编码-"+currentHwcode);
return currentHwcode;
}
/**
* 通过编码或名称模糊查询
*/
public List<DQAssemblyPojo> queryDQAssembly(final String dqcode,final String outtername){
List<DQAssemblyPojo> list=new ArrayList<DQAssemblyPojo>();
list=this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
String hql=null;
Query query=null;
if((dqcode==null && outtername==null) || ("".equals(dqcode) && "".equals(outtername))){
System.out.println("--------null -----null");
hql="from DQAssemblyPojo d order by d.id asc";
query=session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(50);
return query.list();
}
if(dqcode!=null && outtername==null){
hql="from DQAssemblyPojo d where d.dqcode like ? order by d.dqcode asc";
query=session.createQuery(hql);
query.setString(0, "%"+dqcode+"%");
}else if(outtername!=null && dqcode==null){
hql="from DQAssemblyPojo d where d.outtername like ? order by d.dqcode asc";
query=session.createQuery(hql);
query.setString(0, "%"+outtername+"%");
}else if(dqcode!=null && outtername!=null){
hql="from DQAssemblyPojo d where d.dqcode like ? order by d.dqcode asc";
query=session.createQuery(hql);
query.setString(0, "%"+dqcode+"%");
}
return query.list();
}
});
if(list.size()>100){
return null;
}
return list;
}
/**
* 查询多个部件
*/
public List<DQAssemblyPojo> queryMultiAssemblyRecord(final int[] ids){
List<DQAssemblyPojo> list=new ArrayList<DQAssemblyPojo>();
list=this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int len=ids.length;
String[] s=new String[len];
StringBuffer sb=new StringBuffer("from DQAssemblyPojo d where d.id in(");
for(int i=0;i<len;i++){
if(i==len-1){
sb.append("?");
}else{
sb.append("?"+",");
}
}
sb.append(")");
System.out.println(sb.toString());
Query query = session.createQuery(sb.toString());
for(int i=0;i<ids.length;i++){
query.setInteger(i, ids[i]);
}
return query.list();
}
});
return list;
}
}
|