package com.mrm.industry.repo.dao;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.lang.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.mrm.industry.repo.entity.BusiRepositoryBillsDetails;
import com.mrm.industry.repo.entity.BusiRepositoryMaterial;
import com.mrm.system.dao.BaseDAO;
import com.mrm.system.dao.BaseDAOImpl;
/**
* 用途:业务模块名称
*/
@Repository("busiRepositoryBillsDetailsDao")
public class BusiRepositoryBillsDetailsDaoImpl extends BaseDAOImpl<BusiRepositoryBillsDetails> implements BusiRepositoryBillsDetailsDao{
@Resource
private BaseDAO<BusiRepositoryBillsDetails> myDao;
@Resource
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
@Override
public List<BusiRepositoryBillsDetails> findLinkPage(int userid, String temporaryId, Integer pageNo, Integer pageSize) {
StringBuffer sql = new StringBuffer("SELECT d.*" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_material_file f ON d.fk_material_id=f.bills_detail" +
" WHERE f.creator = "+userid+" AND temporary_id = '"+temporaryId+"'" +
" GROUP BY fk_material_id"+
" LIMIT "+pageNo+","+pageSize);
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<BusiRepositoryBillsDetails> findLinkPage(int userid, String temporaryId) {
StringBuffer sql = new StringBuffer("SELECT d.*" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_material_file f ON d.fk_material_id=f.bills_detail" +
" WHERE f.creator = "+userid+" AND temporary_id = '"+temporaryId+"' GROUP BY fk_material_id");
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<BusiRepositoryBillsDetails> findLY(String itemid) {
StringBuffer sql = new StringBuffer("select * from busi_repository_bills_details where bills_no ='"+itemid+"'");
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<BusiRepositoryBillsDetails> findByZyerid(Integer zyerId, Integer zcId, String zcSn, String zcName, Integer zcType, int inouttype) {
StringBuffer sql = new StringBuffer("SELECT d.*" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_bills b ON b.odd_no = d.bills_no" +
" WHERE b.fk_user_id = " +zyerId+
" AND b.bills_type = "+inouttype);
if (zcId != null){
sql.append(" and d.fk_material_id like '%"+zcId+"%'");
}
if (StringUtils.isNotBlank(zcSn)){
sql.append(" and d.sn_no like '%"+zcSn+"%'");
}
if (StringUtils.isNotBlank(zcName)){
sql.append(" and d.materiel_name like '%"+zcName+"%'");
}
if (zcType != null && zcType != 0){
sql.append(" and materiel_type ="+zcType);
}
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<BusiRepositoryBillsDetails> queryByRoomId(BusiRepositoryMaterial model,Integer roomId,Integer pageNo,Integer pageSize) {
StringBuffer hql = new StringBuffer("select d from BusiRepositoryMaterial m "
+ " LEFT JOIN BusiRepositoryBillsDetails d on m.id =d.fkMaterialId "
+ " LEFT JOIN BusiRepositoryBills b ON d.billsNo = b.oddNo "
+ " LEFT JOIN BusiRepositoryType t ON t.id = m.propertyTypeId "
+ " where b.billsStatus=1 and m.propertyStatus=0 and b.billsType=1 ");
if(roomId!=null) {
hql.append("and m.fkRoomId="+roomId+"");
}
if(model.getSnNo()!=null && !model.getSnNo().equals("")){
hql.append(" and d.snNo like'%"+model.getSnNo()+"%'");
}
if (model.getMaterielType() != null) {
hql.append(" and t.materielType ="+model.getMaterielType()+"");
}
// if (model.getReservedNo()!=null && !model.getReservedNo() .equals("")) {
// hql.append(" and d.reservedNo like '%"+model.getReservedNo()+"%'");
// }
if (model.getMaterielName()!=null && !model.getMaterielName().equals("")) {
hql.append(" and t.materielName like '%"+model.getMaterielName()+"%'");
}
if(model.getMaterialNo()!=null && !model.getMaterialNo().equals("")) {
hql.append(" and m.materialNo like '%"+model.getMaterialNo()+"%'");
}
Query<BusiRepositoryBillsDetails> q =getSessionFactory().getCurrentSession().createQuery(hql.toString());
if(pageNo!=null && pageSize!=null) {
q.setFirstResult((pageNo-1)*pageSize);
q.setMaxResults(pageSize);
}
List<BusiRepositoryBillsDetails> list = q.list();
return list;
}
@Override
public List<BusiRepositoryBillsDetails> findByOrgid(Integer orgId, Integer zcId, String zcSn, String zcName, Integer zcType, int inouttype) {
StringBuffer sql = new StringBuffer("SELECT d.*" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_bills b ON b.odd_no = d.bills_no" +
" LEFT JOIN busi_repository_material m ON m.id = d.fk_material_id"+
" WHERE b.bills_status = 1 AND m.property_status = 7 AND b.fk_organization_id = " +orgId +" AND b.bills_type = "+inouttype);
if (zcId != null){
sql.append(" and d.fk_material_id like '%"+zcId+"%'");
}
if (StringUtils.isNotBlank(zcSn)){
sql.append(" and d.sn_no like '%"+zcSn+"%'");
}
if (StringUtils.isNotBlank(zcName)){
sql.append(" and d.materiel_name like '%"+zcName+"%'");
}
if (zcType != null && zcType != 0){
sql.append(" and materiel_type ="+zcType);
}
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<BusiRepositoryBillsDetails> findByKfid(Integer kfId, Integer zcId, String zcSn, String zcName, Integer zcType) {
StringBuffer sql = new StringBuffer("SELECT d.*" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_bills b ON b.odd_no = d.bills_no" +
" LEFT JOIN busi_repository_material m ON m.id = d.fk_material_id"+
" LEFT JOIN busi_repository_type t ON t.id = m.property_type_id"+
" WHERE b.bills_status = 1 AND b.bills_type = 1 AND m.property_status = 0 AND m.fk_room_id = " +kfId);
if (zcId != null){
sql.append(" and m.material_no like '%"+zcId+"%'");
}
if (StringUtils.isNotBlank(zcSn)){
sql.append(" and d.sn_no like '%"+zcSn+"%'");
}
if (StringUtils.isNotBlank(zcName)){
sql.append(" and t.materiel_name like '%"+zcName+"%'");
}
if (zcType != null && zcType != 0){
sql.append(" and t.materiel_type ="+zcType);
}
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<BusiRepositoryBillsDetails> findLYByOrgid(Integer orgId, Integer zcId, String zcSn, String zcName, Integer zcType, int inouttype) {
StringBuffer sql = new StringBuffer("SELECT d.*" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_bills b ON b.odd_no = d.bills_no" +
" LEFT JOIN busi_repository_material m ON m.id = d.fk_material_id"+
" LEFT JOIN busi_repository_type t ON t.id = m.property_type_id"+
" WHERE b.bills_status = 1 AND m.property_status = 7 AND b.fk_organization_id = " +orgId);
if (zcId != null){
sql.append(" and d.fk_material_id like '%"+zcId+"%'");
}
if (StringUtils.isNotBlank(zcSn)){
sql.append(" and d.sn_no like '%"+zcSn+"%'");
}
if (StringUtils.isNotBlank(zcName)){
sql.append(" and t.materiel_name like '%"+zcName+"%'");
}
if (zcType != null && zcType != 0){
sql.append(" and t.materiel_type ="+zcType);
}
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<BusiRepositoryBillsDetails> queryByOrg(int i, Integer orgId) {
StringBuffer hql = new StringBuffer("SELECT d FROM BusiRepositoryBillsDetails d LEFT JOIN BusiRepositoryBills b ON d.billsNo =b.oddNo WHERE b.billsType="+i+" and b.fkOrganizationId="+orgId+"");
Query<BusiRepositoryBillsDetails> q =getSessionFactory().getCurrentSession().createQuery(hql.toString());
List<BusiRepositoryBillsDetails> list = q.list();
return list;
}
@Override
public List<BusiRepositoryBillsDetails> findWXByOrgid(Integer orgId, Integer zcId, String zcSn, String zcName, Integer zcType, int inouttype) {
StringBuffer sql = new StringBuffer("SELECT d.*" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_bills b ON b.odd_no = d.bills_no" +
" LEFT JOIN busi_repository_material m ON m.id = d.fk_material_id"+
" left join busi_repository_type t on t.id = m.property_type_id"+
" WHERE b.bills_status = 1 AND m.property_status = 0 AND b.fk_organization_id = " +orgId);
if (zcId != null){
sql.append(" and d.fk_material_id like '%"+zcId+"%'");
}
if (StringUtils.isNotBlank(zcSn)){
sql.append(" and d.sn_no like '%"+zcSn+"%'");
}
if (StringUtils.isNotBlank(zcName)){
sql.append(" and t.materiel_name like '%"+zcName+"%'");
}
if (zcType != null && zcType != 0){
sql.append(" and t.materiel_type ="+zcType);
}
sql.append(" GROUP BY m.id");
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString()).addEntity(BusiRepositoryBillsDetails.class);
return query.list();
}
@Override
public List<Map<String, String>> findAllBillsAndDetails() {
StringBuffer sql = new StringBuffer("SELECT fk_goal_room_id,sn_no,barcode,unit_price,description,creator,materiel_type,property_version,card_type,reserved_no,iccid,card_operator,fk_material_id,software_version_no,expiration_time,property_type_id" +
" FROM busi_repository_bills_details d" +
" LEFT JOIN busi_repository_bills b ON b.odd_no = d.bills_no");
SQLQuery query = (SQLQuery) this.getCurrentSession().createSQLQuery(sql.toString())
.addScalar("reserved_no", StandardBasicTypes.STRING)
.addScalar("fk_goal_room_id", StandardBasicTypes.STRING)
.addScalar("sn_no", StandardBasicTypes.STRING)
.addScalar("barcode", StandardBasicTypes.STRING)
.addScalar("unit_price", StandardBasicTypes.STRING)
.addScalar("description", StandardBasicTypes.STRING)
.addScalar("creator", StandardBasicTypes.STRING)
.addScalar("materiel_type", StandardBasicTypes.STRING)
.addScalar("property_version", StandardBasicTypes.STRING)
.addScalar("card_type", StandardBasicTypes.STRING)
.addScalar("iccid", StandardBasicTypes.STRING)
.addScalar("card_operator", StandardBasicTypes.STRING)
.addScalar("fk_material_id", StandardBasicTypes.STRING)
.addScalar("software_version_no", StandardBasicTypes.STRING)
.addScalar("expiration_time", StandardBasicTypes.STRING)
.addScalar("property_type_id", StandardBasicTypes.STRING)
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
return query.list();
}
}
package com.mrm.system.dao;
import com.mrm.common.admin.action.JDBCUtils;
import com.mrm.system.entity.BusiForwardSender;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.jdbc.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
/**
* @Description: 数据设置(访问层)
**/
@Repository("busiForwardSenderDao")
public class BusiForwardSenderDaoImpl extends BaseDAOImpl<BusiForwardSender> implements BusiForwardSenderDao {
@Resource
private JDBCUtils jdbcUtils;
@Resource
private BaseDAO<BusiForwardSender> myDao;
/**
* @Description: 保存数据发送数据信息,保存关联车辆信息
**/
@Override
public void saveSenderAndVehicle(Integer primaryKeyId, Integer[] carIds) throws SQLException {
String sql = "insert into busi_forward_sender_vehicle(busi_forward_sender_ID,TMS_VEHICLE_ID)VALUES (?,?)";
// Session currentSession = this.getSessionFactory().getCurrentSession();
// for (Integer carId : carIds) {
// currentSession.createSQLQuery(sql).setParameter(0, primaryKeyId).setParameter(1, carId).executeUpdate();
// }
// 从hibernate获取连接,并用doWork进行原声jdbc操作,这样事务管理机制就是使用的一个Transaction
Transaction transaction = null;
Session currentSession = null;
try {
currentSession = this.getSessionFactory().getCurrentSession();
transaction = currentSession.getTransaction();
currentSession.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (Integer carId : carIds) {
preparedStatement.setInt(1,primaryKeyId);
preparedStatement.setInt(2,carId);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
}
});
// transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
throw new SQLException();
}finally {
}
// Connection connection = null;
// PreparedStatement preparedStatement = null;
// try {
// connection = jdbcUtils.getConnectionNotAutoCommit();
// preparedStatement = connection.prepareStatement(sql);
// for (Integer carId : carIds) {
// preparedStatement.setInt(1,primaryKeyId);
// preparedStatement.setInt(2,carId);
// preparedStatement.addBatch();
// }
// preparedStatement.executeBatch();
// connection.commit();
// } catch (SQLException e) {
// e.printStackTrace();
// throw new SQLException();
// } finally {
// try {
// connection.rollback();
// jdbcUtils.closeConAndStatement(connection,preparedStatement);
// } catch (SQLException e) {
// e.printStackTrace();
// throw new SQLException();
// }
// }
}
/**
* @Description: 获取发送端数据集合
**/
@Override
public List<Map<String, Object>> getBusiSenderList() {
String sql = "select bfs.ID,bfs.FORWARD_PLATFORM_NAME ," +
"(select count(1) from busi_forward_sender_vehicle v where v.busi_forward_sender_ID=bfs.ID)" +
" COUNT_CAR," +
"case" +
" when bfs.FORWARD_WAY = 0 then 'tcp'" +
" when bfs.FORWARD_WAY = 1 then 'kafka'" +
" when bfs.FORWARD_WAY = 2 then 'ftp'" +
" when bfs.FORWARD_WAY = 3 then 'sftp'" +
" end as FORWARD_WAY_EN," +
"bfs.FORWARD_STATU,bfs.FORWARD_PROTOCOL_NAME" +
" from busi_forward_sender bfs order by bfs.CREATE_TIME desc";
Session currentSession = this.getSessionFactory().getCurrentSession();
SQLQuery query = currentSession.createSQLQuery(sql);
query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
//执行查询
List<Map<String, Object>> list = query.list();
return list;
}
/**
* @Description: 删除车辆发送端,删除关联车辆id
**/
@Override
public void deleteRelation(Integer delid) {
String sql = "delete from busi_forward_sender_vehicle where busi_forward_sender_ID=" + delid;
this.getSessionFactory().getCurrentSession().createSQLQuery(sql).executeUpdate();
}
/**
* @Description: 获取车辆关联表车辆ids
**/
@Override
public List<Integer> getRelationCarIds(Integer viewid) {
String sql = "SELECT TMS_VEHICLE_ID FROM busi_forward_sender_vehicle where busi_forward_sender_ID=" + viewid;
Session currentSession = this.getSessionFactory().getCurrentSession();
SQLQuery query = currentSession.createSQLQuery(sql);
//执行查询
List<Integer> list = query.list();
return list;
}
@Override
public List<BusiForwardSender> findStatusTrue() {
String sql = "SELECT * FROM busi_forward_sender where FORWARD_STATU=1";
Session currentSession = this.getSessionFactory().getCurrentSession();
SQLQuery query = currentSession.createSQLQuery(sql).addEntity(BusiForwardSender.class);
return query.list();
}
/**
* @Description: 分页
**/
@Override
public List<Map<String, Object>> findPage(String sql, Integer page, Integer rows) {
Session currentSession = this.getSessionFactory().getCurrentSession();
SQLQuery query = currentSession.createSQLQuery(sql);
query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
query.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
return query.list();
}
}