package com.ld.igds.inout.service; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.bstek.dorado.data.provider.Page; import com.ld.igds.check.dto.CheckItemData; import com.ld.igds.constant.RedisConst; import com.ld.igds.m.InoutManageUtil; import com.ld.igds.models.*; import com.ld.igds.models.InoutSettle; import com.ld.igds.util.ContextUtil; import com.ld.igds.util.DateUtil; 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.Component; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Component public class HSettleService extends HibernateDao { public static final String BEAN_ID = "hSettleServiceImpl"; public static final String CACHE_SETTLE_ID = "SETTLE_ID"; @Autowired private RedisUtil redisUtil; public void pageList(Page page, Map param) throws Exception { String hql = " from " + InoutSettle.class.getName() + " where companyId =:companyId and deptId =:deptId"; Map args = new HashMap(); args.put("companyId", ContextUtil.getCompanyId()); args.put("deptId", ContextUtil.subDeptId(null)); if (null != param) { String str = (String) param.get("depotId"); if (StringUtils.isNotEmpty(str)) { hql += " and depotId=:depotId "; args.put("depotId", str); } str = (String) param.get("contractId"); if (StringUtils.isNotEmpty(str)) { hql += " and contractId=:contractId "; args.put("contractId", str); } str = (String) param.get("bizType"); if (StringUtils.isNotEmpty(str)) { hql += " and bizType=:bizType "; args.put("bizType", str); } Date date = (Date) param.get("start"); if (null != date) { hql += " and payTime >= :start "; args.put("start", DateUtil.getCurZero(date)); } date = (Date) param.get("end"); if (null != date) { hql += " and payTime <= :end "; args.put("end", DateUtil.getNextZero(date)); } } String countHql = "select count(*) " + hql; hql += " order by id desc "; this.pagingQuery(page, hql, countHql, args); } public void saveData(InoutSettle data) { Session session = this.getSessionFactory().openSession(); if (StringUtils.isEmpty(data.getCompanyId())) { data.setCompanyId(ContextUtil.getCompanyId()); } if (StringUtils.isEmpty(data.getDeptId())) { data.setDeptId(ContextUtil.subDeptId(null)); } if (null == data.getPayTime()) { data.setPayTime(new Date()); } try { data.setUpdateTime(new Date()); if (StringUtils.isEmpty(data.getId())) { String id = createId(data.getPayTime(), data.getCompanyId()); data.setId(id); session.save(data); } else { session.update(data); } } finally { session.flush(); session.close(); } } public void delData(InoutSettle data) { Session session = this.getSessionFactory().openSession(); try { if (null != data.getId()) { session.delete(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } public String createId(Date date, String companyId) { // 时间戳标签 String timeKey = DateFormatUtils.format(date, "yyyyMMdd"); // 从缓存中获取已有的组织编码 String cacheKey = RedisConst.buildKey(companyId, this.CACHE_SETTLE_ID); String cacheId = (String) redisUtil.get(cacheKey); if (null != cacheId && cacheId.indexOf(timeKey) >= 0) { String temp = cacheId.substring(cacheId.length() - 4); Integer i = Integer.valueOf(temp); i++; cacheId = timeKey + String.format("%04d", i); } else { Map args = new HashMap<>(); args.put("timeKey", timeKey + "%"); List result = getMaxId(args); if (null == result || result.size() == 0) { cacheId = timeKey + "0001"; } else { String temp = result.get(0).getId(); String tempNum = temp.substring(temp.length() - 4); Integer i = Integer.valueOf(tempNum); cacheId = timeKey + String.format("%04d", ++i); } } // 更新缓存 redisUtil.set(cacheKey, cacheId); return cacheId; } public List getMaxId(Map param) { String hql = "from " + InoutSettle.class.getName() + " where 1 = 1 "; Map args = new HashMap(); if (null != param) { String str = (String) param.get("timeKey"); if (StringUtils.isNotEmpty(str)) { hql += " and id like :timeKey"; args.put("timeKey", str); } } hql += " order by id desc "; return this.query(hql, args); } }