package com.ld.igds.warn.service; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.bstek.dorado.data.provider.Page; import com.ld.igds.models.SecureManager; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.DateUtil; import org.apache.commons.lang3.time.DateFormatUtils; import org.hibernate.Session; import org.springframework.stereotype.Component; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author czt * @date 2023/10/30 **/ @Component public class HSecureManagerService extends HibernateDao { public void listData(Page page, Map param) throws Exception { StringBuffer hql = new StringBuffer(); hql.append(" from " + SecureManager.class.getName() + " where companyId =:companyId and deptId =:deptId "); Map args = new HashMap<>(); args.put("companyId", ContextUtil.getCompanyId()); args.put("deptId", ContextUtil.subDeptId(null)); String count = "select count(*) " + hql.toString(); hql.append(" order by updateTime desc"); this.pagingQuery(page, hql.toString(), count, args); } public void saveData(SecureManager data) { data.setUpdateTime(new Date()); data.setDeptId(ContextUtil.subDeptId(null)); data.setCompanyId(ContextUtil.getCompanyId()); Session session = this.getSessionFactory().openSession(); try { if (null == data.getFxdbm()) { data.setFxdbm(DateFormatUtils.format(new Date(), "yyyyMMdd") + getIndex(data.getCompanyId())); session.save(data); } else { session.update(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } public String getIndex(String companyId) { List list = getDataByTime(companyId); int index = 10000; if (list != null && list.size() > 0) { String id = list.get(0).getFxdbm().substring(8); int temp = Integer.valueOf(id) + 1; index += temp; }else { index ++; } return String.valueOf(index).substring(1); } private List getDataByTime(String companyId) { String hql = " from " + SecureManager.class.getName() + " where companyId =:companyId "; Map args = new HashMap(); args.put("companyId", companyId); hql += " and updateTime >:startTime"; args.put("startTime", DateUtil.getCurZero(new Date())); hql += " and updateTime <:endTime"; args.put("endTime", DateUtil.getNextZero(new Date())); hql += " order by id desc"; return this.query(hql, args); } public void delData(SecureManager data) { Session session = this.getSessionFactory().openSession(); try { if (null != data.getFxdbm()) { session.delete(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } }