PcdCleanCycleEqptAction.java

package com.mycim.webapp.actions.pcd;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.exception.SystemIllegalArgumentException;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.i18n.Languages;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.ems.PcdClean;
import com.mycim.valueobject.sys.ReferenceFile;
import com.mycim.valueobject.sys.ReferenceFileDetail;
import com.mycim.webapp.Constants;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.CarrierSetupAction;
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.List;
import java.util.Map;

/**
 * @author liuji.li
 * @version 6.0.0
 * @date 2019/9/12
 **/
public class PcdCleanCycleEqptAction extends CarrierSetupAction {

    private final static String PCD_CLEAN_EQPT_REFERENCE_ID = "$PCD_CLEAN_EQPT";

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String language = I18nUtils.getCurrentLanguage().name();

        ReferenceFile referenceFile = new ReferenceFile(PCD_CLEAN_EQPT_REFERENCE_ID,
                                                        getNamedSpace(ObjectList.REFERENCE_FILE_KEY, facilityRrn),
                                                        ObjectList.REFERENCE_FILE_KEY);
        referenceFile = sysService.getReferenceFile(referenceFile);
        buildReference(referenceFile, language);
        request.setAttribute("reference", referenceFile);
        return mapping.getInputForward();
    }

    private void buildReference(ReferenceFile referenceFile, String language) throws Exception {
        if ((referenceFile.getKey1FieldRrn() != null) && (referenceFile.getKey1FieldRrn().longValue() != 0)) {
            referenceFile.setKey1LblId(getContentText(referenceFile.getKey1FieldRrn().longValue(), language));
        }

        if ((referenceFile.getKey2FieldRrn() != null) && (referenceFile.getKey2FieldRrn().longValue() != 0)) {
            referenceFile.setKey2LblId(getContentText(referenceFile.getKey2FieldRrn().longValue(), language));
        }

        if ((referenceFile.getData1FieldRrn() != null) && (referenceFile.getData1FieldRrn().longValue() != 0)) {
            referenceFile.setData1LblId(getContentText(referenceFile.getData1FieldRrn().longValue(), language));
        }

        if ((referenceFile.getData2FieldRrn() != null) && (referenceFile.getData2FieldRrn().longValue() != 0)) {
            referenceFile.setData2LblId(getContentText(referenceFile.getData2FieldRrn().longValue(), language));
        }

        if ((referenceFile.getData3FieldRrn() != null) && (referenceFile.getData3FieldRrn().longValue() != 0)) {
            referenceFile.setData3LblId(getContentText(referenceFile.getData3FieldRrn().longValue(), language));
        }

        if ((referenceFile.getData4FieldRrn() != null) && (referenceFile.getData4FieldRrn().longValue() != 0)) {
            referenceFile.setData4LblId(getContentText(referenceFile.getData4FieldRrn().longValue(), language));
        }

        if ((referenceFile.getData5FieldRrn() != null) && (referenceFile.getData5FieldRrn().longValue() != 0)) {
            referenceFile.setData5LblId(getContentText(referenceFile.getData5FieldRrn().longValue(), language));
        }
    }

    private String getContentText(long fieldRrn, String language) throws Exception {
        String contentText = sysService.getContentText(fieldRrn, Languages.valueOf(language));
        return contentText;
    }

    public ActionForward viewPcdCleanEqptInfo(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                              HttpServletResponse response) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String language = I18nUtils.getCurrentLanguage().name();
        String key1value = WebUtils.getParameter("key1value", request);
        ReferenceFile referenceFile = new ReferenceFile(PCD_CLEAN_EQPT_REFERENCE_ID,
                                                        getNamedSpace(ObjectList.REFERENCE_FILE_KEY, facilityRrn),
                                                        ObjectList.REFERENCE_FILE_KEY);
        referenceFile = sysService.getReferenceFile(referenceFile);
        buildReference(referenceFile, language);

        // 重组ReferenceFile,不传递ReferenceFile中的集合对象
        ReferenceFile ref = new ReferenceFile();
        ref.setKey1LblId(referenceFile.getKey1LblId());
        ref.setKey2LblId(referenceFile.getKey2LblId());
        ref.setData1LblId(referenceFile.getData1LblId());
        ref.setData2LblId(referenceFile.getData2LblId());
        ref.setData3LblId(referenceFile.getData3LblId());
        ref.setData4LblId(referenceFile.getData4LblId());
        ref.setData5LblId(referenceFile.getData5LblId());
        request.setAttribute("reference", ref);
        request.setAttribute("referenceDetail", new ReferenceFileDetail());

        if (StringUtils.isNotBlank(key1value)) {
            List<ReferenceFileDetail> refDetailColl = (List<ReferenceFileDetail>) referenceFile.getValuesItems();
            if (CollectionUtils.isNotEmpty(refDetailColl)) {
                for (ReferenceFileDetail detail : refDetailColl) {
                    if (StringUtils.equalsIgnoreCase(detail.getKey1Value(), key1value)) {
                        request.setAttribute("referenceDetail", detail);
                    }
                }
            }
        }

        return mapping.findForward("viewPcdCleanEqptInfo");
    }

    public ActionForward addPcdCleanEqpt(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        ReferenceFileDetail paramDetail = buildRequestParameter(map);
        boolean errorFlag = checkReferenceFileDetail(paramDetail, facilityRrn);
        if (errorFlag) {
            paramDetail.setTransId(Constants.CREATE_KEY);
            paramDetail.setTransPerformedby(user);

            String key1Value = paramDetail.getData1Value() + "_" + paramDetail.getData2Value();
            paramDetail.setKey1Value(key1Value);
            long refRrn = this.getInstanceRrn(PCD_CLEAN_EQPT_REFERENCE_ID, facilityRrn, ObjectList.REFERENCE_FILE_KEY);
            paramDetail.setInstanceRrn(refRrn);
            sysService.insertReferenceFileDetail(paramDetail);
        }
        return WebUtils.NULLActionForward;
    }

    private ReferenceFileDetail buildRequestParameter(Map map) {
        ReferenceFileDetail detail = new ReferenceFileDetail();
        detail.setData1Value(StringUtils.trimToUpperCase(MapUtils.getString(map, "data1Value")));
        detail.setData2Value(StringUtils.trimToUpperCase(MapUtils.getString(map, "pcdObject")));
        detail.setData3Value(StringUtils.trimToUpperCase(MapUtils.getString(map, "data3Value")));
        detail.setData4Value(StringUtils.trimToUpperCase(MapUtils.getString(map, "data4Value")));
        detail.setKey2Value(" ");

        return detail;
    }

    private boolean checkReferenceFileDetail(ReferenceFileDetail paramDetail, Long facilityRrn) {

        boolean errorFlag = true;
        Assert.isFalse(StringUtils.isBlank(paramDetail.getData1Value()),
                       Errors.create().key(MessageIdList.PCD_EQPT_ID_NO_EMPTY).content("设备ID不能为空!").build());

        Assert.isFalse(StringUtils.isBlank(paramDetail.getData2Value()),
                       Errors.create().key(MessageIdList.PCD_OBJECT_NO_EMPTY).content("P/C/D object can not be empty!")
                             .build());

        Assert.isTrue(StringUtils.isNotBlank(paramDetail.getData3Value()),
                      Errors.create().key(MessageIdList.PCD_MAX_COUNT_NO_EMPTY).content("最大计数不能为空!").build());
        String pattern = "^([1-9]+[0-9]*)$";
        Assert.isTrue(paramDetail.getData3Value().matches(pattern),
                      Errors.create().key(MessageIdList.PCD_MAX_COUNT_MUST_INTEGER).content("最大计数必须是正整数!").build());

        Assert.isTrue(StringUtils.isNotBlank(paramDetail.getData4Value()),
                      Errors.create().key(MessageIdList.PCD_MIN_MINUTES_NOT_EMPTY).content("最小分钟数不能为空!").build());

        pattern = "^([1-9]+[0-9]*(\\.\\d{1,2})?)|(0\\.\\d{1,2})$";
        Assert.isTrue(paramDetail.getData4Value().matches(pattern),
                      Errors.create().key(MessageIdList.PCD_MIN_MINUTES_MUST_POSITIVE).content("最小分钟数必须是正数且最多两位小数!")
                            .build());


        if (errorFlag) {
            List<ReferenceFileDetail> deatilColl = sysService
                    .getRefFileValues(PCD_CLEAN_EQPT_REFERENCE_ID, facilityRrn);
            // EQP ID同一种 P/C/D OBJECT 只能有一条记录
            if (CollectionUtils.isNotEmpty(deatilColl)) {
                for (ReferenceFileDetail detail : deatilColl) {
                    if (StringUtils.equals(detail.getData1Value(), paramDetail.getData1Value())) {
                        Assert.isFalse(StringUtils.equals(detail.getData2Value(), paramDetail.getData2Value()),
                                       Errors.create().key(MessageIdList.PCD_CORRESPONDENCE_ALREADY_EXISTED)
                                             .content("设备与P/C/D对象对应关系已经存在").build());
                    }
                }
            }
        }

        return errorFlag;
    }

    public ActionForward modifyPcdCleanEqpt(Map map) {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        ReferenceFileDetail paramDetail = buildRequestParameter(map);
        paramDetail.setTransId(Constants.MODIFY_KEY);
        paramDetail.setTransPerformedby(user);

        String key1Value = paramDetail.getData1Value() + "_" + paramDetail.getData2Value();
        paramDetail.setKey1Value(key1Value);
        long refRrn = this.getInstanceRrn(PCD_CLEAN_EQPT_REFERENCE_ID, facilityRrn, ObjectList.REFERENCE_FILE_KEY);
        paramDetail.setInstanceRrn(refRrn);
        sysService.updateReferenceFileDetail(paramDetail);

        return WebUtils.NULLActionForward;
    }

    public ActionForward deletePcdCleanEqpt(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                            HttpServletResponse response) throws Exception {
        long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        String key1Value = WebUtils.getParameterUpperCase("key1value", request);
        String eqptId = WebUtils.getParameterUpperCase("data1value", request);
        if (StringUtils.isNotBlank(key1Value)) {
            // 验证清洗设备上是否有P/C/D,有则不允许删除
            boolean errorFlag = checkCleanEqptIsInUse(eqptId, facilityRrn);
            if (errorFlag) {
                ReferenceFileDetail paramDetail = new ReferenceFileDetail();
                paramDetail.setKey1Value(key1Value);
                paramDetail.setKey2Value(" ");
                paramDetail.setTransId(Constants.DELETE_KEY);
                paramDetail.setTransPerformedby(user);
                long refRrn = this
                        .getInstanceRrn(PCD_CLEAN_EQPT_REFERENCE_ID, facilityRrn, ObjectList.REFERENCE_FILE_KEY);
                paramDetail.setInstanceRrn(refRrn);
                sysService.deleteReferenceFileDetail(paramDetail);
            }
        }
        return init(mapping, form, request, response);
    }

    /**
     * 检查目标设备是否正在使用
     *
     * @return 不为空,表示正在使用,且内容中包含正在清洗那几个PCD;为空,表示没有使用
     */
    public boolean checkCleanEqptIsInUse(String eqptId, Long facilityRrn) {

        String entityNamedSpace = this.getNamedSpace(ObjectList.ENTITY_KEY, facilityRrn);
        List<PcdClean> list = carrierService.getPcdInfoByCleanEqpt(eqptId, entityNamedSpace);
        boolean errorFlag = true;
        if (CollectionUtils.isNotEmpty(list)) {
            String msg = StringUtils.EMPTY;
            String cleaningP = "P:";
            String cleaningC = "C:";
            String cleaningD = "D:";
            for (PcdClean pc : list) {
                if (StringUtils.equals(pc.getPcdType(), ObjectList.POD_KEY)) {
                    cleaningP += pc.getInstanceId() + " ";
                }
                if (StringUtils.equals(pc.getPcdType(), ObjectList.CARRIER_KEY)) {
                    cleaningC += pc.getInstanceId() + " ";
                }
                if (StringUtils.equals(pc.getPcdType(), ObjectList.DOOR_KEY)) {
                    cleaningD += pc.getInstanceId() + " ";
                }
            }

            if (StringUtils.length(cleaningP) > 3) {
                msg += cleaningP + " ";
            }
            if (StringUtils.length(cleaningC) > 3) {
                msg += cleaningC + " ";
            }
            if (StringUtils.length(cleaningD) > 3) {
                msg += cleaningD + " ";
            }
            errorFlag = false;
            throw new SystemIllegalArgumentException(
                    Errors.create().key(MessageIdList.PCD_EQPT_CLEANING).content("设备{}正在清洗中{}!").args(eqptId, msg)
                          .build());
        }
        return errorFlag;
    }

}