BomSaveAction.java

package com.mycim.webapp.actions.material;

import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.ReferenceDetailNames;
import com.mycim.valueobject.inv.MaterialDO;
import com.mycim.valueobject.sys.ReferenceFileDetail;
import com.mycim.webapp.Constants;
import com.mycim.webapp.forms.MaterialInfoForm;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: can.yang
 * @Date: 2021/10/25 17:42
 */
public class BomSaveAction extends MaterialSaveAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {

        Long facilityRrn = LocalContext.getFacilityRrn();

        MaterialInfoForm theform = (MaterialInfoForm) form;

        String materialId = theform.getInstanceId().trim().toUpperCase();

        MaterialDO material = asmService.getMaterial(facilityRrn, materialId);

        List<ReferenceFileDetail> referenceFileDetails = getReferenceFileDetails(ReferenceDetailNames.LOT_TYPE);
        List<String> lotTypes = new ArrayList<>();
        for (ReferenceFileDetail referenceFileDetail : referenceFileDetails) {
            lotTypes.add(referenceFileDetail.getData1Value());
        }
        request.setAttribute("lotTypes", lotTypes);
        if (material == null || material.getInstanceRrn() < 1) {
            material = new MaterialDO(materialId, this.getNamedSpace(ObjectList.ITEM_KEY, facilityRrn),
                                      ObjectList.ITEM_KEY);

            material.setExpirationLength(28);

            material.setTransId(Constants.CREATE_KEY);
        } else {
            String strLotTypes = material.getAttribute3();
            List<String> selectLotTypes = new ArrayList<>();
            if (StringUtils.isNotEmptyTrim(strLotTypes)) {
                selectLotTypes = StringUtils.splitAsList(strLotTypes, StringUtils.COMMA_SIGN);
                request.setAttribute("selectLotTypes", selectLotTypes);
            }
            material.setTransId(Constants.MODIFY_KEY);
        }
        PropertyUtils.copyProperties(theform, material);
        return mapping.findForward(Constants.MODIFY_KEY);

    }

    public ActionForward create(ActionMapping mapping, MaterialInfoForm theform,  HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        validateMaterial(theform, 0L);

        MaterialDO material = new MaterialDO(theform.getInstanceId(),
                                             this.getNamedSpace(ObjectList.ITEM_KEY, LocalContext.getFacilityRrn()),
                                             ObjectList.ITEM_KEY);
        PropertyUtils.copyProperties(material, theform);
        material.setItemExtendNumber(
                StringUtils.isBlank(material.getItemExtendNumber()) ? null : material.getItemExtendNumber());
        material.setTransPerformedby(LocalContext.getUserId());
        material.setObjectType(ObjectList.ITEM_BOM_KEY);
        material.setAttribute3(theform.getLotTypes());
        asmService.insertMaterial(material);
        List<ReferenceFileDetail> referenceFileDetails = getReferenceFileDetails(ReferenceDetailNames.LOT_TYPE);
        List<String> lotTypes = new ArrayList<>();
        for (ReferenceFileDetail referenceFileDetail : referenceFileDetails) {
            lotTypes.add(referenceFileDetail.getData1Value());
        }
        request.setAttribute("lotTypes", lotTypes);
        MaterialDO endMaterial = asmService.getMaterial(LocalContext.getFacilityRrn(), theform.getInstanceId());
        String strLotTypes = endMaterial.getAttribute3();
        List<String> selectLotTypes = new ArrayList<>();
        if (StringUtils.isNotEmptyTrim(strLotTypes)) {
            selectLotTypes = StringUtils.splitAsList(strLotTypes, StringUtils.COMMA_SIGN);
            request.setAttribute("selectLotTypes", selectLotTypes);
        }
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ActionMapping mapping, MaterialInfoForm theform , HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        MaterialDO material = asmService.getMaterial(LocalContext.getFacilityRrn(), theform.getInstanceId());

        validateMaterial(theform, material.getInstanceRrn());

        PropertyUtils.copyProperties(material, theform);
        material.setItemExtendNumber(
                StringUtils.isBlank(material.getItemExtendNumber()) ? null : material.getItemExtendNumber());
        material.setTransPerformedby(LocalContext.getUserId());
        material.setObjectType(ObjectList.ITEM_BOM_KEY);
        material.setAttribute3(theform.getLotTypes());
        asmService.updateMaterial(material);
        List<ReferenceFileDetail> referenceFileDetails = getReferenceFileDetails(ReferenceDetailNames.LOT_TYPE);
        List<String> lotTypes = new ArrayList<>();
        for (ReferenceFileDetail referenceFileDetail : referenceFileDetails) {
            lotTypes.add(referenceFileDetail.getData1Value());
        }
        request.setAttribute("lotTypes", lotTypes);
        MaterialDO endMaterial = asmService.getMaterial(LocalContext.getFacilityRrn(), theform.getInstanceId());
        String strLotTypes = endMaterial.getAttribute3();
        List<String> selectLotTypes = new ArrayList<>();
        if (StringUtils.isNotEmptyTrim(strLotTypes)) {
            selectLotTypes = StringUtils.splitAsList(strLotTypes, StringUtils.COMMA_SIGN);
            request.setAttribute("selectLotTypes", selectLotTypes);
        }
        theform.setTransId(Constants.MODIFY_KEY);
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward delete(ActionMapping mapping, MaterialInfoForm theform) {
        return super.delete(mapping, theform);
    }


    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) {
        return super.cancel(mapping, form, request, response);
    }

}