package com.ld.igds.verb.service; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.bstek.dorado.data.provider.Page; import com.ld.igds.constant.RedisConst; import com.ld.igds.models.MAreationData; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.RedisUtil; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.time.DateFormatUtils; import org.hibernate.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Service public class HMAreationDataService extends HibernateDao { @Autowired private RedisUtil redisUtil; public static final String CACHE_AREATION_ID = "AREATION_ID"; public void pageData(Page page, Map param) throws Exception { String hql = " from " + MAreationData.class.getName() + " where companyId=:companyId"; Map args = new HashMap(); args.put("companyId", ContextUtil.getCompanyId()); if (null != param) { String str = (String) param.get("depotId"); if (StringUtils.isNotEmpty(str)) { hql += " and depotId =:depotId"; args.put("depotId", str); } } String count = "select count(*) " + hql; hql += " order by startTime desc"; this.pagingQuery(page, hql, count, args); } public void saveData(MAreationData data) { if (null == data.getCompanyId()) { data.setCompanyId(ContextUtil.getCompanyId()); } if (null == data.getDeptId()) { data.setDeptId(ContextUtil.subDeptId(null)); } Session session = this.getSessionFactory().openSession(); data.setUpdateTime(new Date()); data.setUpdateUser(ContextUtil.getLoginUserCName()); try { if (null == data.getId()) { String id = createId(data.getCompanyId(), data.getStartTime()); data.setId(id); session.save(data); } else { session.update(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } public String delData(MAreationData data) { Session session = this.getSessionFactory().openSession(); try { if (null != data.getId()) { session.delete(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return null; } public List areationList(Map param) { StringBuffer hql = new StringBuffer(); Map args = new HashMap<>(); hql.append(" from " + MAreationData.class.getName() + " where companyId=:companyId"); args.put("companyId", ContextUtil.getCompanyId()); if (null != param) { String str = (String) param.get("id"); if (StringUtils.isNotEmpty(str)) { hql.append(" and id like:id"); args.put("id", str + "%"); } } hql.append(" order by id desc"); return this.query(String.valueOf(hql), args); } public String createId(String companyId, Date time) { String timeKey = DateFormatUtils.format(time, "yyyyMMdd"); // 从缓存中获取已有的组织编码 String cacheKey = RedisConst.buildKey(companyId, HMAreationDataService.CACHE_AREATION_ID); String cacheId = (String) redisUtil.get(cacheKey); if (null != cacheId && cacheId.indexOf(timeKey) >= 0) { String temp = cacheId.substring(cacheId.length() - 3); Integer i = Integer.valueOf(temp); cacheId = timeKey + String.format("%03d", ++i); } else { Map param = new HashMap<>(); param.put("id", timeKey); List areationList = this.areationList(param); if (null == areationList || areationList.size() == 0) { cacheId = timeKey + "001"; } else { String temp = areationList.get(0).getId(); String tempNum = temp.substring(temp.length() - 3); Integer i = Integer.valueOf(tempNum); cacheId = timeKey + String.format("%03d", ++i); } } // 更新缓存 redisUtil.set(cacheKey, cacheId); return cacheId; } }