package com.ld.igds; import com.bstek.bdf2.core.orm.hibernate.HibernateDao; import com.ld.igds.common.CoreCommonService; import com.ld.igds.inout.InoutConstant; import com.ld.igds.models.InoutRecord; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.time.DateUtils; import org.hibernate.Query; 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; @Slf4j @Component public class TestService extends HibernateDao { @Autowired private CoreCommonService commonService; private void initRecordCurStorage2(List listData) { if (null == listData || listData.isEmpty()) return; log.info("----------------开始执行重置出入库信息--------------"); //更新实时库存 Session session = this.getSessionFactory().openSession(); try { InoutRecord perData; InoutRecord curData; for (int i = 1; i < listData.size(); i++) { perData = listData.get(i - 1); curData = listData.get(i); if ("IN".equals(perData.getType())) { curData.setCurStorage(perData.getCurStorage() + perData.getSettleWeight()); } else if ("OUT".equals(perData.getType())) { curData.setCurStorage(perData.getCurStorage() - perData.getSettleWeight()); } else if ("OVER".equals(perData.getType())) { curData.setCurStorage(perData.getCurStorage() + perData.getSettleWeight()); } else if ("LOSS".equals(perData.getType())) { curData.setCurStorage(perData.getCurStorage() - perData.getSettleWeight()); } else { curData.setCurStorage(perData.getCurStorage()); } listData.set(i, curData); //更新庫存 String updateHql = "update " + InoutRecord.class.getName() + " set curStorage=:curStorage where companyId=:companyId and id=:id"; Query query = session.createQuery(updateHql); query.setDouble("curStorage", curData.getCurStorage()); query.setString("companyId", curData.getCompanyId()); query.setString("id", curData.getId()); query.executeUpdate(); log.info("----------------DEPOTID={}----CUR={}----------", curData.getDepotId(), curData.getCurStorage()); } log.info("----------------重置完成--------------"); } catch (Exception e) { e.printStackTrace(); } finally { session.clear(); session.close(); } } public void initRecordCurStorageOUT(String depotId, String startDate) { try { Date start = DateUtils.parseDate(startDate, "yyyy-MM-dd"); //获取所有的详细 String hql = " from " + InoutRecord.class.getName() + " where progress=:progress and recordStatus <>:statusError and recordStatus <>:statusDel and depotId=:depotId" + " and completeTime > :start and (type =:typOut or type=:typeOver) " + " order by completeTime"; Map param = new HashMap<>(); param.put("progress", InoutConstant.PROGRESS_RECORD); param.put("statusError", InoutConstant.RECORD_STATUS_ERROR); param.put("statusDel", InoutConstant.RECORD_STATUS_DEL); param.put("depotId", depotId); param.put("start", start); param.put("typOut", InoutConstant.TYPE_OUT); param.put("typeOver", "OVER"); List listData = this.query(hql, param); if (null == listData || listData.isEmpty()) return; //更新出入库列表 this.initRecordCurStorage2(listData); InoutRecord lastData = listData.get(listData.size()-1); commonService.updateDepotStorage(lastData.getCompanyId(), lastData.getDepotId(), lastData.getCurStorage() - lastData.getSettleWeight()); } catch (Exception e) { e.printStackTrace(); } } public void initRecordCurStorageIN(String depotId, String startDate) { try { Date start = DateUtils.parseDate(startDate, "yyyy-MM-dd"); //获取所有的详细 String hql = " from " + InoutRecord.class.getName() + " where progress=:progress and recordStatus <>:statusError and recordStatus <>:statusDel and depotId=:depotId" + " and completeTime > :start and (type =:typIn or type=:typeLOss) " + " order by completeTime"; Map param = new HashMap(); param.put("progress", InoutConstant.PROGRESS_RECORD); param.put("statusError", InoutConstant.RECORD_STATUS_ERROR); param.put("statusDel", InoutConstant.RECORD_STATUS_DEL); param.put("depotId", depotId); param.put("start", start); param.put("typIn", InoutConstant.TYPE_IN); param.put("typeLOss", "LOSS"); List listData = this.query(hql, param); if (null == listData || listData.isEmpty()) return; this.initRecordCurStorage2(listData); InoutRecord lastData = listData.get(listData.size() - 1); // 重新计算客户完成量 commonService.updateDepotStorage(lastData.getCompanyId(), lastData.getDepotId(), lastData.getCurStorage() + lastData.getSettleWeight()); } catch (Exception e) { e.printStackTrace(); } } }