Index: ssts-web/src/main/java/com/forgon/disinfectsystem/systemexamination/action/DisposableGoodsStockExaminationAction.java =================================================================== diff -u --- ssts-web/src/main/java/com/forgon/disinfectsystem/systemexamination/action/DisposableGoodsStockExaminationAction.java (revision 0) +++ ssts-web/src/main/java/com/forgon/disinfectsystem/systemexamination/action/DisposableGoodsStockExaminationAction.java (revision 29362) @@ -0,0 +1,284 @@ +package com.forgon.disinfectsystem.systemexamination.action; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.struts2.convention.annotation.Action; +import org.apache.struts2.convention.annotation.Namespace; +import org.apache.struts2.convention.annotation.ParentPackage; + +import com.forgon.disinfectsystem.diposablegoods.service.DiposableGoodsManager; +import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoods; +import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoodsBatch; +import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoodsBatchStock; +import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoodsIdentification; +import com.forgon.disinfectsystem.entity.assestmanagement.DisposableGoodsStock; +import com.forgon.tools.StrutsResponseUtils; +import com.forgon.tools.hibernate.ObjectDao; +import com.forgon.tools.json.JSONUtil; + +/** + * 一次性物品库存、批次库存以及标识号的数据体检功能 + * @author ZhouPeiMian + * @since 2020-10-24 + */ +@ParentPackage(value = "default") +@Namespace(value = "/disinfectSystem") +@Action(value = "DisposableGoodsStockExaminationAction") +public class DisposableGoodsStockExaminationAction { + + private DiposableGoodsManager diposableGoodsManager; + + private ObjectDao objectDao; + + /** + * 一次性物品标识号的数据引用错误 + */ + public static final String IDENTIFICATIONERROR = "1"; + + /** + * 批次库存的数量不等于属于该批次的所有标识号的库存数量总和 + */ + public static final String BATCHSTOCKNOTEQUALIDSTOCK = "2"; + + /** + * 一次性物品库存的数量不等于属于该一次性物品的所有批次的库存数量总和 + */ + public static final String STOCKNOTEQUALBATCHSTOCK = "3"; + + /** + * 未发现问题 + */ + public static final String NORMAL = "0"; + + public void setDiposableGoodsManager(DiposableGoodsManager diposableGoodsManager) { + this.diposableGoodsManager = diposableGoodsManager; + } + + public void setObjectDao(ObjectDao objectDao) { + this.objectDao = objectDao; + } + + /** + * 一次性物品库存、批次库存以及标识号的数据体检功能 + * 异常情况1:一次性物品标识号的数据引用错误 + * 异常情况2:批次库存的数量不等于属于该批次的所有标识号的库存数量总和 + * 异常情况3:一次性物品库存的数量不等于属于该一次性物品的所有批次的库存数量总和 + */ + public void disposableGoodsStockExamination(){ + JSONArray data = new JSONArray(); + + List disposableGoodsList = diposableGoodsManager.getAll("sequence"); + if(CollectionUtils.isNotEmpty(disposableGoodsList)){ + // 异常情况1检查 + // 一次性物品标识号的数据引用错误 + // 标识号所引用的一次性物品id不存在 + // 标识号所引用的一次性物品库存id不存在 + // 标识号所引用的一次性物品批次id不存在 + @SuppressWarnings("unchecked") + List identifications = objectDao.findAllObjects(DisposableGoodsIdentification.class.getSimpleName()); + List tempDisposableGoodsList = new ArrayList(); + JSONArray data1 = new JSONArray(); + if(CollectionUtils.isNotEmpty(identifications)){ + for (DisposableGoodsIdentification disposableGoodsIdentification : identifications) { + Long disposableGoodsID = disposableGoodsIdentification.getDisposableGoodsID(); + Long disposableGoodsStockID = disposableGoodsIdentification.getDisposableGoodsStockID(); + Long disposableGoodsBatchID = disposableGoodsIdentification.getDisposableGoodsBatchID(); + DisposableGoods disposableGoods = (DisposableGoods) objectDao.getById(DisposableGoods.class.getSimpleName(), disposableGoodsID == null ? 0l : disposableGoodsID); + DisposableGoodsStock disposableGoodsStock = (DisposableGoodsStock) objectDao.getById(DisposableGoodsStock.class.getSimpleName(), disposableGoodsStockID == null ? 0l : disposableGoodsStockID); + DisposableGoodsBatch disposableGoodsBatch = (DisposableGoodsBatch) objectDao.getById(DisposableGoodsBatch.class.getSimpleName(), disposableGoodsBatchID == null ? 0l : disposableGoodsBatchID); + JSONObject json = null; + if(disposableGoods == null){ + json = buildDisposableGoodsIdentificationJson(disposableGoodsIdentification, disposableGoodsStock, disposableGoodsBatch); + }else if(disposableGoodsStock == null || disposableGoodsBatch == null){ + tempDisposableGoodsList.add(disposableGoods); + List disposableGoodsStockList = diposableGoodsManager.getDisposableGoodsStockByDisposableGoodsId(disposableGoods.getId()); + json = bulidDisposableGoodsJson(disposableGoods, disposableGoodsStockList, IDENTIFICATIONERROR); + } + if(json != null){ + data1.add(json); + } + } + } + + // 异常情况2\异常情况3的检查(异常情况1的一次性物品不检查) + disposableGoodsList.removeAll(tempDisposableGoodsList); + for (DisposableGoods disposableGoods : disposableGoodsList) { + String type = DisposableGoodsStockExaminationAction.NORMAL; + List disposableGoodsStockList = diposableGoodsManager.getDisposableGoodsStockByDisposableGoodsId(disposableGoods.getId()); + type = checkDisposableGoodsStock(disposableGoodsStockList); + JSONObject json = bulidDisposableGoodsJson(disposableGoods, disposableGoodsStockList, type); + if(json != null){ + data.add(json); + } + } + data.addAll(data1); + } + JSONObject result = JSONUtil.buildJsonObject(true, data); + StrutsResponseUtils.output(result); + } + + /** + * 根据DisposableGoodsIdentification构建一次性物品库存信息 + * @param identification + * @param disposableGoodsBatch + * @param disposableGoodsStock + * @return + */ + private JSONObject buildDisposableGoodsIdentificationJson(DisposableGoodsIdentification identification, DisposableGoodsStock disposableGoodsStock, DisposableGoodsBatch disposableGoodsBatch) { + if(identification == null){ + return null; + } + JSONObject json = new JSONObject(); + json.put("disposableGoodsId", identification.getDisposableGoodsID() == null ? "" : identification.getDisposableGoodsID()); + json.put("nameAndSpe", ""); + json.put("type", IDENTIFICATIONERROR); + json.put("stock", disposableGoodsStock == null ? "" : disposableGoodsStock.getAmount()); + json.put("stockId", disposableGoodsStock == null ? "" : disposableGoodsStock.getId()); + json.put("disposableGoodsBatchStock", new JSONArray()); + + JSONArray disposableGoodsBatchStock = new JSONArray(); + JSONArray identificationJsonArr = new JSONArray(); + JSONObject identificationJson = new JSONObject(); + identificationJson.put("id", identification.getId()); + identificationJson.put("stock", identification.getAmount()); + identificationJson.put("disposableGoodsId", identification.getDisposableGoodsID() == null ? "" : identification.getDisposableGoodsID()); + identificationJson.put("stockId", identification.getDisposableGoodsStockID() == null ? "" : identification.getDisposableGoodsStockID()); + identificationJson.put("batchId", identification.getDisposableGoodsBatchID() == null ? "" : identification.getDisposableGoodsBatchID()); + identificationJsonArr.add(identificationJson); + + JSONObject batchStockJson = new JSONObject(); + batchStockJson.put("batchId", ""); + batchStockJson.put("batchNumber", ""); + batchStockJson.put("batchStock", ""); + batchStockJson.put("expDate", ""); + if(disposableGoodsBatch != null){ + batchStockJson.put("batchId", disposableGoodsBatch.getId()); + batchStockJson.put("batchNumber", disposableGoodsBatch.getBatchNumber()); + batchStockJson.put("batchStock", disposableGoodsBatch.getStorage()); + batchStockJson.put("expDate", disposableGoodsBatch.getExpDateStr()); + } + batchStockJson.put("disposableGoodsIdentification", identificationJsonArr); + batchStockJson.put("sum", identificationJsonArr.size()); + + disposableGoodsBatchStock.add(batchStockJson); + json.put("disposableGoodsBatchStock", disposableGoodsBatchStock); + return json; + } + + /** + * 检查一次性物品库存 + * @param disposableGoodsStockList + * @return + */ + private String checkDisposableGoodsStock(List disposableGoodsStockList) { + String type = DisposableGoodsStockExaminationAction.NORMAL; + if(CollectionUtils.isEmpty(disposableGoodsStockList)){ + return type; + } + for (DisposableGoodsStock disposableGoodsStock : disposableGoodsStockList) { + Long stock = disposableGoodsStock.getAmount(); + stock = stock == null ? 0L : stock; + Long totalBatchStock = 0L; + Set batchStockSet = disposableGoodsStock.getGoodsBatchs(); + for (DisposableGoodsBatchStock disposableGoodsBatchStock : batchStockSet) { + totalBatchStock += disposableGoodsBatchStock.getStorage(); + } + if(stock.longValue() != totalBatchStock.longValue()){ + return DisposableGoodsStockExaminationAction.STOCKNOTEQUALBATCHSTOCK; + } + for (DisposableGoodsBatchStock disposableGoodsBatchStock : batchStockSet) { + Long storage = disposableGoodsBatchStock.getStorage(); + storage = storage == null ? 0L : storage; + Long totalStorage = 0L; + List identifications = disposableGoodsBatchStock.getIdentifications(); + for (DisposableGoodsIdentification disposableGoodsIdentification : identifications) { + totalStorage += disposableGoodsIdentification.getAmount(); + } + if(storage.longValue() != totalStorage.longValue()){ + return DisposableGoodsStockExaminationAction.BATCHSTOCKNOTEQUALIDSTOCK; + } + } + } + return type; + } + + /** + * 生成一次性物品定义及库存的json对象 + * @param disposableGoods + * @param disposableGoodsStockList + * @param type + * @return + */ + private JSONObject bulidDisposableGoodsJson(DisposableGoods disposableGoods, List disposableGoodsStockList, String type) { + if(disposableGoods == null){ + return null; + } + JSONObject json = new JSONObject(); + json.put("disposableGoodsId", disposableGoods.getId()); + String Spe = StringUtils.isBlank(disposableGoods.getSpecification()) ? "" : "[" + disposableGoods.getSpecification() + "]"; + json.put("nameAndSpe", disposableGoods.getName() + Spe); + json.put("type", StringUtils.equals(type, NORMAL) ? "" : type); + json.put("stock", ""); + json.put("stockId", ""); + json.put("disposableGoodsBatchStock", new JSONArray()); + int sum = 0; + json.put("sum", sum); + if(CollectionUtils.isNotEmpty(disposableGoodsStockList)){ + Long stock = 0l; + String stockId = ""; + for (int i=0;i identifications = batchStock.getIdentifications(); + JSONArray identificationJsonArr = new JSONArray(); + if(CollectionUtils.isNotEmpty(identifications)){ + for (DisposableGoodsIdentification identification : identifications) { + JSONObject identificationJson = new JSONObject(); + identificationJson.put("id", identification.getId()); + identificationJson.put("stock", identification.getAmount() == null ? "" : identification.getAmount()); + identificationJson.put("disposableGoodsId", identification.getDisposableGoodsID() == null ? "" : identification.getDisposableGoodsID()); + identificationJson.put("stockId", identification.getDisposableGoodsStockID() == null ? "" : identification.getDisposableGoodsStockID()); + identificationJson.put("batchId", identification.getDisposableGoodsBatchID() == null ? "" : identification.getDisposableGoodsBatchID()); + identificationJsonArr.add(identificationJson); + } + } + batchStockJson.put("batchId", batchStock.getDisposableGoodsBatchId() == null ? "" : batchStock.getDisposableGoodsBatchId()); + batchStockJson.put("batchNumber", batchStock.getBatchNumber() == null ? "" : batchStock.getBatchNumber()); + batchStockJson.put("batchStock", batchStock.getStorage() == null ? "" : batchStock.getStorage()); + batchStockJson.put("expDate", batchStock.getExpDateStr()); + batchStockJson.put("disposableGoodsIdentification", identificationJsonArr); + batchStockJson.put("sum", identificationJsonArr.size()); + sum += identificationJsonArr.size(); + disposableGoodsBatchStock.add(batchStockJson); + } + } + json.put("disposableGoodsBatchStock", disposableGoodsBatchStock); + json.put("sum", sum); + } + return json; + } + +}