package com.ld.igds.m.service; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.transaction.Transactional; import com.ld.igds.constant.RedisConst; import com.ld.igds.inout.InoutConstant; import com.ld.igds.inout.dto.InoutData; import com.ld.igds.inout.dto.InoutParam; import com.ld.igds.inout.service.InoutService; import com.ld.igds.m.InoutManageUtil; import com.ld.igds.models.InoutLossOver; import com.ld.igds.models.InoutVarietyChange; 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.apache.commons.lang3.time.DateUtils; import org.hibernate.Session; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.bstek.bdf2.core.business.IUser; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.bstek.dorado.data.provider.Page; import com.ld.igds.util.ContextUtil; @Component public class HLossOverService extends HibernateDao { @Resource private RedisUtil redisUtil; @Autowired private InoutService inoutService; public void pageData(Page page, Map param) throws Exception { String hql = " from " + InoutLossOver.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); } str = (String) param.get("deptId"); if (StringUtils.isNotEmpty(str)) { hql += " and deptId =:deptId"; args.put("deptId", str); } str = (String) param.get("foodVariety"); if (StringUtils.isNotEmpty(str)) { hql += " and foodVariety =:foodVariety"; args.put("foodVariety", str); } str = (String) param.get("type"); if (StringUtils.isNotEmpty(str)) { hql += " and type =:type"; args.put("type", str); } } String count = "select count(*) " + hql; hql += " order by submitTime desc"; this.pagingQuery(page, hql, count, args); } @Transactional(rollbackOn = Exception.class) public void saveData(InoutLossOver data) throws Exception { Session session = this.getSessionFactory().openSession(); data.setUpdateTime(new Date()); data.setUpdateUser(ContextUtil.getLoginUserCName()); try { if (null == data.getId()) { IUser user = ContextUtil.getLoginUser(); data.setDeptId(ContextUtil.subDeptId(user)); data.setCompanyId(user.getCompanyId()); if (null == data.getSubmitTime()) { data.setSubmitTime(new Date()); } //获取提交时间日期的01:00:00时间 Date curDate = DateUtil.getCurZero(data.getSubmitTime()); curDate = DateUtils.addHours(curDate, 1); //如果完成时间在curDate之前,则将完成时间设为当前天的01:00:00 if (data.getSubmitTime().before(curDate)) { data.setSubmitTime(curDate); } String id = createId(data.getSubmitTime(), data.getCompanyId(), data.getHwdm()); // String id = addInoutDataByLossOver(data); data.setId(id); session.save(data); } else { session.update(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } } private String addInoutDataByLossOver(InoutLossOver lossOver) { if (null == lossOver.getSubmitTime()) { lossOver.setSubmitTime(new Date()); } InoutData data = new InoutData(); data.setRecordStatus(InoutConstant.RECORD_STATUS_ADD); data.setProgress(InoutConstant.PROGRESS_RECORD); data.setType(lossOver.getType()); data.setSettleWeight(lossOver.getAmount()); data.setDepotId(lossOver.getDepotId()); data.setDeptId(lossOver.getDeptId()); data.setCompanyId(lossOver.getCompanyId()); //data.setFoodVariety(lossOver.getFoodVariety()); data.setCheckStatus(InoutConstant.STATUS_PASS); String loginUser = lossOver.getUpdateUser(); data.setRegisterUser(loginUser); data.setFullWeightUser(loginUser); data.setEmptyWeightUser(loginUser); data.setHandleUser(loginUser); data.setCompleteUser(loginUser); data.setCompleteTime(lossOver.getSubmitTime()); data.setRegisterTime(data.getCompleteTime()); data.setFullWeightTime(data.getCompleteTime()); data.setEmptyWeightTime(data.getCompleteTime()); if (StringUtils.isEmpty(data.getUserId())) { data.setUserId(InoutConstant.DEFAULT_ID_CARD); } return inoutService.addInoutDataByLossOver(data); } @Transactional(rollbackOn = Exception.class) public String delData(InoutLossOver data) throws Exception { Session session = this.getSessionFactory().openSession(); try { if (null != data.getId()) { this.delInoutDataByLossOver(data); session.delete(data); } } catch (Exception e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return null; } private void delInoutDataByLossOver(InoutLossOver lossOver) { InoutParam param = new InoutParam(); param.setCompanyId(lossOver.getCompanyId()); param.setId(lossOver.getId()); param.setProgress(InoutConstant.PROGRESS_RECORD); param.setRecordStatus(InoutConstant.RECORD_STATUS_DEL); param.setMsg(" [" + ContextUtil.getLoginUserCName() + "]执行删除"); param.setType(lossOver.getType()); param.setDepotId(lossOver.getDepotId()); param.setWeight(lossOver.getAmount()); inoutService.delInoutDataByLossOver(param); } public List getMaxId(Map param) { String hql = "from " + InoutLossOver.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); } public String createId(Date date, String companyId, String freightId) { // 时间戳标签 String timeKey = DateFormatUtils.format(date, "yyyyMMdd"); // 从缓存中获取已有的组织编码 String cacheKey = RedisConst.buildKey(companyId, InoutManageUtil.CACHE_LOSS_OVER_ID); String cacheId = (String) redisUtil.get(cacheKey); if (null != cacheId && cacheId.indexOf(freightId + timeKey) >= 0) { String temp = cacheId.substring(cacheId.length() - 3); Integer i = Integer.valueOf(temp); cacheId = freightId + timeKey + String.format("%03d", ++i); } else { Map args = new HashMap<>(); args.put("timeKey", freightId + timeKey + "%"); List result = getMaxId(args); if (null == result || result.size() == 0) { cacheId = freightId + timeKey + "001"; } else { int max = 0; int num; for (InoutLossOver inoutLossOver : result) { num = Integer.parseInt(inoutLossOver.getId().substring(inoutLossOver.getId().length() - 3)); max = Math.max(max, num); } cacheId = freightId + timeKey + String.format("%03d", ++max); } } // 更新缓存 redisUtil.set(cacheKey, cacheId); return cacheId; } }