ReferenceFileDetailSaveAction.java

/*
 *        @ Copyright 2001 FA Software;
 *        All right reserved. No part of this program may be reproduced or
 *        transmitted in any form or by any means, electronic or
 *        mechanical, including photocopying, recording, or by any
 *        information storage or retrieval system without written
 *        permission from FA Software, except for inclusion of brief
 *        quotations in a review.
 */
package com.mycim.webapp.actions.setting.system.referenceFileDetail;

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.beans.BeanUtils;
import com.mycim.framework.utils.lang.ObjectUtils;
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.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.consts.ReferenceDetailNames;
import com.mycim.valueobject.consts.SessionNames;
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.CimSetupAction;
import com.mycim.webapp.forms.system.ReferenceFileDetailInfoForm;
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.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 类表值定义
 *
 * @author weike.li
 * @version 6.0.0
 * @date 2019/8/21
 **/
public class ReferenceFileDetailSaveAction extends CimSetupAction {

    private static final String TRUE = "true";

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

        Long facilityRrn = LocalContext.getFacilityRrn();

        ReferenceFileDetailInfoForm theform = (ReferenceFileDetailInfoForm) form;

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

        ReferenceFile referenceFile = new ReferenceFile(id, getNamedSpace(ObjectList.REFERENCE_FILE_KEY, facilityRrn),
                                                        ObjectList.REFERENCE_FILE_KEY);

        referenceFile = (ReferenceFile) getInstance(referenceFile);
        Assert.isFalse(referenceFile.getInstanceRrn() <= 0, Errors.create().key(MessageIdList.REFERENCEFILE_MISSING_ID)
                                                                  .content("Reference File ID is not exist!").build());


        if ((referenceFile.getKey1FieldRrn() != null) && (referenceFile.getKey1FieldRrn().longValue() != 0)) {
            referenceFile.setKey1LblId(
                    getContentText(referenceFile.getKey1FieldRrn().longValue(), I18nUtils.getCurrentLanguage()));
        }

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

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

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

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

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

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

        request.setAttribute(SessionNames.REFERENCE_FILE_KEY, referenceFile);

        if (TRUE.equals(request.getAttribute("referencefiledetailinfo"))) {
            theform.setTransId(Constants.CREATE_KEY);
            return mapping.findForward(Constants.MEMBERS_KEY);
        }

        PropertyUtils.copyProperties(theform, referenceFile);

        processItemAction(request, theform);
        theform.setTransId(Constants.MODIFY_KEY);
        needSort((List<ReferenceFileDetail>) referenceFile.getValuesItems(), referenceFile.getInstanceId());
        return mapping.findForward(Constants.MODIFY_KEY);
    }

    @Override
    public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) {
        ReferenceFileDetailInfoForm theform = (ReferenceFileDetailInfoForm) form;
        if (mapping.getAttribute() != null) {
            request.removeAttribute(mapping.getAttribute());
        }
        theform.setInstanceId("");
        return mapping.findForward(Constants.SETUP_KEY);
    }

    public ActionForward members(ActionMapping mapping, ReferenceFileDetailInfoForm theform, HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

        Long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();

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

        if (request.getParameter(Constants.MEMBERS_KEY) != null) {
            ReferenceFileDetail referenceFileDetail = new ReferenceFileDetail(id, getNamedSpace(
                    ObjectList.REFERENCE_FILE_KEY, facilityRrn), ObjectList.REFERENCE_FILE_KEY);
            if (request.getParameter(Constants.ITEM_KEY) != null) {
                String key;
                String key1;
                String key2;
                key = request.getParameter(Constants.ITEM_KEY);

                int num = key.indexOf("|");
                key1 = key.substring(0, num);
                key2 = key.substring(num + 1);

                if ("".equalsIgnoreCase(key2)) {
                    key2 = " ";
                }

                referenceFileDetail.setKey1Value(key1);
                referenceFileDetail.setKey2Value(key2);

                referenceFileDetail = (ReferenceFileDetail) getInstance(referenceFileDetail);
                referenceFileDetail.setInstanceId(id);
                PropertyUtils.copyProperties(theform, referenceFileDetail);

                if (request.getParameter(Constants.MEMBERS_KEY).equalsIgnoreCase(Constants.DELETE_KEY)) {
                    referenceFileDetail.setTransId(Constants.DELETE_KEY);
                    referenceFileDetail.setTransPerformedby(user);
                    String name = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(), "KEY_1_VALUE");

                    if (name.equalsIgnoreCase(referenceFileDetail.getInstanceId())) {
                        String wsName = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(),
                                                      "DATA_1_VALUE");

                        HashMap map = new HashMap(5);
                        map.put("reftabledetail", referenceFileDetail);
                        map.put("rfname", wsName);
                        map.put("wsname", "updateGTSTable");

                        String message = null;

                        List<String> coll = getRefFileValues("$WORKSTREAM_FACILITY", "REFERENCEFILE", "KEY_2_VALUE");

                        if (coll == null || coll != null && coll.size() <= 0) {
                            message = this.connectMesSystem(map, facilityRrn, user);

                            Assert.isFalse(message != null, Errors.create().content(message).build());
                        } else {
                            for (String wsFacility : coll) {
                                message = this.connectMesSystem(map, getInstanceRrn(wsFacility, "MYCIM2", "FACILITY"),
                                                                user);

                                Assert.isFalse(message != null, Errors.create().content(message).build());
                            }
                        }
                    }

                    process(referenceFileDetail);

                    response.sendRedirect(
                            "referencefiledetailsave.do?instanceId=" + referenceFileDetail.getInstanceId());
                    return WebUtils.NULLActionForward;
                }
            }
        }

        ReferenceFile referenceFile = new ReferenceFile(id, getNamedSpace(ObjectList.REFERENCE_FILE_KEY, facilityRrn),
                                                        ObjectList.REFERENCE_FILE_KEY);

        referenceFile = (ReferenceFile) getInstance(referenceFile);

        String language = I18nUtils.getCurrentLanguage().toString();

        if ((referenceFile.getKey1FieldRrn() != null) && (referenceFile.getKey1FieldRrn().longValue() != 0)) {
            referenceFile.setKey1LblId(
                    getContentText(referenceFile.getKey1FieldRrn().longValue(), I18nUtils.getCurrentLanguage()));
        }

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

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

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

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

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

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

        request.setAttribute(SessionNames.REFERENCE_FILE_KEY, referenceFile);

        if (request.getParameter(Constants.MEMBERS_KEY) != null) {
            if (Constants.MODIFY_KEY.equalsIgnoreCase(request.getParameter(Constants.MEMBERS_KEY))) {
                referenceFile.setTransId(Constants.MODIFY_KEY);
                PropertyUtils.copyProperties(theform, referenceFile);
                return mapping.findForward(Constants.MEMBERS_KEY);
            }
            if (!Constants.DELETE_KEY.equalsIgnoreCase(request.getParameter(Constants.MEMBERS_KEY))) {
                theform.setTransId(Constants.CREATE_KEY);
                return (mapping.findForward(Constants.MEMBERS_KEY));
            }
        }
        Assert.isFalse(referenceFile.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                             .args("Not Define Reference File").build());
        PropertyUtils.copyProperties(theform, referenceFile);

        processItemAction(request, theform);
        theform.setTransId(Constants.MODIFY_KEY);

        response.sendRedirect("referencefiledetailsave.do?instanceId=" + id);
        return WebUtils.NULLActionForward;
    }

    public ActionForward detailCancel(ActionMapping mapping, ReferenceFileDetailInfoForm theform,
                                      HttpServletRequest request) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();

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

        ReferenceFile referenceFile = new ReferenceFile(id, getNamedSpace(ObjectList.REFERENCE_FILE_KEY, facilityRrn),
                                                        ObjectList.REFERENCE_FILE_KEY);

        referenceFile = (ReferenceFile) getInstance(referenceFile);

        String language = I18nUtils.getCurrentLanguage().toString();

        if ((referenceFile.getKey1FieldRrn() != null) && (referenceFile.getKey1FieldRrn().longValue() != 0)) {
            referenceFile.setKey1LblId(
                    getContentText(referenceFile.getKey1FieldRrn().longValue(), I18nUtils.getCurrentLanguage()));
        }

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

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

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

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

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

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

        request.setAttribute(SessionNames.REFERENCE_FILE_KEY, referenceFile);

        Assert.isFalse(referenceFile.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.SYSTEM_INSTANCE_NOT_FOUND).content("{} 没有找到对象!")
                             .args("Not Define Reference File").build());
        needSort((List<ReferenceFileDetail>) referenceFile.getValuesItems(), referenceFile.getInstanceId());
        PropertyUtils.copyProperties(theform, referenceFile);

        processItemAction(request, theform);
        theform.setTransId(Constants.MODIFY_KEY);

        return mapping.findForward(Constants.MODIFY_KEY);
    }

    public ActionForward modify(ReferenceFileDetailInfoForm theform, HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        String id = theform.getInstanceId().trim().toUpperCase();
        ReferenceFileDetail referenceFileDetail = new ReferenceFileDetail(id,
                                                                          getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                        facilityRrn),
                                                                          ObjectList.REFERENCE_FILE_KEY);

        referenceFileDetail.setKey1Value(theform.getKey1Value());
        String key2value = theform.getKey2Value();
        if ("".equalsIgnoreCase(key2value) || null == key2value) {
            key2value = " ";
        }
        referenceFileDetail.setKey2Value(key2value);
        referenceFileDetail = (ReferenceFileDetail) getInstance(referenceFileDetail);

        referenceFileDetail.setTransPerformedby(user);

        Assert.isFalse(theform.getKey1Value() == null || "".equals(theform.getKey1Value().trim()),
                       Errors.create().key(MessageIdList.REFERENCEFILE_MISSING_KEY_1_FIELD)
                             .content("Key_1_field是主键,不能为空!").build());
        theform.setKey1Value(theform.getKey1Value().trim().toUpperCase());

        if ((theform.getKey2Value() == null) || "".equals(theform.getKey2Value().trim())) {
            theform.setKey2Value(" ");
        } else {
            theform.setKey2Value(theform.getKey2Value().trim().toUpperCase());
        }

        if (StringUtils.isNotEmpty(theform.getData1Value())) {
            theform.setData1Value(StringUtils.trim(theform.getData1Value()));
        }
        if (StringUtils.isNotEmpty(theform.getData2Value())) {
            theform.setData2Value(StringUtils.trim(theform.getData2Value()));
        }

        PropertyUtils.copyProperties(referenceFileDetail, theform);

        referenceFileDetail.setTransId(Constants.MODIFY_KEY);

        String name = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(), "KEY_1_VALUE");

        if (name.equalsIgnoreCase(referenceFileDetail.getInstanceId())) {
            String wsName = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(), "DATA_1_VALUE");

            HashMap map = new HashMap(5);
            map.put("reftabledetail", referenceFileDetail);

            map.put("rfname", wsName);
            map.put("wsname", "updateGTSTable");

            String message = null;

            List<String> coll = getRefFileValues("$WORKSTREAM_FACILITY", "REFERENCEFILE", "KEY_2_VALUE");

            if (coll == null || coll != null && coll.size() <= 0) {
                message = this.connectMesSystem(map, facilityRrn, user);
                Assert.isFalse(message != null, Errors.create().content(message).build());
            } else {
                for (String wsFacility : coll) {
                    message = this.connectMesSystem(map, getInstanceRrn(wsFacility, "MYCIM2", "FACILITY"), user);

                    Assert.isFalse(message != null, Errors.create().content(message).build());
                }
            }
        }

        process(referenceFileDetail);

        Assert.isFalse(referenceFileDetail.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.REFERENCEFILEDETAIL_MISSING_VALUE)
                             .content("没有referencefiledetail值!").build());
        response.sendRedirect("referencefiledetailsave.do?instanceId=" + id);
        return WebUtils.NULLActionForward;
    }

    public ActionForward delete(ReferenceFileDetailInfoForm theform, HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        String id = theform.getInstanceId().trim().toUpperCase();
        ReferenceFileDetail referenceFileDetail = new ReferenceFileDetail(id,
                                                                          getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                        facilityRrn),
                                                                          ObjectList.REFERENCE_FILE_KEY);

        referenceFileDetail.setKey1Value(theform.getKey1Value());
        String key2value = theform.getKey2Value();
        if ("".equalsIgnoreCase(key2value) || null == key2value) {
            key2value = " ";
        }
        referenceFileDetail.setKey2Value(key2value);
        referenceFileDetail = (ReferenceFileDetail) getInstance(referenceFileDetail);

        referenceFileDetail.setTransPerformedby(user);

        Assert.isFalse(theform.getKey1Value() == null || "".equals(theform.getKey1Value().trim()),
                       Errors.create().key(MessageIdList.REFERENCEFILEDETAIL_KEY1_CAN_NOT_NULL).content("第一项不能为空")
                             .build());

        theform.setKey1Value(theform.getKey1Value().trim().toUpperCase());

        if ((theform.getKey2Value() == null) || "".equals(theform.getKey2Value().trim())) {
            theform.setKey2Value(" ");
        } else {
            theform.setKey2Value(theform.getKey2Value().trim().toUpperCase());
        }

        if (StringUtils.isNotEmpty(theform.getData1Value())) {
            theform.setData1Value(StringUtils.trim(theform.getData1Value()));
        }
        if (StringUtils.isNotEmpty(theform.getData2Value())) {
            theform.setData2Value(StringUtils.trim(theform.getData2Value()));
        }

        PropertyUtils.copyProperties(referenceFileDetail, theform);

        referenceFileDetail.setTransId(Constants.DELETE_KEY);

        String name = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(), "KEY_1_VALUE");

        if (name.equalsIgnoreCase(referenceFileDetail.getInstanceId())) {
            String wsName = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(), "DATA_1_VALUE");

            HashMap map = new HashMap(5);
            map.put("reftabledetail", referenceFileDetail);

            map.put("rfname", wsName);
            map.put("wsname", "updateGTSTable");

            String message = null;

            List<String> coll = getRefFileValues("$WORKSTREAM_FACILITY", "REFERENCEFILE", "KEY_2_VALUE");

            if (coll == null || coll != null && coll.size() <= 0) {
                message = this.connectMesSystem(map, facilityRrn, user);
                Assert.isFalse(message != null, Errors.create().content(message).build());
            } else {
                for (String wsFacility : coll) {
                    message = this.connectMesSystem(map, getInstanceRrn(wsFacility, "MYCIM2", "FACILITY"), user);

                    Assert.isFalse(message != null, Errors.create().content(message).build());
                }
            }
        }

        process(referenceFileDetail);

        Assert.isFalse(referenceFileDetail.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.REFERENCEFILEDETAIL_MISSING_VALUE)
                             .content("没有referencefiledetail值!").build());
        response.sendRedirect("referencefiledetailsave.do?instanceId=" + id);
        return WebUtils.NULLActionForward;
    }

    public ActionForward create(HttpServletRequest request, ReferenceFileDetailInfoForm theform,
                                HttpServletResponse response) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String user = LocalContext.getUserId();
        String id = theform.getInstanceId().trim().toUpperCase();
        ReferenceFileDetail referenceFileDetail = new ReferenceFileDetail(id,
                                                                          getNamedSpace(ObjectList.REFERENCE_FILE_KEY,
                                                                                        facilityRrn),
                                                                          ObjectList.REFERENCE_FILE_KEY);

        referenceFileDetail.setKey1Value(theform.getKey1Value());
        String key2value = theform.getKey2Value();
        if ("".equalsIgnoreCase(key2value) || null == key2value) {
            key2value = " ";
        }
        referenceFileDetail.setKey2Value(key2value);
        referenceFileDetail = (ReferenceFileDetail) getInstance(referenceFileDetail);

        referenceFileDetail.setTransPerformedby(user);

        if ((theform.getKey1Value() == null) || "".equals(theform.getKey1Value().trim())) {
            request.setAttribute("referencefiledetailinfo", "true");
            throw new SystemIllegalArgumentException(
                    Errors.create().key(MessageIdList.REFERENCEFILEDETAIL_KEY1_CAN_NOT_NULL).content("第一项不能为空")
                          .build());
        } else {
            theform.setKey1Value(theform.getKey1Value().trim().toUpperCase());
        }

        if(StringUtils.equalsIgnoreCase(theform.getInstanceId(),"$$LOTTYPE_RMSFLAG")){
            ReferenceFileDetail referenceFileDetail1=new ReferenceFileDetail(ReferenceDetailNames.LOT_TYPE,getNamedSpace(
                    ObjectList.REFERENCE_FILE_KEY, facilityRrn),ObjectList.REFERENCE_FILE_KEY);
            //获取referenceFileDetail的list
            List<ReferenceFileDetail> referenceFileDetails = sysService
                    .getReferenceFileDetails(referenceFileDetail1, LocalContext.getFacilityRrn());
            if (ObjectUtils.isNotEmpty(referenceFileDetails)){
                //获取lot ype的值,并转换为set
                Set<String> collect = referenceFileDetails.stream().map(e -> e.getKey1Value())
                                                          .collect(Collectors.toSet());
                //当已有lot type不包含当前值时,报错
                if (CollectionUtils.isNotEmpty(collect) && !collect.contains(theform.getKey1Value())){
                    request.setAttribute("referencefiledetailinfo", "true");
                    throw new SystemIllegalArgumentException(
                            Errors.create().key(MessageIdList.REFERENCEFILEDETAIL_KEY1_IS_NOT_A_LOTTYPE).content("The field is not a lot type!").args(theform.getKey1Value())
                                  .build());
                }
            }

        }
        if ((theform.getKey2Value() == null) || "".equals(theform.getKey2Value().trim())) {
            theform.setKey2Value(" ");
        } else {
            theform.setKey2Value(theform.getKey2Value().trim().toUpperCase());
        }

        if (StringUtils.isNotEmpty(theform.getData1Value())) {
            theform.setData1Value(StringUtils.trim(theform.getData1Value()));
        }
        if (StringUtils.isNotEmpty(theform.getData2Value())) {
            theform.setData2Value(StringUtils.trim(theform.getData2Value()));
        }

        PropertyUtils.copyProperties(referenceFileDetail, theform);

        referenceFileDetail.setTransId(Constants.CREATE_KEY);

        String name = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(), "KEY_1_VALUE");

        if (name.equalsIgnoreCase(referenceFileDetail.getInstanceId())) {
            String wsName = getReferValus("$WORKSTREAM_GTS", referenceFileDetail.getInstanceId(), "DATA_1_VALUE");

            HashMap map = new HashMap(5);
            map.put("reftabledetail", referenceFileDetail);

            map.put("rfname", wsName);
            map.put("wsname", "updateGTSTable");

            String message = null;

            List<String> coll = getRefFileValues("$WORKSTREAM_FACILITY", "REFERENCEFILE", "KEY_2_VALUE");

            if (coll == null || coll != null && coll.size() <= 0) {
                message = this.connectMesSystem(map, facilityRrn, user);
                Assert.isFalse(message != null, Errors.create().content(message).build());
            } else {
                for (String wsFacility : coll) {
                    message = this.connectMesSystem(map, getInstanceRrn(wsFacility, "MYCIM2", "FACILITY"), user);

                    Assert.isFalse(message != null, Errors.create().content(message).build());
                }
            }
        }

        process(referenceFileDetail);

        Assert.isFalse(referenceFileDetail.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.REFERENCEFILEDETAIL_MISSING_VALUE)
                             .content("没有referencefiledetail值!").build());

        response.sendRedirect("referencefiledetailsave.do?instanceId=" + id);
        return WebUtils.NULLActionForward;
    }

    /**
     * 前端获取 配置项目
     * 目前是(2022-5-5 )正则表达式
     * @param condition
     * @return
     */
    public Map<String,Object> queryRefDetailById(Map<String,Object> condition) {
        if (MapUtils.isEmpty(condition) || StringUtils.isEmpty(MapUtils.getString(condition,"referenceId"))) {
            Map<String,Object> result =new HashMap<>();
            result.put("success",false);
            return result;
        }
        String referenceId = MapUtils.getString(condition,"referenceId");
        List<ReferenceFileDetail> referenceFileDetails=
                //"$$SYS_PARAMETER"
                Optional.ofNullable( sysService.getRefFileValues(referenceId,LocalContext.getFacilityRrn())).orElse(Arrays.asList());
        if(CollectionUtils.isEmpty(referenceFileDetails)){
            Map<String,Object> result =new HashMap<>();
            result.put("success",false);
            return result;
        }
        if(StringUtils.isNotBlank(MapUtils.getString(condition,"key1"))){
            String key1 =MapUtils.getString(condition,"key1");
            Optional<ReferenceFileDetail> warpReferenceDetail =
                    referenceFileDetails.stream().filter(item->StringUtils.equalsIgnoreCase(item.getKey1Value(),key1)).findFirst();
            if(!warpReferenceDetail.isPresent()){
                Map<String,Object> result =new HashMap<>();
                result.put("success",false);
                return result;
            }
            referenceFileDetails.clear();
            referenceFileDetails.add(warpReferenceDetail.get());
        }
        List<Map<String,Object>> results = new ArrayList<>();
        for(ReferenceFileDetail item:  referenceFileDetails){
            results.add(BeanUtils.copyBeanToMap(item));
        }
        Map<String,Object> result =new HashMap<>();
        result.put("success",true);
        result.put("items",results);
        result.put("langauge",I18nUtils.getCurrentLanguage());
        return result;
    }

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

    /**
     * 针对个别类表显示,按要求排序
     **/
    private void needSort(List<ReferenceFileDetail> refDetails, String referenceId) {
        if (ReferenceDetailNames.LOT_LOCATION_REF.equalsIgnoreCase(referenceId)) {
            if (CollectionUtils.isNotEmpty(refDetails)) {
                //把key1Value(必须为整数,否则当字符串处理)转换为数字, 然后按数值大小升序排序
                Collections.sort(refDetails, (o1, o2) -> {
                    String a = StringUtils.defaultString(o1.getKey1Value());
                    String b = StringUtils.defaultString(o2.getKey1Value());
                    if (NumberUtils.isDigits(a) && NumberUtils.isDigits(b)) {//都是数字
                        return new BigDecimal(a).compareTo(new BigDecimal(b));
                    } else if (!NumberUtils.isDigits(a) && !NumberUtils.isDigits(b)) {//都不是数字
                        return a.compareTo(b);
                    } else {
                        return NumberUtils.isDigits(a) ? -1 : 1;//一个是数字, 另一个不是, 数字排前面
                    }
                });
            }
        }
    }

}